PerfectReplicationCollateralizedFunding.java

  1. package org.drip.sample.burgard2013;

  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.DiffusionEvaluatorLinear;
  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.definition.*;
  17. import org.drip.xva.gross.*;
  18. import org.drip.xva.hypothecation.*;
  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.BurgardKjaerBuilder;

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

  27. /*!
  28.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  29.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  30.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  31.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  32.  *
  33.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  34.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  35.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  36.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  37.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  38.  *      and computational support.
  39.  *  
  40.  *      https://lakshmidrip.github.io/DROP/
  41.  *  
  42.  *  DROP is composed of three modules:
  43.  *  
  44.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  45.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  46.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  47.  *
  48.  *  DROP Product Core implements libraries for the following:
  49.  *  - Fixed Income Analytics
  50.  *  - Loan Analytics
  51.  *  - Transaction Cost Analytics
  52.  *
  53.  *  DROP Portfolio Core implements libraries for the following:
  54.  *  - Asset Allocation Analytics
  55.  *  - Asset Liability Management Analytics
  56.  *  - Capital Estimation Analytics
  57.  *  - Exposure Analytics
  58.  *  - Margin Analytics
  59.  *  - XVA Analytics
  60.  *
  61.  *  DROP Computational Core implements libraries for the following:
  62.  *  - Algorithm Support
  63.  *  - Computation Support
  64.  *  - Function Analysis
  65.  *  - Model Validation
  66.  *  - Numerical Analysis
  67.  *  - Numerical Optimizer
  68.  *  - Spline Builder
  69.  *  - Statistical Learning
  70.  *
  71.  *  Documentation for DROP is Spread Over:
  72.  *
  73.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  74.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  75.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  76.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  77.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  78.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  79.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  80.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  81.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  82.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  83.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  84.  *
  85.  *  Licensed under the Apache License, Version 2.0 (the "License");
  86.  *      you may not use this file except in compliance with the License.
  87.  *  
  88.  *  You may obtain a copy of the License at
  89.  *      http://www.apache.org/licenses/LICENSE-2.0
  90.  *  
  91.  *  Unless required by applicable law or agreed to in writing, software
  92.  *      distributed under the License is distributed on an "AS IS" BASIS,
  93.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  94.  *  
  95.  *  See the License for the specific language governing permissions and
  96.  *      limitations under the License.
  97.  */

  98. /**
  99.  * <i>PerfectReplicationCollateralizedFunding</i> examines the Basel BCBS 2012 OTC Accounting Impact to a
  100.  * Portfolio of 10 Swaps resulting from the Addition of a New Swap - Comparison via both FVA/FDA and FCA/FBA
  101.  * Schemes. Simulation is carried out under the following Criteria using one of the Generalized Burgard Kjaer
  102.  * (2013) Scheme.
  103.  *  
  104.  * <br><br>
  105.  *  <ul>
  106.  *      <li>
  107.  *          Collateralization Status - Collateralized
  108.  *      </li>
  109.  *      <li>
  110.  *          Aggregation Unit         - Funding Group
  111.  *      </li>
  112.  *      <li>
  113.  *          Added Swap Type          - Zero Upfront Par Swap (Neutral)
  114.  *      </li>
  115.  *      <li>
  116.  *          Market Dynamics          - Deterministic (Dynamic Market Evolution)
  117.  *      </li>
  118.  *      <li>
  119.  *          Funding Strategy         - Perfect Replication
  120.  *      </li>
  121.  *  </ul>
  122.  *  
  123.  * The References are:
  124.  *  
  125.  * <br><br>
  126.  *  <ul>
  127.  *      <li>
  128.  *          Burgard, C., and M. Kjaer (2014): PDE Representations of Derivatives with Bilateral Counter-party
  129.  *              Risk and Funding Costs <i>Journal of Credit Risk</i> <b>7 (3)</b> 1-19
  130.  *      </li>
  131.  *      <li>
  132.  *          Burgard, C., and M. Kjaer (2014): In the Balance <i>Risk</i> <b>24 (11)</b> 72-75
  133.  *      </li>
  134.  *      <li>
  135.  *          Gregory, J. (2009): Being Two-faced over Counter-party Credit Risk <i>Risk</i> <b>20 (2)</b>
  136.  *              86-90
  137.  *      </li>
  138.  *      <li>
  139.  *          Li, B., and Y. Tang (2007): <i>Quantitative Analysis, Derivatives Modeling, and Trading
  140.  *              Strategies in the Presence of Counter-party Credit Risk for the Fixed Income Market</i>
  141.  *              <b>World Scientific Publishing</b> Singapore
  142.  *      </li>
  143.  *      <li>
  144.  *          Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives Pricing
  145.  *              <i>Risk</i> <b>21 (2)</b> 97-102
  146.  *      </li>
  147.  *  </ul>
  148.  *  
  149.  * <br><br>
  150.  *  <ul>
  151.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  152.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/XVAAnalyticsLibrary.md">XVA Analytics Library</a></li>
  153.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">DROP API Construction and Usage</a></li>
  154.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/burgard2013/README.md">Burgard Kjaer (2013) Valuation Adjustments</a></li>
  155.  *  </ul>
  156.  * <br><br>
  157.  *
  158.  * @author Lakshmi Krishnamurthy
  159.  */

  160. public class PerfectReplicationCollateralizedFunding {

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

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

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

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

  190.         return adblATMSwapRateOffset;
  191.     }

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

  205.         for (int i = 0; i < iNumStep; ++i)
  206.             adblSwapPortfolioValueRealization[i] = 0.;

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

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

  218.         return adblSwapPortfolioValueRealization;
  219.     }

  220.     private static final ExposureAdjustmentAggregator[] Mix (
  221.         final double dblTimeMaturity1,
  222.         final double dblATMSwapRateOffsetStart1,
  223.         final double dblSwapNotional1,
  224.         final double dblTimeMaturity2,
  225.         final double dblATMSwapRateOffsetStart2,
  226.         final double dblSwapNotional2)
  227.         throws Exception
  228.     {
  229.         int iNumStep = 10;
  230.         int iNumPath = 100000;
  231.         int iNumVertex = 10;
  232.         double dblTime = 5.;
  233.         double dblATMSwapRateOffsetDrift = 0.0;
  234.         double dblATMSwapRateOffsetVolatility = 0.25;
  235.         double dblOvernightNumeraireDrift = 0.01;
  236.         double dblCSADrift = 0.01;
  237.         double dblBankHazardRate = 0.015;
  238.         double dblBankSeniorRecoveryRate = 0.40;
  239.         double dblBankSubordinateRecoveryRate = 0.15;
  240.         double dblCounterPartyHazardRate = 0.030;
  241.         double dblCounterPartyRecoveryRate = 0.30;
  242.         double dblBankThreshold = -0.1;
  243.         double dblCounterPartyThreshold = 0.1;

  244.         JulianDate dtSpot = DateUtil.Today();

  245.         double dblTimeWidth = dblTime / iNumStep;
  246.         MarketVertex[] aMV = new MarketVertex[iNumStep + 1];
  247.         JulianDate[] adtVertex = new JulianDate[iNumStep + 1];
  248.         double[][] aadblPortfolio1Value = new double[iNumPath][iNumStep + 1];
  249.         double[][] aadblPortfolio2Value = new double[iNumPath][iNumStep + 1];
  250.         MonoPathExposureAdjustment[] aMPEAGround = new MonoPathExposureAdjustment[iNumPath];
  251.         MonoPathExposureAdjustment[] aMPEAExtended = new MonoPathExposureAdjustment[iNumPath];
  252.         double dblBankSeniorFundingSpread = dblBankHazardRate / (1. - dblBankSeniorRecoveryRate);
  253.         double dblBankSubordinateFundingSpread = dblBankHazardRate / (1. - dblBankSubordinateRecoveryRate);
  254.         double dblCounterPartyFundingSpread = dblCounterPartyHazardRate / (1. - dblCounterPartyRecoveryRate);

  255.         PositionGroupSpecification positionGroupSpecification = PositionGroupSpecification.FixedThreshold (
  256.             "FIXEDTHRESHOLD",
  257.             dblCounterPartyThreshold,
  258.             dblBankThreshold,
  259.             PositionReplicationScheme.BURGARD_KJAER_HEDGE_ERROR_DUAL_BOND_VERTEX,
  260.             BrokenDateScheme.LINEAR_TIME,
  261.             0.,
  262.             CloseOutScheme.BILATERAL
  263.         );

  264.         CloseOut cog = new CloseOutBilateral (
  265.             dblBankSeniorRecoveryRate,
  266.             dblCounterPartyRecoveryRate
  267.         );

  268.         DiffusionEvolver deATMSwapRateOffset = new DiffusionEvolver (
  269.             DiffusionEvaluatorLinear.Standard (
  270.                 dblATMSwapRateOffsetDrift,
  271.                 dblATMSwapRateOffsetVolatility
  272.             )
  273.         );

  274.         for (int i = 0; i <= iNumStep; ++i)
  275.         {
  276.             LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

  277.             latentStateVertexContainer.add (
  278.                 OTCFixFloatLabel.Standard ("USD-3M-10Y"),
  279.                 Double.NaN
  280.             );

  281.             aMV[i] = MarketVertex.Nodal (
  282.                 adtVertex[i] = dtSpot.addMonths (6 * i),
  283.                 dblOvernightNumeraireDrift,
  284.                 Math.exp (-0.5 * dblOvernightNumeraireDrift * iNumStep),
  285.                 dblCSADrift,
  286.                 Math.exp (-0.5 * dblCSADrift * iNumStep),
  287.                 new MarketVertexEntity (
  288.                     Math.exp (-0.5 * dblBankHazardRate * i),
  289.                     dblBankHazardRate,
  290.                     dblBankSeniorRecoveryRate,
  291.                     dblBankSeniorFundingSpread,
  292.                     Math.exp (-0.5 * dblBankHazardRate * (1. - dblBankSeniorRecoveryRate) * iNumStep),
  293.                     dblBankSubordinateRecoveryRate,
  294.                     dblBankSubordinateFundingSpread,
  295.                     Math.exp (-0.5 * dblBankHazardRate * (1. - dblBankSubordinateRecoveryRate) * iNumStep)
  296.                 ),
  297.                 new MarketVertexEntity (
  298.                     Math.exp (-0.5 * dblCounterPartyHazardRate * i),
  299.                     dblCounterPartyHazardRate,
  300.                     dblCounterPartyRecoveryRate,
  301.                     dblCounterPartyFundingSpread,
  302.                     Math.exp (-0.5 * dblCounterPartyHazardRate * (1. - dblCounterPartyRecoveryRate) * iNumStep),
  303.                     Double.NaN,
  304.                     Double.NaN,
  305.                     Double.NaN
  306.                 ),
  307.                 latentStateVertexContainer
  308.             );
  309.         }

  310.         for (int i = 0; i < iNumPath; ++i) {
  311.             aadblPortfolio1Value[i] = SwapPortfolioValueRealization (
  312.                 deATMSwapRateOffset,
  313.                 dblATMSwapRateOffsetStart1,
  314.                 SequenceGenerator.Gaussian (iNumStep),
  315.                 iNumVertex,
  316.                 dblTime,
  317.                 dblTimeWidth,
  318.                 dblTimeMaturity1,
  319.                 dblSwapNotional1
  320.             );

  321.             aadblPortfolio2Value[i] = SwapPortfolioValueRealization (
  322.                 deATMSwapRateOffset,
  323.                 dblATMSwapRateOffsetStart2,
  324.                 SequenceGenerator.Gaussian (iNumStep),
  325.                 iNumVertex,
  326.                 dblTime,
  327.                 dblTimeWidth,
  328.                 dblTimeMaturity2,
  329.                 dblSwapNotional2
  330.             );

  331.             JulianDate dtStart = dtSpot;
  332.             double dblValueStart1 = dblTime * dblATMSwapRateOffsetStart1;
  333.             double dblValueStart2 = dblTime * dblATMSwapRateOffsetStart2;
  334.             CollateralGroupVertex[] aCGV1 = new CollateralGroupVertex[iNumStep + 1];
  335.             CollateralGroupVertex[] aCGV2 = new CollateralGroupVertex[iNumStep + 1];

  336.             for (int j = 0; j <= iNumStep; ++j) {
  337.                 JulianDate dtEnd = adtVertex[j];
  338.                 double dblCollateralBalance1 = 0.;
  339.                 double dblCollateralBalance2 = 0.;
  340.                 double dblValueEnd1 = aadblPortfolio1Value[i][j];
  341.                 double dblValueEnd2 = aadblPortfolio2Value[i][j];

  342.                 if (0 != j) {
  343.                     CollateralAmountEstimator hae1 = new CollateralAmountEstimator (
  344.                         positionGroupSpecification,
  345.                         new BrokenDateInterpolatorLinearT (
  346.                             dtStart.julian(),
  347.                             dtEnd.julian(),
  348.                             dblValueStart1,
  349.                             dblValueEnd1
  350.                         ),
  351.                         Double.NaN
  352.                     );

  353.                     dblCollateralBalance1 = hae1.postingRequirement (dtEnd);

  354.                     CollateralAmountEstimator hae2 = new CollateralAmountEstimator (
  355.                         positionGroupSpecification,
  356.                         new BrokenDateInterpolatorLinearT (
  357.                             dtStart.julian(),
  358.                             dtEnd.julian(),
  359.                             dblValueStart2,
  360.                             dblValueEnd2
  361.                         ),
  362.                         Double.NaN
  363.                     );

  364.                     dblCollateralBalance2 = hae2.postingRequirement (dtEnd);

  365.                     aCGV1[j] = BurgardKjaerBuilder.HedgeErrorDualBond (
  366.                         adtVertex[j],
  367.                         aadblPortfolio1Value[i][j],
  368.                         0.,
  369.                         dblCollateralBalance1,
  370.                         0.,
  371.                         new MarketEdge (
  372.                             aMV[j - 1],
  373.                             aMV[j]
  374.                         ),
  375.                         cog
  376.                     );

  377.                     aCGV2[j] = BurgardKjaerBuilder.HedgeErrorDualBond (
  378.                         adtVertex[j],
  379.                         aadblPortfolio2Value[i][j],
  380.                         0.,
  381.                         dblCollateralBalance2,
  382.                         0.,
  383.                         new MarketEdge (
  384.                             aMV[j - 1],
  385.                             aMV[j]
  386.                         ),
  387.                         cog
  388.                     );
  389.                 } else {
  390.                     aCGV1[j] = BurgardKjaerBuilder.Initial (
  391.                         adtVertex[j],
  392.                         aadblPortfolio1Value[i][0],
  393.                         aMV[j],
  394.                         cog
  395.                     );

  396.                     aCGV2[j] = BurgardKjaerBuilder.Initial (
  397.                         adtVertex[j],
  398.                         aadblPortfolio2Value[i][0],
  399.                         aMV[j],
  400.                         cog
  401.                     );
  402.                 }

  403.                 dtStart = dtEnd;
  404.                 dblValueStart1 = dblValueEnd1;
  405.                 dblValueStart2 = dblValueEnd2;
  406.             }

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

  408.             CollateralGroupPath[] aCGP1 = new CollateralGroupPath[] {
  409.                 new CollateralGroupPath (
  410.                     aCGV1,
  411.                     mp
  412.                 )
  413.             };

  414.             CollateralGroupPath[] aCGP2 = new CollateralGroupPath[] {
  415.                 new CollateralGroupPath (
  416.                     aCGV2,
  417.                     mp
  418.                 )
  419.             };

  420.             aMPEAGround[i] = new MonoPathExposureAdjustment (
  421.                 new AlbaneseAndersenFundingGroupPath[] {
  422.                     new AlbaneseAndersenFundingGroupPath (
  423.                         new AlbaneseAndersenNettingGroupPath[] {
  424.                             new AlbaneseAndersenNettingGroupPath (
  425.                                 aCGP1,
  426.                                 mp
  427.                             )
  428.                         },
  429.                         mp
  430.                     )
  431.                 }
  432.             );

  433.             aMPEAExtended[i] = new MonoPathExposureAdjustment (
  434.                 new AlbaneseAndersenFundingGroupPath[] {
  435.                     new AlbaneseAndersenFundingGroupPath (
  436.                         new AlbaneseAndersenNettingGroupPath[] {
  437.                             new AlbaneseAndersenNettingGroupPath (
  438.                                 aCGP1,
  439.                                 mp
  440.                             ),
  441.                             new AlbaneseAndersenNettingGroupPath (
  442.                                 aCGP2,
  443.                                 mp
  444.                             )
  445.                         },
  446.                         mp
  447.                     )
  448.                 }
  449.             );
  450.         }

  451.         return new ExposureAdjustmentAggregator[] {
  452.             new ExposureAdjustmentAggregator (aMPEAGround),
  453.             new ExposureAdjustmentAggregator (aMPEAExtended)
  454.         };
  455.     }

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

  462.         UnivariateDiscreteThin udtUCOLVA = ead.ucolva();

  463.         UnivariateDiscreteThin udtFTDCOLVA = ead.ftdcolva();

  464.         UnivariateDiscreteThin udtUCVA = ead.ucva();

  465.         UnivariateDiscreteThin udtFTDCVA = ead.ftdcva();

  466.         UnivariateDiscreteThin udtCVACL = ead.cvacl();

  467.         UnivariateDiscreteThin udtCVA = ead.cva();

  468.         UnivariateDiscreteThin udtDVA = ead.dva();

  469.         UnivariateDiscreteThin udtFVA = ead.fva();

  470.         UnivariateDiscreteThin udtFDA = ead.fda();

  471.         UnivariateDiscreteThin udtFCA = ead.fca();

  472.         UnivariateDiscreteThin udtFBA = ead.fba();

  473.         UnivariateDiscreteThin udtSFVA = ead.sfva();

  474.         System.out.println (
  475.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  476.         );

  477.         System.out.println (strHeader);

  478.         System.out.println (
  479.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  480.         );

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

  484.         System.out.println (
  485.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  486.         );

  487.         System.out.println (
  488.             "\t|| Average => " +
  489.             FormatUtil.FormatDouble (udtUCOLVA.average(), 2, 2, 1.) + "  |  " +
  490.             FormatUtil.FormatDouble (udtFTDCOLVA.average(), 2, 2, 1.) + "  | " +
  491.             FormatUtil.FormatDouble (udtUCVA.average(), 2, 2, 1.) + "  | " +
  492.             FormatUtil.FormatDouble (udtFTDCVA.average(), 2, 2, 1.) + "  | " +
  493.             FormatUtil.FormatDouble (udtCVACL.average(), 2, 2, 1.) + "  | " +
  494.             FormatUtil.FormatDouble (udtCVA.average(), 2, 2, 1.) + "  | " +
  495.             FormatUtil.FormatDouble (udtDVA.average(), 2, 2, 1.) + "  | " +
  496.             FormatUtil.FormatDouble (udtFVA.average(), 2, 2, 1.) + "  | " +
  497.             FormatUtil.FormatDouble (udtFDA.average(), 2, 2, 1.) + "  | " +
  498.             FormatUtil.FormatDouble (udtFCA.average(), 2, 2, 1.) + "  | " +
  499.             FormatUtil.FormatDouble (udtFBA.average(), 2, 2, 1.) + "  | " +
  500.             FormatUtil.FormatDouble (udtSFVA.average(), 2, 2, 1.) + "  ||"
  501.         );

  502.         System.out.println (
  503.             "\t|| Minimum => " +
  504.             FormatUtil.FormatDouble (udtUCOLVA.minimum(), 2, 2, 1.) + "  |  " +
  505.             FormatUtil.FormatDouble (udtFTDCOLVA.minimum(), 2, 2, 1.) + "  | " +
  506.             FormatUtil.FormatDouble (udtUCVA.minimum(), 2, 2, 1.) + "  | " +
  507.             FormatUtil.FormatDouble (udtFTDCVA.minimum(), 2, 2, 1.) + "  | " +
  508.             FormatUtil.FormatDouble (udtCVACL.minimum(), 2, 2, 1.) + "  | " +
  509.             FormatUtil.FormatDouble (udtCVA.minimum(), 2, 2, 1.) + "  | " +
  510.             FormatUtil.FormatDouble (udtDVA.minimum(), 2, 2, 1.) + "  | " +
  511.             FormatUtil.FormatDouble (udtFVA.minimum(), 2, 2, 1.) + "  | " +
  512.             FormatUtil.FormatDouble (udtFDA.minimum(), 2, 2, 1.) + "  | " +
  513.             FormatUtil.FormatDouble (udtFCA.minimum(), 2, 2, 1.) + "  | " +
  514.             FormatUtil.FormatDouble (udtFBA.minimum(), 2, 2, 1.) + "  | " +
  515.             FormatUtil.FormatDouble (udtSFVA.minimum(), 2, 2, 1.) + "  ||"
  516.         );

  517.         System.out.println (
  518.             "\t|| Maximum => " +
  519.             FormatUtil.FormatDouble (udtUCOLVA.maximum(), 2, 2, 1.) + "  |  " +
  520.             FormatUtil.FormatDouble (udtFTDCOLVA.maximum(), 2, 2, 1.) + "  | " +
  521.             FormatUtil.FormatDouble (udtUCVA.maximum(), 2, 2, 1.) + "  | " +
  522.             FormatUtil.FormatDouble (udtFTDCVA.maximum(), 2, 2, 1.) + "  | " +
  523.             FormatUtil.FormatDouble (udtCVACL.maximum(), 2, 2, 1.) + "  | " +
  524.             FormatUtil.FormatDouble (udtCVA.maximum(), 2, 2, 1.) + "  | " +
  525.             FormatUtil.FormatDouble (udtDVA.maximum(), 2, 2, 1.) + "  | " +
  526.             FormatUtil.FormatDouble (udtFVA.maximum(), 2, 2, 1.) + "  | " +
  527.             FormatUtil.FormatDouble (udtFDA.maximum(), 2, 2, 1.) + "  | " +
  528.             FormatUtil.FormatDouble (udtFCA.maximum(), 2, 2, 1.) + "  | " +
  529.             FormatUtil.FormatDouble (udtFBA.maximum(), 2, 2, 1.) + "  | " +
  530.             FormatUtil.FormatDouble (udtSFVA.maximum(), 2, 2, 1.) + "  ||"
  531.         );

  532.         System.out.println (
  533.             "\t||  Error  => " +
  534.             FormatUtil.FormatDouble (udtUCOLVA.error(), 2, 2, 1.) + "  |  " +
  535.             FormatUtil.FormatDouble (udtFTDCOLVA.error(), 2, 2, 1.) + "  | " +
  536.             FormatUtil.FormatDouble (udtUCVA.error(), 2, 2, 1.) + "  | " +
  537.             FormatUtil.FormatDouble (udtFTDCVA.error(), 2, 2, 1.) + "  | " +
  538.             FormatUtil.FormatDouble (udtCVACL.error(), 2, 2, 1.) + "  | " +
  539.             FormatUtil.FormatDouble (udtCVA.error(), 2, 2, 1.) + "  | " +
  540.             FormatUtil.FormatDouble (udtDVA.error(), 2, 2, 1.) + "  | " +
  541.             FormatUtil.FormatDouble (udtFVA.error(), 2, 2, 1.) + "  | " +
  542.             FormatUtil.FormatDouble (udtFDA.error(), 2, 2, 1.) + "  | " +
  543.             FormatUtil.FormatDouble (udtFCA.error(), 2, 2, 1.) + "  | " +
  544.             FormatUtil.FormatDouble (udtFBA.error(), 2, 2, 1.) + "  | " +
  545.             FormatUtil.FormatDouble (udtSFVA.error(), 2, 2, 1.) + "  ||"
  546.         );

  547.         System.out.println (
  548.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  549.         );
  550.     }

  551.     private static final void CPGDDiffDump (
  552.         final String strHeader,
  553.         final ExposureAdjustmentDigest eadGround,
  554.         final ExposureAdjustmentDigest eadExpanded)
  555.         throws Exception
  556.     {
  557.         System.out.println();

  558.         System.out.println (
  559.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  560.         );

  561.         System.out.println (strHeader);

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

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

  568.         System.out.println (
  569.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  570.         );

  571.         System.out.println (
  572.             "\t|| Average => " +
  573.             FormatUtil.FormatDouble (eadExpanded.ucolva().average() - eadGround.ucolva().average(), 3, 1, 10000.) + "  |  " +
  574.             FormatUtil.FormatDouble (eadExpanded.ftdcolva().average() - eadGround.ftdcolva().average(), 3, 1, 10000.) + "  | " +
  575.             FormatUtil.FormatDouble (eadExpanded.ucva().average() - eadGround.ucva().average(), 3, 1, 10000.) + "  | " +
  576.             FormatUtil.FormatDouble (eadExpanded.ftdcva().average() - eadGround.ftdcva().average(), 3, 1, 10000.) + "  | " +
  577.             FormatUtil.FormatDouble (eadExpanded.cvacl().average() - eadGround.cvacl().average(), 3, 1, 10000.) + "  | " +
  578.             FormatUtil.FormatDouble (eadExpanded.cva().average() - eadGround.cva().average(), 3, 1, 10000.) + "  | " +
  579.             FormatUtil.FormatDouble (eadExpanded.dva().average() - eadGround.dva().average(), 3, 1, 10000.) + "  | " +
  580.             FormatUtil.FormatDouble (eadExpanded.fva().average() - eadGround.fva().average(), 3, 1, 10000.) + "  | " +
  581.             FormatUtil.FormatDouble (eadExpanded.fda().average() - eadGround.fda().average(), 3, 1, 10000.) + "  | " +
  582.             FormatUtil.FormatDouble (eadExpanded.fca().average() - eadGround.fca().average(), 3, 1, 10000.) + "  | " +
  583.             FormatUtil.FormatDouble (eadExpanded.fba().average() - eadGround.fba().average(), 3, 1, 10000.) + "  | " +
  584.             FormatUtil.FormatDouble (eadExpanded.sfva().average() - eadGround.sfva().average(), 3, 1, 10000.) + "  ||"
  585.         );

  586.         System.out.println (
  587.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  588.         );
  589.     }

  590.     private static final void BaselAccountingMetrics (
  591.         final String strHeader,
  592.         final ExposureAdjustmentAggregator eadGround,
  593.         final ExposureAdjustmentAggregator eadExpanded)
  594.         throws Exception
  595.     {
  596.         OTCAccountingModus oasFCAFBA = new OTCAccountingModusFCAFBA (eadGround);

  597.         OTCAccountingModus oasFVAFDA = new OTCAccountingModusFVAFDA (eadGround);

  598.         OTCAccountingPolicy oapFCAFBA = oasFCAFBA.feePolicy (eadExpanded);

  599.         OTCAccountingPolicy oapFVAFDA = oasFVAFDA.feePolicy (eadExpanded);

  600.         System.out.println();

  601.         System.out.println (
  602.             "\t||---------------------------------------------------------------------||"
  603.         );

  604.         System.out.println (strHeader);

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

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

  611.         System.out.println (
  612.             "\t||         - Accounting Type (FCA/FBA vs. FVA/FDA)                     ||"
  613.         );

  614.         System.out.println (
  615.             "\t||         - Contra Asset Adjustment                                   ||"
  616.         );

  617.         System.out.println (
  618.             "\t||         - Contra Liability Adjustment                               ||"
  619.         );

  620.         System.out.println (
  621.             "\t||         - FTP (Funding Transfer Pricing) (bp)                       ||"
  622.         );

  623.         System.out.println (
  624.             "\t||         - CET1 (Common Equity Tier I) Change (bp)                   ||"
  625.         );

  626.         System.out.println (
  627.             "\t||         - CL (Contra Liability) Change (bp)                         ||"
  628.         );

  629.         System.out.println (
  630.             "\t||         - PFV (Porfolio Value) Change (Income) (bp)                 ||"
  631.         );

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

  635.         System.out.println ("\t|| FCA/FBA Accounting => " +
  636.             FormatUtil.FormatDouble (oasFCAFBA.contraAssetAdjustment(), 1, 4, 1.) + " | " +
  637.             FormatUtil.FormatDouble (oasFCAFBA.contraLiabilityAdjustment(), 1, 4, 1.) + " | " +
  638.             FormatUtil.FormatDouble (oapFCAFBA.fundingTransferPricing(), 3, 0, 10000.) + " | " +
  639.             FormatUtil.FormatDouble (oapFCAFBA.cet1Change(), 3, 0, 10000.) + " | " +
  640.             FormatUtil.FormatDouble (oapFCAFBA.contraLiabilityChange(), 3, 0, 10000.) + " | " +
  641.             FormatUtil.FormatDouble (oapFCAFBA.portfolioValueChange(), 3, 0, 10000.) + " || "
  642.         );

  643.         System.out.println ("\t|| FVA/FDA Accounting => " +
  644.             FormatUtil.FormatDouble (oasFVAFDA.contraAssetAdjustment(), 1, 4, 1.) + " | " +
  645.             FormatUtil.FormatDouble (oasFVAFDA.contraLiabilityAdjustment(), 1, 4, 1.) + " | " +
  646.             FormatUtil.FormatDouble (oapFVAFDA.fundingTransferPricing(), 3, 0, 10000.) + " | " +
  647.             FormatUtil.FormatDouble (oapFVAFDA.cet1Change(), 3, 0, 10000.) + " | " +
  648.             FormatUtil.FormatDouble (oapFVAFDA.contraLiabilityChange(), 3, 0, 10000.) + " | " +
  649.             FormatUtil.FormatDouble (oapFVAFDA.portfolioValueChange(), 3, 0, 10000.) + " || "
  650.         );

  651.         System.out.println (
  652.             "\t||---------------------------------------------------------------------||"
  653.         );

  654.         System.out.println();
  655.     }

  656.     public static final void main (
  657.         final String[] astrArgs)
  658.         throws Exception
  659.     {
  660.         EnvManager.InitEnv ("");

  661.         ExposureAdjustmentAggregator[] aEEA = Mix (
  662.             5.,
  663.             0.,
  664.             100.,
  665.             5.,
  666.             0.,
  667.             1.
  668.         );

  669.         ExposureAdjustmentAggregator eeaGround = aEEA[0];
  670.         ExposureAdjustmentAggregator eeaExtended = aEEA[1];

  671.         ExposureAdjustmentDigest eadGround = eeaGround.digest();

  672.         ExposureAdjustmentDigest eadExtended = eeaExtended.digest();

  673.         CPGDDump (
  674.             "\t||                                                  GROUND BOOK ADJUSTMENT METRICS                                                   ||",
  675.             eadGround
  676.         );

  677.         CPGDDump (
  678.             "\t||                                                 EXTENDED BOOK ADJUSTMENT METRICS                                                  ||",
  679.             eadExtended
  680.         );

  681.         CPGDDiffDump (
  682.             "\t||                                             TRADE INCREMENT ADJUSTMENT METRICS (bp)                                               ||",
  683.             eadGround,
  684.             eadExtended
  685.         );

  686.         BaselAccountingMetrics (
  687.             "\t||           ALBANESE & ANDERSEN (2015) BCBS OTC ACCOUNTING            ||",
  688.             eeaGround,
  689.             eeaExtended
  690.         );

  691.         EnvManager.TerminateEnv();
  692.     }
  693. }