PortfolioPathAggregationDeterministic.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.  * PortfolioPathAggregationDeterministic generates an Aggregation of the Portfolio Paths evolved using
  62.  *  Deterministic Market Parameters. The References are:
  63.  *  
  64.  *  - Burgard, C., and M. Kjaer (2014): PDE Representations of Derivatives with Bilateral Counter-party Risk
  65.  *      and Funding Costs, Journal of Credit Risk, 7 (3) 1-19.
  66.  *  
  67.  *  - Burgard, C., and M. Kjaer (2014): In the Balance, Risk, 24 (11) 72-75.
  68.  *  
  69.  *  - Gregory, J. (2009): Being Two-faced over Counter-party Credit Risk, Risk 20 (2) 86-90.
  70.  *  
  71.  *  - Li, B., and Y. Tang (2007): Quantitative Analysis, Derivatives Modeling, and Trading Strategies in the
  72.  *      Presence of Counter-party Credit Risk for the Fixed Income Market, World Scientific Publishing,
  73.  *      Singapore.
  74.  *
  75.  *  - Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives Pricing, Risk
  76.  *      21 (2) 97-102.
  77.  *
  78.  * @author Lakshmi Krishnamurthy
  79.  */

  80. public class PortfolioPathAggregationDeterministic {

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

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

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

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

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

  112.         return aablCollateralPortfolioValue;
  113.     }

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

  119.         int iNumStep = 10;
  120.         double dblTime = 5.;
  121.         int iNumPath = 50000;
  122.         double dblCollateralPortfolioValueDrift = 0.06;
  123.         double dblCollateralPortfolioValueVolatility = 0.15;
  124.         double dblCollateralPortfolioValueInitial = 1.;
  125.         double dblOvernightNumeraireDrift = 0.004;
  126.         double dblCSADrift = 0.01;
  127.         double dblBankHazardRate = 0.015;
  128.         double dblBankRecoveryRate = 0.40;
  129.         double dblCounterPartyHazardRate = 0.030;
  130.         double dblCounterPartyRecoveryRate = 0.30;

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

  138.         JulianDate dtSpot = DateUtil.Today();

  139.         double[][] aadblCollateralPortfolioValue = CollateralPortfolioValueRealization (
  140.             new DiffusionEvolver (
  141.                 DiffusionEvaluatorLogarithmic.Standard (
  142.                     dblCollateralPortfolioValueDrift,
  143.                     dblCollateralPortfolioValueVolatility
  144.                 )
  145.             ),
  146.             dblCollateralPortfolioValueInitial,
  147.             dblTime,
  148.             dblTimeWidth,
  149.             iNumStep,
  150.             iNumPath
  151.         );

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

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

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

  187.             for (int j = 0; j < iNumPath; ++j)
  188.                 aadblCollateralBalance[j][i] = 0.;
  189.         }

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

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

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

  201.             CollateralGroupPath[] aHGP = new CollateralGroupPath[] {
  202.                 new CollateralGroupPath (
  203.                     aHGVR,
  204.                     mp
  205.                 )
  206.             };

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

  221.         ExposureAdjustmentAggregator eaa = new ExposureAdjustmentAggregator (aMPEA);

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

  223.         System.out.println();

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

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

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

  228.         System.out.println (strDump);

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

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

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

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

  234.         System.out.println (strDump);

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

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

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

  239.         System.out.println (strDump);

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

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

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

  244.         System.out.println (strDump);

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

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

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

  249.         System.out.println (strDump);

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

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

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

  254.         System.out.println (strDump);

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

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

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

  259.         System.out.println (strDump);

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

  261.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

  274.         EnvManager.TerminateEnv();
  275.     }
  276. }