PositionGroupTrajectory.java

  1. package org.drip.xva.dynamics;

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

  75. /**
  76.  * <i>PositionGroupTrajectory</i> generates the Customized Position Group Trajectories. The References are:
  77.  *
  78.  *  <br><br>
  79.  *  <ul>
  80.  *      <li>
  81.  *          Albanese, C., L. Andersen, and, S. Iabichino (2015): The FVA Puzzle: Accounting, Risk Management,
  82.  *              and Collateral Trading https://papers.ssrn.com/sol3/paper.cfm?abstract_id_2517301
  83.  *              <b>eSSRN</b>
  84.  *      </li>
  85.  *      <li>
  86.  *          Burgard, C., and M. Kjaer (2013): Funding Strategies, Funding Costs <i>Risk</i> <b>24 (12)</b>
  87.  *              82-87
  88.  *      </li>
  89.  *      <li>
  90.  *          Burgard, C., and M. Kjaer (2014): PDE Representations of Derivatives with Bilateral Counter-party
  91.  *              Risk and Funding Costs <i>Journal of Credit Risk</i> <b>7 (3)</b> 1-19
  92.  *      </li>
  93.  *      <li>
  94.  *          Burgard, C., and M. Kjaer (2014): In the Balance <i>Risk</i> <b>24 (11)</b> 72-75
  95.  *      </li>
  96.  *      <li>
  97.  *          Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives Pricing
  98.  *              <i>Risk</i> <b>21 (2)</b> 97-102
  99.  *      </li>
  100.  *  </ul>
  101.  *
  102.  *  <br><br>
  103.  *  <ul>
  104.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  105.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/XVAAnalyticsLibrary.md">XVA Analytics Library</a></li>
  106.  *      <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>
  107.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/xva/dynamics/README.md">XVA Dynamics - Settings and Evolution</a></li>
  108.  *  </ul>
  109.  * <br><br>
  110.  *
  111.  * @author Lakshmi Krishnamurthy
  112.  */

  113. public class PositionGroupTrajectory
  114. {
  115.     private double[][] _positionGroupArrayVertex = null;
  116.     private org.drip.exposure.universe.MarketPath _marketPath = null;
  117.     private org.drip.xva.proto.PositionGroupSpecification _positionGroupSpecification = null;

  118.     private org.drip.measure.bridge.BrokenDateInterpolator brokenDateInterpolator (
  119.         final int positionGroupIndex,
  120.         final int vertexIndex)
  121.     {
  122.         int brokenDateScheme = _positionGroupSpecification.brokenDateScheme();

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

  124.         try
  125.         {
  126.             if (org.drip.xva.settings.BrokenDateScheme.LINEAR_TIME == brokenDateScheme)
  127.             {
  128.                 return 0 == vertexIndex ? null : new org.drip.measure.bridge.BrokenDateInterpolatorLinearT (
  129.                     vertexDateArray[vertexIndex - 1].julian(),
  130.                     vertexDateArray[vertexIndex].julian(),
  131.                     _positionGroupArrayVertex[positionGroupIndex][vertexIndex - 1],
  132.                     _positionGroupArrayVertex[positionGroupIndex][vertexIndex]
  133.                 );
  134.             }

  135.             if (org.drip.xva.settings.BrokenDateScheme.SQUARE_ROOT_OF_TIME == brokenDateScheme)
  136.             {
  137.                 return 0 == vertexIndex ? null : new org.drip.measure.bridge.BrokenDateInterpolatorSqrtT (
  138.                     vertexDateArray[vertexIndex - 1].julian(),
  139.                     vertexDateArray[vertexIndex].julian(),
  140.                     _positionGroupArrayVertex[positionGroupIndex][vertexIndex - 1],
  141.                     _positionGroupArrayVertex[positionGroupIndex][vertexIndex]
  142.                 );
  143.             }

  144.             if (org.drip.xva.settings.BrokenDateScheme.THREE_POINT_BROWNIAN_BRIDGE == brokenDateScheme)
  145.             {
  146.                 return 0 == vertexIndex || 1 == vertexIndex ? null : new
  147.                     org.drip.measure.bridge.BrokenDateInterpolatorBrownian3P (
  148.                         vertexDateArray[vertexIndex - 2].julian(),
  149.                         vertexDateArray[vertexIndex - 1].julian(),
  150.                         vertexDateArray[vertexIndex].julian(),
  151.                         _positionGroupArrayVertex[positionGroupIndex][vertexIndex - 2],
  152.                         _positionGroupArrayVertex[positionGroupIndex][vertexIndex - 1],
  153.                         _positionGroupArrayVertex[positionGroupIndex][vertexIndex]
  154.                     );
  155.             }
  156.         }
  157.         catch (java.lang.Exception e)
  158.         {
  159.             e.printStackTrace();
  160.         }

  161.         return null;
  162.     }

  163.     private double collateralBalance (
  164.         final int positionGroupIndex,
  165.         final int vertexIndex)
  166.         throws java.lang.Exception
  167.     {
  168.         org.drip.measure.bridge.BrokenDateInterpolator brokenDateInterpolator = brokenDateInterpolator (
  169.             positionGroupIndex,
  170.             vertexIndex
  171.         );

  172.         return null == brokenDateInterpolator ? 0. : new org.drip.exposure.mpor.CollateralAmountEstimator (
  173.             _positionGroupSpecification,
  174.             brokenDateInterpolator,
  175.             java.lang.Double.NaN
  176.         ).postingRequirement (_marketPath.anchorDates()[vertexIndex]);
  177.     }

  178.     private double[][] positionGroupCollateralBalanceArray()
  179.     {
  180.         int vertexCount = _marketPath.anchorDates().length;

  181.         int positionGroupCount = _positionGroupArrayVertex.length;
  182.         double[][] collateralBalanceArray = new double[positionGroupCount][vertexCount];

  183.         for (int positionGroupIndex = 0; positionGroupIndex < positionGroupCount; ++positionGroupIndex)
  184.         {
  185.             for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  186.             {
  187.                 try
  188.                 {
  189.                     collateralBalanceArray[positionGroupIndex][vertexIndex] = collateralBalance (
  190.                         positionGroupIndex,
  191.                         vertexIndex
  192.                     );
  193.                 }
  194.                 catch (java.lang.Exception e)
  195.                 {
  196.                     e.printStackTrace();

  197.                     return null;
  198.                 }
  199.             }
  200.         }

  201.         return collateralBalanceArray;
  202.     }

  203.     private org.drip.xva.hypothecation.CollateralGroupVertex positionGroupVertex (
  204.         final org.drip.analytics.date.JulianDate anchorDate,
  205.         final double positionGroupValue,
  206.         final double realizedCashFlow,
  207.         final double collateralBalance,
  208.         final org.drip.exposure.universe.MarketVertex marketVertexLeft,
  209.         final org.drip.exposure.universe.MarketVertex marketVertexRight)
  210.     {
  211.         int closeOutScheme = _positionGroupSpecification.closeOutScheme();

  212.         int positionReplicationScheme = _positionGroupSpecification.positionReplicationScheme();

  213.         org.drip.xva.definition.CloseOut closeOut =
  214.             org.drip.xva.settings.CloseOutScheme.ISDA_92 == closeOutScheme ? null :
  215.                 org.drip.xva.definition.CloseOutBilateral.Market (marketVertexRight);

  216.         try
  217.         {
  218.             if (org.drip.xva.settings.PositionReplicationScheme.ALBANESE_ANDERSEN_VERTEX ==
  219.                 positionReplicationScheme)
  220.             {
  221.                 return new org.drip.xva.vertex.AlbaneseAndersen (
  222.                     anchorDate,
  223.                     positionGroupValue,
  224.                     realizedCashFlow,
  225.                     collateralBalance
  226.                 );
  227.             }

  228.             if (org.drip.xva.settings.PositionReplicationScheme.BURGARD_KJAER_HEDGE_ERROR_DUAL_BOND_VERTEX ==
  229.                 positionReplicationScheme)
  230.             {
  231.                 return null != marketVertexLeft ?
  232.                     org.drip.xva.vertex.BurgardKjaerBuilder.HedgeErrorDualBond (
  233.                         anchorDate,
  234.                         positionGroupValue,
  235.                         realizedCashFlow,
  236.                         collateralBalance,
  237.                         _positionGroupSpecification.hedgeError(),
  238.                         new org.drip.exposure.universe.MarketEdge (
  239.                             marketVertexLeft,
  240.                             marketVertexRight
  241.                         ),
  242.                         closeOut
  243.                     ) : org.drip.xva.vertex.BurgardKjaerBuilder.Initial (
  244.                         anchorDate,
  245.                         positionGroupValue,
  246.                         marketVertexRight,
  247.                         closeOut
  248.                     );
  249.             }

  250.             if (org.drip.xva.settings.PositionReplicationScheme.BURGARD_KJAER_SEMI_REPLICATION_DUAL_BOND_VERTEX
  251.                 == positionReplicationScheme)
  252.             {
  253.                 return null != marketVertexLeft ?
  254.                     org.drip.xva.vertex.BurgardKjaerBuilder.SemiReplicationDualBond (
  255.                         anchorDate,
  256.                         positionGroupValue,
  257.                         realizedCashFlow,
  258.                         collateralBalance,
  259.                         new org.drip.exposure.universe.MarketEdge (
  260.                             marketVertexLeft,
  261.                             marketVertexRight
  262.                         ),
  263.                         closeOut
  264.                     ) : org.drip.xva.vertex.BurgardKjaerBuilder.Initial (
  265.                         anchorDate,
  266.                         positionGroupValue,
  267.                         marketVertexRight,
  268.                         closeOut
  269.                     );
  270.             }

  271.             if (org.drip.xva.settings.PositionReplicationScheme.BURGARD_KJAER_GOLD_PLATED_TWO_WAY_CSA_VERTEX
  272.                 == positionReplicationScheme)
  273.             {
  274.                 return null != marketVertexLeft ?
  275.                     org.drip.xva.vertex.BurgardKjaerBuilder.GoldPlatedTwoWayCSA (
  276.                         anchorDate,
  277.                         positionGroupValue,
  278.                         realizedCashFlow,
  279.                         new org.drip.exposure.universe.MarketEdge (
  280.                             marketVertexLeft,
  281.                             marketVertexRight
  282.                         ),
  283.                         closeOut
  284.                     ) : org.drip.xva.vertex.BurgardKjaerBuilder.Initial (
  285.                         anchorDate,
  286.                         positionGroupValue,
  287.                         marketVertexRight,
  288.                         closeOut
  289.                     );
  290.             }

  291.             if (org.drip.xva.settings.PositionReplicationScheme.BURGARD_KJAER_ONE_WAY_CSA_VERTEX ==
  292.                 positionReplicationScheme)
  293.             {
  294.                 return null != marketVertexLeft ?
  295.                     org.drip.xva.vertex.BurgardKjaerBuilder.OneWayCSA (
  296.                         anchorDate,
  297.                         positionGroupValue,
  298.                         realizedCashFlow,
  299.                         new org.drip.exposure.universe.MarketEdge (
  300.                             marketVertexLeft,
  301.                             marketVertexRight
  302.                         ),
  303.                         closeOut
  304.                     ) : org.drip.xva.vertex.BurgardKjaerBuilder.Initial (
  305.                         anchorDate,
  306.                         positionGroupValue,
  307.                         marketVertexRight,
  308.                         closeOut
  309.                     );
  310.             }

  311.             if (org.drip.xva.settings.PositionReplicationScheme.BURGARD_KJAER_SET_OFF_VERTEX ==
  312.                 positionReplicationScheme)
  313.             {
  314.                 return null != marketVertexLeft ?
  315.                     org.drip.xva.vertex.BurgardKjaerBuilder.SetOff (
  316.                         anchorDate,
  317.                         positionGroupValue,
  318.                         realizedCashFlow,
  319.                         collateralBalance,
  320.                         new org.drip.exposure.universe.MarketEdge (
  321.                             marketVertexLeft,
  322.                             marketVertexRight
  323.                         )
  324.                     ) : org.drip.xva.vertex.BurgardKjaerBuilder.Initial (
  325.                         anchorDate,
  326.                         positionGroupValue,
  327.                         marketVertexRight,
  328.                         closeOut
  329.                     );
  330.             }
  331.         }
  332.         catch (java.lang.Exception e)
  333.         {
  334.             e.printStackTrace();
  335.         }

  336.         return null;
  337.     }

  338.     /**
  339.      * PositionGroupTrajectory Constructor
  340.      *
  341.      * @param positionGroupSpecification The Position Group Specification
  342.      * @param marketPath The Market Path
  343.      * @param positionGroupArrayVertex Vertexes of the Position Group Array
  344.      *
  345.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  346.      */

  347.     public PositionGroupTrajectory (
  348.         final org.drip.xva.proto.PositionGroupSpecification positionGroupSpecification,
  349.         final org.drip.exposure.universe.MarketPath marketPath,
  350.         final double[][] positionGroupArrayVertex)
  351.         throws java.lang.Exception
  352.     {
  353.         if (null == (_positionGroupSpecification = positionGroupSpecification) ||
  354.             null == (_marketPath = marketPath) ||
  355.             null == (_positionGroupArrayVertex = positionGroupArrayVertex))
  356.         {
  357.             throw new java.lang.Exception ("PositionGroupTrajectory Constructor => Invalid Inputs");
  358.         }

  359.         int positionGroupCount = _positionGroupArrayVertex.length;

  360.         if (0 == positionGroupCount)
  361.         {
  362.             throw new java.lang.Exception ("PositionGroupTrajectory Constructor => Invalid Inputs");
  363.         }

  364.         int vertexCount = _marketPath.anchorDates().length;

  365.         for (int positionGroupIndex = 0; positionGroupIndex < positionGroupCount; ++positionGroupIndex)
  366.         {
  367.             if (null == _positionGroupArrayVertex[positionGroupIndex] ||
  368.                 vertexCount != _positionGroupArrayVertex[positionGroupIndex].length ||
  369.                 !org.drip.numerical.common.NumberUtil.IsValid (_positionGroupArrayVertex[positionGroupIndex]))
  370.             {
  371.                 throw new java.lang.Exception ("PositionGroupTrajectory Constructor => Invalid Inputs");
  372.             }
  373.         }
  374.     }

  375.     /**
  376.      * Retrieve the Position Group Specification
  377.      *
  378.      * @return The Position Group Specification
  379.      */

  380.     public org.drip.xva.proto.PositionGroupSpecification positionGroupSpecification()
  381.     {
  382.         return _positionGroupSpecification;
  383.     }

  384.     /**
  385.      * Retrieve the Market Path
  386.      *
  387.      * @return The Market Path
  388.      */

  389.     public org.drip.exposure.universe.MarketPath marketPath()
  390.     {
  391.         return _marketPath;
  392.     }

  393.     /**
  394.      * Retrieve the Position Group Array Vertex Value
  395.      *
  396.      * @return The Position Group Array Vertex Value
  397.      */

  398.     public double[][] positionGroupArrayVertex()
  399.     {
  400.         return _positionGroupArrayVertex;
  401.     }

  402.     /**
  403.      * Generate the Position Collateral Group Vertex Array
  404.      *
  405.      * @return The Position Collateral Group Vertex Array
  406.      */

  407.     public org.drip.xva.hypothecation.CollateralGroupVertex[][] positionGroupVertexArray()
  408.     {
  409.         double[][] collateralBalanceArray = positionGroupCollateralBalanceArray();

  410.         if (null == collateralBalanceArray)
  411.         {
  412.             return null;
  413.         }

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

  415.         int vertexCount = vertexDateArray.length;
  416.         int positionGroupCount = _positionGroupArrayVertex.length;
  417.         org.drip.xva.hypothecation.CollateralGroupVertex[][] positionGroupVertexArray = new
  418.             org.drip.xva.hypothecation.CollateralGroupVertex[positionGroupCount][vertexCount];

  419.         for (int positionGroupIndex = 0; positionGroupIndex < positionGroupCount; ++positionGroupIndex)
  420.         {
  421.             for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  422.             {
  423.                 org.drip.analytics.date.JulianDate vertexDate = vertexDateArray[vertexIndex];

  424.                 try
  425.                 {
  426.                     positionGroupVertexArray[positionGroupIndex][vertexIndex] = positionGroupVertex (
  427.                         vertexDateArray[vertexIndex],
  428.                         _positionGroupArrayVertex[positionGroupIndex][vertexIndex],
  429.                         0.,
  430.                         collateralBalanceArray[positionGroupIndex][vertexIndex],
  431.                         0 == vertexIndex ? null :
  432.                             _marketPath.marketVertex (vertexDateArray[vertexIndex - 1].julian()),
  433.                             _marketPath.marketVertex (vertexDate.julian())
  434.                     );
  435.                 }
  436.                 catch (java.lang.Exception e)
  437.                 {
  438.                     e.printStackTrace();

  439.                     return null;
  440.                 }
  441.             }
  442.         }

  443.         return positionGroupVertexArray;
  444.     }
  445. }