LocalVolatilityRegressor.java

  1. package org.drip.sample.pykhtin2009;

  2. import org.drip.exposure.regression.LocalVolatilityGenerationControl;
  3. import org.drip.exposure.regression.PykhtinPillar;
  4. import org.drip.exposure.regression.PykhtinPillarDynamics;
  5. import org.drip.function.definition.R1ToR1;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.service.env.EnvManager;

  8. /*
  9.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  10.  */

  11. /*!
  12.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  13.  *
  14.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  15.  *      libraries targeting analysts and developers
  16.  *      https://lakshmidrip.github.io/DRIP/
  17.  *  
  18.  *  DRIP is composed of four main libraries:
  19.  *  
  20.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  21.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  22.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  23.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  24.  *
  25.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  26.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  27.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  28.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  29.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  30.  *
  31.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  32.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  33.  *
  34.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  35.  *
  36.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  37.  *
  38.  *  Licensed under the Apache License, Version 2.0 (the "License");
  39.  *      you may not use this file except in compliance with the License.
  40.  *  
  41.  *  You may obtain a copy of the License at
  42.  *      http://www.apache.org/licenses/LICENSE-2.0
  43.  *  
  44.  *  Unless required by applicable law or agreed to in writing, software
  45.  *      distributed under the License is distributed on an "AS IS" BASIS,
  46.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  47.  *  
  48.  *  See the License for the specific language governing permissions and
  49.  *      limitations under the License.
  50.  */

  51. /**
  52.  * LocalVolatilityRegressor is a Demonstration of the Exposure Regression Local Volatility Methodology of
  53.  *  Pykhtin (2009). The References are:
  54.  *  
  55.  *  - Andersen, L. B. G., M. Pykhtin, and A. Sokol (2017): Re-thinking Margin Period of Risk,
  56.  *      https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2902737, eSSRN.
  57.  *  
  58.  *  - Andersen, L. B. G., M. Pykhtin, and A. Sokol (2017): Credit Exposure in the Presence of Initial Margin,
  59.  *      https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2806156, eSSRN.
  60.  *  
  61.  *  - Albanese, C., and L. Andersen (2014): Accounting for OTC Derivatives: Funding Adjustments and the
  62.  *      Re-Hypothecation Option, eSSRN, https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2482955.
  63.  *  
  64.  *  - Burgard, C., and M. Kjaer (2017): Derivatives Funding, Netting, and Accounting, eSSRN,
  65.  *      https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2534011.
  66.  *
  67.  *  - Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives Pricing, Risk
  68.  *      21 (2) 97-102.
  69.  *
  70.  * @author Lakshmi Krishnamurthy
  71.  */

  72. public class LocalVolatilityRegressor
  73. {

  74.     public static final void main (
  75.         final String[] args)
  76.         throws Exception
  77.     {
  78.         EnvManager.InitEnv ("");

  79.         int exposureCount = 1000;
  80.         double exposureLow = 70.;
  81.         double exposureHigh = 130.;
  82.         double[] exposureArray = new double[exposureCount];

  83.         LocalVolatilityGenerationControl localVolatilityGenerationControl =
  84.             LocalVolatilityGenerationControl.Standard (exposureCount);

  85.         for (int exposureIndex = 0; exposureIndex < exposureCount; ++exposureIndex)
  86.         {
  87.             exposureArray[exposureIndex] = exposureLow + (exposureHigh - exposureLow) * Math.random();
  88.         }

  89.         PykhtinPillarDynamics vertexRealization = PykhtinPillarDynamics.Standard (exposureArray);

  90.         PykhtinPillar[] pillarVertexArray = vertexRealization.pillarVertexArray
  91.             (localVolatilityGenerationControl);

  92.         R1ToR1 localVolatilityR1ToR1 = vertexRealization.localVolatilityR1ToR1 (
  93.             localVolatilityGenerationControl,
  94.             pillarVertexArray
  95.         );

  96.         System.out.println ("\t||-----------------------------------------------------||");

  97.         System.out.println ("\t||       Pykhtin (2009) Terminal Brownian Bridge       ||");

  98.         System.out.println ("\t||-----------------------------------------------------||");

  99.         System.out.println ("\t||                                                     ||");

  100.         System.out.println ("\t||  L -> R:                                            ||");

  101.         System.out.println ("\t||                                                     ||");

  102.         System.out.println ("\t||      Terminal Numeraire                             ||");

  103.         System.out.println ("\t||      Ranking Ordinal                                ||");

  104.         System.out.println ("\t||      Uniform CDF                                    ||");

  105.         System.out.println ("\t||      Gaussian Predictor Variate                     ||");

  106.         System.out.println ("\t||      Local Volatility Estimate                      ||");

  107.         System.out.println ("\t||-----------------------------------------------------||");

  108.         for (PykhtinPillar pillarVertex : pillarVertexArray)
  109.         {
  110.             double exposure = pillarVertex.exposure();

  111.             System.out.println (
  112.                 "\t|| " +
  113.                 FormatUtil.FormatDouble (exposure, 3, 2, 1.) + " | " +
  114.                 FormatUtil.FormatDouble (pillarVertex.order(), 3, 0, 1.) + " | " +
  115.                 FormatUtil.FormatDouble (pillarVertex.cdf(), 1, 3, 1.) + " | " +
  116.                 FormatUtil.FormatDouble (pillarVertex.variate(), 1, 4, 1.) + " | " +
  117.                 FormatUtil.FormatDouble (pillarVertex.localVolatility(), 2, 2, 1.) + " | " +
  118.                 FormatUtil.FormatDouble (localVolatilityR1ToR1.evaluate (exposure), 2, 2, 1.) + " ||"
  119.             );
  120.         }

  121.         System.out.println ("\t||-----------------------------------------------------||");

  122.         EnvManager.TerminateEnv();
  123.     }
  124. }