CSAImpliedMeasureDifference.java

  1. package org.drip.sample.piterbarg2010;

  2. import java.util.Map;

  3. import org.drip.analytics.date.*;
  4. import org.drip.exposure.csadynamics.*;
  5. import org.drip.function.r1tor1.FlatUnivariate;
  6. import org.drip.measure.dynamics.*;
  7. import org.drip.numerical.common.FormatUtil;
  8. import org.drip.param.valuation.ValuationParams;
  9. import org.drip.pricer.option.BlackScholesAlgorithm;
  10. import org.drip.product.option.EuropeanCallPut;
  11. import org.drip.service.env.EnvManager;
  12. import org.drip.service.template.LatentMarketStateBuilder;
  13. import org.drip.state.discount.MergedDiscountForwardCurve;

  14. /*
  15.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  16.  */

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

  58. /**
  59.  * CSAImpliedMeasureDifference compares the Differences between the CSA and the non-CSA Implied Distribution,
  60.  *  expressed in Implied Volatilities across Strikes, and across Correlations. The References are:
  61.  *  
  62.  *  - Barden, P. (2009): Equity Forward Prices in the Presence of Funding Spreads, ICBI Conference, Rome.
  63.  *  
  64.  *  - Burgard, C., and M. Kjaer (2009): Modeling and successful Management of Credit Counter-party Risk of
  65.  *      Derivative Portfolios, ICBI Conference, Rome.
  66.  *  
  67.  *  - Gregory, J. (2009): Being Two-faced over Counter-party Credit Risk, Risk 20 (2) 86-90.
  68.  *  
  69.  *  - Johannes, M., and S. Sundaresan (2007): Pricing Collateralized Swaps, Journal of Finance 62 383-410.
  70.  *
  71.  *  - Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives Pricing, Risk
  72.  *      21 (2) 97-102.
  73.  *
  74.  * @author Lakshmi Krishnamurthy
  75.  */

  76. public class CSAImpliedMeasureDifference {

  77.     private static final MergedDiscountForwardCurve OvernightCurve (
  78.         final String strCurrency,
  79.         final JulianDate dtSpot)
  80.         throws Exception
  81.     {
  82.         String[] astrDepositMaturityTenor = new String[] {
  83.             "1D",
  84.             // "2D",
  85.             "3D"
  86.         };

  87.         double[] adblDepositQuote = new double[] {
  88.             0.0004,     // 1D
  89.             // 0.0004,      // 2D
  90.             0.0004      // 3D
  91.         };

  92.         String[] astrShortEndOISMaturityTenor = new String[] {
  93.             "1W",
  94.             "2W",
  95.             "3W",
  96.             "1M"
  97.         };

  98.         double[] adblShortEndOISQuote = new double[] {
  99.             0.00070,    //   1W
  100.             0.00069,    //   2W
  101.             0.00078,    //   3W
  102.             0.00074     //   1M
  103.         };

  104.         String[] astrOISFuturesEffectiveTenor = new String[] {
  105.             "1M",
  106.             "2M",
  107.             "3M",
  108.             "4M",
  109.             "5M"
  110.         };

  111.         String[] astrOISFuturesMaturityTenor = new String[] {
  112.             "1M",
  113.             "1M",
  114.             "1M",
  115.             "1M",
  116.             "1M"
  117.         };

  118.         double[] adblOISFuturesQuote = new double[] {
  119.              0.00046,    //   1M x 1M
  120.              0.00016,    //   2M x 1M
  121.             -0.00007,    //   3M x 1M
  122.             -0.00013,    //   4M x 1M
  123.             -0.00014     //   5M x 1M
  124.         };

  125.         String[] astrLongEndOISMaturityTenor = new String[] {
  126.             "15M",
  127.             "18M",
  128.             "21M",
  129.             "02Y",
  130.             "03Y",
  131.             "04Y",
  132.             "05Y",
  133.             "06Y",
  134.             "07Y",
  135.             "08Y",
  136.             "09Y",
  137.             "10Y",
  138.             "11Y",
  139.             "12Y",
  140.             "15Y",
  141.             "20Y",
  142.             "25Y",
  143.             "30Y"
  144.         };

  145.         double[] adblLongEndOISQuote = new double[] {
  146.             0.00002,    //  15M
  147.             0.00008,    //  18M
  148.             0.00021,    //  21M
  149.             0.00036,    //   2Y
  150.             0.00127,    //   3Y
  151.             0.00274,    //   4Y
  152.             0.00456,    //   5Y
  153.             0.00647,    //   6Y
  154.             0.00827,    //   7Y
  155.             0.00996,    //   8Y
  156.             0.01147,    //   9Y
  157.             0.01280,    //  10Y
  158.             0.01404,    //  11Y
  159.             0.01516,    //  12Y
  160.             0.01764,    //  15Y
  161.             0.01939,    //  20Y
  162.             0.02003,    //  25Y
  163.             0.02038     //  30Y
  164.         };

  165.         return LatentMarketStateBuilder.SmoothOvernightCurve (
  166.             dtSpot,
  167.             strCurrency,
  168.             astrDepositMaturityTenor,
  169.             adblDepositQuote,
  170.             "Rate",
  171.             astrShortEndOISMaturityTenor,
  172.             adblShortEndOISQuote,
  173.             "SwapRate",
  174.             astrOISFuturesEffectiveTenor,
  175.             astrOISFuturesMaturityTenor,
  176.             adblOISFuturesQuote,
  177.             "SwapRate",
  178.             astrLongEndOISMaturityTenor,
  179.             adblLongEndOISQuote,
  180.             "SwapRate"
  181.         );
  182.     }

  183.     public static final void main (
  184.         final String[] astrArgs)
  185.         throws Exception
  186.     {
  187.         EnvManager.InitEnv ("");

  188.         String strTenor = "10Y";
  189.         double dblCSAForward = 100.;
  190.         double dblFundingSpreadVolatility = 0.015;
  191.         double dblFundingSpreadMeanReversionRate = 0.05;
  192.         String strCurrency = "USD";

  193.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  194.             2017,
  195.             DateUtil.DECEMBER,
  196.             21
  197.         );

  198.         double[] adblCorrelation = new double[] {
  199.             -0.30,
  200.             -0.10,
  201.              0.00,
  202.              0.10
  203.         };

  204.         double[] adblStrike = new double[] {
  205.              50.,
  206.              60.,
  207.              70.,
  208.              80.,
  209.              90.,
  210.             100.,
  211.             110.,
  212.             120.,
  213.             130.,
  214.             140.,
  215.             150.
  216.         };

  217.         double[] adblCSAImpliedVolatility = new double[] {
  218.              0.30,
  219.              0.30,
  220.              0.30,
  221.              0.30,
  222.              0.30,
  223.              0.30,
  224.              0.30,
  225.              0.30,
  226.              0.30,
  227.              0.30,
  228.              0.30
  229.         };

  230.         double[][] aadblNoCSAForward = new double[adblCorrelation.length][adblStrike.length];
  231.         double[][] aadblMeasureShiftScale = new double[adblCorrelation.length][adblStrike.length];

  232.         DiffusionEvaluatorMeanReversion demrFundingSpread = DiffusionEvaluatorMeanReversion.Standard (
  233.             dblFundingSpreadMeanReversionRate,
  234.             0.,
  235.             dblFundingSpreadVolatility
  236.         );

  237.         System.out.println ();

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

  239.         System.out.println ("\t||                                        PROBABILITY MEASURE DISTRIBUTION SHIFT                                        ||");

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

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

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

  243.         System.out.println ("\t||           - Adjustments for Strikes in unit of 10, from 50 to 150                                                    ||");

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

  245.         for (int j = 0; j < adblCorrelation.length; ++j) {
  246.             String strDump = "\t|| " + FormatUtil.FormatDouble (adblCorrelation[j], 2, 0, 100.) + "% => ";

  247.             for (int i = 0; i < adblStrike.length; ++i) {
  248.                 DiffusionEvaluatorLogarithmic delUnderlying = DiffusionEvaluatorLogarithmic.Standard (
  249.                     0.,
  250.                     adblCSAImpliedVolatility[i]
  251.                 );

  252.                 FundingBasisEvolver fbe = new FundingBasisEvolver (
  253.                     delUnderlying,
  254.                     demrFundingSpread,
  255.                     adblCorrelation[j]
  256.                 );

  257.                 aadblNoCSAForward[j][i] = dblCSAForward * fbe.CSANoCSARatio (strTenor);

  258.                 NumeraireInducedMeasureShift nims = new NumeraireInducedMeasureShift (
  259.                     dblCSAForward,
  260.                     aadblNoCSAForward[j][i],
  261.                     dblCSAForward * dblCSAForward * adblCSAImpliedVolatility[i] * adblCSAImpliedVolatility[i]
  262.                 );

  263.                 aadblMeasureShiftScale[j][i] = nims.densityRescale (adblStrike[i]);

  264.                 strDump = strDump + " " + FormatUtil.FormatDouble (aadblMeasureShiftScale[j][i], 1, 4, 1.) + " |";
  265.             }

  266.             System.out.println (strDump + "|");;
  267.         }

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

  269.         System.out.println ();

  270.         MergedDiscountForwardCurve dcOvernight = OvernightCurve (
  271.             strCurrency,
  272.             dtSpot
  273.         );

  274.         JulianDate dtMaturity = dtSpot.addTenor (strTenor);

  275.         ValuationParams valParams = new ValuationParams (
  276.             dtSpot,
  277.             dtSpot,
  278.             strCurrency
  279.         );

  280.         double[] adblPrice = new double[adblStrike.length];
  281.         EuropeanCallPut[] aECP = new EuropeanCallPut[adblStrike.length];

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

  283.         String strDump = "\t|| CSA ATM Option Price => ";

  284.         for (int i = 0; i < adblStrike.length; ++i) {
  285.             aECP[i] = new EuropeanCallPut (
  286.                 dtMaturity,
  287.                 adblStrike[i]
  288.             );

  289.             Map<String, Double> mapOptionCalc = aECP[i].value (
  290.                 valParams,
  291.                 dblCSAForward,
  292.                 true,
  293.                 dcOvernight,
  294.                 new FlatUnivariate (adblCSAImpliedVolatility[i]),
  295.                 new BlackScholesAlgorithm()
  296.             );

  297.             adblPrice[i] = mapOptionCalc.get ("CallPrice");

  298.             strDump = strDump + FormatUtil.FormatDouble (adblPrice[i], 2, 2, 1.) + "  |";
  299.         }

  300.         System.out.println (strDump + "|");

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

  302.         System.out.println ();

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

  304.         System.out.println ("\t||                                CSA CONVEXITY ADJUSTMENT IMPLIED VOLATILITY                                ||");

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

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

  307.         System.out.println ("\t||           - Implied Volatility (%)                                                                        ||");

  308.         System.out.println ("\t||           - Adjustments for Strikes in unit of 10, from 50 to 150                                         ||");

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

  310.         for (int j = 0; j < adblCorrelation.length; ++j) {
  311.             strDump = "\t|| " + FormatUtil.FormatDouble (adblCorrelation[j], 2, 0, 100.) + "% => ";

  312.             for (int i = 0; i < adblStrike.length; ++i) {
  313.                 double dblReimpliedVolatility = aECP[i].implyVolatilityFromCallPrice (
  314.                     valParams,
  315.                     aadblNoCSAForward[j][i],
  316.                     true,
  317.                     dcOvernight,
  318.                     adblPrice[i]
  319.                 );

  320.                 strDump = strDump + FormatUtil.FormatDouble (dblReimpliedVolatility, 2, 2, 100.) + "% |";
  321.             }

  322.             System.out.println (strDump + "|");
  323.         }

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

  325.         System.out.println ();
  326.     }
  327. }