PykhtinPillarDynamics.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>PykhtinPillarDynamics</i> generates the Dynamics off of the Pillar Vertex Exposure Realizations to be
  77.  * used in eventual Exposure Regression using the Pykhtin (2009) Scheme. The References are:
  78.  *  
  79.  * <br><br>
  80.  *      <ul>
  81.  *          <li>
  82.  *              Andersen, L. B. G., M. Pykhtin, and A. Sokol (2017): Re-thinking Margin Period of Risk
  83.  *                  https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2902737 <b>eSSRN</b>
  84.  *          </li>
  85.  *          <li>
  86.  *              Andersen, L. B. G., M. Pykhtin, and A. Sokol (2017): Credit Exposure in the Presence of
  87.  *                  Initial Margin https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2806156 <b>eSSRN</b>
  88.  *          </li>
  89.  *          <li>
  90.  *              Albanese, C., and L. Andersen (2014): Accounting for OTC Derivatives: Funding Adjustments and
  91.  *                  the Re-Hypothecation Option https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2482955
  92.  *                  <b>eSSRN</b>
  93.  *          </li>
  94.  *          <li>
  95.  *              Burgard, C., and M. Kjaer (2017): Derivatives Funding, Netting, and Accounting
  96.  *                  https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2534011 <b>eSSRN</b>
  97.  *          </li>
  98.  *          <li>
  99.  *              Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives
  100.  *                  Pricing <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/ExposureAnalyticsLibrary.md">Exposure Analytics</a></li>
  108.  *      <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>
  109.  *      <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>
  110.  *  </ul>
  111.  *
  112.  * @author Lakshmi Krishnamurthy
  113.  */

  114. public class PykhtinPillarDynamics
  115. {
  116.     private java.util.List<java.lang.Double> _exposureList = null;

  117.     /**
  118.      * Construct an Instance of PykhtinPillarDynamics from the Exposure Array
  119.      *
  120.      * @param exposureArray The Exposure Array
  121.      *
  122.      * @return The VertexRealization Instance
  123.      */

  124.     public static final PykhtinPillarDynamics Standard (
  125.         final double[] exposureArray)
  126.     {
  127.         if (null == exposureArray)
  128.         {
  129.             return null;
  130.         }

  131.         java.util.List<java.lang.Double> exposureList = new java.util.ArrayList<java.lang.Double>();

  132.         int exposureCount = exposureArray.length;

  133.         if (0 == exposureCount)
  134.         {
  135.             return null;
  136.         }

  137.         for (double exposure : exposureArray)
  138.         {
  139.             if (!org.drip.numerical.common.NumberUtil.IsValid (exposure))
  140.             {
  141.                 return null;
  142.             }

  143.             exposureList.add (exposure);
  144.         }

  145.         java.util.Collections.sort (exposureList);

  146.         try
  147.         {
  148.             return new PykhtinPillarDynamics (exposureList);
  149.         }
  150.         catch (java.lang.Exception e)
  151.         {
  152.             e.printStackTrace();
  153.         }

  154.         return null;
  155.     }

  156.     protected PykhtinPillarDynamics (
  157.         final java.util.List<java.lang.Double> exposureList)
  158.         throws java.lang.Exception
  159.     {
  160.         if (null == (_exposureList = exposureList) || 0 == _exposureList.size())
  161.         {
  162.             throw new java.lang.Exception ("PykhtinPillarVertexDynamics Constructor => Invalid Inputs");
  163.         }
  164.     }

  165.     /**
  166.      * Retrieve the Exposure Set
  167.      *
  168.      * @return The Exposure Set
  169.      */

  170.     public java.util.List<java.lang.Double> exposureList()
  171.     {
  172.         return _exposureList;
  173.     }

  174.     /**
  175.      * Retrieve the Pykhtin Pillar Vertex Array
  176.      *
  177.      * @param localVolatilityGenerationControl The Local Volatility Generation Control
  178.      *
  179.      * @return The Pykhtin Pillar Vertex Array
  180.      */

  181.     public org.drip.exposure.regression.PykhtinPillar[] pillarVertexArray (
  182.         final org.drip.exposure.regression.LocalVolatilityGenerationControl localVolatilityGenerationControl)
  183.     {
  184.         if (null == localVolatilityGenerationControl)
  185.         {
  186.             return null;
  187.         }

  188.         int realizationCount = _exposureList.size();

  189.         double[] uniformCPDArray = localVolatilityGenerationControl.uniformCPDArray();

  190.         int localVolatilityIndexShift = localVolatilityGenerationControl.localVolatilityIndexShift();

  191.         double[] impliedBrownianVariateArray = localVolatilityGenerationControl.impliedBrownianVariateArray();

  192.         int realizationIndex = 0;
  193.         double[] exposureArray = new double[realizationCount];
  194.         int localVolatilityIndexFloor = localVolatilityIndexShift;
  195.         double[] localVolatilityArray = new double[realizationCount];
  196.         int localVolatilityIndexCeiling = realizationCount - localVolatilityIndexShift;
  197.         org.drip.exposure.regression.PykhtinPillar[] pillarVertexArray = new
  198.             org.drip.exposure.regression.PykhtinPillar[realizationCount];

  199.         for (double exposure : _exposureList)
  200.         {
  201.             exposureArray[realizationIndex++] = exposure;
  202.         }

  203.         for (int realizationCoordinate = localVolatilityIndexFloor;
  204.             realizationCoordinate < localVolatilityIndexCeiling;
  205.             ++realizationCoordinate)
  206.         {
  207.             localVolatilityArray[realizationCoordinate] =
  208.                 (exposureArray[realizationCoordinate - localVolatilityIndexShift] -
  209.                     exposureArray[realizationCoordinate + localVolatilityIndexShift]) /
  210.                 (impliedBrownianVariateArray[realizationCoordinate - localVolatilityIndexShift] -
  211.                     impliedBrownianVariateArray[realizationCoordinate + localVolatilityIndexShift]);
  212.         }

  213.         for (int realizationCoordinate = 0;
  214.             realizationCoordinate < localVolatilityIndexFloor;
  215.             ++realizationCoordinate)
  216.         {
  217.             localVolatilityArray[realizationCoordinate] = localVolatilityArray[localVolatilityIndexFloor];
  218.         }

  219.         for (int realizationCoordinate = localVolatilityIndexCeiling;
  220.             realizationCoordinate < realizationCount;
  221.             ++realizationCoordinate)
  222.         {
  223.             localVolatilityArray[realizationCoordinate] =
  224.                 localVolatilityArray[localVolatilityIndexCeiling - 1];
  225.         }

  226.         for (int realizationCoordinate = 0; realizationCoordinate < realizationCount;
  227.             ++realizationCoordinate)
  228.         {
  229.             try
  230.             {
  231.                 pillarVertexArray[realizationCoordinate] =
  232.                     new org.drip.exposure.regression.PykhtinPillar (
  233.                         exposureArray[realizationCoordinate],
  234.                         realizationCoordinate,
  235.                         uniformCPDArray[realizationCoordinate],
  236.                         impliedBrownianVariateArray[realizationCoordinate],
  237.                         localVolatilityArray[realizationCoordinate]
  238.                     );

  239.                 ++realizationIndex;
  240.             }
  241.             catch (java.lang.Exception e)
  242.             {
  243.                 e.printStackTrace();

  244.                 return null;
  245.             }
  246.         }

  247.         return pillarVertexArray;
  248.     }

  249.     /**
  250.      * Generate a Local Volatility R^1 To R^1
  251.      *
  252.      * @param localVolatilityGenerationControl The Local Volatility Generation Control
  253.      * @param pillarVertexArray The Array of Pykhtin Pillar Vertexes
  254.      *
  255.      * @return The Local Volatility R^1 To R^1
  256.      */

  257.     public org.drip.function.definition.R1ToR1 localVolatilityR1ToR1 (
  258.         final org.drip.exposure.regression.LocalVolatilityGenerationControl localVolatilityGenerationControl,
  259.         final org.drip.exposure.regression.PykhtinPillar[] pillarVertexArray)
  260.     {
  261.         if (null == localVolatilityGenerationControl)
  262.         {
  263.             return null;
  264.         }

  265.         int vertexCount = pillarVertexArray.length;
  266.         double[] exposureArray = new double[vertexCount];
  267.         double[] localVolatilityArray = new double[vertexCount];

  268.         for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  269.         {
  270.             exposureArray[vertexIndex] = pillarVertexArray[vertexIndex].exposure();

  271.             localVolatilityArray[vertexIndex] = pillarVertexArray[vertexIndex].localVolatility();
  272.         }

  273.         org.drip.spline.stretch.MultiSegmentSequence multiSegmentSequence =
  274.             org.drip.spline.stretch.MultiSegmentSequenceBuilder.CreateCalibratedStretchEstimator (
  275.                 "LocalVolatilityR1ToR1_" + org.drip.numerical.common.StringUtil.GUID(),
  276.                 exposureArray,
  277.                 localVolatilityArray,
  278.                 localVolatilityGenerationControl.segmentCustomBuilderControlArray(),
  279.                 null,
  280.                 org.drip.spline.stretch.BoundarySettings.NaturalStandard(),
  281.                 org.drip.spline.stretch.MultiSegmentSequence.CALIBRATE
  282.             );

  283.         return null == multiSegmentSequence ? null : multiSegmentSequence.toAU();
  284.     }

  285.     /**
  286.      * Generate a Local Volatility R^1 To R^1
  287.      *
  288.      * @param localVolatilityGenerationControl The Local Volatility Generation Control
  289.      *
  290.      * @return The Local Volatility R^1 To R^1
  291.      */

  292.     public org.drip.function.definition.R1ToR1 localVolatilityR1ToR1 (
  293.         final org.drip.exposure.regression.LocalVolatilityGenerationControl localVolatilityGenerationControl)
  294.     {
  295.         return localVolatilityR1ToR1 (
  296.             localVolatilityGenerationControl,
  297.             pillarVertexArray (localVolatilityGenerationControl)
  298.         );
  299.     }
  300. }