ZeroThresholdCollateralGroup.java

  1. package org.drip.sample.xva;

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

  20. /*
  21.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  22.  */

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

  64. /**
  65.  * ZeroThresholdCollateralGroup illustrates the Sample Run of a Single Partially Collateralized Collateral
  66.  *  Group under Zero Bank/Counter Party Threshold with several Fix-Float Swaps. The References are:
  67.  *  
  68.  *  - Burgard, C., and M. Kjaer (2014): PDE Representations of Derivatives with Bilateral Counter-party Risk
  69.  *      and Funding Costs, Journal of Credit Risk, 7 (3) 1-19.
  70.  *  
  71.  *  - Burgard, C., and M. Kjaer (2014): In the Balance, Risk, 24 (11) 72-75.
  72.  *  
  73.  *  - Gregory, J. (2009): Being Two-faced over Counter-party Credit Risk, Risk 20 (2) 86-90.
  74.  *  
  75.  *  - Li, B., and Y. Tang (2007): Quantitative Analysis, Derivatives Modeling, and Trading Strategies in the
  76.  *      Presence of Counter-party Credit Risk for the Fixed Income Market, World Scientific Publishing,
  77.  *      Singapore.
  78.  *
  79.  *  - Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives Pricing, Risk
  80.  *      21 (2) 97-102.
  81.  *
  82.  * @author Lakshmi Krishnamurthy
  83.  */

  84. public class ZeroThresholdCollateralGroup {

  85.     private static final double[] ATMSwapRateOffsetRealization (
  86.         final DiffusionEvolver deATMSwapRateOffset,
  87.         final double dblATMSwapRateOffsetInitial,
  88.         final double dblTime,
  89.         final double dblTimeWidth,
  90.         final int iNumStep)
  91.         throws Exception
  92.     {
  93.         double[] adblATMSwapRateOffset = new double[iNumStep + 1];
  94.         adblATMSwapRateOffset[0] = dblATMSwapRateOffsetInitial;
  95.         double[] adblTimeWidth = new double[iNumStep];

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

  98.         JumpDiffusionEdge[] aJDE = deATMSwapRateOffset.incrementSequence (
  99.             new JumpDiffusionVertex (
  100.                 dblTime,
  101.                 dblATMSwapRateOffsetInitial,
  102.                 0.,
  103.                 false
  104.             ),
  105.             JumpDiffusionEdgeUnit.Diffusion (
  106.                 adblTimeWidth,
  107.                 SequenceGenerator.Gaussian (iNumStep)
  108.             ),
  109.             dblTimeWidth
  110.         );

  111.         for (int j = 1; j <= iNumStep; ++j)
  112.             adblATMSwapRateOffset[j] = aJDE[j - 1].finish();

  113.         return adblATMSwapRateOffset;
  114.     }

  115.     private static final double[] SwapPortfolioValueRealization (
  116.         final DiffusionEvolver deATMSwapRate,
  117.         final double dblATMSwapRateStart,
  118.         final int iNumStep,
  119.         final double dblTime,
  120.         final double dblTimeWidth,
  121.         final int iNumSwap)
  122.         throws Exception
  123.     {
  124.         double[] adblSwapPortfolioValueRealization = new double[iNumStep + 1];

  125.         for (int i = 0; i < iNumStep; ++i)
  126.             adblSwapPortfolioValueRealization[i] = 0.;

  127.         for (int i = 0; i < iNumSwap; ++i) {
  128.             double[] adblATMSwapRateOffsetRealization = ATMSwapRateOffsetRealization (
  129.                 deATMSwapRate,
  130.                 dblATMSwapRateStart,
  131.                 dblTime,
  132.                 dblTimeWidth,
  133.                 iNumStep
  134.             );

  135.             for (int j = 0; j <= iNumStep; ++j)
  136.                 adblSwapPortfolioValueRealization[j] += dblTimeWidth * (iNumStep - j) * adblATMSwapRateOffsetRealization[j];
  137.         }

  138.         return adblSwapPortfolioValueRealization;
  139.     }

  140.     private static final double[][] SwapPortfolioValueRealization (
  141.         final DiffusionEvolver deATMSwapRate,
  142.         final double dblSwapPortfolioValueStart,
  143.         final int iNumStep,
  144.         final double dblTime,
  145.         final double dblTimeWidth,
  146.         final int iNumSwap,
  147.         final int iNumSimulation)
  148.         throws Exception
  149.     {
  150.         double[][] aadblSwapPortfolioValueRealization = new double[iNumSimulation][];

  151.         for (int i = 0; i < iNumSimulation; ++i)
  152.             aadblSwapPortfolioValueRealization[i] = SwapPortfolioValueRealization (
  153.                 deATMSwapRate,
  154.                 dblSwapPortfolioValueStart,
  155.                 iNumStep,
  156.                 dblTime,
  157.                 dblTimeWidth,
  158.                 iNumSwap
  159.             );

  160.         return aadblSwapPortfolioValueRealization;
  161.     }

  162.     public static final void main (
  163.         final String[] astrArgs)
  164.         throws Exception
  165.     {
  166.         EnvManager.InitEnv ("");

  167.         int iNumStep = 10;
  168.         int iNumSwap = 10;
  169.         double dblTime = 5.;
  170.         int iNumPath = 10000;
  171.         double dblATMSwapRateStart = 0.;
  172.         double dblATMSwapRateDrift = 0.0;
  173.         double dblATMSwapRateVolatility = 0.25;
  174.         double dblOvernightNumeraireDrift = 0.004;
  175.         double dblCSADrift = 0.01;
  176.         double dblBankHazardRate = 0.015;
  177.         double dblBankRecoveryRate = 0.40;
  178.         double dblCounterPartyHazardRate = 0.030;
  179.         double dblCounterPartyRecoveryRate = 0.30;

  180.         JulianDate dtSpot = DateUtil.Today();

  181.         double dblTimeWidth = dblTime / iNumStep;
  182.         MarketVertex[] aMV = new MarketVertex[iNumStep + 1];
  183.         JulianDate[] adtVertex = new JulianDate[iNumStep + 1];
  184.         double dblBankFundingSpread = dblBankHazardRate / (1. - dblBankRecoveryRate);
  185.         MonoPathExposureAdjustment[] aMPEA = new MonoPathExposureAdjustment[iNumPath];
  186.         double dblCounterPartyFundingSpread = dblCounterPartyHazardRate / (1. - dblCounterPartyRecoveryRate);

  187.         PositionGroupSpecification positionGroupSpecification = PositionGroupSpecification.FixedThreshold (
  188.             "FIXEDTHRESHOLD",
  189.             0.,
  190.             0.,
  191.             PositionReplicationScheme.ALBANESE_ANDERSEN_VERTEX,
  192.             BrokenDateScheme.LINEAR_TIME,
  193.             0.,
  194.             CloseOutScheme.ISDA_92
  195.         );

  196.         double[][] aadblSwapPortfolioValueRealization = SwapPortfolioValueRealization (
  197.             new DiffusionEvolver (
  198.                 DiffusionEvaluatorLinear.Standard (
  199.                     dblATMSwapRateDrift,
  200.                     dblATMSwapRateVolatility
  201.                 )
  202.             ),
  203.             dblATMSwapRateStart,
  204.             iNumStep,
  205.             dblTime,
  206.             dblTimeWidth,
  207.             iNumSwap,
  208.             iNumPath
  209.         );

  210.         for (int i = 0; i <= iNumStep; ++i)
  211.         {
  212.             LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

  213.             latentStateVertexContainer.add (
  214.                 OTCFixFloatLabel.Standard ("USD-3M-10Y"),
  215.                 Double.NaN
  216.             );

  217.             aMV[i] = MarketVertex.Nodal (
  218.                 adtVertex[i] = dtSpot.addMonths (6 * i),
  219.                 dblOvernightNumeraireDrift,
  220.                 Math.exp (-0.5 * dblOvernightNumeraireDrift * (iNumStep - i)),
  221.                 dblCSADrift,
  222.                 Math.exp (-0.5 * dblCSADrift * iNumStep),
  223.                 new MarketVertexEntity (
  224.                     Math.exp (-0.5 * dblBankHazardRate * i),
  225.                     dblBankHazardRate,
  226.                     dblBankRecoveryRate,
  227.                     dblBankFundingSpread,
  228.                     Math.exp (-0.5 * dblBankHazardRate * (1. - dblBankRecoveryRate) * (iNumStep - i)),
  229.                     Double.NaN,
  230.                     Double.NaN,
  231.                     Double.NaN
  232.                 ),
  233.                 new MarketVertexEntity (
  234.                     Math.exp (-0.5 * dblCounterPartyHazardRate * i),
  235.                     dblCounterPartyHazardRate,
  236.                     dblCounterPartyRecoveryRate,
  237.                     dblCounterPartyFundingSpread,
  238.                     Math.exp (-0.5 * dblCounterPartyHazardRate * (1. - dblCounterPartyRecoveryRate) * (iNumStep - i)),
  239.                     Double.NaN,
  240.                     Double.NaN,
  241.                     Double.NaN
  242.                 ),
  243.                 latentStateVertexContainer
  244.             );
  245.         }

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

  247.         for (int i = 0; i < iNumPath; ++i) {
  248.             JulianDate dtStart = dtSpot;
  249.             double dblValueStart = dblTime * dblATMSwapRateStart;
  250.             AlbaneseAndersen[] aHGVR = new AlbaneseAndersen[iNumStep + 1];

  251.             for (int j = 0; j <= iNumStep; ++j) {
  252.                 double dblCollateralBalance = 0.;
  253.                 JulianDate dtEnd = adtVertex[j];
  254.                 double dblValueEnd = aadblSwapPortfolioValueRealization[i][j];

  255.                 if (0 != j) {
  256.                     CollateralAmountEstimator hae = new CollateralAmountEstimator (
  257.                         positionGroupSpecification,
  258.                         new BrokenDateInterpolatorLinearT (
  259.                             dtStart.julian(),
  260.                             dtEnd.julian(),
  261.                             dblValueStart,
  262.                             dblValueEnd
  263.                         ),
  264.                         Double.NaN
  265.                     );

  266.                     dblCollateralBalance = hae.postingRequirement (dtEnd);
  267.                 }

  268.                 aHGVR[j] = new AlbaneseAndersen (
  269.                     adtVertex[j],
  270.                     aadblSwapPortfolioValueRealization[i][j],
  271.                     0.,
  272.                     dblCollateralBalance
  273.                 );

  274.                 dtStart = dtEnd;
  275.                 dblValueStart = dblValueEnd;
  276.             }

  277.             CollateralGroupPath[] aHGP = new CollateralGroupPath[] {
  278.                 new CollateralGroupPath (
  279.                     aHGVR,
  280.                     mp
  281.                 )
  282.             };

  283.             aMPEA[i] = new MonoPathExposureAdjustment (
  284.                 new AlbaneseAndersenFundingGroupPath[] {
  285.                     new AlbaneseAndersenFundingGroupPath (
  286.                         new AlbaneseAndersenNettingGroupPath[] {
  287.                             new AlbaneseAndersenNettingGroupPath (
  288.                                 aHGP,
  289.                                 mp
  290.                             )
  291.                         },
  292.                         mp
  293.                     )
  294.                 }
  295.             );
  296.         }

  297.         ExposureAdjustmentAggregator eaa = new ExposureAdjustmentAggregator (aMPEA);

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

  299.         System.out.println();

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

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

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

  304.         System.out.println (strDump);

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

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

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

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

  310.         System.out.println (strDump);

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

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

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

  315.         System.out.println (strDump);

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

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

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

  320.         System.out.println (strDump);

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

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

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

  325.         System.out.println (strDump);

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

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

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

  330.         System.out.println (strDump);

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

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

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

  335.         System.out.println (strDump);

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

  337.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

  350.         EnvManager.TerminateEnv();
  351.     }
  352. }