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

  95. public class ZeroThresholdFundingReceivable {

  96.     private static final double[] ATMSwapRateOffsetRealization (
  97.         final DiffusionEvolver deATMSwapRateOffset,
  98.         final double dblATMSwapRateOffsetInitial,
  99.         final double[] adblRandom,
  100.         final double dblTime,
  101.         final double dblTimeWidth,
  102.         final int iNumStep)
  103.         throws Exception
  104.     {
  105.         double[] adblATMSwapRateOffset = new double[iNumStep + 1];
  106.         adblATMSwapRateOffset[0] = dblATMSwapRateOffsetInitial;
  107.         double[] adblTimeWidth = new double[iNumStep];

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

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

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

  125.         return adblATMSwapRateOffset;
  126.     }

  127.     private static final double[] SwapPortfolioValueRealization (
  128.         final DiffusionEvolver deATMSwapRate,
  129.         final double dblATMSwapRateStart,
  130.         final double[] adblRandom,
  131.         final int iNumStep,
  132.         final double dblTime,
  133.         final double dblTimeWidth,
  134.         final double dblTimeMaturity,
  135.         final double dblSwapNotional)
  136.         throws Exception
  137.     {
  138.         double[] adblSwapPortfolioValueRealization = new double[iNumStep + 1];
  139.         int iMaturityStep = (int) (dblTimeMaturity / dblTimeWidth);

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

  142.         double[] adblATMSwapRateOffsetRealization = ATMSwapRateOffsetRealization (
  143.             deATMSwapRate,
  144.             dblATMSwapRateStart,
  145.             adblRandom,
  146.             dblTime,
  147.             dblTimeWidth,
  148.             iNumStep
  149.         );

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

  153.         return adblSwapPortfolioValueRealization;
  154.     }

  155.     private static final ExposureAdjustmentAggregator[] Mix (
  156.         final double dblTimeMaturity1,
  157.         final double dblATMSwapRateOffsetStart1,
  158.         final double dblSwapNotional1,
  159.         final double dblTimeMaturity2,
  160.         final double dblATMSwapRateOffsetStart2,
  161.         final double dblSwapNotional2)
  162.         throws Exception
  163.     {
  164.         int iNumStep = 10;
  165.         int iNumPath = 100000;
  166.         int iNumVertex = 10;
  167.         double dblTime = 5.;
  168.         double dblATMSwapRateOffsetDrift = 0.0;
  169.         double dblATMSwapRateOffsetVolatility = 0.25;
  170.         double dblOvernightNumeraireDrift = 0.004;
  171.         double dblCSADrift = 0.01;
  172.         double dblBankHazardRate = 0.015;
  173.         double dblBankRecoveryRate = 0.40;
  174.         double dblCounterPartyHazardRate = 0.030;
  175.         double dblCounterPartyRecoveryRate = 0.30;
  176.         double dblBankThreshold = 0.;
  177.         double dblCounterPartyThreshold = 0.;

  178.         JulianDate dtSpot = DateUtil.Today();

  179.         double dblTimeWidth = dblTime / iNumStep;
  180.         JulianDate[] adtVertex = new JulianDate[iNumStep + 1];
  181.         MarketVertex[] aNV = new MarketVertex[iNumStep + 1];
  182.         double[][] aadblPortfolio1Value = new double[iNumPath][iNumStep + 1];
  183.         double[][] aadblPortfolio2Value = new double[iNumPath][iNumStep + 1];
  184.         double dblBankFundingSpread = dblBankHazardRate / (1. - dblBankRecoveryRate);
  185.         MonoPathExposureAdjustment[] aCPGPGround = new MonoPathExposureAdjustment[iNumPath];
  186.         MonoPathExposureAdjustment[] aCPGPExtended = new MonoPathExposureAdjustment[iNumPath];
  187.         double dblCounterPartyFundingSpread = dblCounterPartyHazardRate / (1. - dblCounterPartyRecoveryRate);

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

  197.         DiffusionEvolver deATMSwapRateOffset = new DiffusionEvolver (
  198.             DiffusionEvaluatorLinear.Standard (
  199.                 dblATMSwapRateOffsetDrift,
  200.                 dblATMSwapRateOffsetVolatility
  201.             )
  202.         );

  203.         for (int i = 0; i <= iNumStep; ++i)
  204.         {
  205.             LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

  206.             latentStateVertexContainer.add (
  207.                 OTCFixFloatLabel.Standard ("USD-3M-10Y"),
  208.                 Double.NaN
  209.             );

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

  239.         for (int i = 0; i < iNumPath; ++i) {
  240.             aadblPortfolio1Value[i] = SwapPortfolioValueRealization (
  241.                 deATMSwapRateOffset,
  242.                 dblATMSwapRateOffsetStart1,
  243.                 SequenceGenerator.Gaussian (iNumStep),
  244.                 iNumVertex,
  245.                 dblTime,
  246.                 dblTimeWidth,
  247.                 dblTimeMaturity1,
  248.                 dblSwapNotional1
  249.             );

  250.             aadblPortfolio2Value[i] = SwapPortfolioValueRealization (
  251.                 deATMSwapRateOffset,
  252.                 dblATMSwapRateOffsetStart2,
  253.                 SequenceGenerator.Gaussian (iNumStep),
  254.                 iNumVertex,
  255.                 dblTime,
  256.                 dblTimeWidth,
  257.                 dblTimeMaturity2,
  258.                 dblSwapNotional2
  259.             );

  260.             JulianDate dtStart = dtSpot;
  261.             double dblValueStart1 = dblTime * dblATMSwapRateOffsetStart1;
  262.             double dblValueStart2 = dblTime * dblATMSwapRateOffsetStart2;
  263.             AlbaneseAndersen[] aCGV1 = new AlbaneseAndersen[iNumStep + 1];
  264.             AlbaneseAndersen[] aCGV2 = new AlbaneseAndersen[iNumStep + 1];

  265.             for (int j = 0; j <= iNumStep; ++j) {
  266.                 JulianDate dtEnd = adtVertex[j];
  267.                 double dblCollateralBalance1 = 0.;
  268.                 double dblCollateralBalance2 = 0.;
  269.                 double dblValueEnd1 = aadblPortfolio1Value[i][j];
  270.                 double dblValueEnd2 = aadblPortfolio2Value[i][j];

  271.                 if (0 != j) {
  272.                     CollateralAmountEstimator cae1 = new CollateralAmountEstimator (
  273.                         positionGroupSpecification,
  274.                         new BrokenDateInterpolatorLinearT (
  275.                             dtStart.julian(),
  276.                             dtEnd.julian(),
  277.                             dblValueStart1,
  278.                             dblValueEnd1
  279.                         ),
  280.                         Double.NaN
  281.                     );

  282.                     dblCollateralBalance1 = cae1.postingRequirement (dtEnd);

  283.                     CollateralAmountEstimator cae2 = new CollateralAmountEstimator (
  284.                         positionGroupSpecification,
  285.                         new BrokenDateInterpolatorLinearT (
  286.                             dtStart.julian(),
  287.                             dtEnd.julian(),
  288.                             dblValueStart2,
  289.                             dblValueEnd2
  290.                         ),
  291.                         Double.NaN
  292.                     );

  293.                     dblCollateralBalance2 = cae2.postingRequirement (dtEnd);
  294.                 }

  295.                 aCGV1[j] = new AlbaneseAndersen (
  296.                     adtVertex[j],
  297.                     aadblPortfolio1Value[i][j],
  298.                     0.,
  299.                     dblCollateralBalance1
  300.                 );

  301.                 aCGV2[j] = new AlbaneseAndersen (
  302.                     adtVertex[j],
  303.                     aadblPortfolio2Value[i][j],
  304.                     0.,
  305.                     dblCollateralBalance2
  306.                 );

  307.                 dtStart = dtEnd;
  308.                 dblValueStart1 = dblValueEnd1;
  309.                 dblValueStart2 = dblValueEnd2;
  310.             }

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

  312.             CollateralGroupPath[] aCGP1 = new CollateralGroupPath[] {
  313.                 new CollateralGroupPath (
  314.                     aCGV1,
  315.                     np
  316.                 )
  317.             };

  318.             CollateralGroupPath[] aCGP2 = new CollateralGroupPath[] {
  319.                 new CollateralGroupPath (
  320.                     aCGV2,
  321.                     np
  322.                 )
  323.             };

  324.             aCPGPGround[i] = new MonoPathExposureAdjustment (
  325.                 new AlbaneseAndersenFundingGroupPath[] {
  326.                     new AlbaneseAndersenFundingGroupPath (
  327.                         new AlbaneseAndersenNettingGroupPath[] {
  328.                             new AlbaneseAndersenNettingGroupPath (
  329.                                 aCGP1,
  330.                                 np
  331.                             )
  332.                         },
  333.                         np
  334.                     )
  335.                 }
  336.             );

  337.             aCPGPExtended[i] = new MonoPathExposureAdjustment (
  338.                 new AlbaneseAndersenFundingGroupPath[] {
  339.                     new AlbaneseAndersenFundingGroupPath (
  340.                         new AlbaneseAndersenNettingGroupPath[] {
  341.                             new AlbaneseAndersenNettingGroupPath (
  342.                                 aCGP1,
  343.                                 np
  344.                             ),
  345.                             new AlbaneseAndersenNettingGroupPath (
  346.                                 aCGP2,
  347.                                 np
  348.                             )
  349.                         },
  350.                         np
  351.                     )
  352.                 }
  353.             );
  354.         }

  355.         return new ExposureAdjustmentAggregator[] {
  356.             new ExposureAdjustmentAggregator (aCPGPGround),
  357.             new ExposureAdjustmentAggregator (aCPGPExtended)
  358.         };
  359.     }

  360.     private static final void CPGDDump (
  361.         final String strHeader,
  362.         final ExposureAdjustmentDigest ead)
  363.         throws Exception
  364.     {
  365.         System.out.println();

  366.         UnivariateDiscreteThin udtUCOLVA = ead.ucolva();

  367.         UnivariateDiscreteThin udtFTDCOLVA = ead.ftdcolva();

  368.         UnivariateDiscreteThin udtUCVA = ead.ucva();

  369.         UnivariateDiscreteThin udtFTDCVA = ead.ftdcva();

  370.         UnivariateDiscreteThin udtCVACL = ead.cvacl();

  371.         UnivariateDiscreteThin udtCVA = ead.cva();

  372.         UnivariateDiscreteThin udtDVA = ead.dva();

  373.         UnivariateDiscreteThin udtFVA = ead.fva();

  374.         UnivariateDiscreteThin udtFDA = ead.fda();

  375.         UnivariateDiscreteThin udtFCA = ead.fca();

  376.         UnivariateDiscreteThin udtFBA = ead.fba();

  377.         UnivariateDiscreteThin udtSFVA = ead.sfva();

  378.         System.out.println (
  379.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  380.         );

  381.         System.out.println (strHeader);

  382.         System.out.println (
  383.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  384.         );

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

  388.         System.out.println (
  389.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  390.         );

  391.         System.out.println (
  392.             "\t|| Average => " +
  393.             FormatUtil.FormatDouble (udtUCOLVA.average(), 2, 2, 1.) + "  |  " +
  394.             FormatUtil.FormatDouble (udtFTDCOLVA.average(), 2, 2, 1.) + "  | " +
  395.             FormatUtil.FormatDouble (udtUCVA.average(), 2, 2, 1.) + "  | " +
  396.             FormatUtil.FormatDouble (udtFTDCVA.average(), 2, 2, 1.) + "  | " +
  397.             FormatUtil.FormatDouble (udtCVACL.average(), 2, 2, 1.) + "  | " +
  398.             FormatUtil.FormatDouble (udtCVA.average(), 2, 2, 1.) + "  | " +
  399.             FormatUtil.FormatDouble (udtDVA.average(), 2, 2, 1.) + "  | " +
  400.             FormatUtil.FormatDouble (udtFVA.average(), 2, 2, 1.) + "  | " +
  401.             FormatUtil.FormatDouble (udtFDA.average(), 2, 2, 1.) + "  | " +
  402.             FormatUtil.FormatDouble (udtFCA.average(), 2, 2, 1.) + "  | " +
  403.             FormatUtil.FormatDouble (udtFBA.average(), 2, 2, 1.) + "  | " +
  404.             FormatUtil.FormatDouble (udtSFVA.average(), 2, 2, 1.) + "  ||"
  405.         );

  406.         System.out.println (
  407.             "\t|| Minimum => " +
  408.             FormatUtil.FormatDouble (udtUCOLVA.minimum(), 2, 2, 1.) + "  |  " +
  409.             FormatUtil.FormatDouble (udtFTDCOLVA.minimum(), 2, 2, 1.) + "  | " +
  410.             FormatUtil.FormatDouble (udtUCVA.minimum(), 2, 2, 1.) + "  | " +
  411.             FormatUtil.FormatDouble (udtFTDCVA.minimum(), 2, 2, 1.) + "  | " +
  412.             FormatUtil.FormatDouble (udtCVACL.minimum(), 2, 2, 1.) + "  | " +
  413.             FormatUtil.FormatDouble (udtCVA.minimum(), 2, 2, 1.) + "  | " +
  414.             FormatUtil.FormatDouble (udtDVA.minimum(), 2, 2, 1.) + "  | " +
  415.             FormatUtil.FormatDouble (udtFVA.minimum(), 2, 2, 1.) + "  | " +
  416.             FormatUtil.FormatDouble (udtFDA.minimum(), 2, 2, 1.) + "  | " +
  417.             FormatUtil.FormatDouble (udtFCA.minimum(), 2, 2, 1.) + "  | " +
  418.             FormatUtil.FormatDouble (udtFBA.minimum(), 2, 2, 1.) + "  | " +
  419.             FormatUtil.FormatDouble (udtSFVA.minimum(), 2, 2, 1.) + "  ||"
  420.         );

  421.         System.out.println (
  422.             "\t|| Maximum => " +
  423.             FormatUtil.FormatDouble (udtUCOLVA.maximum(), 2, 2, 1.) + "  |  " +
  424.             FormatUtil.FormatDouble (udtFTDCOLVA.maximum(), 2, 2, 1.) + "  | " +
  425.             FormatUtil.FormatDouble (udtUCVA.maximum(), 2, 2, 1.) + "  | " +
  426.             FormatUtil.FormatDouble (udtFTDCVA.maximum(), 2, 2, 1.) + "  | " +
  427.             FormatUtil.FormatDouble (udtCVACL.maximum(), 2, 2, 1.) + "  | " +
  428.             FormatUtil.FormatDouble (udtCVA.maximum(), 2, 2, 1.) + "  | " +
  429.             FormatUtil.FormatDouble (udtDVA.maximum(), 2, 2, 1.) + "  | " +
  430.             FormatUtil.FormatDouble (udtFVA.maximum(), 2, 2, 1.) + "  | " +
  431.             FormatUtil.FormatDouble (udtFDA.maximum(), 2, 2, 1.) + "  | " +
  432.             FormatUtil.FormatDouble (udtFCA.maximum(), 2, 2, 1.) + "  | " +
  433.             FormatUtil.FormatDouble (udtFBA.maximum(), 2, 2, 1.) + "  | " +
  434.             FormatUtil.FormatDouble (udtSFVA.maximum(), 2, 2, 1.) + "  ||"
  435.         );

  436.         System.out.println (
  437.             "\t||  Error  => " +
  438.             FormatUtil.FormatDouble (udtUCOLVA.error(), 2, 2, 1.) + "  |  " +
  439.             FormatUtil.FormatDouble (udtFTDCOLVA.error(), 2, 2, 1.) + "  | " +
  440.             FormatUtil.FormatDouble (udtUCVA.error(), 2, 2, 1.) + "  | " +
  441.             FormatUtil.FormatDouble (udtFTDCVA.error(), 2, 2, 1.) + "  | " +
  442.             FormatUtil.FormatDouble (udtCVACL.error(), 2, 2, 1.) + "  | " +
  443.             FormatUtil.FormatDouble (udtCVA.error(), 2, 2, 1.) + "  | " +
  444.             FormatUtil.FormatDouble (udtDVA.error(), 2, 2, 1.) + "  | " +
  445.             FormatUtil.FormatDouble (udtFVA.error(), 2, 2, 1.) + "  | " +
  446.             FormatUtil.FormatDouble (udtFDA.error(), 2, 2, 1.) + "  | " +
  447.             FormatUtil.FormatDouble (udtFCA.error(), 2, 2, 1.) + "  | " +
  448.             FormatUtil.FormatDouble (udtFBA.error(), 2, 2, 1.) + "  | " +
  449.             FormatUtil.FormatDouble (udtSFVA.error(), 2, 2, 1.) + "  ||"
  450.         );

  451.         System.out.println (
  452.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  453.         );
  454.     }

  455.     private static final void CPGDDiffDump (
  456.         final String strHeader,
  457.         final ExposureAdjustmentDigest eadGround,
  458.         final ExposureAdjustmentDigest eadExpanded)
  459.         throws Exception
  460.     {
  461.         System.out.println();

  462.         System.out.println (
  463.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  464.         );

  465.         System.out.println (strHeader);

  466.         System.out.println (
  467.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  468.         );

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

  472.         System.out.println (
  473.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  474.         );

  475.         System.out.println (
  476.             "\t|| Average => " +
  477.             FormatUtil.FormatDouble (eadExpanded.ucolva().average() - eadGround.ucolva().average(), 3, 1, 10000.) + "  |  " +
  478.             FormatUtil.FormatDouble (eadExpanded.ftdcolva().average() - eadGround.ftdcolva().average(), 3, 1, 10000.) + "  | " +
  479.             FormatUtil.FormatDouble (eadExpanded.ucva().average() - eadGround.ucva().average(), 3, 1, 10000.) + "  | " +
  480.             FormatUtil.FormatDouble (eadExpanded.ftdcva().average() - eadGround.ftdcva().average(), 3, 1, 10000.) + "  | " +
  481.             FormatUtil.FormatDouble (eadExpanded.cvacl().average() - eadGround.cvacl().average(), 3, 1, 10000.) + "  | " +
  482.             FormatUtil.FormatDouble (eadExpanded.cva().average() - eadGround.cva().average(), 3, 1, 10000.) + "  | " +
  483.             FormatUtil.FormatDouble (eadExpanded.dva().average() - eadGround.dva().average(), 3, 1, 10000.) + "  | " +
  484.             FormatUtil.FormatDouble (eadExpanded.fva().average() - eadGround.fva().average(), 3, 1, 10000.) + "  | " +
  485.             FormatUtil.FormatDouble (eadExpanded.fda().average() - eadGround.fda().average(), 3, 1, 10000.) + "  | " +
  486.             FormatUtil.FormatDouble (eadExpanded.fca().average() - eadGround.fca().average(), 3, 1, 10000.) + "  | " +
  487.             FormatUtil.FormatDouble (eadExpanded.fba().average() - eadGround.fba().average(), 3, 1, 10000.) + "  | " +
  488.             FormatUtil.FormatDouble (eadExpanded.sfva().average() - eadGround.sfva().average(), 3, 1, 10000.) + "  ||"
  489.         );

  490.         System.out.println (
  491.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  492.         );
  493.     }

  494.     private static final void BaselAccountingMetrics (
  495.         final String strHeader,
  496.         final ExposureAdjustmentAggregator cpgaGround,
  497.         final ExposureAdjustmentAggregator cpgaExpanded)
  498.         throws Exception
  499.     {
  500.         OTCAccountingModus oasFCAFBA = new OTCAccountingModusFCAFBA (cpgaGround);

  501.         OTCAccountingModus oasFVAFDA = new OTCAccountingModusFVAFDA (cpgaGround);

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

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

  504.         System.out.println();

  505.         System.out.println (
  506.             "\t||---------------------------------------------------------------------||"
  507.         );

  508.         System.out.println (strHeader);

  509.         System.out.println (
  510.             "\t||---------------------------------------------------------------------||"
  511.         );

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

  515.         System.out.println (
  516.             "\t||         - Accounting Type (FCA/FBA vs. FVA/FDA)                     ||"
  517.         );

  518.         System.out.println (
  519.             "\t||         - Contra Asset Adjustment                                   ||"
  520.         );

  521.         System.out.println (
  522.             "\t||         - Contra Liability Adjustment                               ||"
  523.         );

  524.         System.out.println (
  525.             "\t||         - FTP (Funding Transfer Pricing) (bp)                       ||"
  526.         );

  527.         System.out.println (
  528.             "\t||         - CET1 (Common Equity Tier I) Change (bp)                   ||"
  529.         );

  530.         System.out.println (
  531.             "\t||         - CL (Contra Liability) Change (bp)                         ||"
  532.         );

  533.         System.out.println (
  534.             "\t||         - PFV (Porfolio Value) Change (Income) (bp)                 ||"
  535.         );

  536.         System.out.println (
  537.             "\t||---------------------------------------------------------------------||"
  538.         );

  539.         System.out.println ("\t|| FCA/FBA Accounting => " +
  540.             FormatUtil.FormatDouble (oasFCAFBA.contraAssetAdjustment(), 1, 4, 1.) + " | " +
  541.             FormatUtil.FormatDouble (oasFCAFBA.contraLiabilityAdjustment(), 1, 4, 1.) + " | " +
  542.             FormatUtil.FormatDouble (oapFCAFBA.fundingTransferPricing(), 3, 0, 10000.) + " | " +
  543.             FormatUtil.FormatDouble (oapFCAFBA.cet1Change(), 3, 0, 10000.) + " | " +
  544.             FormatUtil.FormatDouble (oapFCAFBA.contraLiabilityChange(), 3, 0, 10000.) + " | " +
  545.             FormatUtil.FormatDouble (oapFCAFBA.portfolioValueChange(), 3, 0, 10000.) + " || "
  546.         );

  547.         System.out.println ("\t|| FVA/FDA Accounting => " +
  548.             FormatUtil.FormatDouble (oasFVAFDA.contraAssetAdjustment(), 1, 4, 1.) + " | " +
  549.             FormatUtil.FormatDouble (oasFVAFDA.contraLiabilityAdjustment(), 1, 4, 1.) + " | " +
  550.             FormatUtil.FormatDouble (oapFVAFDA.fundingTransferPricing(), 3, 0, 10000.) + " | " +
  551.             FormatUtil.FormatDouble (oapFVAFDA.cet1Change(), 3, 0, 10000.) + " | " +
  552.             FormatUtil.FormatDouble (oapFVAFDA.contraLiabilityChange(), 3, 0, 10000.) + " | " +
  553.             FormatUtil.FormatDouble (oapFVAFDA.portfolioValueChange(), 3, 0, 10000.) + " || "
  554.         );

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

  558.         System.out.println();
  559.     }

  560.     public static final void main (
  561.         final String[] astrArgs)
  562.         throws Exception
  563.     {
  564.         EnvManager.InitEnv ("");

  565.         ExposureAdjustmentAggregator[] aCPGA = Mix (
  566.             5.,
  567.             0.,
  568.             100.,
  569.             5.,
  570.             -0.05,
  571.             1.
  572.         );

  573.         ExposureAdjustmentAggregator cpgaGround = aCPGA[0];
  574.         ExposureAdjustmentAggregator cpgaExtended = aCPGA[1];

  575.         ExposureAdjustmentDigest cpgdGround = cpgaGround.digest();

  576.         ExposureAdjustmentDigest cpgdExtended = cpgaExtended.digest();

  577.         CPGDDump (
  578.             "\t||                                                  GROUND BOOK ADJUSTMENT METRICS                                                   ||",
  579.             cpgdGround
  580.         );

  581.         CPGDDump (
  582.             "\t||                                                 EXTENDED BOOK ADJUSTMENT METRICS                                                  ||",
  583.             cpgdExtended
  584.         );

  585.         CPGDDiffDump (
  586.             "\t||                                             TRADE INCREMENT ADJUSTMENT METRICS (bp)                                               ||",
  587.             cpgdGround,
  588.             cpgdExtended
  589.         );

  590.         BaselAccountingMetrics (
  591.             "\t||           ALBANESE & ANDERSEN (2015) BCBS OTC ACCOUNTING            ||",
  592.             cpgaGround,
  593.             cpgaExtended
  594.         );

  595.         EnvManager.TerminateEnv();
  596.     }
  597. }