CollateralizedCollateralPayableStochastic.java

  1. package org.drip.sample.xvabasel;

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

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

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

  68. /**
  69.  * CollateralizedCollateralPayableStochastic examines the Basel BCBS 2012 OTC Accounting Impact to a
  70.  *  Portfolio of 10 Swaps resulting from the Addition of a New Swap - Comparison via both FVA/FDA and FCA/FBA
  71.  *  Schemes. Simulation is carried out under the following Criteria:
  72.  *  
  73.  *    - Collateralization Status - Partially Collateralized
  74.  *    - Aggregation Unit         - Collateral Group
  75.  *    - Added Swap Type          - Positive Upfront Swap (Payable)
  76.  *    - Market Dynamics          - Fully Stochastic (Correlated Market Evolution)
  77.  *  
  78.  *  
  79.  *  The References are:
  80.  *  
  81.  *  - Burgard, C., and M. Kjaer (2014): PDE Representations of Derivatives with Bilateral Counter-party Risk
  82.  *      and Funding Costs, Journal of Credit Risk, 7 (3) 1-19.
  83.  *  
  84.  *  - Burgard, C., and M. Kjaer (2014): In the Balance, Risk, 24 (11) 72-75.
  85.  *  
  86.  *  - Gregory, J. (2009): Being Two-faced over Counter-party Credit Risk, Risk 20 (2) 86-90.
  87.  *  
  88.  *  - Li, B., and Y. Tang (2007): Quantitative Analysis, Derivatives Modeling, and Trading Strategies in the
  89.  *      Presence of Counter-party Credit Risk for the Fixed Income Market, World Scientific Publishing,
  90.  *      Singapore.
  91.  *
  92.  *  - Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives Pricing, Risk
  93.  *      21 (2) 97-102.
  94.  *
  95.  * @author Lakshmi Krishnamurthy
  96.  */

  97. public class CollateralizedCollateralPayableStochastic {

  98.     private static final double[] NumeraireValueRealization (
  99.         final DiffusionEvolver deNumeraireValue,
  100.         final double dblNumeraireValueInitial,
  101.         final double dblTime,
  102.         final double dblTimeWidth,
  103.         final double[] adblRandom,
  104.         final int iNumStep)
  105.         throws Exception
  106.     {
  107.         double[] adblNumeraireValue = new double[iNumStep + 1];
  108.         adblNumeraireValue[0] = dblNumeraireValueInitial;
  109.         double[] adblTimeWidth = new double[iNumStep];

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

  112.         JumpDiffusionEdge[] aJDE = deNumeraireValue.incrementSequence (
  113.             new JumpDiffusionVertex (
  114.                 dblTime,
  115.                 dblNumeraireValueInitial,
  116.                 0.,
  117.                 false
  118.             ),
  119.             JumpDiffusionEdgeUnit.Diffusion (
  120.                 adblTimeWidth,
  121.                 adblRandom
  122.             ),
  123.             dblTimeWidth
  124.         );

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

  127.         return adblNumeraireValue;
  128.     }

  129.     private static final double[] VertexNumeraireRealization (
  130.         final DiffusionEvolver deNumeraireValue,
  131.         final double dblNumeraireValueInitial,
  132.         final double dblTime,
  133.         final double dblTimeWidth,
  134.         final double[] adblRandom,
  135.         final int iNumStep)
  136.         throws Exception
  137.     {
  138.         double[] adblNumeraireValue = new double[iNumStep + 1];
  139.         double[] adblTimeWidth = new double[iNumStep];

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

  142.         JumpDiffusionVertex[] aJDV = deNumeraireValue.vertexSequenceReverse (
  143.             new JumpDiffusionVertex (
  144.                 dblTime,
  145.                 dblNumeraireValueInitial,
  146.                 0.,
  147.                 false
  148.             ),
  149.             JumpDiffusionEdgeUnit.Diffusion (
  150.                 adblTimeWidth,
  151.                 adblRandom
  152.             ),
  153.             adblTimeWidth
  154.         );

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

  157.         return adblNumeraireValue;
  158.     }

  159.     private static final double[] ATMSwapRateOffsetRealization (
  160.         final DiffusionEvolver deATMSwapRateOffset,
  161.         final double dblATMSwapRateOffsetInitial,
  162.         final double[] adblRandom,
  163.         final double dblTime,
  164.         final double dblTimeWidth,
  165.         final int iNumStep)
  166.         throws Exception
  167.     {
  168.         double[] adblATMSwapRateOffset = new double[iNumStep + 1];
  169.         adblATMSwapRateOffset[0] = dblATMSwapRateOffsetInitial;
  170.         double[] adblTimeWidth = new double[iNumStep];

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

  173.         JumpDiffusionEdge[] aJDE = deATMSwapRateOffset.incrementSequence (
  174.             new JumpDiffusionVertex (
  175.                 dblTime,
  176.                 dblATMSwapRateOffsetInitial,
  177.                 0.,
  178.                 false
  179.             ),
  180.             JumpDiffusionEdgeUnit.Diffusion (
  181.                 adblTimeWidth,
  182.                 adblRandom
  183.             ),
  184.             dblTimeWidth
  185.         );

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

  188.         return adblATMSwapRateOffset;
  189.     }

  190.     private static final double[] SwapPortfolioValueRealization (
  191.         final DiffusionEvolver deATMSwapRate,
  192.         final double dblATMSwapRateStart,
  193.         final double[] adblRandom,
  194.         final int iNumStep,
  195.         final double dblTime,
  196.         final double dblTimeWidth,
  197.         final double dblTimeMaturity,
  198.         final double dblSwapNotional)
  199.         throws Exception
  200.     {
  201.         double[] adblSwapPortfolioValueRealization = new double[iNumStep + 1];
  202.         int iMaturityStep = (int) (dblTimeMaturity / dblTimeWidth);

  203.         for (int i = 0; i < iNumStep; ++i)
  204.             adblSwapPortfolioValueRealization[i] = 0.;

  205.         double[] adblATMSwapRateOffsetRealization = ATMSwapRateOffsetRealization (
  206.             deATMSwapRate,
  207.             dblATMSwapRateStart,
  208.             adblRandom,
  209.             dblTime,
  210.             dblTimeWidth,
  211.             iNumStep
  212.         );

  213.         for (int j = 0; j <= iNumStep; ++j)
  214.             adblSwapPortfolioValueRealization[j] = j > iMaturityStep ? 0. :
  215.                 dblSwapNotional * dblTimeWidth * (iMaturityStep - j) * adblATMSwapRateOffsetRealization[j];

  216.         return adblSwapPortfolioValueRealization;
  217.     }

  218.     private static final double[][] Path (
  219.         final double[][] aadblCorrelation,
  220.         final int iNumVertex)
  221.         throws Exception
  222.     {
  223.         CorrelatedPathVertexDimension cpvd = new CorrelatedPathVertexDimension (
  224.             new RandomNumberGenerator(),
  225.             aadblCorrelation,
  226.             iNumVertex,
  227.             1,
  228.             false,
  229.             null
  230.         );

  231.         return cpvd.multiPathVertexRd()[0].flatform();
  232.     }

  233.     private static final ExposureAdjustmentAggregator[] Mix (
  234.         final double dblTimeMaturity1,
  235.         final double dblATMSwapRateOffsetStart1,
  236.         final double dblSwapNotional1,
  237.         final double dblTimeMaturity2,
  238.         final double dblATMSwapRateOffsetStart2,
  239.         final double dblSwapNotional2)
  240.         throws Exception
  241.     {
  242.         int iNumStep = 10;
  243.         int iNumPath = 60000;
  244.         int iNumVertex = 10;
  245.         double dblTime = 5.;
  246.         double dblATMSwapRateOffsetDrift = 0.0;
  247.         double dblATMSwapRateOffsetVolatility = 0.25;
  248.         double dblOvernightNumeraireDrift = 0.004;
  249.         double dblOvernightNumeraireVolatility = 0.02;
  250.         double dblOvernightNumeraireInitial = 1.;
  251.         double dblCSADrift = 0.01;
  252.         double dblCSAVolatility = 0.05;
  253.         double dblCSAInitial = 1.;
  254.         double dblBankHazardRateDrift = 0.002;
  255.         double dblBankHazardRateVolatility = 0.20;
  256.         double dblBankHazardRateInitial = 0.015;
  257.         double dblBankRecoveryRateDrift = 0.002;
  258.         double dblBankRecoveryRateVolatility = 0.02;
  259.         double dblBankRecoveryRateInitial = 0.40;
  260.         double dblCounterPartyHazardRateDrift = 0.002;
  261.         double dblCounterPartyHazardRateVolatility = 0.30;
  262.         double dblCounterPartyHazardRateInitial = 0.030;
  263.         double dblCounterPartyRecoveryRateDrift = 0.002;
  264.         double dblCounterPartyRecoveryRateVolatility = 0.02;
  265.         double dblCounterPartyRecoveryRateInitial = 0.30;
  266.         double dblBankFundingSpreadDrift = 0.00002;
  267.         double dblBankFundingSpreadVolatility = 0.002;
  268.         double dblCounterPartyFundingSpreadDrift = 0.00002;
  269.         double dblCounterPartyFundingSpreadVolatility = 0.002;
  270.         double dblBankThreshold = -0.1;
  271.         double dblCounterPartyThreshold = 0.1;

  272.         double[][] aadblCorrelation = new double[][] {
  273.             {1.00,  0.00,  0.03,  0.07,  0.04,  0.05,  0.08,  0.00,  0.00},  // PORTFOLIO
  274.             {0.00,  1.00,  0.00,  0.00,  0.00,  0.00,  0.00,  0.00,  1.00},  // OVERNIGHT
  275.             {0.03,  0.00,  1.00,  0.26,  0.33,  0.21,  0.35,  0.13,  0.00},  // CSA
  276.             {0.07,  0.00,  0.26,  1.00,  0.45, -0.17,  0.07,  0.77,  0.00},  // BANK HAZARD
  277.             {0.04,  0.00,  0.33,  0.45,  1.00, -0.22, -0.54,  0.58,  0.00},  // COUNTER PARTY HAZARD
  278.             {0.05,  0.00,  0.21, -0.17, -0.22,  1.00,  0.47, -0.23,  0.00},  // BANK RECOVERY
  279.             {0.08,  0.00,  0.35,  0.07, -0.54,  0.47,  1.00,  0.01,  0.00},  // COUNTER PARTY RECOVERY
  280.             {0.00,  0.00,  0.13,  0.77,  0.58, -0.23,  0.01,  1.00,  0.00},  // BANK FUNDING SPREAD
  281.             {0.00,  0.00,  0.00,  0.00,  0.00,  0.00,  0.00,  0.00,  1.00}   // COUNTER PARTY FUNDING SPREAD
  282.         };

  283.         PositionGroupSpecification positionGroupSpecification = PositionGroupSpecification.FixedThreshold (
  284.             "FIXEDTHRESHOLD",
  285.             dblCounterPartyThreshold,
  286.             dblBankThreshold,
  287.             PositionReplicationScheme.ALBANESE_ANDERSEN_VERTEX,
  288.             BrokenDateScheme.LINEAR_TIME,
  289.             0.,
  290.             CloseOutScheme.ISDA_92
  291.         );

  292.         JulianDate dtSpot = DateUtil.Today();

  293.         double dblTimeWidth = dblTime / iNumStep;
  294.         JulianDate[] adtVertex = new JulianDate[iNumStep + 1];
  295.         double[][] aadblPortfolio1Value = new double[iNumPath][iNumStep + 1];
  296.         double[][] aadblPortfolio2Value = new double[iNumPath][iNumStep + 1];
  297.         MonoPathExposureAdjustment[] aMPEAGround = new MonoPathExposureAdjustment[iNumPath];
  298.         MonoPathExposureAdjustment[] aMPEAExtended = new MonoPathExposureAdjustment[iNumPath];
  299.         double dblBankFundingSpreadInitial = dblBankHazardRateInitial / (1. - dblBankRecoveryRateInitial);
  300.         double dblCounterPartyFundingSpreadInitial = dblCounterPartyHazardRateInitial / (1. - dblCounterPartyRecoveryRateInitial);

  301.         DiffusionEvolver deATMSwapRateOffset = new DiffusionEvolver (
  302.             DiffusionEvaluatorLinear.Standard (
  303.                 dblATMSwapRateOffsetDrift,
  304.                 dblATMSwapRateOffsetVolatility
  305.             )
  306.         );

  307.         DiffusionEvolver deOvernightNumeraire = new DiffusionEvolver (
  308.             DiffusionEvaluatorLogarithmic.Standard (
  309.                 dblOvernightNumeraireDrift,
  310.                 dblOvernightNumeraireVolatility
  311.             )
  312.         );

  313.         DiffusionEvolver deCSA = new DiffusionEvolver (
  314.             DiffusionEvaluatorLogarithmic.Standard (
  315.                 dblCSADrift,
  316.                 dblCSAVolatility
  317.             )
  318.         );

  319.         DiffusionEvolver deBankHazardRate = new DiffusionEvolver (
  320.             DiffusionEvaluatorLogarithmic.Standard (
  321.                 dblBankHazardRateDrift,
  322.                 dblBankHazardRateVolatility
  323.             )
  324.         );

  325.         DiffusionEvolver deBankRecoveryRate = new DiffusionEvolver (
  326.             DiffusionEvaluatorLogarithmic.Standard (
  327.                 dblBankRecoveryRateDrift,
  328.                 dblBankRecoveryRateVolatility
  329.             )
  330.         );

  331.         DiffusionEvolver deCounterPartyHazardRate = new DiffusionEvolver (
  332.             DiffusionEvaluatorLogarithmic.Standard (
  333.                 dblCounterPartyHazardRateDrift,
  334.                 dblCounterPartyHazardRateVolatility
  335.             )
  336.         );

  337.         DiffusionEvolver deCounterPartyRecoveryRate = new DiffusionEvolver (
  338.             DiffusionEvaluatorLogarithmic.Standard (
  339.                 dblCounterPartyRecoveryRateDrift,
  340.                 dblCounterPartyRecoveryRateVolatility
  341.             )
  342.         );

  343.         DiffusionEvolver deBankFundingSpread = new DiffusionEvolver (
  344.             DiffusionEvaluatorLinear.Standard (
  345.                 dblBankFundingSpreadDrift,
  346.                 dblBankFundingSpreadVolatility
  347.             )
  348.         );

  349.         DiffusionEvolver deCounterPartyFundingSpread = new DiffusionEvolver (
  350.             DiffusionEvaluatorLinear.Standard (
  351.                 dblCounterPartyFundingSpreadDrift,
  352.                 dblCounterPartyFundingSpreadVolatility
  353.             )
  354.         );

  355.         for (int i = 0; i < iNumPath; ++i) {
  356.             double[][] aadblNumeraire = Matrix.Transpose (
  357.                 Path (
  358.                     aadblCorrelation,
  359.                     iNumVertex
  360.                 )
  361.             );

  362.             aadblPortfolio1Value[i] = SwapPortfolioValueRealization (
  363.                 deATMSwapRateOffset,
  364.                 dblATMSwapRateOffsetStart1,
  365.                 aadblNumeraire[0],
  366.                 iNumVertex,
  367.                 dblTime,
  368.                 dblTimeWidth,
  369.                 dblTimeMaturity1,
  370.                 dblSwapNotional1
  371.             );

  372.             aadblPortfolio2Value[i] = SwapPortfolioValueRealization (
  373.                 deATMSwapRateOffset,
  374.                 dblATMSwapRateOffsetStart2,
  375.                 aadblNumeraire[0],
  376.                 iNumVertex,
  377.                 dblTime,
  378.                 dblTimeWidth,
  379.                 dblTimeMaturity2,
  380.                 dblSwapNotional2
  381.             );

  382.             double[] adblOvernightNumeraire = VertexNumeraireRealization (
  383.                 deOvernightNumeraire,
  384.                 dblOvernightNumeraireInitial,
  385.                 dblTime,
  386.                 dblTimeWidth,
  387.                 aadblNumeraire[1],
  388.                 iNumStep
  389.             );

  390.             double[] adblCSA = VertexNumeraireRealization (
  391.                 deCSA,
  392.                 dblCSAInitial,
  393.                 dblTime,
  394.                 dblTimeWidth,
  395.                 aadblNumeraire[2],
  396.                 iNumStep
  397.             );

  398.             double[] adblBankHazardRate = NumeraireValueRealization (
  399.                 deBankHazardRate,
  400.                 dblBankHazardRateInitial,
  401.                 dblTime,
  402.                 dblTimeWidth,
  403.                 aadblNumeraire[3],
  404.                 iNumStep
  405.             );

  406.             double[] adblCounterPartyHazardRate = NumeraireValueRealization (
  407.                 deCounterPartyHazardRate,
  408.                 dblCounterPartyHazardRateInitial,
  409.                 dblTime,
  410.                 dblTimeWidth,
  411.                 aadblNumeraire[4],
  412.                 iNumStep
  413.             );

  414.             double[] adblBankRecoveryRate = NumeraireValueRealization (
  415.                 deBankRecoveryRate,
  416.                 dblBankRecoveryRateInitial,
  417.                 dblTime,
  418.                 dblTimeWidth,
  419.                 aadblNumeraire[5],
  420.                 iNumStep
  421.             );

  422.             double[] adblCounterPartyRecoveryRate = NumeraireValueRealization (
  423.                 deCounterPartyRecoveryRate,
  424.                 dblCounterPartyRecoveryRateInitial,
  425.                 dblTime,
  426.                 dblTimeWidth,
  427.                 aadblNumeraire[6],
  428.                 iNumStep
  429.             );

  430.             double[] adblBankFundingSpread = NumeraireValueRealization (
  431.                 deBankFundingSpread,
  432.                 dblBankFundingSpreadInitial,
  433.                 dblTime,
  434.                 dblTimeWidth,
  435.                 aadblNumeraire[7],
  436.                 iNumStep
  437.             );

  438.             double[] adblCounterPartyFundingSpread = NumeraireValueRealization (
  439.                 deCounterPartyFundingSpread,
  440.                 dblCounterPartyFundingSpreadInitial,
  441.                 dblTime,
  442.                 dblTimeWidth,
  443.                 aadblNumeraire[8],
  444.                 iNumStep
  445.             );

  446.             JulianDate dtStart = dtSpot;
  447.             MarketVertex[] aMV = new MarketVertex [iNumStep + 1];
  448.             double dblValueStart1 = dblTime * dblATMSwapRateOffsetStart1;
  449.             double dblValueStart2 = dblTime * dblATMSwapRateOffsetStart2;
  450.             AlbaneseAndersen[] aHGVR1 = new AlbaneseAndersen[iNumStep + 1];
  451.             AlbaneseAndersen[] aHGVR2 = new AlbaneseAndersen[iNumStep + 1];

  452.             for (int j = 0; j <= iNumStep; ++j) {
  453.                 JulianDate dtEnd = (adtVertex[j] = dtSpot.addMonths (6 * j + 6));

  454.                 double dblCollateralBalance1 = 0.;
  455.                 double dblCollateralBalance2 = 0.;
  456.                 double dblValueEnd1 = aadblPortfolio1Value[i][j];
  457.                 double dblValueEnd2 = aadblPortfolio2Value[i][j];

  458.                 if (0 != j) {
  459.                     CollateralAmountEstimator hae1 = new CollateralAmountEstimator (
  460.                         positionGroupSpecification,
  461.                         new BrokenDateInterpolatorLinearT (
  462.                             dtStart.julian(),
  463.                             dtEnd.julian(),
  464.                             dblValueStart1,
  465.                             dblValueEnd1
  466.                         ),
  467.                         Double.NaN
  468.                     );

  469.                     dblCollateralBalance1 = hae1.postingRequirement (dtEnd);

  470.                     CollateralAmountEstimator hae2 = new CollateralAmountEstimator (
  471.                         positionGroupSpecification,
  472.                         new BrokenDateInterpolatorLinearT (
  473.                             dtStart.julian(),
  474.                             dtEnd.julian(),
  475.                             dblValueStart2,
  476.                             dblValueEnd2
  477.                         ),
  478.                         Double.NaN
  479.                     );

  480.                     dblCollateralBalance2 = hae2.postingRequirement (dtEnd);
  481.                 }

  482.                 LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

  483.                 latentStateVertexContainer.add (
  484.                     OTCFixFloatLabel.Standard ("USD-3M-10Y"),
  485.                     Double.NaN
  486.                 );

  487.                 aMV[j] = MarketVertex.Nodal (
  488.                     adtVertex[j] = dtSpot.addMonths (6 * j),
  489.                     dblOvernightNumeraireDrift,
  490.                     adblOvernightNumeraire[j],
  491.                     dblCSADrift,
  492.                     adblCSA[j],
  493.                     new MarketVertexEntity (
  494.                         Math.exp (-0.5 * adblBankHazardRate[j] * j),
  495.                         adblBankHazardRate[j],
  496.                         adblBankRecoveryRate[j],
  497.                         adblBankFundingSpread[j],
  498.                         Math.exp (-0.5 * adblBankHazardRate[j] * (1. - adblBankRecoveryRate[j]) * (iNumStep - j)),
  499.                         Double.NaN,
  500.                         Double.NaN,
  501.                         Double.NaN
  502.                     ),
  503.                     new MarketVertexEntity (
  504.                         Math.exp (-0.5 * adblCounterPartyHazardRate[j] * j),
  505.                         adblCounterPartyHazardRate[j],
  506.                         adblCounterPartyRecoveryRate[j],
  507.                         adblCounterPartyFundingSpread[j],
  508.                         Math.exp (-0.5 * adblCounterPartyHazardRate[j] * (1. - adblCounterPartyRecoveryRate[j]) * (iNumStep - j)),
  509.                         Double.NaN,
  510.                         Double.NaN,
  511.                         Double.NaN
  512.                     ),
  513.                     latentStateVertexContainer
  514.                 );

  515.                 aHGVR1[j] = new AlbaneseAndersen (
  516.                     adtVertex[j],
  517.                     aadblPortfolio1Value[i][j],
  518.                     0.,
  519.                     dblCollateralBalance1
  520.                 );

  521.                 aHGVR2[j] = new AlbaneseAndersen (
  522.                     adtVertex[j],
  523.                     aadblPortfolio2Value[i][j],
  524.                     0.,
  525.                     dblCollateralBalance2
  526.                 );
  527.             }

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

  529.             CollateralGroupPath[] aHGPGround = new CollateralGroupPath[] {
  530.                 new CollateralGroupPath (
  531.                     aHGVR1,
  532.                     mp
  533.                 )
  534.             };

  535.             CollateralGroupPath[] aHGPExtended = new CollateralGroupPath[] {
  536.                 new CollateralGroupPath (
  537.                     aHGVR1,
  538.                     mp
  539.                 ),
  540.                 new CollateralGroupPath (
  541.                     aHGVR2,
  542.                     mp
  543.                 )
  544.             };

  545.             aMPEAGround[i] = new MonoPathExposureAdjustment (
  546.                 new AlbaneseAndersenFundingGroupPath[] {
  547.                     new AlbaneseAndersenFundingGroupPath (
  548.                         new AlbaneseAndersenNettingGroupPath[] {
  549.                             new AlbaneseAndersenNettingGroupPath (
  550.                                 aHGPGround,
  551.                                 mp
  552.                             )
  553.                         },
  554.                         mp
  555.                     )
  556.                 }
  557.             );

  558.             aMPEAExtended[i] = new MonoPathExposureAdjustment (
  559.                 new AlbaneseAndersenFundingGroupPath[] {
  560.                     new AlbaneseAndersenFundingGroupPath (
  561.                         new AlbaneseAndersenNettingGroupPath[] {
  562.                             new AlbaneseAndersenNettingGroupPath (
  563.                                 aHGPExtended,
  564.                                 mp
  565.                             )
  566.                         },
  567.                         mp
  568.                     )
  569.                 }
  570.             );
  571.         }

  572.         return new ExposureAdjustmentAggregator[] {
  573.             new ExposureAdjustmentAggregator (aMPEAGround),
  574.             new ExposureAdjustmentAggregator (aMPEAExtended)
  575.         };
  576.     }

  577.     private static final void CPGDDump (
  578.         final String strHeader,
  579.         final ExposureAdjustmentDigest ead)
  580.         throws Exception
  581.     {
  582.         System.out.println();

  583.         UnivariateDiscreteThin udtUCOLVA = ead.ucolva();

  584.         UnivariateDiscreteThin udtFTDCOLVA = ead.ftdcolva();

  585.         UnivariateDiscreteThin udtUCVA = ead.ucva();

  586.         UnivariateDiscreteThin udtFTDCVA = ead.ftdcva();

  587.         UnivariateDiscreteThin udtCVACL = ead.cvacl();

  588.         UnivariateDiscreteThin udtCVA = ead.cva();

  589.         UnivariateDiscreteThin udtDVA = ead.dva();

  590.         UnivariateDiscreteThin udtFVA = ead.fva();

  591.         UnivariateDiscreteThin udtFDA = ead.fda();

  592.         UnivariateDiscreteThin udtFCA = ead.fca();

  593.         UnivariateDiscreteThin udtFBA = ead.fba();

  594.         UnivariateDiscreteThin udtSFVA = ead.sfva();

  595.         System.out.println (
  596.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  597.         );

  598.         System.out.println (strHeader);

  599.         System.out.println (
  600.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  601.         );

  602.         System.out.println (
  603.             "\t||  OODLE  => UCOLVA  | FTDCOLVA |  UCVA   | FTDCVA  |  CVACL  |   CVA   |   DVA   |   FVA   |   FDA   |   FCA   |   FBA   |   SFVA  ||"
  604.         );

  605.         System.out.println (
  606.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  607.         );

  608.         System.out.println (
  609.             "\t|| Average => " +
  610.             FormatUtil.FormatDouble (udtUCOLVA.average(), 2, 2, 1.) + "  |  " +
  611.             FormatUtil.FormatDouble (udtFTDCOLVA.average(), 2, 2, 1.) + "  | " +
  612.             FormatUtil.FormatDouble (udtUCVA.average(), 2, 2, 1.) + "  | " +
  613.             FormatUtil.FormatDouble (udtFTDCVA.average(), 2, 2, 1.) + "  | " +
  614.             FormatUtil.FormatDouble (udtCVACL.average(), 2, 2, 1.) + "  | " +
  615.             FormatUtil.FormatDouble (udtCVA.average(), 2, 2, 1.) + "  | " +
  616.             FormatUtil.FormatDouble (udtDVA.average(), 2, 2, 1.) + "  | " +
  617.             FormatUtil.FormatDouble (udtFVA.average(), 2, 2, 1.) + "  | " +
  618.             FormatUtil.FormatDouble (udtFDA.average(), 2, 2, 1.) + "  | " +
  619.             FormatUtil.FormatDouble (udtFCA.average(), 2, 2, 1.) + "  | " +
  620.             FormatUtil.FormatDouble (udtFBA.average(), 2, 2, 1.) + "  | " +
  621.             FormatUtil.FormatDouble (udtSFVA.average(), 2, 2, 1.) + "  ||"
  622.         );

  623.         System.out.println (
  624.             "\t|| Minimum => " +
  625.             FormatUtil.FormatDouble (udtUCOLVA.minimum(), 2, 2, 1.) + "  |  " +
  626.             FormatUtil.FormatDouble (udtFTDCOLVA.minimum(), 2, 2, 1.) + "  | " +
  627.             FormatUtil.FormatDouble (udtUCVA.minimum(), 2, 2, 1.) + "  | " +
  628.             FormatUtil.FormatDouble (udtFTDCVA.minimum(), 2, 2, 1.) + "  | " +
  629.             FormatUtil.FormatDouble (udtCVACL.minimum(), 2, 2, 1.) + "  | " +
  630.             FormatUtil.FormatDouble (udtCVA.minimum(), 2, 2, 1.) + "  | " +
  631.             FormatUtil.FormatDouble (udtDVA.minimum(), 2, 2, 1.) + "  | " +
  632.             FormatUtil.FormatDouble (udtFVA.minimum(), 2, 2, 1.) + "  | " +
  633.             FormatUtil.FormatDouble (udtFDA.minimum(), 2, 2, 1.) + "  | " +
  634.             FormatUtil.FormatDouble (udtFCA.minimum(), 2, 2, 1.) + "  | " +
  635.             FormatUtil.FormatDouble (udtFBA.minimum(), 2, 2, 1.) + "  | " +
  636.             FormatUtil.FormatDouble (udtSFVA.minimum(), 2, 2, 1.) + "  ||"
  637.         );

  638.         System.out.println (
  639.             "\t|| Maximum => " +
  640.             FormatUtil.FormatDouble (udtUCOLVA.maximum(), 2, 2, 1.) + "  |  " +
  641.             FormatUtil.FormatDouble (udtFTDCOLVA.maximum(), 2, 2, 1.) + "  | " +
  642.             FormatUtil.FormatDouble (udtUCVA.maximum(), 2, 2, 1.) + "  | " +
  643.             FormatUtil.FormatDouble (udtFTDCVA.maximum(), 2, 2, 1.) + "  | " +
  644.             FormatUtil.FormatDouble (udtCVACL.maximum(), 2, 2, 1.) + "  | " +
  645.             FormatUtil.FormatDouble (udtCVA.maximum(), 2, 2, 1.) + "  | " +
  646.             FormatUtil.FormatDouble (udtDVA.maximum(), 2, 2, 1.) + "  | " +
  647.             FormatUtil.FormatDouble (udtFVA.maximum(), 2, 2, 1.) + "  | " +
  648.             FormatUtil.FormatDouble (udtFDA.maximum(), 2, 2, 1.) + "  | " +
  649.             FormatUtil.FormatDouble (udtFCA.maximum(), 2, 2, 1.) + "  | " +
  650.             FormatUtil.FormatDouble (udtFBA.maximum(), 2, 2, 1.) + "  | " +
  651.             FormatUtil.FormatDouble (udtSFVA.maximum(), 2, 2, 1.) + "  ||"
  652.         );

  653.         System.out.println (
  654.             "\t||  Error  => " +
  655.             FormatUtil.FormatDouble (udtUCOLVA.error(), 2, 2, 1.) + "  |  " +
  656.             FormatUtil.FormatDouble (udtFTDCOLVA.error(), 2, 2, 1.) + "  | " +
  657.             FormatUtil.FormatDouble (udtUCVA.error(), 2, 2, 1.) + "  | " +
  658.             FormatUtil.FormatDouble (udtFTDCVA.error(), 2, 2, 1.) + "  | " +
  659.             FormatUtil.FormatDouble (udtCVACL.error(), 2, 2, 1.) + "  | " +
  660.             FormatUtil.FormatDouble (udtCVA.error(), 2, 2, 1.) + "  | " +
  661.             FormatUtil.FormatDouble (udtDVA.error(), 2, 2, 1.) + "  | " +
  662.             FormatUtil.FormatDouble (udtFVA.error(), 2, 2, 1.) + "  | " +
  663.             FormatUtil.FormatDouble (udtFDA.error(), 2, 2, 1.) + "  | " +
  664.             FormatUtil.FormatDouble (udtFCA.error(), 2, 2, 1.) + "  | " +
  665.             FormatUtil.FormatDouble (udtFBA.error(), 2, 2, 1.) + "  | " +
  666.             FormatUtil.FormatDouble (udtSFVA.error(), 2, 2, 1.) + "  ||"
  667.         );

  668.         System.out.println (
  669.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  670.         );
  671.     }

  672.     private static final void CPGDDiffDump (
  673.         final String strHeader,
  674.         final ExposureAdjustmentDigest eadGround,
  675.         final ExposureAdjustmentDigest eadExpanded)
  676.         throws Exception
  677.     {
  678.         System.out.println();

  679.         System.out.println (
  680.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  681.         );

  682.         System.out.println (strHeader);

  683.         System.out.println (
  684.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  685.         );

  686.         System.out.println (
  687.             "\t||  OODLE  => UCOLVA  | FTDCOLVA |  UCVA   | FTDCVA  |  CVACL  |   CVA   |   DVA   |   FVA   |   FDA   |   FCA   |   FBA   |   SFVA  ||"
  688.         );

  689.         System.out.println (
  690.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  691.         );

  692.         System.out.println (
  693.             "\t|| Average => " +
  694.             FormatUtil.FormatDouble (eadExpanded.ucolva().average() - eadGround.ucolva().average(), 3, 1, 10000.) + "  |  " +
  695.             FormatUtil.FormatDouble (eadExpanded.ftdcolva().average() - eadGround.ftdcolva().average(), 3, 1, 10000.) + "  | " +
  696.             FormatUtil.FormatDouble (eadExpanded.ucva().average() - eadGround.ucva().average(), 3, 1, 10000.) + "  | " +
  697.             FormatUtil.FormatDouble (eadExpanded.ftdcva().average() - eadGround.ftdcva().average(), 3, 1, 10000.) + "  | " +
  698.             FormatUtil.FormatDouble (eadExpanded.cvacl().average() - eadGround.cvacl().average(), 3, 1, 10000.) + "  | " +
  699.             FormatUtil.FormatDouble (eadExpanded.cva().average() - eadGround.cva().average(), 3, 1, 10000.) + "  | " +
  700.             FormatUtil.FormatDouble (eadExpanded.dva().average() - eadGround.dva().average(), 3, 1, 10000.) + "  | " +
  701.             FormatUtil.FormatDouble (eadExpanded.fva().average() - eadGround.fva().average(), 3, 1, 10000.) + "  | " +
  702.             FormatUtil.FormatDouble (eadExpanded.fda().average() - eadGround.fda().average(), 3, 1, 10000.) + "  | " +
  703.             FormatUtil.FormatDouble (eadExpanded.fca().average() - eadGround.fca().average(), 3, 1, 10000.) + "  | " +
  704.             FormatUtil.FormatDouble (eadExpanded.fba().average() - eadGround.fba().average(), 3, 1, 10000.) + "  | " +
  705.             FormatUtil.FormatDouble (eadExpanded.sfva().average() - eadGround.sfva().average(), 3, 1, 10000.) + "  ||"
  706.         );

  707.         System.out.println (
  708.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  709.         );
  710.     }

  711.     private static final void BaselAccountingMetrics (
  712.         final String strHeader,
  713.         final ExposureAdjustmentAggregator eaaGround,
  714.         final ExposureAdjustmentAggregator eaaExpanded)
  715.         throws Exception
  716.     {
  717.         OTCAccountingModus oasFCAFBA = new OTCAccountingModusFCAFBA (eaaGround);

  718.         OTCAccountingModus oasFVAFDA = new OTCAccountingModusFVAFDA (eaaGround);

  719.         OTCAccountingPolicy oapFCAFBA = oasFCAFBA.feePolicy (eaaExpanded);

  720.         OTCAccountingPolicy oapFVAFDA = oasFVAFDA.feePolicy (eaaExpanded);

  721.         System.out.println();

  722.         System.out.println (
  723.             "\t||---------------------------------------------------------------------||"
  724.         );

  725.         System.out.println (strHeader);

  726.         System.out.println (
  727.             "\t||---------------------------------------------------------------------||"
  728.         );

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

  732.         System.out.println (
  733.             "\t||         - Accounting Type (FCA/FBA vs. FVA/FDA)                     ||"
  734.         );

  735.         System.out.println (
  736.             "\t||         - Contra Asset Adjustment                                   ||"
  737.         );

  738.         System.out.println (
  739.             "\t||         - Contra Liability Adjustment                               ||"
  740.         );

  741.         System.out.println (
  742.             "\t||         - FTP (Funding Transfer Pricing) (bp)                       ||"
  743.         );

  744.         System.out.println (
  745.             "\t||         - CET1 (Common Equity Tier I) Change (bp)                   ||"
  746.         );

  747.         System.out.println (
  748.             "\t||         - CL (Contra Liability) Change (bp)                         ||"
  749.         );

  750.         System.out.println (
  751.             "\t||         - PFV (Porfolio Value) Change (Income) (bp)                 ||"
  752.         );

  753.         System.out.println (
  754.             "\t||---------------------------------------------------------------------||"
  755.         );

  756.         System.out.println ("\t|| FCA/FBA Accounting => " +
  757.             FormatUtil.FormatDouble (oasFCAFBA.contraAssetAdjustment(), 1, 4, 1.) + " | " +
  758.             FormatUtil.FormatDouble (oasFCAFBA.contraLiabilityAdjustment(), 1, 4, 1.) + " | " +
  759.             FormatUtil.FormatDouble (oapFCAFBA.fundingTransferPricing(), 3, 0, 10000.) + " | " +
  760.             FormatUtil.FormatDouble (oapFCAFBA.cet1Change(), 3, 0, 10000.) + " | " +
  761.             FormatUtil.FormatDouble (oapFCAFBA.contraLiabilityChange(), 3, 0, 10000.) + " | " +
  762.             FormatUtil.FormatDouble (oapFCAFBA.portfolioValueChange(), 3, 0, 10000.) + " || "
  763.         );

  764.         System.out.println ("\t|| FVA/FDA Accounting => " +
  765.             FormatUtil.FormatDouble (oasFVAFDA.contraAssetAdjustment(), 1, 4, 1.) + " | " +
  766.             FormatUtil.FormatDouble (oasFVAFDA.contraLiabilityAdjustment(), 1, 4, 1.) + " | " +
  767.             FormatUtil.FormatDouble (oapFVAFDA.fundingTransferPricing(), 3, 0, 10000.) + " | " +
  768.             FormatUtil.FormatDouble (oapFVAFDA.cet1Change(), 3, 0, 10000.) + " | " +
  769.             FormatUtil.FormatDouble (oapFVAFDA.contraLiabilityChange(), 3, 0, 10000.) + " | " +
  770.             FormatUtil.FormatDouble (oapFVAFDA.portfolioValueChange(), 3, 0, 10000.) + " || "
  771.         );

  772.         System.out.println (
  773.             "\t||---------------------------------------------------------------------||"
  774.         );

  775.         System.out.println();
  776.     }

  777.     public static final void main (
  778.         final String[] astrArgs)
  779.         throws Exception
  780.     {
  781.         EnvManager.InitEnv ("");

  782.         ExposureAdjustmentAggregator[] aEAA = Mix (
  783.             5.,
  784.             0.,
  785.             100.,
  786.             5.,
  787.             0.05,
  788.             1.
  789.         );

  790.         ExposureAdjustmentAggregator eaaGround = aEAA[0];
  791.         ExposureAdjustmentAggregator eaaExtended = aEAA[1];

  792.         ExposureAdjustmentDigest eadGround = eaaGround.digest();

  793.         ExposureAdjustmentDigest eadExtended = eaaExtended.digest();

  794.         CPGDDump (
  795.             "\t||                                                   GROUND BOOK ADJUSTMENT METRICS                                                  ||",
  796.             eadGround
  797.         );

  798.         CPGDDump (
  799.             "\t||                                                  EXTENDED BOOK ADJUSTMENT METRICS                                                 ||",
  800.             eadExtended
  801.         );

  802.         CPGDDiffDump (
  803.             "\t||                                             TRADE INCREMENT ADJUSTMENT METRICS (bp)                                               ||",
  804.             eadGround,
  805.             eadExtended
  806.         );

  807.         BaselAccountingMetrics (
  808.             "\t||           ALBANESE & ANDERSEN (2015) BCBS OTC ACCOUNTING            ||",
  809.             eaaGround,
  810.             eaaExtended
  811.         );

  812.         EnvManager.TerminateEnv();
  813.     }
  814. }