CollateralGroupPath.java

  1. package org.drip.xva.netting;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  9.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  10.  *
  11.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  12.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  13.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  14.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  15.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  16.  *      and computational support.
  17.  *  
  18.  *      https://lakshmidrip.github.io/DROP/
  19.  *  
  20.  *  DROP is composed of three modules:
  21.  *  
  22.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  23.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  24.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  25.  *
  26.  *  DROP Product Core implements libraries for the following:
  27.  *  - Fixed Income Analytics
  28.  *  - Loan Analytics
  29.  *  - Transaction Cost Analytics
  30.  *
  31.  *  DROP Portfolio Core implements libraries for the following:
  32.  *  - Asset Allocation Analytics
  33.  *  - Asset Liability Management Analytics
  34.  *  - Capital Estimation Analytics
  35.  *  - Exposure Analytics
  36.  *  - Margin Analytics
  37.  *  - XVA Analytics
  38.  *
  39.  *  DROP Computational Core implements libraries for the following:
  40.  *  - Algorithm Support
  41.  *  - Computation Support
  42.  *  - Function Analysis
  43.  *  - Model Validation
  44.  *  - Numerical Analysis
  45.  *  - Numerical Optimizer
  46.  *  - Spline Builder
  47.  *  - Statistical Learning
  48.  *
  49.  *  Documentation for DROP is Spread Over:
  50.  *
  51.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  52.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  53.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  54.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  55.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  56.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  57.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  58.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  59.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  60.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  61.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  62.  *
  63.  *  Licensed under the Apache License, Version 2.0 (the "License");
  64.  *      you may not use this file except in compliance with the License.
  65.  *  
  66.  *  You may obtain a copy of the License at
  67.  *      http://www.apache.org/licenses/LICENSE-2.0
  68.  *  
  69.  *  Unless required by applicable law or agreed to in writing, software
  70.  *      distributed under the License is distributed on an "AS IS" BASIS,
  71.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  72.  *  
  73.  *  See the License for the specific language governing permissions and
  74.  *      limitations under the License.
  75.  */

  76. /**
  77.  * <i>CollateralGroupPath</i> accumulates the Vertex Realizations of the Sequence in a Single Path Projection
  78.  * Run along the Granularity of a Regular Collateral Hypothecation Group. The References are:
  79.  *
  80.  *  <br><br>
  81.  *  <ul>
  82.  *      <li>
  83.  *          Burgard, C., and M. Kjaer (2014): PDE Representations of Derivatives with Bilateral Counter-party
  84.  *              Risk and Funding Costs <i>Journal of Credit Risk</i> <b>7 (3)</b> 1-19
  85.  *      </li>
  86.  *      <li>
  87.  *          Burgard, C., and M. Kjaer (2014): In the Balance <i>Risk</i> <b>24 (11)</b> 72-75
  88.  *      </li>
  89.  *      <li>
  90.  *          Gregory, J. (2009): Being Two-faced over Counter-party Credit Risk <i>Risk</i> <b>20 (2)</b>
  91.  *              86-90
  92.  *      </li>
  93.  *      <li>
  94.  *          Li, B., and Y. Tang (2007): <i>Quantitative Analysis, Derivatives Modeling, and Trading
  95.  *              Strategies in the Presence of Counter-party Credit Risk for the Fixed Income Market</i>
  96.  *              <b>World Scientific Publishing</b> Singapore
  97.  *      </li>
  98.  *      <li>
  99.  *          Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives Pricing
  100.  *              <i>Risk</i> <b>21 (2)</b> 97-102
  101.  *      </li>
  102.  *  </ul>
  103.  *
  104.  *  <br><br>
  105.  *  <ul>
  106.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  107.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/XVAAnalyticsLibrary.md">XVA Analytics Library</a></li>
  108.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/xva/README.md">Valuation Adjustments that account for Collateral, CC Credit/Debt and Funding Overhead</a></li>
  109.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/xva/netting/README.md">Credit/Debt/Funding Netting Groups</a></li>
  110.  *  </ul>
  111.  * <br><br>
  112.  *
  113.  * @author Lakshmi Krishnamurthy
  114.  */

  115. public class CollateralGroupPath
  116. {
  117.     private double _overnightReplicatorStart = java.lang.Double.NaN;
  118.     private org.drip.exposure.universe.MarketPath _marketPath = null;
  119.     private org.drip.xva.hypothecation.CollateralGroupVertex[] _collateralGroupVertexArray = null;

  120.     /**
  121.      * CollateralGroupPath Constructor
  122.      *
  123.      * @param collateralGroupVertexArray The Array of Collateral Hypothecation Group Trajectory Vertexes
  124.      * @param marketPath The Market Path
  125.      *
  126.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  127.      */

  128.     public CollateralGroupPath (
  129.         final org.drip.xva.hypothecation.CollateralGroupVertex[] collateralGroupVertexArray,
  130.         final org.drip.exposure.universe.MarketPath marketPath)
  131.         throws java.lang.Exception
  132.     {
  133.         if (null == (_collateralGroupVertexArray = collateralGroupVertexArray) ||
  134.             null == (_marketPath = marketPath))
  135.         {
  136.             throw new java.lang.Exception ("CollateralGroupPath Constructor => Invalid Inputs");
  137.         }

  138.         _overnightReplicatorStart = _marketPath.epochalMarketVertex().overnightReplicator();

  139.         int vertexCount = _collateralGroupVertexArray.length;

  140.         if (1 >= vertexCount)
  141.         {
  142.             throw new java.lang.Exception ("CollateralGroupPath Constructor => Invalid Inputs");
  143.         }

  144.         for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  145.         {
  146.             if (null == _collateralGroupVertexArray[vertexIndex])
  147.             {
  148.                 throw new java.lang.Exception ("CollateralGroupPath Constructor => Invalid Inputs");
  149.             }

  150.             if (0 != vertexIndex && _collateralGroupVertexArray[vertexIndex - 1].vertexDate().julian() >=
  151.                 _collateralGroupVertexArray[vertexIndex].vertexDate().julian())
  152.             {
  153.                 throw new java.lang.Exception ("CollateralGroupPath Constructor => Invalid Inputs");
  154.             }
  155.         }
  156.     }

  157.     /**
  158.      * Retrieve the Array of Collateral Group Trajectory Vertexes
  159.      *
  160.      * @return The Array of Collateral Group Trajectory Vertexes
  161.      */

  162.     public org.drip.xva.hypothecation.CollateralGroupVertex[] collateralGroupVertex()
  163.     {
  164.         return _collateralGroupVertexArray;
  165.     }

  166.     /**
  167.      * Retrieve the Market Path
  168.      *
  169.      * @return The Market Path
  170.      */

  171.     public org.drip.exposure.universe.MarketPath marketPath()
  172.     {
  173.         return _marketPath;
  174.     }

  175.     /**
  176.      * Retrieve the Array of the Vertex Anchor Dates
  177.      *
  178.      * @return The Array of the Vertex Anchor Dates
  179.      */

  180.     public org.drip.analytics.date.JulianDate[] vertexDates()
  181.     {
  182.         int vertexCount = _collateralGroupVertexArray.length;
  183.         org.drip.analytics.date.JulianDate[] vertexDateArray = new
  184.             org.drip.analytics.date.JulianDate[vertexCount];

  185.         for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  186.         {
  187.             vertexDateArray[vertexIndex] = _collateralGroupVertexArray[vertexIndex].vertexDate();
  188.         }

  189.         return vertexDateArray;
  190.     }

  191.     /**
  192.      * Retrieve the Array of Vertex Collateralized Exposures
  193.      *
  194.      * @return The Array of Vertex Collateralized Exposures
  195.      */

  196.     public double[] vertexCollateralizedExposure()
  197.     {
  198.         int vertexCount = _collateralGroupVertexArray.length;
  199.         double[] collateralizedExposure = new double[vertexCount];

  200.         for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  201.         {
  202.             collateralizedExposure[vertexIndex] = _collateralGroupVertexArray[vertexIndex].collateralized();
  203.         }

  204.         return collateralizedExposure;
  205.     }

  206.     /**
  207.      * Retrieve the Array of Vertex Collateralized Exposure PV
  208.      *
  209.      * @return The Array of Vertex Collateralized Exposure PV
  210.      */

  211.     public double[] vertexCollateralizedExposurePV()
  212.     {
  213.         int vertexCount = _collateralGroupVertexArray.length;
  214.         double[] collateralizedExposurePV = new double[vertexCount];

  215.         org.drip.analytics.date.JulianDate[] vertexDateArray = _marketPath.anchorDates();

  216.         for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  217.         {
  218.             collateralizedExposurePV[vertexIndex] = _collateralGroupVertexArray[vertexIndex].collateralized()
  219.                 * _overnightReplicatorStart /
  220.                 _marketPath.marketVertex (vertexDateArray[vertexIndex].julian()).overnightReplicator();
  221.         }

  222.         return collateralizedExposurePV;
  223.     }

  224.     /**
  225.      * Retrieve the Array of Vertex Uncollateralized Exposures
  226.      *
  227.      * @return The Array of Vertex Uncollateralized Exposures
  228.      */

  229.     public double[] vertexUncollateralizedExposure()
  230.     {
  231.         int vertexCount = _collateralGroupVertexArray.length;
  232.         double[] uncollateralizedExposure = new double[vertexCount];

  233.         for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  234.         {
  235.             uncollateralizedExposure[vertexIndex] =
  236.                 _collateralGroupVertexArray[vertexIndex].uncollateralized();
  237.         }

  238.         return uncollateralizedExposure;
  239.     }

  240.     /**
  241.      * Retrieve the Array of Vertex Uncollateralized Exposure PV
  242.      *
  243.      * @return The Array of Vertex Uncollateralized Exposure PV
  244.      */

  245.     public double[] vertexUncollateralizedExposurePV()
  246.     {
  247.         int vertexCount = _collateralGroupVertexArray.length;
  248.         double[] uncollateralizedExposurePV = new double[vertexCount];

  249.         org.drip.analytics.date.JulianDate[] vertexDateArray = _marketPath.anchorDates();

  250.         for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  251.         {
  252.             uncollateralizedExposurePV[vertexIndex] =
  253.                 _collateralGroupVertexArray[vertexIndex].uncollateralized() *
  254.                 _overnightReplicatorStart /
  255.                 _marketPath.marketVertex (vertexDateArray[vertexIndex].julian()).overnightReplicator();
  256.         }

  257.         return uncollateralizedExposurePV;
  258.     }

  259.     /**
  260.      * Retrieve the Array of Vertex Credit Exposures
  261.      *
  262.      * @return The Array of Vertex Credit Exposures
  263.      */

  264.     public double[] vertexCreditExposure()
  265.     {
  266.         int vertexCount = _collateralGroupVertexArray.length;
  267.         double[] creditExposure = new double[vertexCount];

  268.         for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  269.         {
  270.             creditExposure[vertexIndex] = _collateralGroupVertexArray[vertexIndex].credit();
  271.         }

  272.         return creditExposure;
  273.     }

  274.     /**
  275.      * Retrieve the Array of Vertex Credit Exposure PV
  276.      *
  277.      * @return The Array of Vertex Credit Exposure PV
  278.      */

  279.     public double[] vertexCreditExposurePV()
  280.     {
  281.         int vertexCount = _collateralGroupVertexArray.length;
  282.         double[] creditExposurePV = new double[vertexCount];

  283.         org.drip.analytics.date.JulianDate[] vertexDateArray = _marketPath.anchorDates();

  284.         for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  285.         {
  286.             creditExposurePV[vertexIndex] = _collateralGroupVertexArray[vertexIndex].credit() *
  287.                 _overnightReplicatorStart /
  288.                 _marketPath.marketVertex (vertexDateArray[vertexIndex].julian()).overnightReplicator();
  289.         }

  290.         return creditExposurePV;
  291.     }

  292.     /**
  293.      * Retrieve the Array of Vertex Debt Exposures
  294.      *
  295.      * @return The Array of Vertex Debt Exposures
  296.      */

  297.     public double[] vertexDebtExposure()
  298.     {
  299.         int vertexCount = _collateralGroupVertexArray.length;
  300.         double[] debtExposure = new double[vertexCount];

  301.         for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  302.         {
  303.             debtExposure[vertexIndex] = _collateralGroupVertexArray[vertexIndex].debt();
  304.         }

  305.         return debtExposure;
  306.     }

  307.     /**
  308.      * Retrieve the Array of Vertex Debt Exposures PV
  309.      *
  310.      * @return The Array of Vertex Debt Exposures PV
  311.      */

  312.     public double[] vertexDebtExposurePV()
  313.     {
  314.         int vertexCount = _collateralGroupVertexArray.length;
  315.         double[] debtExposurePV = new double[vertexCount];

  316.         org.drip.analytics.date.JulianDate[] vertexDateArray = _marketPath.anchorDates();

  317.         for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  318.         {
  319.             debtExposurePV[vertexIndex] = _collateralGroupVertexArray[vertexIndex].debt() *
  320.                 _overnightReplicatorStart /
  321.                 _marketPath.marketVertex (vertexDateArray[vertexIndex].julian()).overnightReplicator();
  322.         }

  323.         return debtExposurePV;
  324.     }

  325.     /**
  326.      * Retrieve the Array of Vertex Funding Exposures
  327.      *
  328.      * @return The Array of Vertex Funding Exposures
  329.      */

  330.     public double[] vertexFundingExposure()
  331.     {
  332.         int vertexCount = _collateralGroupVertexArray.length;
  333.         double[] fundingExposure = new double[vertexCount];

  334.         for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  335.         {
  336.             fundingExposure[vertexIndex] = _collateralGroupVertexArray[vertexIndex].funding();
  337.         }

  338.         return fundingExposure;
  339.     }

  340.     /**
  341.      * Retrieve the Array of Vertex Funding Exposures PV
  342.      *
  343.      * @return The Array of Vertex Funding Exposures PV
  344.      */

  345.     public double[] vertexFundingExposurePV()
  346.     {
  347.         int vertexCount = _collateralGroupVertexArray.length;
  348.         double[] fundingExposurePV = new double[vertexCount];

  349.         org.drip.analytics.date.JulianDate[] vertexDateArray = _marketPath.anchorDates();

  350.         for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  351.         {
  352.             fundingExposurePV[vertexIndex] = _collateralGroupVertexArray[vertexIndex].funding() *
  353.                 _overnightReplicatorStart /
  354.                 _marketPath.marketVertex (vertexDateArray[vertexIndex].julian()).overnightReplicator();
  355.         }

  356.         return fundingExposurePV;
  357.     }

  358.     /**
  359.      * Retrieve the Array of Vertex Collateral Balances
  360.      *
  361.      * @return The Array of Vertex Collateral Balances
  362.      */

  363.     public double[] vertexCollateralBalance()
  364.     {
  365.         int vertexCount = _collateralGroupVertexArray.length;
  366.         double[] collateralizedBalance = new double[vertexCount];

  367.         for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  368.         {
  369.             collateralizedBalance[vertexIndex] =
  370.                 _collateralGroupVertexArray[vertexIndex].variationMarginPosting();
  371.         }

  372.         return collateralizedBalance;
  373.     }

  374.     /**
  375.      * Retrieve the Array of Vertex Collateral Balances PV
  376.      *
  377.      * @return The Array of Vertex Collateral Balances PV
  378.      */

  379.     public double[] vertexCollateralBalancePV()
  380.     {
  381.         int vertexCount = _collateralGroupVertexArray.length;
  382.         double[] collateralizedBalancePV = new double[vertexCount];

  383.         org.drip.analytics.date.JulianDate[] vertexDateArray = _marketPath.anchorDates();

  384.         for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  385.         {
  386.             collateralizedBalancePV[vertexIndex] =
  387.                 _collateralGroupVertexArray[vertexIndex].variationMarginPosting() * _overnightReplicatorStart /
  388.                 _marketPath.marketVertex (vertexDateArray[vertexIndex].julian()).overnightReplicator();
  389.         }

  390.         return collateralizedBalancePV;
  391.     }

  392.     /**
  393.      * Compute Period-wise Path Collateral Spread 01
  394.      *
  395.      * @return The Period-wise Path Collateral Spread 01
  396.      */

  397.     public double[] periodCollateralSpread01()
  398.     {
  399.         org.drip.analytics.date.JulianDate[] vertexDateArray = _marketPath.anchorDates();

  400.         double[] vertexCollateralBalancePV = vertexCollateralBalancePV();

  401.         int vertexCount = vertexCollateralBalancePV.length;
  402.         double[] periodCollateralValueAdjustment = new double[vertexCount - 1];

  403.         for (int vertexIndex = 1; vertexIndex < vertexCount; ++vertexIndex)
  404.         {
  405.             periodCollateralValueAdjustment[vertexIndex - 1] = -0.5 *
  406.                 (vertexCollateralBalancePV[vertexIndex - 1] + vertexCollateralBalancePV[vertexIndex]) *
  407.                 (vertexDateArray[vertexIndex].julian() - vertexDateArray[vertexIndex - 1].julian()) / 365.25;
  408.         }

  409.         return periodCollateralValueAdjustment;
  410.     }

  411.     /**
  412.      * Compute Period-wise Path Collateral Value Adjustment
  413.      *
  414.      * @return The Period-wise Path Collateral Value Adjustment
  415.      */

  416.     public double[] periodCollateralValueAdjustment()
  417.     {
  418.         org.drip.analytics.date.JulianDate[] vertexDateArray = _marketPath.anchorDates();

  419.         double[] vertexCollateralBalancePV = vertexCollateralBalancePV();

  420.         int vertexCount = vertexCollateralBalancePV.length;
  421.         double[] periodCollateralValueAdjustment = new double[vertexCount - 1];

  422.         for (int vertexIndex = 1; vertexIndex < vertexCount; ++vertexIndex)
  423.         {
  424.             int previousVertexDate = vertexDateArray[vertexIndex - 1].julian();

  425.             int currentVertexDate = vertexDateArray[vertexIndex].julian();

  426.             double periodIntegrandStart = vertexCollateralBalancePV[vertexIndex - 1] *
  427.                 _marketPath.marketVertex (previousVertexDate).csaSpread();

  428.             double periodIntegrandEnd = vertexCollateralBalancePV[vertexIndex] *
  429.                 _marketPath.marketVertex (currentVertexDate).csaSpread();

  430.             periodCollateralValueAdjustment[vertexIndex - 1] =
  431.                 -0.5 * (periodIntegrandStart + periodIntegrandEnd) *
  432.                 (currentVertexDate - previousVertexDate) / 365.25;
  433.         }

  434.         return periodCollateralValueAdjustment;
  435.     }
  436. }