AndersenPykhtinSokolStretch.java

  1. package org.drip.exposure.regression;

  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>AndersenPykhtinSokolStretch</i> generates the Regression Based Path Exposures off of the Pillar
  77.  * Vertexes using the Pykhtin (2009) Scheme. Eventual Unadjusted Variation Margin Calculation follows
  78.  * Andersen, Pykhtin, and Sokol (2017). The References are:
  79.  *  
  80.  * <br><br>
  81.  *      <ul>
  82.  *          <li>
  83.  *              Andersen, L. B. G., M. Pykhtin, and A. Sokol (2017): Re-thinking Margin Period of Risk
  84.  *                  https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2902737 <b>eSSRN</b>
  85.  *          </li>
  86.  *          <li>
  87.  *              Andersen, L. B. G., M. Pykhtin, and A. Sokol (2017): Credit Exposure in the Presence of
  88.  *                  Initial Margin https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2806156 <b>eSSRN</b>
  89.  *          </li>
  90.  *          <li>
  91.  *              Albanese, C., and L. Andersen (2014): Accounting for OTC Derivatives: Funding Adjustments and
  92.  *                  the Re-Hypothecation Option https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2482955
  93.  *                  <b>eSSRN</b>
  94.  *          </li>
  95.  *          <li>
  96.  *              Burgard, C., and M. Kjaer (2017): Derivatives Funding, Netting, and Accounting
  97.  *                  https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2534011 <b>eSSRN</b>
  98.  *          </li>
  99.  *          <li>
  100.  *              Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives
  101.  *                  Pricing <i>Risk</i> <b>21 (2)</b> 97-102
  102.  *          </li>
  103.  *      </ul>
  104.  *
  105.  *  <br><br>
  106.  *  <ul>
  107.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  108.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ExposureAnalyticsLibrary.md">Exposure Analytics</a></li>
  109.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/exposure/README.md">Exposure Group Level Collateralized/Uncollateralized Exposure</a></li>
  110.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/exposure/regression/README.md">Regression Based Path Exposure Generation</a></li>
  111.  *  </ul>
  112.  *
  113.  * @author Lakshmi Krishnamurthy
  114.  */

  115. public class AndersenPykhtinSokolStretch
  116. {
  117.     private int[] _sparseDateArray = null;
  118.     private double[] _sparseExposureArray = null;
  119.     private org.drip.exposure.mpor.TradePayment[] _denseTradePaymentArray = null;
  120.     private org.drip.function.definition.R1ToR1[] _sparseLocalVolatilityArray = null;

  121.     /**
  122.      * AndersenPykhtinSokolStretch Constructor
  123.      *
  124.      * @param sparseDateArray Array of Sparse Exposure Dates
  125.      * @param sparseExposureArray Array of Sparse Exposures
  126.      * @param sparseLocalVolatilityArray Array of Sparse Local Volatility R1 To R1 Functions
  127.      * @param denseTradePaymentArray Array of Dense Trade Payments
  128.      *
  129.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  130.      */

  131.     public AndersenPykhtinSokolStretch (
  132.         final int[] sparseDateArray,
  133.         final double[] sparseExposureArray,
  134.         final org.drip.function.definition.R1ToR1[] sparseLocalVolatilityArray,
  135.         final org.drip.exposure.mpor.TradePayment[] denseTradePaymentArray)
  136.         throws java.lang.Exception
  137.     {
  138.         if (null == (_sparseDateArray = sparseDateArray) ||
  139.             null == (_sparseExposureArray = sparseExposureArray) ||
  140.             null == (_sparseLocalVolatilityArray = sparseLocalVolatilityArray) ||
  141.             null == (_denseTradePaymentArray = denseTradePaymentArray))
  142.         {
  143.             throw new java.lang.Exception ("AndersenPykhtinSokolStretch Constructor => Invalid Inputs");
  144.         }

  145.         int sparseExposureDateCount = _sparseDateArray.length;
  146.         int denseExposureDateCount = _denseTradePaymentArray.length;

  147.         if (0 == sparseExposureDateCount ||
  148.             sparseExposureDateCount != _sparseExposureArray.length ||
  149.             !org.drip.numerical.common.NumberUtil.IsValid (_sparseExposureArray) ||
  150.             sparseExposureDateCount != _sparseLocalVolatilityArray.length ||
  151.             sparseExposureDateCount > denseExposureDateCount)
  152.         {
  153.             throw new java.lang.Exception ("AndersenPykhtinSokolStretch Constructor => Invalid Inputs");
  154.         }

  155.         for (int sparseExposureDateIndex = 0;
  156.             sparseExposureDateIndex < sparseExposureDateCount;
  157.             ++sparseExposureDateIndex)
  158.         {
  159.             if (null == _sparseLocalVolatilityArray[sparseExposureDateIndex])
  160.             {
  161.                 throw new java.lang.Exception ("AndersenPykhtinSokolStretch Constructor => Invalid Inputs");
  162.             }
  163.         }

  164.         for (int denseExposureDateIndex = 0;
  165.             denseExposureDateIndex < denseExposureDateCount;
  166.             ++denseExposureDateIndex)
  167.         {
  168.             if (null == _denseTradePaymentArray[denseExposureDateIndex])
  169.             {
  170.                 throw new java.lang.Exception ("AndersenPykhtinSokolStretch Constructor => Invalid Inputs");
  171.             }
  172.         }
  173.     }

  174.     /**
  175.      * Retrieve the Sparse Exposure Date Array
  176.      *
  177.      * @return The Sparse Exposure Date Array
  178.      */

  179.     public int[] sparseDateArray()
  180.     {
  181.         return _sparseDateArray;
  182.     }

  183.     /**
  184.      * Retrieve the Sparse Exposure Array
  185.      *
  186.      * @return The Sparse Exposure Array
  187.      */

  188.     public double[] sparseExposureArray()
  189.     {
  190.         return _sparseExposureArray;
  191.     }

  192.     /**
  193.      * Retrieve the Sparse Local Volatility R1 To R1 Array
  194.      *
  195.      * @return The Sparse Local Volatility R1 To R1 Array
  196.      */

  197.     public org.drip.function.definition.R1ToR1[] sparseLocalVolatilityArray()
  198.     {
  199.         return _sparseLocalVolatilityArray;
  200.     }

  201.     /**
  202.      * Retrieve the Dense Trade Payment Array
  203.      *
  204.      * @return The Dense Trade Payment Array
  205.      */

  206.     public org.drip.exposure.mpor.TradePayment[] denseTradePaymentArray()
  207.     {
  208.         return _denseTradePaymentArray;
  209.     }

  210.     /**
  211.      * Generate the Dense (Complete) Segment Exposures
  212.      *
  213.      * @param wanderTrajectory The Wander Date Trajectory
  214.      *
  215.      * @return The Dense (Complete) Segment Exposures
  216.      */

  217.     public double[] denseExposure (
  218.         final double[] wanderTrajectory)
  219.     {
  220.         int epochDate = _sparseDateArray[0];
  221.         int sparseExposureDateCount = _sparseDateArray.length;
  222.         int denseExposureDateCount = _denseTradePaymentArray.length;
  223.         double[] denseExposureTrajectory = new double[denseExposureDateCount];

  224.         for (int sparseExposureDateIndex = 1;
  225.             sparseExposureDateIndex < sparseExposureDateCount;
  226.             ++sparseExposureDateIndex)
  227.         {
  228.             try
  229.             {
  230.                 new AndersenPykhtinSokolSegment (
  231.                     epochDate,
  232.                     new org.drip.exposure.regression.PillarVertex (
  233.                         _sparseDateArray[sparseExposureDateIndex - 1],
  234.                         _sparseExposureArray[sparseExposureDateIndex - 1]
  235.                     ),
  236.                     new org.drip.exposure.regression.PillarVertex (
  237.                         _sparseDateArray[sparseExposureDateIndex],
  238.                         _sparseExposureArray[sparseExposureDateIndex]
  239.                     ),
  240.                     _sparseLocalVolatilityArray[sparseExposureDateIndex]
  241.                 ).denseExposureTrajectoryUpdate (
  242.                     denseExposureTrajectory,
  243.                     wanderTrajectory
  244.                 );
  245.             }
  246.             catch (java.lang.Exception e)
  247.             {
  248.                 e.printStackTrace();

  249.                 return null;
  250.             }
  251.         }

  252.         for (int denseExposureDateIndex = 0;
  253.             denseExposureDateIndex < denseExposureDateCount;
  254.             ++denseExposureDateIndex)
  255.         {
  256.             org.drip.exposure.mpor.TradePayment tradePayment =
  257.                 _denseTradePaymentArray[denseExposureDateIndex];

  258.             denseExposureTrajectory[denseExposureDateIndex] = denseExposureTrajectory[denseExposureDateIndex]
  259.                 + tradePayment.dealer() - tradePayment.client();
  260.         }

  261.         return denseExposureTrajectory;
  262.     }
  263. }