SetOffCollateralizedFunding.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>SetOffCollateralizedFunding</i> examines the Basel BCBS 2012 OTC Accounting Impact to a Portfolio of 10
  100.  * Swaps resulting from the Addition of a New Swap - Comparison via both FVA/FDA and FCA/FBA Schemes.
  101.  * Simulation is carried out under the following Criteria using one of the Generalized Burgard Kjaer (2013)
  102.  * 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 (Static Market Evolution)
  117.  *      </li>
  118.  *      <li>
  119.  *          Funding Strategy         - Set Off
  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 SetOffCollateralizedFunding {

  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_SET_OFF_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.SetOff (
  366.                         adtVertex[j],
  367.                         aadblPortfolio1Value[i][j],
  368.                         0.,
  369.                         dblCollateralBalance1,
  370.                         new MarketEdge (
  371.                             aMV[j - 1],
  372.                             aMV[j]
  373.                         )
  374.                     );

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

  392.                     aCGV2[j] = BurgardKjaerBuilder.Initial (
  393.                         adtVertex[j],
  394.                         aadblPortfolio2Value[i][0],
  395.                         aMV[j],
  396.                         cog
  397.                     );
  398.                 }

  399.                 dtStart = dtEnd;
  400.                 dblValueStart1 = dblValueEnd1;
  401.                 dblValueStart2 = dblValueEnd2;
  402.             }

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

  404.             CollateralGroupPath[] aCGP1 = new CollateralGroupPath[] {
  405.                 new CollateralGroupPath (
  406.                     aCGV1,
  407.                     mp
  408.                 )
  409.             };

  410.             CollateralGroupPath[] aCGP2 = new CollateralGroupPath[] {
  411.                 new CollateralGroupPath (
  412.                     aCGV2,
  413.                     mp
  414.                 )
  415.             };

  416.             aMPEAGround[i] = new MonoPathExposureAdjustment (
  417.                 new AlbaneseAndersenFundingGroupPath[] {
  418.                     new AlbaneseAndersenFundingGroupPath (
  419.                         new AlbaneseAndersenNettingGroupPath[] {
  420.                             new AlbaneseAndersenNettingGroupPath (
  421.                                 aCGP1,
  422.                                 mp
  423.                             )
  424.                         },
  425.                         mp
  426.                     )
  427.                 }
  428.             );

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

  447.         return new ExposureAdjustmentAggregator[] {
  448.             new ExposureAdjustmentAggregator (aMPEAGround),
  449.             new ExposureAdjustmentAggregator (aMPEAExtended)
  450.         };
  451.     }

  452.     private static final void CPGDDump (
  453.         final String strHeader,
  454.         final ExposureAdjustmentDigest ead)
  455.         throws Exception
  456.     {
  457.         System.out.println();

  458.         UnivariateDiscreteThin udtUCOLVA = ead.ucolva();

  459.         UnivariateDiscreteThin udtFTDCOLVA = ead.ftdcolva();

  460.         UnivariateDiscreteThin udtUCVA = ead.ucva();

  461.         UnivariateDiscreteThin udtFTDCVA = ead.ftdcva();

  462.         UnivariateDiscreteThin udtCVACL = ead.cvacl();

  463.         UnivariateDiscreteThin udtCVA = ead.cva();

  464.         UnivariateDiscreteThin udtDVA = ead.dva();

  465.         UnivariateDiscreteThin udtFVA = ead.fva();

  466.         UnivariateDiscreteThin udtFDA = ead.fda();

  467.         UnivariateDiscreteThin udtFCA = ead.fca();

  468.         UnivariateDiscreteThin udtFBA = ead.fba();

  469.         UnivariateDiscreteThin udtSFVA = ead.sfva();

  470.         System.out.println (
  471.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  472.         );

  473.         System.out.println (strHeader);

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

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

  480.         System.out.println (
  481.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  482.         );

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

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

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

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

  543.         System.out.println (
  544.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  545.         );
  546.     }

  547.     private static final void CPGDDiffDump (
  548.         final String strHeader,
  549.         final ExposureAdjustmentDigest eadGround,
  550.         final ExposureAdjustmentDigest eadExpanded)
  551.         throws Exception
  552.     {
  553.         System.out.println();

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

  557.         System.out.println (strHeader);

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

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

  564.         System.out.println (
  565.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  566.         );

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

  582.         System.out.println (
  583.             "\t||-----------------------------------------------------------------------------------------------------------------------------------||"
  584.         );
  585.     }

  586.     private static final void BaselAccountingMetrics (
  587.         final String strHeader,
  588.         final ExposureAdjustmentAggregator eadGround,
  589.         final ExposureAdjustmentAggregator eadExpanded)
  590.         throws Exception
  591.     {
  592.         OTCAccountingModus oasFCAFBA = new OTCAccountingModusFCAFBA (eadGround);

  593.         OTCAccountingModus oasFVAFDA = new OTCAccountingModusFVAFDA (eadGround);

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

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

  596.         System.out.println();

  597.         System.out.println (
  598.             "\t||---------------------------------------------------------------------||"
  599.         );

  600.         System.out.println (strHeader);

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

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

  607.         System.out.println (
  608.             "\t||         - Accounting Type (FCA/FBA vs. FVA/FDA)                     ||"
  609.         );

  610.         System.out.println (
  611.             "\t||         - Contra Asset Adjustment                                   ||"
  612.         );

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

  616.         System.out.println (
  617.             "\t||         - FTP (Funding Transfer Pricing) (bp)                       ||"
  618.         );

  619.         System.out.println (
  620.             "\t||         - CET1 (Common Equity Tier I) Change (bp)                   ||"
  621.         );

  622.         System.out.println (
  623.             "\t||         - CL (Contra Liability) Change (bp)                         ||"
  624.         );

  625.         System.out.println (
  626.             "\t||         - PFV (Porfolio Value) Change (Income) (bp)                 ||"
  627.         );

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

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

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

  647.         System.out.println (
  648.             "\t||---------------------------------------------------------------------||"
  649.         );

  650.         System.out.println();
  651.     }

  652.     public static final void main (
  653.         final String[] astrArgs)
  654.         throws Exception
  655.     {
  656.         EnvManager.InitEnv ("");

  657.         ExposureAdjustmentAggregator[] aEEA = Mix (
  658.             5.,
  659.             0.,
  660.             100.,
  661.             5.,
  662.             0.,
  663.             1.
  664.         );

  665.         ExposureAdjustmentAggregator eeaGround = aEEA[0];
  666.         ExposureAdjustmentAggregator eeaExtended = aEEA[1];

  667.         ExposureAdjustmentDigest eadGround = eeaGround.digest();

  668.         ExposureAdjustmentDigest eadExtended = eeaExtended.digest();

  669.         CPGDDump (
  670.             "\t||                                                  GROUND BOOK ADJUSTMENT METRICS                                                   ||",
  671.             eadGround
  672.         );

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

  677.         CPGDDiffDump (
  678.             "\t||                                             TRADE INCREMENT ADJUSTMENT METRICS (bp)                                               ||",
  679.             eadGround,
  680.             eadExtended
  681.         );

  682.         BaselAccountingMetrics (
  683.             "\t||           ALBANESE & ANDERSEN (2015) BCBS OTC ACCOUNTING            ||",
  684.             eeaGround,
  685.             eeaExtended
  686.         );

  687.         EnvManager.TerminateEnv();
  688.     }
  689. }