PortfolioGroupSimulation.java

  1. package org.drip.sample.netting;

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

  60. /**
  61.  * PortfolioGroupRun demonstrates a Set of Netting Group Exposure Simulations. The References are:
  62.  *  
  63.  *  - Burgard, C., and M. Kjaer (2014): PDE Representations of Derivatives with Bilateral Counter-party Risk
  64.  *      and Funding Costs, Journal of Credit Risk, 7 (3) 1-19.
  65.  *  
  66.  *  - Burgard, C., and M. Kjaer (2014): In the Balance, Risk, 24 (11) 72-75.
  67.  *  
  68.  *  - Gregory, J. (2009): Being Two-faced over Counter-party Credit Risk, Risk 20 (2) 86-90.
  69.  *  
  70.  *  - Li, B., and Y. Tang (2007): Quantitative Analysis, Derivatives Modeling, and Trading Strategies in the
  71.  *      Presence of Counter-party Credit Risk for the Fixed Income Market, World Scientific Publishing,
  72.  *      Singapore.
  73.  *
  74.  *  - Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives Pricing, Risk
  75.  *      21 (2) 97-102.
  76.  *
  77.  * @author Lakshmi Krishnamurthy
  78.  */

  79. public class PortfolioGroupSimulation {

  80.     private static final double[][] CollateralPortfolioValueRealization (
  81.         final DiffusionEvolver deCollateralPortfolioValue,
  82.         final double dblCollateralPortfolioValueInitial,
  83.         final double dblTime,
  84.         final double dblTimeWidth,
  85.         final int iNumStep,
  86.         final int iNumPath)
  87.         throws Exception
  88.     {
  89.         double[][] aablCollateralPortfolioValue = new double[iNumPath][iNumStep + 1];
  90.         double[] adblTimeWidth = new double[iNumStep];

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

  93.         for (int i = 0; i < iNumPath; ++i) {
  94.             JumpDiffusionEdge[] aJDE = deCollateralPortfolioValue.incrementSequence (
  95.                 new JumpDiffusionVertex (
  96.                     dblTime,
  97.                     dblCollateralPortfolioValueInitial,
  98.                     0.,
  99.                     false
  100.                 ),
  101.                 JumpDiffusionEdgeUnit.Diffusion (
  102.                     adblTimeWidth,
  103.                     SequenceGenerator.Gaussian (iNumStep)
  104.                 ),
  105.                 dblTimeWidth
  106.             );

  107.             aablCollateralPortfolioValue[i][0] = dblCollateralPortfolioValueInitial;

  108.             for (int j = 1; j <= iNumStep; ++j)
  109.                 aablCollateralPortfolioValue[i][j] = aJDE[j - 1].finish();
  110.         }

  111.         return aablCollateralPortfolioValue;
  112.     }

  113.     public static final void main (
  114.         final String[] astrArgs)
  115.         throws Exception
  116.     {
  117.         EnvManager.InitEnv ("");

  118.         int iNumStep = 10;
  119.         double dblTime = 5.;
  120.         int iNumPath = 10000;
  121.         double dblAssetDrift = 0.06;
  122.         double dblAssetVolatility = 0.15;
  123.         double dblCollateralPortfolioValueInitial = 1.;
  124.         double dblOISRate = 0.004;
  125.         double dblCSADrift = 0.01;
  126.         double dblBankHazardRate = 0.015;
  127.         double dblBankRecoveryRate = 0.40;
  128.         double dblCounterPartyHazardRate = 0.030;
  129.         double dblCounterPartyRecoveryRate = 0.30;

  130.         double dblTimeWidth = dblTime / iNumStep;
  131.         MarketVertex[] aMV = new MarketVertex[iNumStep + 1];
  132.         JulianDate[] adtVertex = new JulianDate[iNumStep + 1];
  133.         double dblBankFundingSpread = dblBankHazardRate / (1. - dblBankRecoveryRate);
  134.         MonoPathExposureAdjustment[] aMPEA = new MonoPathExposureAdjustment[iNumPath];
  135.         double dblCounterPartyFundingSpread = dblCounterPartyHazardRate / (1. - dblCounterPartyRecoveryRate);

  136.         JulianDate dtSpot = DateUtil.Today();

  137.         DiffusionEvolver deCollateralPortfolioValue = new DiffusionEvolver (
  138.             DiffusionEvaluatorLogarithmic.Standard (
  139.                 dblAssetDrift,
  140.                 dblAssetVolatility
  141.             )
  142.         );

  143.         double[][] aadblCollateralPortfolioValue = CollateralPortfolioValueRealization (
  144.             deCollateralPortfolioValue,
  145.             dblCollateralPortfolioValueInitial,
  146.             dblTime,
  147.             dblTimeWidth,
  148.             iNumStep,
  149.             iNumPath
  150.         );

  151.         for (int i = 0; i <= iNumStep; ++i)
  152.         {
  153.             LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

  154.             latentStateVertexContainer.add (
  155.                 OTCFixFloatLabel.Standard ("USD-3M-10Y"),
  156.                 Double.NaN
  157.             );

  158.             aMV[i] = MarketVertex.Nodal (
  159.                 adtVertex[i] = dtSpot.addMonths (6 * i),
  160.                 0.,
  161.                 Math.exp (-0.5 * dblOISRate * (iNumStep - i)),
  162.                 dblCSADrift,
  163.                 Math.exp (-0.5 * dblCSADrift * (iNumStep - i)),
  164.                 new MarketVertexEntity (
  165.                     Math.exp (-0.5 * dblBankHazardRate * i),
  166.                     dblBankHazardRate,
  167.                     dblBankRecoveryRate,
  168.                     dblBankFundingSpread,
  169.                     Math.exp (-0.5 * dblBankHazardRate * (1. - dblBankRecoveryRate) * (iNumStep - i)),
  170.                     Double.NaN,
  171.                     Double.NaN,
  172.                     Double.NaN
  173.                 ),
  174.                 new MarketVertexEntity (
  175.                     Math.exp (-0.5 * dblCounterPartyHazardRate * i),
  176.                     dblCounterPartyHazardRate,
  177.                     dblCounterPartyRecoveryRate,
  178.                     dblCounterPartyFundingSpread,
  179.                     Math.exp (-0.5 * dblCounterPartyHazardRate * (1. - dblCounterPartyRecoveryRate) * (iNumStep - i)),
  180.                     Double.NaN,
  181.                     Double.NaN,
  182.                     Double.NaN
  183.                 ),
  184.                 latentStateVertexContainer
  185.             );
  186.         }

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

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

  190.             for (int j = 0; j <= iNumStep; ++j) {
  191.                 aHGVR[j] = new AlbaneseAndersen (
  192.                     adtVertex[j],
  193.                     aadblCollateralPortfolioValue[i][j],
  194.                     0.,
  195.                     0.
  196.                 );
  197.             }

  198.             CollateralGroupPath[] aHGP = new CollateralGroupPath[] {
  199.                 new CollateralGroupPath (
  200.                     aHGVR,
  201.                     mp
  202.                 )
  203.             };

  204.             aMPEA[i] = new MonoPathExposureAdjustment (
  205.                 new AlbaneseAndersenFundingGroupPath[] {
  206.                     new AlbaneseAndersenFundingGroupPath (
  207.                         new AlbaneseAndersenNettingGroupPath[] {
  208.                             new AlbaneseAndersenNettingGroupPath (
  209.                                 aHGP,
  210.                                 mp
  211.                             )
  212.                         },
  213.                         mp
  214.                     )
  215.                 }
  216.             );
  217.         }

  218.         ExposureAdjustmentAggregator eaa = new ExposureAdjustmentAggregator (aMPEA);

  219.         JulianDate[] adtVertexNode = eaa.vertexDates();

  220.         System.out.println();

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

  222.         String strDump = "\t|         DATE         =>" ;

  223.         for (int i = 0; i < adtVertexNode.length; ++i)
  224.             strDump = strDump + " " + adtVertexNode[i] + " |";

  225.         System.out.println (strDump);

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

  227.         double[] adblExposure = eaa.collateralizedExposure();

  228.         strDump = "\t|       EXPOSURE       =>";

  229.         for (int j = 0; j < adblExposure.length; ++j)
  230.             strDump = strDump + "   " + FormatUtil.FormatDouble (adblExposure[j], 1, 4, 1.) + "   |";

  231.         System.out.println (strDump);

  232.         double[] adblPositiveExposure = eaa.collateralizedPositiveExposure();

  233.         strDump = "\t|  POSITIVE EXPOSURE   =>";

  234.         for (int j = 0; j < adblPositiveExposure.length; ++j)
  235.             strDump = strDump + "   " + FormatUtil.FormatDouble (adblPositiveExposure[j], 1, 4, 1.) + "   |";

  236.         System.out.println (strDump);

  237.         double[] adblNegativeExposure = eaa.collateralizedNegativeExposure();

  238.         strDump = "\t|  NEGATIVE EXPOSURE   =>";

  239.         for (int j = 0; j < adblNegativeExposure.length; ++j)
  240.             strDump = strDump + "   " + FormatUtil.FormatDouble (adblNegativeExposure[j], 1, 4, 1.) + "   |";

  241.         System.out.println (strDump);

  242.         double[] adblExposurePV = eaa.collateralizedExposurePV();

  243.         strDump = "\t|      EXPOSURE PV     =>";

  244.         for (int j = 0; j < adblExposurePV.length; ++j)
  245.             strDump = strDump + "   " + FormatUtil.FormatDouble (adblExposurePV[j], 1, 4, 1.) + "   |";

  246.         System.out.println (strDump);

  247.         double[] adblPositiveExposurePV = eaa.collateralizedPositiveExposurePV();

  248.         strDump = "\t| POSITIVE EXPOSURE PV =>";

  249.         for (int j = 0; j < adblPositiveExposurePV.length; ++j)
  250.             strDump = strDump + "   " + FormatUtil.FormatDouble (adblPositiveExposurePV[j], 1, 4, 1.) + "   |";

  251.         System.out.println (strDump);

  252.         double[] adblNegativeExposurePV = eaa.collateralizedNegativeExposurePV();

  253.         strDump = "\t| NEGATIVE EXPOSURE PV =>";

  254.         for (int j = 0; j < adblNegativeExposurePV.length; ++j)
  255.             strDump = strDump + "   " + FormatUtil.FormatDouble (adblNegativeExposurePV[j], 1, 4, 1.) + "   |";

  256.         System.out.println (strDump);

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

  258.         System.out.println();

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

  260.         System.out.println ("\t||  UCVA  => " + FormatUtil.FormatDouble (eaa.ucva().amount(), 2, 2, 100.) + "% ||");

  261.         System.out.println ("\t|| FTDCVA => " + FormatUtil.FormatDouble (eaa.ftdcva().amount(), 2, 2, 100.) + "% ||");

  262.         System.out.println ("\t||  CVA   => " + FormatUtil.FormatDouble (eaa.cva().amount(), 2, 2, 100.) + "% ||");

  263.         System.out.println ("\t||  CVACL => " + FormatUtil.FormatDouble (eaa.cvacl().amount(), 2, 2, 100.) + "% ||");

  264.         System.out.println ("\t||  DVA   => " + FormatUtil.FormatDouble (eaa.dva().amount(), 2, 2, 100.) + "% ||");

  265.         System.out.println ("\t||  FVA   => " + FormatUtil.FormatDouble (eaa.fva().amount(), 2, 2, 100.) + "% ||");

  266.         System.out.println ("\t||  FDA   => " + FormatUtil.FormatDouble (eaa.fda().amount(), 2, 2, 100.) + "% ||");

  267.         System.out.println ("\t||  FCA   => " + FormatUtil.FormatDouble (eaa.fca().amount(), 2, 2, 100.) + "% ||");

  268.         System.out.println ("\t||  FBA   => " + FormatUtil.FormatDouble (eaa.fba().amount(), 2, 2, 100.) + "% ||");

  269.         System.out.println ("\t||  SFVA  => " + FormatUtil.FormatDouble (eaa.sfva().amount(), 2, 2, 100.) + "% ||");

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

  271.         EnvManager.TerminateEnv();
  272.     }
  273. }