CPGACollateralizedCorrelated.java

  1. package org.drip.sample.xvadigest;

  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.*;
  9. import org.drip.measure.process.DiffusionEvolver;
  10. import org.drip.measure.realization.*;
  11. import org.drip.measure.statistics.UnivariateDiscreteThin;
  12. import org.drip.numerical.common.FormatUtil;
  13. import org.drip.numerical.linearalgebra.Matrix;
  14. import org.drip.service.env.EnvManager;
  15. import org.drip.state.identifier.OTCFixFloatLabel;
  16. import org.drip.xva.gross.*;
  17. import org.drip.xva.netting.CollateralGroupPath;
  18. import org.drip.xva.proto.*;
  19. import org.drip.xva.settings.*;
  20. import org.drip.xva.strategy.*;
  21. import org.drip.xva.vertex.AlbaneseAndersen;

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

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

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

  87. public class CPGACollateralizedCorrelated {

  88.     private static final double[] NumeraireValueRealization (
  89.         final DiffusionEvolver deNumeraireValue,
  90.         final double dblNumeraireValueInitial,
  91.         final double dblTime,
  92.         final double dblTimeWidth,
  93.         final double[] adblRandom,
  94.         final int iNumStep)
  95.         throws Exception
  96.     {
  97.         double[] adblNumeraireValue = new double[iNumStep + 1];
  98.         adblNumeraireValue[0] = dblNumeraireValueInitial;
  99.         double[] adblTimeWidth = new double[iNumStep];

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

  102.         JumpDiffusionEdge[] aJDE = deNumeraireValue.incrementSequence (
  103.             new JumpDiffusionVertex (
  104.                 dblTime,
  105.                 dblNumeraireValueInitial,
  106.                 0.,
  107.                 false
  108.             ),
  109.             JumpDiffusionEdgeUnit.Diffusion (
  110.                 adblTimeWidth,
  111.                 adblRandom
  112.             ),
  113.             dblTimeWidth
  114.         );

  115.         for (int j = 1; j <= iNumStep; ++j)
  116.             adblNumeraireValue[j] = aJDE[j - 1].finish();

  117.         return adblNumeraireValue;
  118.     }

  119.     private static final double[] VertexNumeraireRealization (
  120.         final DiffusionEvolver deNumeraireValue,
  121.         final double dblNumeraireValueInitial,
  122.         final double dblTime,
  123.         final double dblTimeWidth,
  124.         final double[] adblRandom,
  125.         final int iNumStep)
  126.         throws Exception
  127.     {
  128.         double[] adblNumeraireValue = new double[iNumStep + 1];
  129.         double[] adblTimeWidth = new double[iNumStep];

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

  132.         JumpDiffusionVertex[] aJDV = deNumeraireValue.vertexSequenceReverse (
  133.             new JumpDiffusionVertex (
  134.                 dblTime,
  135.                 dblNumeraireValueInitial,
  136.                 0.,
  137.                 false
  138.             ),
  139.             JumpDiffusionEdgeUnit.Diffusion (
  140.                 adblTimeWidth,
  141.                 adblRandom
  142.             ),
  143.             adblTimeWidth
  144.         );

  145.         for (int j = 0; j <= iNumStep; ++j)
  146.             adblNumeraireValue[j] = aJDV[j].value();

  147.         return adblNumeraireValue;
  148.     }

  149.     private static final double[] ATMSwapRateOffsetRealization (
  150.         final DiffusionEvolver deATMSwapRateOffset,
  151.         final double dblATMSwapRateOffsetInitial,
  152.         final double[] adblRandom,
  153.         final double dblTime,
  154.         final double dblTimeWidth,
  155.         final int iNumStep)
  156.         throws Exception
  157.     {
  158.         double[] adblATMSwapRateOffset = new double[iNumStep + 1];
  159.         adblATMSwapRateOffset[0] = dblATMSwapRateOffsetInitial;
  160.         double[] adblTimeWidth = new double[iNumStep];

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

  163.         JumpDiffusionEdge[] aJDE = deATMSwapRateOffset.incrementSequence (
  164.             new JumpDiffusionVertex (
  165.                 dblTime,
  166.                 dblATMSwapRateOffsetInitial,
  167.                 0.,
  168.                 false
  169.             ),
  170.             JumpDiffusionEdgeUnit.Diffusion (
  171.                 adblTimeWidth,
  172.                 adblRandom
  173.             ),
  174.             dblTimeWidth
  175.         );

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

  178.         return adblATMSwapRateOffset;
  179.     }

  180.     private static final double[] SwapPortfolioValueRealization (
  181.         final DiffusionEvolver deATMSwapRate,
  182.         final double dblATMSwapRateStart,
  183.         final double[] adblRandom,
  184.         final int iNumStep,
  185.         final double dblTime,
  186.         final double dblTimeWidth,
  187.         final int iNumSwap)
  188.         throws Exception
  189.     {
  190.         double[] adblSwapPortfolioValueRealization = new double[iNumStep + 1];

  191.         for (int i = 0; i < iNumStep; ++i)
  192.             adblSwapPortfolioValueRealization[i] = 0.;

  193.         for (int i = 0; i < iNumSwap; ++i) {
  194.             double[] adblATMSwapRateOffsetRealization = ATMSwapRateOffsetRealization (
  195.                 deATMSwapRate,
  196.                 dblATMSwapRateStart,
  197.                 adblRandom,
  198.                 dblTime,
  199.                 dblTimeWidth,
  200.                 iNumStep
  201.             );

  202.             for (int j = 0; j <= iNumStep; ++j)
  203.                 adblSwapPortfolioValueRealization[j] += dblTimeWidth * (iNumStep - j) * adblATMSwapRateOffsetRealization[j];
  204.         }

  205.         return adblSwapPortfolioValueRealization;
  206.     }

  207.     private static final void UDTDump (
  208.         final String strHeader,
  209.         final JulianDate[] adtVertexNode,
  210.         final UnivariateDiscreteThin[] aUDT)
  211.         throws Exception
  212.     {
  213.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|");

  214.         System.out.println (strHeader);

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

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

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

  219.         System.out.println (strDump);

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

  221.          strDump = "\t|     AVERAGE     =>";

  222.         for (int j = 0; j < aUDT.length; ++j)
  223.             strDump = strDump + "   " + FormatUtil.FormatDouble (aUDT[j].average(), 2, 4, 1.) + "   |";

  224.         System.out.println (strDump);

  225.         strDump = "\t|     MAXIMUM     =>";

  226.         for (int j = 0; j < aUDT.length; ++j)
  227.             strDump = strDump + "   " + FormatUtil.FormatDouble (aUDT[j].maximum(), 2, 4, 1.) + "   |";

  228.         System.out.println (strDump);

  229.         strDump = "\t|     MINIMUM     =>";

  230.         for (int j = 0; j < aUDT.length; ++j)
  231.             strDump = strDump + "   " + FormatUtil.FormatDouble (aUDT[j].minimum(), 2, 4, 1.) + "   |";

  232.         System.out.println (strDump);

  233.         strDump = "\t|      ERROR      =>";

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

  236.         System.out.println (strDump);

  237.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|");
  238.     }

  239.     private static final void UDTDump (
  240.         final String strHeader,
  241.         final UnivariateDiscreteThin udt)
  242.         throws Exception
  243.     {
  244.         System.out.println (
  245.             strHeader +
  246.             FormatUtil.FormatDouble (udt.average(), 3, 2, 100.) + "% | " +
  247.             FormatUtil.FormatDouble (udt.maximum(), 3, 2, 100.) + "% | " +
  248.             FormatUtil.FormatDouble (udt.minimum(), 3, 2, 100.) + "% | " +
  249.             FormatUtil.FormatDouble (udt.error(), 3, 2, 100.) + "% ||"
  250.         );
  251.     }

  252.     public static final void main (
  253.         final String[] astrArgs)
  254.         throws Exception
  255.     {
  256.         EnvManager.InitEnv ("");

  257.         int iNumStep = 10;
  258.         int iNumSwap = 10;
  259.         double dblTime = 5.;
  260.         int iNumPath = 10000;
  261.         double dblATMSwapRateOffsetDrift = 0.0;
  262.         double dblATMSwapRateOffsetVolatility = 0.25;
  263.         double dblATMSwapRateOffsetStart = 0.;
  264.         double dblOvernightNumeraireDrift = 0.01;
  265.         double dblOvernightNumeraireVolatility = 0.05;
  266.         double dblOvernightNumeraireInitial = 1.;
  267.         double dblCSADrift = 0.01;
  268.         double dblCSAVolatility = 0.05;
  269.         double dblCSAInitial = 1.;
  270.         double dblBankHazardRateDrift = 0.002;
  271.         double dblBankHazardRateVolatility = 0.20;
  272.         double dblBankHazardRateInitial = 0.015;
  273.         double dblBankRecoveryRateDrift = 0.002;
  274.         double dblBankRecoveryRateVolatility = 0.02;
  275.         double dblBankRecoveryRateInitial = 0.40;
  276.         double dblCounterPartyHazardRateDrift = 0.002;
  277.         double dblCounterPartyHazardRateVolatility = 0.30;
  278.         double dblCounterPartyHazardRateInitial = 0.030;
  279.         double dblCounterPartyRecoveryRateDrift = 0.002;
  280.         double dblCounterPartyRecoveryRateVolatility = 0.02;
  281.         double dblCounterPartyRecoveryRateInitial = 0.30;
  282.         double dblBankFundingSpreadDrift = 0.00002;
  283.         double dblBankFundingSpreadVolatility = 0.002;
  284.         double dblCounterPartyFundingSpreadDrift = 0.00002;
  285.         double dblCounterPartyFundingSpreadVolatility = 0.002;
  286.         double dblBankThreshold = -0.1;
  287.         double dblCounterPartyThreshold = 0.1;

  288.         double[][] aadblCorrelation = new double[][] {
  289.             {1.00,  0.00,  0.03,  0.07,  0.04,  0.05,  0.08,  0.00,  0.00},  // PORTFOLIO
  290.             {0.00,  1.00,  0.00,  0.00,  0.00,  0.00,  0.00,  0.00,  1.00},  // OVERNIGHT
  291.             {0.03,  0.00,  1.00,  0.26,  0.33,  0.21,  0.35,  0.13,  0.00},  // CSA
  292.             {0.07,  0.00,  0.26,  1.00,  0.45, -0.17,  0.07,  0.77,  0.00},  // BANK HAZARD
  293.             {0.04,  0.00,  0.33,  0.45,  1.00, -0.22, -0.54,  0.58,  0.00},  // COUNTER PARTY HAZARD
  294.             {0.05,  0.00,  0.21, -0.17, -0.22,  1.00,  0.47, -0.23,  0.00},  // BANK RECOVERY
  295.             {0.08,  0.00,  0.35,  0.07, -0.54,  0.47,  1.00,  0.01,  0.00},  // COUNTER PARTY RECOVERY
  296.             {0.00,  0.00,  0.13,  0.77,  0.58, -0.23,  0.01,  1.00,  0.00},  // BANK FUNDING SPREAD
  297.             {0.00,  0.00,  0.00,  0.00,  0.00,  0.00,  0.00,  0.00,  1.00}   // COUNTER PARTY FUNDING SPREAD
  298.         };

  299.         JulianDate dtSpot = DateUtil.Today();

  300.         PositionGroupSpecification positionGroupSpecification = PositionGroupSpecification.FixedThreshold (
  301.             "FIXEDTHRESHOLD",
  302.             dblCounterPartyThreshold,
  303.             dblBankThreshold,
  304.             PositionReplicationScheme.ALBANESE_ANDERSEN_VERTEX,
  305.             BrokenDateScheme.LINEAR_TIME,
  306.             0.,
  307.             CloseOutScheme.ISDA_92
  308.         );

  309.         double dblTimeWidth = dblTime / iNumStep;
  310.         JulianDate[] adtVertex = new JulianDate[iNumStep + 1];
  311.         double[][] aadblPortfolioValue = new double[iNumPath][iNumStep + 1];
  312.         MonoPathExposureAdjustment[] aMPEA = new MonoPathExposureAdjustment[iNumPath];
  313.         double dblBankFundingSpreadInitial = dblBankHazardRateInitial / (1. - dblBankRecoveryRateInitial);
  314.         double dblCounterPartyFundingSpreadInitial = dblCounterPartyHazardRateInitial / (1. - dblCounterPartyRecoveryRateInitial);

  315.         for (int j = 0; j < iNumStep; ++j)
  316.             adtVertex[j] = dtSpot.addMonths (6 * j + 6);

  317.         DiffusionEvolver deATMSwapRateOffset = new DiffusionEvolver (
  318.             DiffusionEvaluatorLinear.Standard (
  319.                 dblATMSwapRateOffsetDrift,
  320.                 dblATMSwapRateOffsetVolatility
  321.             )
  322.         );

  323.         DiffusionEvolver deOvernightNumeraire = new DiffusionEvolver (
  324.             DiffusionEvaluatorLogarithmic.Standard (
  325.                 dblOvernightNumeraireDrift,
  326.                 dblOvernightNumeraireVolatility
  327.             )
  328.         );

  329.         DiffusionEvolver deCSA = new DiffusionEvolver (
  330.             DiffusionEvaluatorLogarithmic.Standard (
  331.                 dblCSADrift,
  332.                 dblCSAVolatility
  333.             )
  334.         );

  335.         DiffusionEvolver deBankHazardRate = new DiffusionEvolver (
  336.             DiffusionEvaluatorLogarithmic.Standard (
  337.                 dblBankHazardRateDrift,
  338.                 dblBankHazardRateVolatility
  339.             )
  340.         );

  341.         DiffusionEvolver deCounterPartyHazardRate = new DiffusionEvolver (
  342.             DiffusionEvaluatorLogarithmic.Standard (
  343.                 dblCounterPartyHazardRateDrift,
  344.                 dblCounterPartyHazardRateVolatility
  345.             )
  346.         );

  347.         DiffusionEvolver deBankRecoveryRate = new DiffusionEvolver (
  348.             DiffusionEvaluatorLogarithmic.Standard (
  349.                 dblBankRecoveryRateDrift,
  350.                 dblBankRecoveryRateVolatility
  351.             )
  352.         );

  353.         DiffusionEvolver deCounterPartyRecoveryRate = new DiffusionEvolver (
  354.             DiffusionEvaluatorLogarithmic.Standard (
  355.                 dblCounterPartyRecoveryRateDrift,
  356.                 dblCounterPartyRecoveryRateVolatility
  357.             )
  358.         );

  359.         DiffusionEvolver deBankFundingSpread = new DiffusionEvolver (
  360.             DiffusionEvaluatorLinear.Standard (
  361.                 dblBankFundingSpreadDrift,
  362.                 dblBankFundingSpreadVolatility
  363.             )
  364.         );

  365.         DiffusionEvolver deCounterPartyFundingSpread = new DiffusionEvolver (
  366.             DiffusionEvaluatorLinear.Standard (
  367.                 dblCounterPartyFundingSpreadDrift,
  368.                 dblCounterPartyFundingSpreadVolatility
  369.             )
  370.         );

  371.         for (int i = 0; i < iNumPath; ++i) {
  372.             double[][] aadblNumeraire = Matrix.Transpose (
  373.                 SequenceGenerator.GaussianJoint (
  374.                     iNumStep,
  375.                     aadblCorrelation
  376.                 )
  377.             );

  378.             aadblPortfolioValue[i] = SwapPortfolioValueRealization (
  379.                 deATMSwapRateOffset,
  380.                 dblATMSwapRateOffsetStart,
  381.                 aadblNumeraire[0],
  382.                 iNumStep,
  383.                 dblTime,
  384.                 dblTimeWidth,
  385.                 iNumSwap
  386.             );

  387.             double[] adblOvernightNumeraire = VertexNumeraireRealization (
  388.                 deOvernightNumeraire,
  389.                 dblOvernightNumeraireInitial,
  390.                 dblTime,
  391.                 dblTimeWidth,
  392.                 aadblNumeraire[1],
  393.                 iNumStep
  394.             );

  395.             double[] adblCSA = VertexNumeraireRealization (
  396.                 deCSA,
  397.                 dblCSAInitial,
  398.                 dblTime,
  399.                 dblTimeWidth,
  400.                 aadblNumeraire[2],
  401.                 iNumStep
  402.             );

  403.             double[] adblBankHazardRate = NumeraireValueRealization (
  404.                 deBankHazardRate,
  405.                 dblBankHazardRateInitial,
  406.                 dblTime,
  407.                 dblTimeWidth,
  408.                 aadblNumeraire[3],
  409.                 iNumStep
  410.             );

  411.             double[] adblCounterPartyHazardRate = NumeraireValueRealization (
  412.                 deCounterPartyHazardRate,
  413.                 dblCounterPartyHazardRateInitial,
  414.                 dblTime,
  415.                 dblTimeWidth,
  416.                 aadblNumeraire[4],
  417.                 iNumStep
  418.             );

  419.             double[] adblBankRecoveryRate = NumeraireValueRealization (
  420.                 deBankRecoveryRate,
  421.                 dblBankRecoveryRateInitial,
  422.                 dblTime,
  423.                 dblTimeWidth,
  424.                 aadblNumeraire[5],
  425.                 iNumStep
  426.             );

  427.             double[] adblCounterPartyRecoveryRate = NumeraireValueRealization (
  428.                 deCounterPartyRecoveryRate,
  429.                 dblCounterPartyRecoveryRateInitial,
  430.                 dblTime,
  431.                 dblTimeWidth,
  432.                 aadblNumeraire[6],
  433.                 iNumStep
  434.             );

  435.             double[] adblBankFundingSpread = NumeraireValueRealization (
  436.                 deBankFundingSpread,
  437.                 dblBankFundingSpreadInitial,
  438.                 dblTime,
  439.                 dblTimeWidth,
  440.                 aadblNumeraire[7],
  441.                 iNumStep
  442.             );

  443.             double[] adblCounterPartyFundingSpread = NumeraireValueRealization (
  444.                 deCounterPartyFundingSpread,
  445.                 dblCounterPartyFundingSpreadInitial,
  446.                 dblTime,
  447.                 dblTimeWidth,
  448.                 aadblNumeraire[8],
  449.                 iNumStep
  450.             );

  451.             JulianDate dtStart = dtSpot;
  452.             MarketVertex[] aMV = new MarketVertex [iNumStep + 1];
  453.             double dblValueStart = dblTime * dblATMSwapRateOffsetStart;
  454.             AlbaneseAndersen[] aHGVR = new AlbaneseAndersen[iNumStep + 1];

  455.             for (int j = 0; j <= iNumStep; ++j)
  456.             {
  457.                 LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

  458.                 latentStateVertexContainer.add (
  459.                     OTCFixFloatLabel.Standard ("USD-3M-10Y"),
  460.                     Double.NaN
  461.                 );

  462.                 aMV[j] = MarketVertex.Nodal (
  463.                     adtVertex[j] = dtSpot.addMonths (6 * j),
  464.                     dblOvernightNumeraireDrift,
  465.                     adblOvernightNumeraire[j],
  466.                     dblCSADrift,
  467.                     adblCSA[j],
  468.                     new MarketVertexEntity (
  469.                         Math.exp (-0.5 * adblBankHazardRate[j] * j),
  470.                         adblBankHazardRate[j],
  471.                         adblBankRecoveryRate[j],
  472.                         adblBankFundingSpread[j],
  473.                         Math.exp (-0.5 * adblBankHazardRate[j] * (1. - adblBankRecoveryRate[j]) * (iNumStep - j)),
  474.                         Double.NaN,
  475.                         Double.NaN,
  476.                         Double.NaN
  477.                     ),
  478.                     new MarketVertexEntity (
  479.                         Math.exp (-0.5 * adblCounterPartyHazardRate[j] * j),
  480.                         adblCounterPartyHazardRate[j],
  481.                         adblCounterPartyRecoveryRate[j],
  482.                         adblCounterPartyFundingSpread[j],
  483.                         Math.exp (-0.5 * adblCounterPartyHazardRate[j] * (1. - adblCounterPartyRecoveryRate[j]) * (iNumStep - j)),
  484.                         Double.NaN,
  485.                         Double.NaN,
  486.                         Double.NaN
  487.                     ),
  488.                     latentStateVertexContainer
  489.                 );

  490.                 double dblCollateralBalance = 0.;
  491.                 JulianDate dtEnd = adtVertex[j];
  492.                 double dblValueEnd = aadblPortfolioValue[i][j];

  493.                 if (0 != j) {
  494.                     CollateralAmountEstimator hae = new CollateralAmountEstimator (
  495.                         positionGroupSpecification,
  496.                         new BrokenDateInterpolatorLinearT (
  497.                             dtStart.julian(),
  498.                             dtEnd.julian(),
  499.                             dblValueStart,
  500.                             dblValueEnd
  501.                         ),
  502.                         Double.NaN
  503.                     );

  504.                     dblCollateralBalance = hae.postingRequirement (dtEnd);
  505.                 }

  506.                 aHGVR[j] = new AlbaneseAndersen (
  507.                     adtVertex[j],
  508.                     aadblPortfolioValue[i][j],
  509.                     0.,
  510.                     dblCollateralBalance
  511.                 );

  512.                 dtStart = dtEnd;
  513.                 dblValueStart = dblValueEnd;
  514.             }

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

  516.             CollateralGroupPath[] aHGP = new CollateralGroupPath[] {
  517.                 new CollateralGroupPath (
  518.                     aHGVR,
  519.                     mp
  520.                 )
  521.             };

  522.             aMPEA[i] = new MonoPathExposureAdjustment (
  523.                 new AlbaneseAndersenFundingGroupPath[] {
  524.                     new AlbaneseAndersenFundingGroupPath (
  525.                         new AlbaneseAndersenNettingGroupPath[] {
  526.                             new AlbaneseAndersenNettingGroupPath (
  527.                                 aHGP,
  528.                                 mp
  529.                             )
  530.                         },
  531.                         mp
  532.                     )
  533.                 }
  534.             );
  535.         }

  536.         ExposureAdjustmentAggregator eaa = new ExposureAdjustmentAggregator (aMPEA);

  537.         ExposureAdjustmentDigest ead = eaa.digest();

  538.         System.out.println();

  539.         UDTDump (
  540.             "\t|                                                                                COLLATERALIZED EXPOSURE                                                                                |",
  541.             eaa.vertexDates(),
  542.             ead.collateralizedExposure()
  543.         );

  544.         UDTDump (
  545.             "\t|                                                                               UNCOLLATERALIZED EXPOSURE                                                                               |",
  546.             eaa.vertexDates(),
  547.             ead.uncollateralizedExposure()
  548.         );

  549.         UDTDump (
  550.             "\t|                                                                                COLLATERALIZED EXPOSURE PV                                                                             |",
  551.             eaa.vertexDates(),
  552.             ead.collateralizedExposurePV()
  553.         );

  554.         UDTDump (
  555.             "\t|                                                                               UNCOLLATERALIZED EXPOSURE PV                                                                            |",
  556.             eaa.vertexDates(),
  557.             ead.uncollateralizedExposurePV()
  558.         );

  559.         UDTDump (
  560.             "\t|                                                                            COLLATERALIZED POSITIVE EXPOSURE PV                                                                        |",
  561.             eaa.vertexDates(),
  562.             ead.collateralizedPositiveExposure()
  563.         );

  564.         UDTDump (
  565.             "\t|                                                                           UNCOLLATERALIZED POSITIVE EXPOSURE PV                                                                       |",
  566.             eaa.vertexDates(),
  567.             ead.uncollateralizedPositiveExposure()
  568.         );

  569.         UDTDump (
  570.             "\t|                                                                            COLLATERALIZED NEGATIVE EXPOSURE PV                                                                        |",
  571.             eaa.vertexDates(),
  572.             ead.collateralizedNegativeExposure()
  573.         );

  574.         UDTDump (
  575.             "\t|                                                                           UNCOLLATERALIZED NEGATIVE EXPOSURE PV                                                                       |",
  576.             eaa.vertexDates(),
  577.             ead.uncollateralizedNegativeExposure()
  578.         );

  579.         System.out.println();

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

  581.         System.out.println ("\t||  UCVA CVA FTDCVA DVA FCA UNIVARIATE THIN STATISTICS ||");

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

  583.         System.out.println ("\t||    L -> R:                                          ||");

  584.         System.out.println ("\t||            - Path Average                           ||");

  585.         System.out.println ("\t||            - Path Maximum                           ||");

  586.         System.out.println ("\t||            - Path Minimum                           ||");

  587.         System.out.println ("\t||            - Monte Carlo Error                      ||");

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

  589.         UDTDump (
  590.             "\t||  UCVA  => ",
  591.             ead.ucva()
  592.         );

  593.         UDTDump (
  594.             "\t|| FTDCVA => ",
  595.             ead.ftdcva()
  596.         );

  597.         UDTDump (
  598.             "\t||   CVA  => ",
  599.             ead.cva()
  600.         );

  601.         UDTDump (
  602.             "\t||  CVACL => ",
  603.             ead.cvacl()
  604.         );

  605.         UDTDump (
  606.             "\t||   DVA  => ",
  607.             ead.dva()
  608.         );

  609.         UDTDump (
  610.             "\t||   FVA  => ",
  611.             ead.fva()
  612.         );

  613.         UDTDump (
  614.             "\t||   FDA  => ",
  615.             ead.fda()
  616.         );

  617.         UDTDump (
  618.             "\t||   FCA  => ",
  619.             ead.fca()
  620.         );

  621.         UDTDump (
  622.             "\t||   FBA  => ",
  623.             ead.fba()
  624.         );

  625.         UDTDump (
  626.             "\t||  SFVA  => ",
  627.             ead.sfva()
  628.         );

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

  630.         UDTDump (
  631.             "\t||  Total => ",
  632.             ead.totalVA()
  633.         );

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

  635.         System.out.println();

  636.         EnvManager.TerminateEnv();
  637.     }
  638. }