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

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

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

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

  93. public class UncollateralizedNettingReceivableStochastic {

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

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

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

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

  123.         return adblNumeraireValue;
  124.     }

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

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

  138.         JumpDiffusionVertex[] aJDV = deNumeraireValue.vertexSequenceReverse (
  139.             new JumpDiffusionVertex (
  140.                 dblTime,
  141.                 dblNumeraireValueInitial,
  142.                 0.,
  143.                 false
  144.             ),
  145.             JumpDiffusionEdgeUnit.Diffusion (
  146.                 adblTimeWidth,
  147.                 adblRandom
  148.             ),
  149.             adblTimeWidth
  150.         );

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

  153.         return adblNumeraireValue;
  154.     }

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

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

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

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

  184.         return adblATMSwapRateOffset;
  185.     }

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

  199.         for (int i = 0; i < iNumStep; ++i)
  200.             adblSwapPortfolioValueRealization[i] = 0.;

  201.         double[] adblATMSwapRateOffsetRealization = ATMSwapRateOffsetRealization (
  202.             deATMSwapRate,
  203.             dblATMSwapRateStart,
  204.             adblRandom,
  205.             dblTime,
  206.             dblTimeWidth,
  207.             iNumStep
  208.         );

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

  212.         return adblSwapPortfolioValueRealization;
  213.     }

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

  227.         return cpvd.multiPathVertexRd()[0].flatform();
  228.     }

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

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

  277.         JulianDate dtSpot = DateUtil.Today();

  278.         double dblTimeWidth = dblTime / iNumStep;
  279.         JulianDate[] adtVertex = new JulianDate[iNumStep + 1];
  280.         double[][] aadblPortfolio1Value = new double[iNumPath][iNumStep + 1];
  281.         double[][] aadblPortfolio2Value = new double[iNumPath][iNumStep + 1];
  282.         double[][] aadblCollateralBalance = new double[iNumPath][iNumStep + 1];
  283.         MonoPathExposureAdjustment[] aCPGPGround = new MonoPathExposureAdjustment[iNumPath];
  284.         MonoPathExposureAdjustment[] aCPGPExtended = new MonoPathExposureAdjustment[iNumPath];
  285.         double dblBankFundingSpreadInitial = dblBankHazardRateInitial / (1. - dblBankRecoveryRateInitial);
  286.         double dblCounterPartyFundingSpreadInitial = dblCounterPartyHazardRateInitial / (1. - dblCounterPartyRecoveryRateInitial);

  287.         DiffusionEvolver deATMSwapRateOffset = new DiffusionEvolver (
  288.             DiffusionEvaluatorLinear.Standard (
  289.                 dblATMSwapRateOffsetDrift,
  290.                 dblATMSwapRateOffsetVolatility
  291.             )
  292.         );

  293.         DiffusionEvolver deOvernightNumeraire = new DiffusionEvolver (
  294.             DiffusionEvaluatorLogarithmic.Standard (
  295.                 dblOvernightNumeraireDrift,
  296.                 dblOvernightNumeraireVolatility
  297.             )
  298.         );

  299.         DiffusionEvolver deCSA = new DiffusionEvolver (
  300.             DiffusionEvaluatorLogarithmic.Standard (
  301.                 dblCSADrift,
  302.                 dblCSAVolatility
  303.             )
  304.         );

  305.         DiffusionEvolver deBankHazardRate = new DiffusionEvolver (
  306.             DiffusionEvaluatorLogarithmic.Standard (
  307.                 dblBankHazardRateDrift,
  308.                 dblBankHazardRateVolatility
  309.             )
  310.         );

  311.         DiffusionEvolver deBankRecoveryRate = new DiffusionEvolver (
  312.             DiffusionEvaluatorLogarithmic.Standard (
  313.                 dblBankRecoveryRateDrift,
  314.                 dblBankRecoveryRateVolatility
  315.             )
  316.         );

  317.         DiffusionEvolver deCounterPartyHazardRate = new DiffusionEvolver (
  318.             DiffusionEvaluatorLogarithmic.Standard (
  319.                 dblCounterPartyHazardRateDrift,
  320.                 dblCounterPartyHazardRateVolatility
  321.             )
  322.         );

  323.         DiffusionEvolver deCounterPartyRecoveryRate = new DiffusionEvolver (
  324.             DiffusionEvaluatorLogarithmic.Standard (
  325.                 dblCounterPartyRecoveryRateDrift,
  326.                 dblCounterPartyRecoveryRateVolatility
  327.             )
  328.         );

  329.         DiffusionEvolver deBankFundingSpread = new DiffusionEvolver (
  330.             DiffusionEvaluatorLinear.Standard (
  331.                 dblBankFundingSpreadDrift,
  332.                 dblBankFundingSpreadVolatility
  333.             )
  334.         );

  335.         DiffusionEvolver deCounterPartyFundingSpread = new DiffusionEvolver (
  336.             DiffusionEvaluatorLinear.Standard (
  337.                 dblCounterPartyFundingSpreadDrift,
  338.                 dblCounterPartyFundingSpreadVolatility
  339.             )
  340.         );

  341.         for (int i = 0; i < iNumPath; ++i) {
  342.             double[][] aadblNumeraire = Matrix.Transpose (
  343.                 Path (
  344.                     aadblCorrelation,
  345.                     iNumVertex
  346.                 )
  347.             );

  348.             aadblPortfolio1Value[i] = SwapPortfolioValueRealization (
  349.                 deATMSwapRateOffset,
  350.                 dblATMSwapRateOffsetStart1,
  351.                 aadblNumeraire[0],
  352.                 iNumVertex,
  353.                 dblTime,
  354.                 dblTimeWidth,
  355.                 dblTimeMaturity1,
  356.                 dblSwapNotional1
  357.             );

  358.             aadblPortfolio2Value[i] = SwapPortfolioValueRealization (
  359.                 deATMSwapRateOffset,
  360.                 dblATMSwapRateOffsetStart2,
  361.                 aadblNumeraire[0],
  362.                 iNumVertex,
  363.                 dblTime,
  364.                 dblTimeWidth,
  365.                 dblTimeMaturity2,
  366.                 dblSwapNotional2
  367.             );

  368.             double[] adblOvernightNumeraire = VertexNumeraireRealization (
  369.                 deOvernightNumeraire,
  370.                 dblOvernightNumeraireInitial,
  371.                 dblTime,
  372.                 dblTimeWidth,
  373.                 aadblNumeraire[1],
  374.                 iNumStep
  375.             );

  376.             double[] adblCSA = VertexNumeraireRealization (
  377.                 deCSA,
  378.                 dblCSAInitial,
  379.                 dblTime,
  380.                 dblTimeWidth,
  381.                 aadblNumeraire[2],
  382.                 iNumStep
  383.             );

  384.             double[] adblBankHazardRate = NumeraireValueRealization (
  385.                 deBankHazardRate,
  386.                 dblBankHazardRateInitial,
  387.                 dblTime,
  388.                 dblTimeWidth,
  389.                 aadblNumeraire[3],
  390.                 iNumStep
  391.             );

  392.             double[] adblCounterPartyHazardRate = NumeraireValueRealization (
  393.                 deCounterPartyHazardRate,
  394.                 dblCounterPartyHazardRateInitial,
  395.                 dblTime,
  396.                 dblTimeWidth,
  397.                 aadblNumeraire[4],
  398.                 iNumStep
  399.             );

  400.             double[] adblBankRecoveryRate = NumeraireValueRealization (
  401.                 deBankRecoveryRate,
  402.                 dblBankRecoveryRateInitial,
  403.                 dblTime,
  404.                 dblTimeWidth,
  405.                 aadblNumeraire[5],
  406.                 iNumStep
  407.             );

  408.             double[] adblCounterPartyRecoveryRate = NumeraireValueRealization (
  409.                 deCounterPartyRecoveryRate,
  410.                 dblCounterPartyRecoveryRateInitial,
  411.                 dblTime,
  412.                 dblTimeWidth,
  413.                 aadblNumeraire[6],
  414.                 iNumStep
  415.             );

  416.             double[] adblBankFundingSpread = NumeraireValueRealization (
  417.                 deBankFundingSpread,
  418.                 dblBankFundingSpreadInitial,
  419.                 dblTime,
  420.                 dblTimeWidth,
  421.                 aadblNumeraire[7],
  422.                 iNumStep
  423.             );

  424.             double[] adblCounterPartyFundingSpread = NumeraireValueRealization (
  425.                 deCounterPartyFundingSpread,
  426.                 dblCounterPartyFundingSpreadInitial,
  427.                 dblTime,
  428.                 dblTimeWidth,
  429.                 aadblNumeraire[8],
  430.                 iNumStep
  431.             );

  432.             MarketVertex[] aNV = new MarketVertex [iNumStep + 1];
  433.             AlbaneseAndersen[] aCGV1 = new AlbaneseAndersen[iNumStep + 1];
  434.             AlbaneseAndersen[] aCGV2 = new AlbaneseAndersen[iNumStep + 1];

  435.             for (int j = 0; j <= iNumStep; ++j)
  436.             {
  437.                 LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

  438.                 latentStateVertexContainer.add (
  439.                     OTCFixFloatLabel.Standard ("USD-3M-10Y"),
  440.                     Double.NaN
  441.                 );

  442.                 aNV[j] = MarketVertex.Nodal (
  443.                     adtVertex[j] = dtSpot.addMonths (6 * j),
  444.                     dblOvernightNumeraireDrift,
  445.                     adblOvernightNumeraire[j],
  446.                     dblCSADrift,
  447.                     adblCSA[j],
  448.                     new MarketVertexEntity (
  449.                         Math.exp (-0.5 * adblBankHazardRate[j] * j),
  450.                         adblBankHazardRate[j],
  451.                         adblBankRecoveryRate[j],
  452.                         adblBankFundingSpread[j],
  453.                         Math.exp (-0.5 * adblBankHazardRate[j] * (1. - adblBankRecoveryRate[j]) * iNumStep),
  454.                         Double.NaN,
  455.                         Double.NaN,
  456.                         Double.NaN
  457.                     ),
  458.                     new MarketVertexEntity (
  459.                         Math.exp (-0.5 * adblCounterPartyHazardRate[j] * j),
  460.                         adblCounterPartyHazardRate[j],
  461.                         adblCounterPartyRecoveryRate[j],
  462.                         adblCounterPartyFundingSpread[j],
  463.                         Math.exp (-0.5 * adblCounterPartyHazardRate[j] * (1. - adblCounterPartyRecoveryRate[j]) * iNumStep),
  464.                         Double.NaN,
  465.                         Double.NaN,
  466.                         Double.NaN
  467.                     ),
  468.                     latentStateVertexContainer
  469.                 );

  470.                 aadblCollateralBalance[i][j] = 0.;

  471.                 aCGV1[j] = new AlbaneseAndersen (
  472.                     adtVertex[j],
  473.                     aadblPortfolio1Value[i][j],
  474.                     0.,
  475.                     0.
  476.                 );

  477.                 aCGV2[j] = new AlbaneseAndersen (
  478.                     adtVertex[j],
  479.                     aadblPortfolio2Value[i][j],
  480.                     0.,
  481.                     0.
  482.                 );
  483.             }

  484.             MarketPath np = MarketPath.FromMarketVertexArray (aNV);

  485.             CollateralGroupPath[] aCGP1 = new CollateralGroupPath[] {
  486.                 new CollateralGroupPath (
  487.                     aCGV1,
  488.                     np
  489.                 )
  490.             };

  491.             CollateralGroupPath[] aCGP2 = new CollateralGroupPath[] {
  492.                 new CollateralGroupPath (
  493.                     aCGV2,
  494.                     np
  495.                 )
  496.             };

  497.             aCPGPGround[i] = new MonoPathExposureAdjustment (
  498.                 new AlbaneseAndersenFundingGroupPath[] {
  499.                     new AlbaneseAndersenFundingGroupPath (
  500.                         new AlbaneseAndersenNettingGroupPath[] {
  501.                             new AlbaneseAndersenNettingGroupPath (
  502.                                 aCGP1,
  503.                                 np
  504.                             )
  505.                         },
  506.                         np
  507.                     )
  508.                 }
  509.             );

  510.             aCPGPExtended[i] = new MonoPathExposureAdjustment (
  511.                 new AlbaneseAndersenFundingGroupPath[] {
  512.                     new AlbaneseAndersenFundingGroupPath (
  513.                         new AlbaneseAndersenNettingGroupPath[] {
  514.                             new AlbaneseAndersenNettingGroupPath (
  515.                                 aCGP1,
  516.                                 np
  517.                             ),
  518.                             new AlbaneseAndersenNettingGroupPath (
  519.                                 aCGP2,
  520.                                 np
  521.                             )
  522.                         },
  523.                         np
  524.                     )
  525.                 }
  526.             );
  527.         }

  528.         return new ExposureAdjustmentAggregator[] {
  529.             new ExposureAdjustmentAggregator (aCPGPGround),
  530.             new ExposureAdjustmentAggregator (aCPGPExtended)
  531.         };
  532.     }

  533.     private static final void CPGDDump (
  534.         final String strHeader,
  535.         final ExposureAdjustmentDigest ead)
  536.         throws Exception
  537.     {
  538.         System.out.println();

  539.         UnivariateDiscreteThin udtUCOLVA = ead.ucolva();

  540.         UnivariateDiscreteThin udtFTDCOLVA = ead.ftdcolva();

  541.         UnivariateDiscreteThin udtUCVA = ead.ucva();

  542.         UnivariateDiscreteThin udtFTDCVA = ead.ftdcva();

  543.         UnivariateDiscreteThin udtCVACL = ead.cvacl();

  544.         UnivariateDiscreteThin udtCVA = ead.cva();

  545.         UnivariateDiscreteThin udtDVA = ead.dva();

  546.         UnivariateDiscreteThin udtFVA = ead.fva();

  547.         UnivariateDiscreteThin udtFDA = ead.fda();

  548.         UnivariateDiscreteThin udtFCA = ead.fca();

  549.         UnivariateDiscreteThin udtFBA = ead.fba();

  550.         UnivariateDiscreteThin udtSFVA = ead.sfva();

  551.         System.out.println (
  552.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  553.         );

  554.         System.out.println (strHeader);

  555.         System.out.println (
  556.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  557.         );

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

  561.         System.out.println (
  562.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  563.         );

  564.         System.out.println (
  565.             "\t|| Average => " +
  566.             FormatUtil.FormatDouble (udtUCOLVA.average(), 2, 2, 1.) + "  |  " +
  567.             FormatUtil.FormatDouble (udtFTDCOLVA.average(), 2, 2, 1.) + "  | " +
  568.             FormatUtil.FormatDouble (udtUCVA.average(), 2, 2, 1.) + "  | " +
  569.             FormatUtil.FormatDouble (udtFTDCVA.average(), 2, 2, 1.) + "  | " +
  570.             FormatUtil.FormatDouble (udtCVACL.average(), 2, 2, 1.) + "  | " +
  571.             FormatUtil.FormatDouble (udtCVA.average(), 2, 2, 1.) + "  | " +
  572.             FormatUtil.FormatDouble (udtDVA.average(), 2, 2, 1.) + "  | " +
  573.             FormatUtil.FormatDouble (udtFVA.average(), 2, 2, 1.) + "  | " +
  574.             FormatUtil.FormatDouble (udtFDA.average(), 2, 2, 1.) + "  | " +
  575.             FormatUtil.FormatDouble (udtFCA.average(), 2, 2, 1.) + "  | " +
  576.             FormatUtil.FormatDouble (udtFBA.average(), 2, 2, 1.) + "  | " +
  577.             FormatUtil.FormatDouble (udtSFVA.average(), 2, 2, 1.) + "  ||"
  578.         );

  579.         System.out.println (
  580.             "\t|| Minimum => " +
  581.             FormatUtil.FormatDouble (udtUCOLVA.minimum(), 2, 2, 1.) + "  |  " +
  582.             FormatUtil.FormatDouble (udtFTDCOLVA.minimum(), 2, 2, 1.) + "  | " +
  583.             FormatUtil.FormatDouble (udtUCVA.minimum(), 2, 2, 1.) + "  | " +
  584.             FormatUtil.FormatDouble (udtFTDCVA.minimum(), 2, 2, 1.) + "  | " +
  585.             FormatUtil.FormatDouble (udtCVACL.minimum(), 2, 2, 1.) + "  | " +
  586.             FormatUtil.FormatDouble (udtCVA.minimum(), 2, 2, 1.) + "  | " +
  587.             FormatUtil.FormatDouble (udtDVA.minimum(), 2, 2, 1.) + "  | " +
  588.             FormatUtil.FormatDouble (udtFVA.minimum(), 2, 2, 1.) + "  | " +
  589.             FormatUtil.FormatDouble (udtFDA.minimum(), 2, 2, 1.) + "  | " +
  590.             FormatUtil.FormatDouble (udtFCA.minimum(), 2, 2, 1.) + "  | " +
  591.             FormatUtil.FormatDouble (udtFBA.minimum(), 2, 2, 1.) + "  | " +
  592.             FormatUtil.FormatDouble (udtSFVA.minimum(), 2, 2, 1.) + "  ||"
  593.         );

  594.         System.out.println (
  595.             "\t|| Maximum => " +
  596.             FormatUtil.FormatDouble (udtUCOLVA.maximum(), 2, 2, 1.) + "  |  " +
  597.             FormatUtil.FormatDouble (udtFTDCOLVA.maximum(), 2, 2, 1.) + "  | " +
  598.             FormatUtil.FormatDouble (udtUCVA.maximum(), 2, 2, 1.) + "  | " +
  599.             FormatUtil.FormatDouble (udtFTDCVA.maximum(), 2, 2, 1.) + "  | " +
  600.             FormatUtil.FormatDouble (udtCVACL.maximum(), 2, 2, 1.) + "  | " +
  601.             FormatUtil.FormatDouble (udtCVA.maximum(), 2, 2, 1.) + "  | " +
  602.             FormatUtil.FormatDouble (udtDVA.maximum(), 2, 2, 1.) + "  | " +
  603.             FormatUtil.FormatDouble (udtFVA.maximum(), 2, 2, 1.) + "  | " +
  604.             FormatUtil.FormatDouble (udtFDA.maximum(), 2, 2, 1.) + "  | " +
  605.             FormatUtil.FormatDouble (udtFCA.maximum(), 2, 2, 1.) + "  | " +
  606.             FormatUtil.FormatDouble (udtFBA.maximum(), 2, 2, 1.) + "  | " +
  607.             FormatUtil.FormatDouble (udtSFVA.maximum(), 2, 2, 1.) + "  ||"
  608.         );

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

  624.         System.out.println (
  625.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  626.         );
  627.     }

  628.     private static final void CPGDDiffDump (
  629.         final String strHeader,
  630.         final ExposureAdjustmentDigest eadGround,
  631.         final ExposureAdjustmentDigest eadExpanded)
  632.         throws Exception
  633.     {
  634.         System.out.println();

  635.         System.out.println (
  636.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  637.         );

  638.         System.out.println (strHeader);

  639.         System.out.println (
  640.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  641.         );

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

  645.         System.out.println (
  646.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  647.         );

  648.         System.out.println (
  649.             "\t|| Average => " +
  650.             FormatUtil.FormatDouble (eadExpanded.ucolva().average() - eadGround.ucolva().average(), 3, 1, 10000.) + "  |  " +
  651.             FormatUtil.FormatDouble (eadExpanded.ftdcolva().average() - eadGround.ftdcolva().average(), 3, 1, 10000.) + "  | " +
  652.             FormatUtil.FormatDouble (eadExpanded.ucva().average() - eadGround.ucva().average(), 3, 1, 10000.) + "  | " +
  653.             FormatUtil.FormatDouble (eadExpanded.ftdcva().average() - eadGround.ftdcva().average(), 3, 1, 10000.) + "  | " +
  654.             FormatUtil.FormatDouble (eadExpanded.cvacl().average() - eadGround.cvacl().average(), 3, 1, 10000.) + "  | " +
  655.             FormatUtil.FormatDouble (eadExpanded.cva().average() - eadGround.cva().average(), 3, 1, 10000.) + "  | " +
  656.             FormatUtil.FormatDouble (eadExpanded.dva().average() - eadGround.dva().average(), 3, 1, 10000.) + "  | " +
  657.             FormatUtil.FormatDouble (eadExpanded.fva().average() - eadGround.fva().average(), 3, 1, 10000.) + "  | " +
  658.             FormatUtil.FormatDouble (eadExpanded.fda().average() - eadGround.fda().average(), 3, 1, 10000.) + "  | " +
  659.             FormatUtil.FormatDouble (eadExpanded.fca().average() - eadGround.fca().average(), 3, 1, 10000.) + "  | " +
  660.             FormatUtil.FormatDouble (eadExpanded.fba().average() - eadGround.fba().average(), 3, 1, 10000.) + "  | " +
  661.             FormatUtil.FormatDouble (eadExpanded.sfva().average() - eadGround.sfva().average(), 3, 1, 10000.) + "  ||"
  662.         );

  663.         System.out.println (
  664.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  665.         );
  666.     }

  667.     private static final void BaselAccountingMetrics (
  668.         final String strHeader,
  669.         final ExposureAdjustmentAggregator cpgaGround,
  670.         final ExposureAdjustmentAggregator cpgaExpanded)
  671.         throws Exception
  672.     {
  673.         OTCAccountingModus oasFCAFBA = new OTCAccountingModusFCAFBA (cpgaGround);

  674.         OTCAccountingModus oasFVAFDA = new OTCAccountingModusFVAFDA (cpgaGround);

  675.         OTCAccountingPolicy oapFCAFBA = oasFCAFBA.feePolicy (cpgaExpanded);

  676.         OTCAccountingPolicy oapFVAFDA = oasFVAFDA.feePolicy (cpgaExpanded);

  677.         System.out.println();

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

  681.         System.out.println (strHeader);

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

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

  688.         System.out.println (
  689.             "\t||         - Accounting Type (FCA/FBA vs. FVA/FDA)                     ||"
  690.         );

  691.         System.out.println (
  692.             "\t||         - Contra Asset Adjustment                                   ||"
  693.         );

  694.         System.out.println (
  695.             "\t||         - Contra Liability Adjustment                               ||"
  696.         );

  697.         System.out.println (
  698.             "\t||         - FTP (Funding Transfer Pricing) (bp)                       ||"
  699.         );

  700.         System.out.println (
  701.             "\t||         - CET1 (Common Equity Tier I) Change (bp)                   ||"
  702.         );

  703.         System.out.println (
  704.             "\t||         - CL (Contra Liability) Change (bp)                         ||"
  705.         );

  706.         System.out.println (
  707.             "\t||         - PFV (Porfolio Value) Change (Income) (bp)                 ||"
  708.         );

  709.         System.out.println (
  710.             "\t||---------------------------------------------------------------------||"
  711.         );

  712.         System.out.println ("\t|| FCA/FBA Accounting => " +
  713.             FormatUtil.FormatDouble (oasFCAFBA.contraAssetAdjustment(), 1, 4, 1.) + " | " +
  714.             FormatUtil.FormatDouble (oasFCAFBA.contraLiabilityAdjustment(), 1, 4, 1.) + " | " +
  715.             FormatUtil.FormatDouble (oapFCAFBA.fundingTransferPricing(), 3, 0, 10000.) + " | " +
  716.             FormatUtil.FormatDouble (oapFCAFBA.cet1Change(), 3, 0, 10000.) + " | " +
  717.             FormatUtil.FormatDouble (oapFCAFBA.contraLiabilityChange(), 3, 0, 10000.) + " | " +
  718.             FormatUtil.FormatDouble (oapFCAFBA.portfolioValueChange(), 3, 0, 10000.) + " || "
  719.         );

  720.         System.out.println ("\t|| FVA/FDA Accounting => " +
  721.             FormatUtil.FormatDouble (oasFVAFDA.contraAssetAdjustment(), 1, 4, 1.) + " | " +
  722.             FormatUtil.FormatDouble (oasFVAFDA.contraLiabilityAdjustment(), 1, 4, 1.) + " | " +
  723.             FormatUtil.FormatDouble (oapFVAFDA.fundingTransferPricing(), 3, 0, 10000.) + " | " +
  724.             FormatUtil.FormatDouble (oapFVAFDA.cet1Change(), 3, 0, 10000.) + " | " +
  725.             FormatUtil.FormatDouble (oapFVAFDA.contraLiabilityChange(), 3, 0, 10000.) + " | " +
  726.             FormatUtil.FormatDouble (oapFVAFDA.portfolioValueChange(), 3, 0, 10000.) + " || "
  727.         );

  728.         System.out.println (
  729.             "\t||---------------------------------------------------------------------||"
  730.         );

  731.         System.out.println();
  732.     }

  733.     public static final void main (
  734.         final String[] astrArgs)
  735.         throws Exception
  736.     {
  737.         EnvManager.InitEnv ("");

  738.         ExposureAdjustmentAggregator[] aCPGA = Mix (
  739.             5.,
  740.             0.,
  741.             100.,
  742.             5.,
  743.             -0.05,
  744.             1.
  745.         );

  746.         ExposureAdjustmentAggregator cpgaGround = aCPGA[0];
  747.         ExposureAdjustmentAggregator cpgaExtended = aCPGA[1];

  748.         ExposureAdjustmentDigest cpgdGround = cpgaGround.digest();

  749.         ExposureAdjustmentDigest cpgdExtended = cpgaExtended.digest();

  750.         CPGDDump (
  751.             "\t||                                                  GROUND BOOK ADJUSTMENT METRICS                                                   ||",
  752.             cpgdGround
  753.         );

  754.         CPGDDump (
  755.             "\t||                                                 EXTENDED BOOK ADJUSTMENT METRICS                                                  ||",
  756.             cpgdExtended
  757.         );

  758.         CPGDDiffDump (
  759.             "\t||                                             TRADE INCREMENT ADJUSTMENT METRICS (bp)                                               ||",
  760.             cpgdGround,
  761.             cpgdExtended
  762.         );

  763.         BaselAccountingMetrics (
  764.             "\t||           ALBANESE & ANDERSEN (2015) BCBS OTC ACCOUNTING            ||",
  765.             cpgaGround,
  766.             cpgaExtended
  767.         );
  768.     }
  769. }