CSAFundingRelativeForward.java

  1. package org.drip.sample.piterbarg2010;

  2. import org.drip.exposure.csadynamics.FundingBasisEvolver;
  3. import org.drip.measure.dynamics.*;
  4. import org.drip.numerical.common.FormatUtil;
  5. import org.drip.service.env.EnvManager;

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

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

  50. /**
  51.  * CSAFundingRelativeForward compares the Relative Differences between the CSA and the non-CSA Forward Prices
  52.  *  under a Stochastic Funding Model. The References are:
  53.  *  
  54.  *  - Barden, P. (2009): Equity Forward Prices in the Presence of Funding Spreads, ICBI Conference, Rome.
  55.  *  
  56.  *  - Burgard, C., and M. Kjaer (2009): Modeling and successful Management of Credit Counter-party Risk of
  57.  *      Derivative Portfolios, ICBI Conference, Rome.
  58.  *  
  59.  *  - Gregory, J. (2009): Being Two-faced over Counter-party Credit Risk, Risk 20 (2) 86-90.
  60.  *  
  61.  *  - Johannes, M., and S. Sundaresan (2007): Pricing Collateralized Swaps, Journal of Finance 62 383-410.
  62.  *
  63.  *  - Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives Pricing, Risk
  64.  *      21 (2) 97-102.
  65.  *
  66.  * @author Lakshmi Krishnamurthy
  67.  */

  68. public class CSAFundingRelativeForward {

  69.     public static final void main (
  70.         final String[] astrArgs)
  71.         throws Exception
  72.     {
  73.         EnvManager.InitEnv ("");

  74.         double dblUnderlyingVolatility = 0.3;
  75.         double dblFundingSpreadVolatility = 0.015;
  76.         double dblFundingSpreadMeanReversionRate = 0.05;

  77.         double[] adblCorrelation = new double[] {
  78.             -0.30,
  79.             -0.20,
  80.             -0.10,
  81.              0.00,
  82.              0.10
  83.         };

  84.         int[] aiTenor = new int[] {
  85.              1,
  86.              2,
  87.              3,
  88.              4,
  89.              5,
  90.              6,
  91.              7,
  92.              8,
  93.              9,
  94.             10
  95.         };

  96.         double[][] aadblRelativeDifferenceReconciler = new double[][] {
  97.             { 0.0007,  0.0004,  0.0002,  0.0000, -0.0002},
  98.             { 0.0026,  0.0017,  0.0009,  0.0000, -0.0009},
  99.             { 0.0058,  0.0039,  0.0019,  0.0000, -0.0019},
  100.             { 0.0102,  0.0068,  0.0034,  0.0000, -0.0034},
  101.             { 0.0157,  0.0104,  0.0052,  0.0000, -0.0052},
  102.             { 0.0223,  0.0148,  0.0074,  0.0000, -0.0073},
  103.             { 0.0300,  0.0199,  0.0099,  0.0000, -0.0098},
  104.             { 0.0387,  0.0256,  0.0127,  0.0000, -0.0126},
  105.             { 0.0485,  0.0320,  0.0159,  0.0000, -0.0156},
  106.             { 0.0592,  0.0391,  0.0194,  0.0000, -0.0190}
  107.         };

  108.         DiffusionEvaluatorLogarithmic delUnderlying = DiffusionEvaluatorLogarithmic.Standard (
  109.             0.,
  110.             dblUnderlyingVolatility
  111.         );

  112.         DiffusionEvaluatorMeanReversion demrFundingSpread = DiffusionEvaluatorMeanReversion.Standard (
  113.             dblFundingSpreadMeanReversionRate,
  114.             0.,
  115.             dblFundingSpreadVolatility
  116.         );

  117.         System.out.println();

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

  119.         System.out.println ("\t||         DRIP CSA vs Non CSA Forward Prices          ||");

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

  121.         String strHeader = "\t|| CORR => ";

  122.         for (double dblCorrelation : adblCorrelation)
  123.             strHeader = strHeader + "  " + FormatUtil.FormatDouble (dblCorrelation, 2, 0, 100.) + "%  |";

  124.         System.out.println (strHeader + "|");

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

  126.         for (int iTenor : aiTenor) {
  127.             String strDump = "\t|| " + FormatUtil.FormatDouble (iTenor, 2, 0, 1.) + "Y => ";

  128.             for (double dblCorrelation : adblCorrelation) {
  129.                 FundingBasisEvolver fbe = new FundingBasisEvolver (
  130.                     delUnderlying,
  131.                     demrFundingSpread,
  132.                     dblCorrelation
  133.                 );

  134.                 strDump = strDump + " " + FormatUtil.FormatDouble (fbe.CSANoCSARatio (iTenor + "Y") - 1., 1, 2, 100.) + "% |";
  135.             }

  136.             System.out.println (strDump + "|");
  137.         }

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

  139.         System.out.println();

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

  141.         System.out.println ("\t||     Piterbarg 2010 CSA vs Non CSA Forward Prices    ||");

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

  143.         strHeader = "\t|| CORR => ";

  144.         for (double dblCorrelation : adblCorrelation)
  145.             strHeader = strHeader + "  " + FormatUtil.FormatDouble (dblCorrelation, 2, 0, 100.) + "%  |";

  146.         System.out.println (strHeader + "|");

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

  148.         for (int i = 0; i < aiTenor.length; ++i) {
  149.             String strDump = "\t|| " + FormatUtil.FormatDouble (aiTenor[i], 2, 0, 1.) + "Y => ";

  150.             for (int j = 0; j < adblCorrelation.length; ++j)
  151.                 strDump = strDump + " " + FormatUtil.FormatDouble (aadblRelativeDifferenceReconciler[i][j], 1, 2, 100.) + "% |";

  152.             System.out.println (strDump + "|");
  153.         }

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

  155.         System.out.println();
  156.     }
  157. }