LocalVolatilityGenerationControl.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>LocalVolatilityGenerationControl</i> holds the Parameters the control the Calculation of the Local
  77.  * Volatility in the Pykhtin (2009) Brownian Bridge Calibration. 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 LocalVolatilityGenerationControl
  115. {

  116.     /**
  117.      * The Pyhktin (2009) Empirical Floor
  118.      */

  119.     public static final int PYKHTIN_2009_EMPIRICAL_FLOOR = 20;

  120.     /**
  121.      * The Pyhktin (2009) Empirical Ceiling Factor
  122.      */

  123.     public static final double PYKHTIN_2009_EMPIRICAL_CEILING_FACTOR = 0.05;

  124.     /**
  125.      * The Local Volatility Smooth Floor Bias
  126.      */

  127.     public static final double LOCAL_VOLATILITY_SMOOTHING_FLOOR_BIAS = 0.90;

  128.     private double[] _uniformCPDArray = null;
  129.     private int _localVolatilityIndexShift = -1;
  130.     private double[] _impliedBrownianVariateArray = null;
  131.     private org.drip.spline.params.SegmentCustomBuilderControl[] _segmentCustomBuilderControlArray = null;

  132.     /**
  133.      * Construct a Standard Instance of LocalVolatilityGenerationControl
  134.      *
  135.      * @param ensembleSize Size of the Distribution Ensemble
  136.      *
  137.      * @return Standard Instance of LocalVolatilityGenerationControl
  138.      */

  139.     public static final LocalVolatilityGenerationControl Standard (
  140.         final int ensembleSize)
  141.     {
  142.         if (PYKHTIN_2009_EMPIRICAL_FLOOR > ensembleSize)
  143.         {
  144.             return null;
  145.         }

  146.         double[] uniformCPDArray = new double[ensembleSize];
  147.         double[] impliedBrownianVariateArray = new double[ensembleSize];
  148.         int localVolatilityIndexShift = (int) (LOCAL_VOLATILITY_SMOOTHING_FLOOR_BIAS *
  149.             PYKHTIN_2009_EMPIRICAL_FLOOR + (1 - LOCAL_VOLATILITY_SMOOTHING_FLOOR_BIAS) * ensembleSize);

  150.         if (PYKHTIN_2009_EMPIRICAL_FLOOR > localVolatilityIndexShift)
  151.         {
  152.             return null;
  153.         }

  154.         for (int realizationCoordinate = 0;
  155.             realizationCoordinate < ensembleSize;
  156.             ++realizationCoordinate)
  157.         {
  158.             try
  159.             {
  160.                 impliedBrownianVariateArray[realizationCoordinate] =
  161.                     org.drip.measure.gaussian.NormalQuadrature.InverseCDF
  162.                         (uniformCPDArray[realizationCoordinate] = (((double) realizationCoordinate) + 0.5) /
  163.                             ((double) ensembleSize));
  164.             }
  165.             catch (java.lang.Exception e)
  166.             {
  167.                 e.printStackTrace();

  168.                 return null;
  169.             }
  170.         }

  171.         try
  172.         {
  173.             org.drip.spline.params.SegmentCustomBuilderControl[] segmentCustomBuilderControlArray = new
  174.                 org.drip.spline.params.SegmentCustomBuilderControl[ensembleSize - 1];
  175.             org.drip.spline.params.SegmentCustomBuilderControl segmentCustomBuilderControl = new
  176.                 org.drip.spline.params.SegmentCustomBuilderControl (
  177.                     org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  178.                     new org.drip.spline.basis.PolynomialFunctionSetParams (2),
  179.                     org.drip.spline.params.SegmentInelasticDesignControl.Create (
  180.                         0,
  181.                         2
  182.                     ),
  183.                     new org.drip.spline.params.ResponseScalingShapeControl (
  184.                         true,
  185.                         new org.drip.function.r1tor1.QuadraticRationalShapeControl (0.)
  186.                     ),
  187.                     null
  188.                 );

  189.             for (int realizationCoordinate = 0;
  190.                 realizationCoordinate < ensembleSize - 1;
  191.                 ++realizationCoordinate)
  192.             {
  193.                 segmentCustomBuilderControlArray[realizationCoordinate] = segmentCustomBuilderControl;
  194.             }

  195.             return new LocalVolatilityGenerationControl (
  196.                 localVolatilityIndexShift,
  197.                 uniformCPDArray,
  198.                 impliedBrownianVariateArray,
  199.                 segmentCustomBuilderControlArray
  200.             );
  201.         }
  202.         catch (java.lang.Exception e)
  203.         {
  204.             e.printStackTrace();
  205.         }

  206.         return null;
  207.     }

  208.     /**
  209.      * LocalVolatilityGenerationControl Constructor
  210.      *
  211.      * @param localVolatilityIndexShift The Local Volatility Index Shift
  212.      * @param uniformCPDArray The Uniform Cumulative Probability Density Array
  213.      * @param impliedBrownianVariateArray The Implied Brownian Variate Array
  214.      * @param segmentCustomBuilderControlArray Array of Segment Builder Control
  215.      *
  216.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  217.      */

  218.     public LocalVolatilityGenerationControl (
  219.         final int localVolatilityIndexShift,
  220.         final double[] uniformCPDArray,
  221.         final double[] impliedBrownianVariateArray,
  222.         final org.drip.spline.params.SegmentCustomBuilderControl[] segmentCustomBuilderControlArray)
  223.         throws java.lang.Exception
  224.     {
  225.         if (0 >= (_localVolatilityIndexShift = localVolatilityIndexShift) ||
  226.             null == (_uniformCPDArray = uniformCPDArray) ||
  227.             null == (_impliedBrownianVariateArray = impliedBrownianVariateArray) ||
  228.             null == (_segmentCustomBuilderControlArray = segmentCustomBuilderControlArray))
  229.         {
  230.             throw new java.lang.Exception ("LocalVolatilityGenerationControl Constructor => Invalid Inputs");
  231.         }

  232.         int uniformCPDArraySize = _uniformCPDArray.length;

  233.         if (0 == uniformCPDArraySize ||
  234.             uniformCPDArraySize != _impliedBrownianVariateArray.length ||
  235.             uniformCPDArraySize != _segmentCustomBuilderControlArray.length + 1)
  236.         {
  237.             throw new java.lang.Exception ("LocalVolatilityGenerationControl Constructor => Invalid Inputs");
  238.         }
  239.     }

  240.     /**
  241.      * Retrieve the Local Volatility Index Shift
  242.      *
  243.      * @return The Local Volatility Index Shift
  244.      */

  245.     public int localVolatilityIndexShift()
  246.     {
  247.         return _localVolatilityIndexShift;
  248.     }

  249.     /**
  250.      * Retrieve the Uniform Cumulative Probability Density Array
  251.      *
  252.      * @return The Uniform Cumulative Probability Density Array
  253.      */

  254.     public double[] uniformCPDArray()
  255.     {
  256.         return _uniformCPDArray;
  257.     }

  258.     /**
  259.      * Retrieve the Implied Brownian Variate Array
  260.      *
  261.      * @return The Implied Brownian Variate Array
  262.      */

  263.     public double[] impliedBrownianVariateArray()
  264.     {
  265.         return _impliedBrownianVariateArray;
  266.     }

  267.     /**
  268.      * Retrieve the Custom Segment Builder Control Array
  269.      *
  270.      * @return The Custom Segment Builder Control Array
  271.      */

  272.     public org.drip.spline.params.SegmentCustomBuilderControl[] segmentCustomBuilderControlArray()
  273.     {
  274.         return _segmentCustomBuilderControlArray;
  275.     }
  276. }