FixFloatVACounterParty.java

  1. package org.drip.sample.burgard2012;

  2. import org.drip.analytics.date.*;
  3. import org.drip.exposure.evolver.LatentStateVertexContainer;
  4. import org.drip.exposure.universe.*;
  5. import org.drip.measure.discrete.SequenceGenerator;
  6. import org.drip.measure.dynamics.DiffusionEvaluatorLinear;
  7. import org.drip.measure.process.DiffusionEvolver;
  8. import org.drip.measure.realization.*;
  9. import org.drip.numerical.common.FormatUtil;
  10. import org.drip.service.env.EnvManager;
  11. import org.drip.state.identifier.OTCFixFloatLabel;
  12. import org.drip.xva.gross.*;
  13. import org.drip.xva.netting.CollateralGroupPath;
  14. import org.drip.xva.strategy.*;
  15. import org.drip.xva.vertex.AlbaneseAndersen;

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

  19. /*!
  20.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  21.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  22.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  23.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  24.  *
  25.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  26.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  27.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  28.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  29.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  30.  *      and computational support.
  31.  *  
  32.  *      https://lakshmidrip.github.io/DROP/
  33.  *  
  34.  *  DROP is composed of three modules:
  35.  *  
  36.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  37.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  38.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  39.  *
  40.  *  DROP Product Core implements libraries for the following:
  41.  *  - Fixed Income Analytics
  42.  *  - Loan Analytics
  43.  *  - Transaction Cost Analytics
  44.  *
  45.  *  DROP Portfolio Core implements libraries for the following:
  46.  *  - Asset Allocation Analytics
  47.  *  - Asset Liability Management Analytics
  48.  *  - Capital Estimation Analytics
  49.  *  - Exposure Analytics
  50.  *  - Margin Analytics
  51.  *  - XVA Analytics
  52.  *
  53.  *  DROP Computational Core implements libraries for the following:
  54.  *  - Algorithm Support
  55.  *  - Computation Support
  56.  *  - Function Analysis
  57.  *  - Model Validation
  58.  *  - Numerical Analysis
  59.  *  - Numerical Optimizer
  60.  *  - Spline Builder
  61.  *  - Statistical Learning
  62.  *
  63.  *  Documentation for DROP is Spread Over:
  64.  *
  65.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  66.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  67.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  68.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  69.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  70.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  71.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  72.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  73.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  74.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  75.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  76.  *
  77.  *  Licensed under the Apache License, Version 2.0 (the "License");
  78.  *      you may not use this file except in compliance with the License.
  79.  *  
  80.  *  You may obtain a copy of the License at
  81.  *      http://www.apache.org/licenses/LICENSE-2.0
  82.  *  
  83.  *  Unless required by applicable law or agreed to in writing, software
  84.  *      distributed under the License is distributed on an "AS IS" BASIS,
  85.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  86.  *  
  87.  *  See the License for the specific language governing permissions and
  88.  *      limitations under the License.
  89.  */

  90. /**
  91.  * <i>FixFloatVACounterParty</i> illustrates the Fix-Float Swap Valuation Adjustment Metrics Dependence on
  92.  * the Counter Party Spread using the Set of Netting Group Exposure Simulations. The References are:
  93.  *  
  94.  * <br><br>
  95.  *  <ul>
  96.  *      <li>
  97.  *          Burgard, C., and M. Kjaer (2014): PDE Representations of Derivatives with Bilateral Counter-party
  98.  *              Risk and Funding Costs <i>Journal of Credit Risk</i> <b>7 (3)</b> 1-19
  99.  *      </li>
  100.  *      <li>
  101.  *          Burgard, C., and M. Kjaer (2014): In the Balance <i>Risk</i> <b>24 (11)</b> 72-75
  102.  *      </li>
  103.  *      <li>
  104.  *          Gregory, J. (2009): Being Two-faced over Counter-party Credit Risk <i>Risk</i> <b>20 (2)</b>
  105.  *              86-90
  106.  *      </li>
  107.  *      <li>
  108.  *          Li, B., and Y. Tang (2007): <i>Quantitative Analysis, Derivatives Modeling, and Trading
  109.  *              Strategies in the Presence of Counter-party Credit Risk for the Fixed Income Market</i>
  110.  *              <b>World Scientific Publishing</b> Singapore
  111.  *      </li>
  112.  *      <li>
  113.  *          Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives Pricing
  114.  *              <i>Risk</i> <b>21 (2)</b> 97-102
  115.  *      </li>
  116.  *  </ul>
  117.  *  
  118.  * <br><br>
  119.  *  <ul>
  120.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  121.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/XVAAnalyticsLibrary.md">XVA Analytics Library</a></li>
  122.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">DROP API Construction and Usage</a></li>
  123.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/burgard2012/README.md">Burgard Kjaer (2012) Valuation Adjustments</a></li>
  124.  *  </ul>
  125.  * <br><br>
  126.  *
  127.  * @author Lakshmi Krishnamurthy
  128.  */

  129. public class FixFloatVACounterParty {

  130.     private static final double[][] ATMSwapRateOffsetRealization (
  131.         final DiffusionEvolver deATMSwapRateOffset,
  132.         final double dblATMSwapRateOffsetInitial,
  133.         final double dblTime,
  134.         final double dblTimeWidth,
  135.         final int iNumStep,
  136.         final int iNumSimulation)
  137.         throws Exception
  138.     {
  139.         double[][] aablATMSwapRateOffset = new double[iNumSimulation][iNumStep + 1];
  140.         double[] adblTimeWidth = new double[iNumStep];

  141.         for (int i = 0; i < iNumStep; ++i)
  142.             adblTimeWidth[i] = dblTimeWidth;

  143.         for (int i = 0; i < iNumSimulation; ++i) {
  144.             JumpDiffusionEdge[] aJDE = deATMSwapRateOffset.incrementSequence (
  145.                 new JumpDiffusionVertex (
  146.                     dblTime,
  147.                     dblATMSwapRateOffsetInitial,
  148.                     0.,
  149.                     false
  150.                 ),
  151.                 JumpDiffusionEdgeUnit.Diffusion (
  152.                     adblTimeWidth,
  153.                     SequenceGenerator.Gaussian (iNumStep)
  154.                 ),
  155.                 dblTimeWidth
  156.             );

  157.             aablATMSwapRateOffset[i][0] = dblATMSwapRateOffsetInitial;

  158.             for (int j = 1; j <= iNumStep; ++j)
  159.                 aablATMSwapRateOffset[i][j] = aJDE[j - 1].finish();
  160.         }

  161.         return aablATMSwapRateOffset;
  162.     }

  163.     public static final void VA (
  164.         final double dblCounterPartyHazardRate)
  165.         throws Exception
  166.     {
  167.         int iNumStep = 10;
  168.         double dblTime = 5.;
  169.         int iNumPath = 10000;
  170.         double dblATMSwapRateOffsetDrift = 0.0;
  171.         double dblATMSwapRateOffsetVolatility = 0.15;
  172.         double dblATMSwapRateOffsetInitial = 0.;
  173.         double dblCSADrift = 0.01;
  174.         double dblBankHazardRate = 0.015;
  175.         double dblBankRecoveryRate = 0.40;
  176.         double dblCounterPartyRecoveryRate = 0.30;

  177.         double dblTimeWidth = dblTime / iNumStep;
  178.         MarketVertex[] aMV = new MarketVertex[iNumStep + 1];
  179.         JulianDate[] adtVertex = new JulianDate[iNumStep + 1];
  180.         MonoPathExposureAdjustment[] aMPEA = new MonoPathExposureAdjustment[iNumPath];
  181.         double dblBankFundingSpread = dblBankHazardRate / (1. - dblBankRecoveryRate);
  182.         double dblCounterPartyFundingSpread = dblCounterPartyHazardRate / (1. - dblCounterPartyRecoveryRate);

  183.         JulianDate dtSpot = DateUtil.Today();

  184.         double[][] aablATMSwapRateOffset = ATMSwapRateOffsetRealization (
  185.             new DiffusionEvolver (
  186.                 DiffusionEvaluatorLinear.Standard (
  187.                     dblATMSwapRateOffsetDrift,
  188.                     dblATMSwapRateOffsetVolatility
  189.                 )
  190.             ),
  191.             dblATMSwapRateOffsetInitial,
  192.             dblTime,
  193.             dblTimeWidth,
  194.             iNumStep,
  195.             iNumPath
  196.         );

  197.         for (int i = 0; i <= iNumStep; ++i)
  198.         {
  199.             LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

  200.             latentStateVertexContainer.add (
  201.                 OTCFixFloatLabel.Standard ("USD-3M-10Y"),
  202.                 Double.NaN
  203.             );

  204.             aMV[i] = MarketVertex.Nodal (
  205.                 adtVertex[i] = dtSpot.addMonths (6 * i),
  206.                 0.,
  207.                 1.,
  208.                 dblCSADrift,
  209.                 Math.exp (-0.5 * dblCSADrift * iNumStep),
  210.                 new MarketVertexEntity (
  211.                     Math.exp (-0.5 * dblBankHazardRate * i),
  212.                     dblBankHazardRate,
  213.                     dblBankRecoveryRate,
  214.                     dblBankFundingSpread,
  215.                     Math.exp (-0.5 * dblBankHazardRate * (1. - dblBankRecoveryRate) * iNumStep),
  216.                     Double.NaN,
  217.                     Double.NaN,
  218.                     Double.NaN
  219.                 ),
  220.                 new MarketVertexEntity (
  221.                     Math.exp (-0.5 * dblCounterPartyHazardRate * i),
  222.                     dblCounterPartyHazardRate,
  223.                     dblCounterPartyRecoveryRate,
  224.                     dblCounterPartyFundingSpread,
  225.                     Math.exp (-0.5 * dblCounterPartyHazardRate * (1. - dblCounterPartyRecoveryRate) * iNumStep),
  226.                     Double.NaN,
  227.                     Double.NaN,
  228.                     Double.NaN
  229.                 ),
  230.                 latentStateVertexContainer
  231.             );
  232.         }

  233.         MarketPath mp = MarketPath.FromMarketVertexArray (aMV);

  234.         for (int i = 0; i < iNumPath; ++i) {
  235.             AlbaneseAndersen[] aHGVR = new AlbaneseAndersen[iNumStep + 1];

  236.             for (int j = 0; j <= iNumStep; ++j)
  237.                 aHGVR[j] = new AlbaneseAndersen (
  238.                     adtVertex[j],
  239.                     dblTimeWidth * (iNumStep - j) * aablATMSwapRateOffset[i][j],
  240.                     0.,
  241.                     0.
  242.                 );

  243.             CollateralGroupPath[] aHGP = new CollateralGroupPath[] {
  244.                 new CollateralGroupPath (
  245.                     aHGVR,
  246.                     mp
  247.                 )
  248.             };

  249.             aMPEA[i] = new MonoPathExposureAdjustment (
  250.                 new AlbaneseAndersenFundingGroupPath[] {
  251.                      AlbaneseAndersenFundingGroupPath.Mono (
  252.                         new AlbaneseAndersenNettingGroupPath (
  253.                             aHGP,
  254.                             mp
  255.                         ),
  256.                         mp
  257.                     )
  258.                 }
  259.             );
  260.         }

  261.         ExposureAdjustmentAggregator eaa = new ExposureAdjustmentAggregator (aMPEA);

  262.         System.out.println ("\t|| " +
  263.             FormatUtil.FormatDouble (dblCounterPartyHazardRate, 3, 0, 10000.) + " bp => " +
  264.             FormatUtil.FormatDouble (eaa.ucva().amount(), 1, 2, 100.) + "% | " +
  265.             FormatUtil.FormatDouble (eaa.ftdcva().amount(), 1, 2, 100.) + "% | " +
  266.             FormatUtil.FormatDouble (eaa.cva().amount(), 1, 2, 100.) + "% | " +
  267.             FormatUtil.FormatDouble (eaa.cvacl().amount(), 1, 2, 100.) + "% | " +
  268.             FormatUtil.FormatDouble (eaa.dva().amount(), 1, 2, 100.) + "% | " +
  269.             FormatUtil.FormatDouble (eaa.fva().amount(), 1, 2, 100.) + "% | " +
  270.             FormatUtil.FormatDouble (eaa.fda().amount(), 1, 2, 100.) + "% | " +
  271.             FormatUtil.FormatDouble (eaa.fca().amount(), 1, 2, 100.) + "% | " +
  272.             FormatUtil.FormatDouble (eaa.fba().amount(), 1, 2, 100.) + "% | " +
  273.             FormatUtil.FormatDouble (eaa.sfva().amount(), 1, 2, 100.) + "% | " +
  274.             FormatUtil.FormatDouble (eaa.total(), 1, 2, 100.) + "% ||"
  275.         );
  276.     }

  277.     public static final void main (
  278.         final String[] astrArgs)
  279.         throws Exception
  280.     {
  281.         EnvManager.InitEnv ("");

  282.         double[] adblCounterPartyHazardRate = new double[] {
  283.             0.0025,
  284.             0.0050,
  285.             0.0075,
  286.             0.0100,
  287.             0.0125,
  288.             0.0150,
  289.             0.0175,
  290.             0.0200,
  291.             0.0225,
  292.             0.0250,
  293.             0.0275,
  294.             0.0300
  295.         };

  296.         System.out.println();

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

  298.         System.out.println ("\t||                                 VA DEPENDENCE ON COUNTER PARTY HAZARD RATE                                  ||");

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

  300.         System.out.println ("\t||  Hazard =>  UCVA  | FTDCVA |   CVA  |  CVACL |   DVA  |   FVA  |   FDA  |   FCA  |   FBA  |  SFVA  |  Total ||");

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

  302.         for (double dblCounterPartyHazardRate : adblCounterPartyHazardRate)
  303.             VA (dblCounterPartyHazardRate);

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

  305.         System.out.println();

  306.         EnvManager.TerminateEnv();
  307.     }
  308. }