UncollateralizedCollateralGroup.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.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) 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.  * UncollateralizedCollateralGroup illustrates the Sample Run of a Single Uncollateralized Collateral Group
  62.  *  with several Fix-Float Swaps. 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 UncollateralizedCollateralGroup {

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

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

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

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

  109.         return adblATMSwapRateOffset;
  110.     }

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

  121.         for (int i = 0; i < iNumStep; ++i)
  122.             adblSwapPortfolioValueRealization[i] = 0.;

  123.         for (int i = 0; i < iNumSwap; ++i) {
  124.             double[] adblATMSwapRateOffsetRealization = ATMSwapRateOffsetRealization (
  125.                 deATMSwapRate,
  126.                 dblATMSwapRateStart,
  127.                 dblTime,
  128.                 dblTimeWidth,
  129.                 iNumStep
  130.             );

  131.             for (int j = 0; j <= iNumStep; ++j)
  132.                 adblSwapPortfolioValueRealization[j] += dblTimeWidth * (iNumStep - j) * adblATMSwapRateOffsetRealization[j];
  133.         }

  134.         return adblSwapPortfolioValueRealization;
  135.     }

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

  147.         for (int i = 0; i < iNumSimulation; ++i)
  148.             aadblSwapPortfolioValueRealization[i] = SwapPortfolioValueRealization (
  149.                 deATMSwapRate,
  150.                 dblSwapPortfolioValueStart,
  151.                 iNumStep,
  152.                 dblTime,
  153.                 dblTimeWidth,
  154.                 iNumSwap
  155.             );

  156.         return aadblSwapPortfolioValueRealization;
  157.     }

  158.     public static final void main (
  159.         final String[] astrArgs)
  160.         throws Exception
  161.     {
  162.         EnvManager.InitEnv ("");

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

  176.         JulianDate dtSpot = DateUtil.Today();

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

  184.         double[][] aadblSwapPortfolioValueRealization = SwapPortfolioValueRealization (
  185.             new DiffusionEvolver (
  186.                 DiffusionEvaluatorLinear.Standard (
  187.                     dblATMSwapRateDrift,
  188.                     dblATMSwapRateVolatility
  189.                 )
  190.             ),
  191.             dblATMSwapRateStart,
  192.             iNumStep,
  193.             dblTime,
  194.             dblTimeWidth,
  195.             iNumSwap,
  196.             iNumPath
  197.         );

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

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

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

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

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

  237.             for (int j = 0; j <= iNumStep; ++j) {
  238.                 aadblCollateralBalance[i][j] = 0.;

  239.                 aHGVR[j] = new AlbaneseAndersen (
  240.                     adtVertex[j],
  241.                     aadblSwapPortfolioValueRealization[i][j],
  242.                     0.,
  243.                     aadblCollateralBalance[i][j]
  244.                 );
  245.             }

  246.             CollateralGroupPath[] aHGP = new CollateralGroupPath[] {
  247.                 new CollateralGroupPath (
  248.                     aHGVR,
  249.                     mp
  250.                 )
  251.             };

  252.             aMPEA[i] = new MonoPathExposureAdjustment (
  253.                 new AlbaneseAndersenFundingGroupPath[] {
  254.                     new AlbaneseAndersenFundingGroupPath (
  255.                         new AlbaneseAndersenNettingGroupPath[] {
  256.                             new AlbaneseAndersenNettingGroupPath (
  257.                                 aHGP,
  258.                                 mp
  259.                             )
  260.                         },
  261.                         mp
  262.                     )
  263.                 }
  264.             );
  265.         }

  266.         ExposureAdjustmentAggregator eaa = new ExposureAdjustmentAggregator (aMPEA);

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

  268.         System.out.println();

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

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

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

  273.         System.out.println (strDump);

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

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

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

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

  279.         System.out.println (strDump);

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

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

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

  284.         System.out.println (strDump);

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

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

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

  289.         System.out.println (strDump);

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

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

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

  294.         System.out.println (strDump);

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

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

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

  299.         System.out.println (strDump);

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

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

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

  304.         System.out.println (strDump);

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

  306.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

  319.         EnvManager.TerminateEnv();
  320.     }
  321. }