XVAExplain.java

  1. package org.drip.sample.burgard2011;

  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Map;

  5. import org.drip.analytics.date.*;
  6. import org.drip.analytics.support.VertexDateBuilder;
  7. import org.drip.exposure.evolver.*;
  8. import org.drip.exposure.universe.*;
  9. import org.drip.measure.crng.RandomNumberGenerator;
  10. import org.drip.measure.discrete.CorrelatedPathVertexDimension;
  11. import org.drip.measure.dynamics.*;
  12. import org.drip.measure.process.*;
  13. import org.drip.measure.realization.*;
  14. import org.drip.numerical.common.FormatUtil;
  15. import org.drip.numerical.linearalgebra.Matrix;
  16. import org.drip.service.env.EnvManager;
  17. import org.drip.state.identifier.*;
  18. import org.drip.xva.definition.*;
  19. import org.drip.xva.derivative.*;
  20. import org.drip.xva.pde.*;

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

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

  95. /**
  96.  * <i>XVAExplain</i> demonstrates the Trajectory Attribution of the Bank and Counter-Party Default Based
  97.  * Derivative Evolution of the Dynamic XVA Replication Porfolio. The References are:
  98.  *  
  99.  * <br><br>
  100.  *  <ul>
  101.  *      <li>
  102.  *          Burgard, C., and M. Kjaer (2014): PDE Representations of Derivatives with Bilateral Counter-party
  103.  *              Risk and Funding Costs <i>Journal of Credit Risk</i> <b>7 (3)</b> 1-19
  104.  *      </li>
  105.  *      <li>
  106.  *          Cesari, G., J. Aquilina, N. Charpillon, X. Filipovic, G. Lee, and L. Manda (2009): <i>Modeling,
  107.  *              Pricing, and Hedging Counter-party Credit Exposure - A Technical Guide</i> <b>Springer
  108.  *              Finance</b> New York
  109.  *      </li>
  110.  *      <li>
  111.  *          Gregory, J. (2009): Being Two-faced over Counter-party Credit Risk <i>Risk</i> <b>20 (2)</b>
  112.  *              86-90
  113.  *      </li>
  114.  *      <li>
  115.  *          Li, B., and Y. Tang (2007): <i>Quantitative Analysis, Derivatives Modeling, and Trading
  116.  *              Strategies in the Presence of Counter-party Credit Risk for the Fixed Income Market</i>
  117.  *              <b>World Scientific Publishing</b> Singapore
  118.  *      </li>
  119.  *      <li>
  120.  *          Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives Pricing
  121.  *              <i>Risk</i> <b>21 (2)</b> 97-102
  122.  *      </li>
  123.  *  </ul>
  124.  *  
  125.  * <br><br>
  126.  *  <ul>
  127.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  128.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/XVAAnalyticsLibrary.md">XVA Analytics Library</a></li>
  129.  *      <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>
  130.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/burgard2011/README.md">Burgard Kjaer (2011) PDE Evolver</a></li>
  131.  *  </ul>
  132.  * <br><br>
  133.  *
  134.  * @author Lakshmi Krishnamurthy
  135.  */

  136. public class XVAExplain
  137. {

  138.     private static final PrimarySecurity AssetValueReplicator (
  139.         final String currency)
  140.         throws Exception
  141.     {
  142.         double assetValueReplicatorDrift = 0.0025;
  143.         double assetValueReplicatorVolatility = 0.10;
  144.         double assetValueReplicatorRepo = 0.03;
  145.         double assetValueReplicatorDividend = 0.02;

  146.         EntityEquityLabel equityLabel = EntityEquityLabel.Standard (
  147.             "AAPL",
  148.             currency
  149.         );

  150.         return new PrimarySecurity (
  151.             "AAPL",
  152.             equityLabel,
  153.             new DiffusionEvolver (
  154.                 DiffusionEvaluatorLogarithmic.Standard (
  155.                     assetValueReplicatorDrift - assetValueReplicatorDividend,
  156.                     assetValueReplicatorVolatility
  157.                 )
  158.             ),
  159.             assetValueReplicatorRepo
  160.         );
  161.     }

  162.     private static final PrimarySecurity OvernightReplicator (
  163.         final String currency,
  164.         final List<LatentStateLabel> latentStateLabelList)
  165.         throws Exception
  166.     {
  167.         double overnightReplicatorDrift = 0.0025;
  168.         double overnightReplicatorVolatility = 0.001;
  169.         double overnightReplicatorRepo = 0.0;

  170.         LatentStateLabel overnightLabel = OvernightLabel.Create (currency);

  171.         latentStateLabelList.add (overnightLabel);

  172.         return new PrimarySecurity (
  173.             currency + "_OVERNIGHT",
  174.             overnightLabel,
  175.             new DiffusionEvolver (
  176.                 DiffusionEvaluatorLogarithmic.Standard (
  177.                     overnightReplicatorDrift,
  178.                     overnightReplicatorVolatility
  179.                 )
  180.             ),
  181.             overnightReplicatorRepo
  182.         );
  183.     }

  184.     private static final PrimarySecurity CSAReplicator (
  185.         final String currency,
  186.         final List<LatentStateLabel> latentStateLabelList)
  187.         throws Exception
  188.     {
  189.         double csaReplicatorDrift = 0.01;
  190.         double csaReplicatorVolatility = 0.002;
  191.         double csaReplicatorRepo = 0.005;

  192.         LatentStateLabel csaLabel = CSALabel.ISDA (currency);

  193.         latentStateLabelList.add (csaLabel);

  194.         return new PrimarySecurity (
  195.             currency + "_CSA",
  196.             csaLabel,
  197.             new DiffusionEvolver (
  198.                 DiffusionEvaluatorLogarithmic.Standard (
  199.                     csaReplicatorDrift,
  200.                     csaReplicatorVolatility
  201.                 )
  202.             ),
  203.             csaReplicatorRepo
  204.         );
  205.     }

  206.     private static final PrimarySecurity DealerSeniorFundingReplicator (
  207.         final String currency,
  208.         final String dealer,
  209.         final List<LatentStateLabel> latentStateLabelList)
  210.         throws Exception
  211.     {
  212.         double dealerSeniorFundingReplicatorDrift = 0.03;
  213.         double dealerSeniorFundingReplicatorVolatility = 0.002;
  214.         double dealerSeniorFundingReplicatorRepo = 0.028;

  215.         LatentStateLabel dealerSeniorFundingLabel = EntityFundingLabel.Senior (
  216.             dealer,
  217.             currency
  218.         );

  219.         latentStateLabelList.add (dealerSeniorFundingLabel);

  220.         return new PrimarySecurity (
  221.             dealer + "_" + currency + "_SENIOR_ZERO",
  222.             dealerSeniorFundingLabel,
  223.             new JumpDiffusionEvolver (
  224.                 DiffusionEvaluatorLogarithmic.Standard (
  225.                     dealerSeniorFundingReplicatorDrift,
  226.                     dealerSeniorFundingReplicatorVolatility
  227.                 ),
  228.                 HazardJumpEvaluator.Standard (
  229.                     0.3,
  230.                     0.45
  231.                 )
  232.             ),
  233.             dealerSeniorFundingReplicatorRepo
  234.         );
  235.     }

  236.     private static final PrimarySecurity DealerSubordinateFundingReplicator (
  237.         final String currency,
  238.         final String dealer,
  239.         final List<LatentStateLabel> latentStateLabelList)
  240.         throws Exception
  241.     {
  242.         double dealerSubordinateFundingReplicatorDrift = 0.045;
  243.         double dealerSubordinateFundingReplicatorVolatility = 0.002;
  244.         double dealerSubordinateFundingReplicatorRepo = 0.028;

  245.         LatentStateLabel dealerSubordinateFundingLabel = EntityFundingLabel.Subordinate (
  246.             dealer,
  247.             currency
  248.         );

  249.         latentStateLabelList.add (dealerSubordinateFundingLabel);

  250.         return new PrimarySecurity (
  251.             dealer + "_" + currency + "_SUBORDINATE_ZERO",
  252.             dealerSubordinateFundingLabel,
  253.             new JumpDiffusionEvolver (
  254.                 DiffusionEvaluatorLogarithmic.Standard (
  255.                     dealerSubordinateFundingReplicatorDrift,
  256.                     dealerSubordinateFundingReplicatorVolatility
  257.                 ),
  258.                 HazardJumpEvaluator.Standard (
  259.                     0.3,
  260.                     0.25
  261.                 )
  262.             ),
  263.             dealerSubordinateFundingReplicatorRepo
  264.         );
  265.     }

  266.     private static final PrimarySecurity ClientFundingReplicator (
  267.         final String currency,
  268.         final String client,
  269.         final List<LatentStateLabel> latentStateLabelList)
  270.         throws Exception
  271.     {
  272.         double clientFundingReplicatorDrift = 0.03;
  273.         double clientFundingReplicatorVolatility = 0.003;
  274.         double clientFundingReplicatorRepo = 0.028;

  275.         LatentStateLabel clientFundingLabel = EntityFundingLabel.Senior (
  276.             client,
  277.             currency
  278.         );

  279.         latentStateLabelList.add (clientFundingLabel);

  280.         return new PrimarySecurity (
  281.             client + "_" + currency + "_SENIOR_ZERO",
  282.             clientFundingLabel,
  283.             new JumpDiffusionEvolver (
  284.                 DiffusionEvaluatorLogarithmic.Standard (
  285.                     clientFundingReplicatorDrift,
  286.                     clientFundingReplicatorVolatility
  287.                 ),
  288.                 HazardJumpEvaluator.Standard (
  289.                     0.5,
  290.                     0.30
  291.                 )
  292.             ),
  293.             clientFundingReplicatorRepo
  294.         );
  295.     }

  296.     private static final PrimarySecurityDynamicsContainer PrimarySecurityEvolver (
  297.         final String currency,
  298.         final String dealer,
  299.         final String client,
  300.         final List<LatentStateLabel> latentStateLabelList)
  301.         throws Exception
  302.     {
  303.         List<PrimarySecurity> assetList = new ArrayList<PrimarySecurity>();

  304.         assetList.add (AssetValueReplicator (currency));

  305.         return new PrimarySecurityDynamicsContainer (
  306.             assetList,
  307.             OvernightReplicator (
  308.                 currency,
  309.                 latentStateLabelList
  310.             ),
  311.             CSAReplicator (
  312.                 currency,
  313.                 latentStateLabelList
  314.             ),
  315.             DealerSeniorFundingReplicator (
  316.                 currency,
  317.                 dealer,
  318.                 latentStateLabelList
  319.             ),
  320.             DealerSubordinateFundingReplicator (
  321.                 currency,
  322.                 dealer,
  323.                 latentStateLabelList
  324.             ),
  325.             ClientFundingReplicator (
  326.                 currency,
  327.                 client,
  328.                 latentStateLabelList
  329.             )
  330.         );
  331.     }

  332.     private static final TerminalLatentState DealerHazard (
  333.         final String currency,
  334.         final String dealer,
  335.         final List<LatentStateLabel> latentStateLabelList)
  336.         throws Exception
  337.     {
  338.         double dealerHazardDrift = 0.0002;
  339.         double dealerHazardVolatility = 0.02;

  340.         LatentStateLabel dealerHazardLabel = EntityHazardLabel.Standard (
  341.             dealer,
  342.             currency
  343.         );

  344.         latentStateLabelList.add (dealerHazardLabel);

  345.         return new TerminalLatentState (
  346.             dealerHazardLabel,
  347.             new DiffusionEvolver (
  348.                 DiffusionEvaluatorLogarithmic.Standard (
  349.                     dealerHazardDrift,
  350.                     dealerHazardVolatility
  351.                 )
  352.             )
  353.         );
  354.     }

  355.     private static final TerminalLatentState DealerRecovery (
  356.         final String currency,
  357.         final String dealer,
  358.         final List<LatentStateLabel> latentStateLabelList)
  359.         throws Exception
  360.     {
  361.         double dealerRecoveryDrift = 0.0002;
  362.         double dealerRecoveryVolatility = 0.02;

  363.         LatentStateLabel dealerRecoveryLabel = EntityRecoveryLabel.Senior (
  364.             dealer,
  365.             currency
  366.         );

  367.         latentStateLabelList.add (dealerRecoveryLabel);

  368.         return new TerminalLatentState (
  369.             dealerRecoveryLabel,
  370.             new DiffusionEvolver (
  371.                 DiffusionEvaluatorLogarithmic.Standard (
  372.                     dealerRecoveryDrift,
  373.                     dealerRecoveryVolatility
  374.                 )
  375.             )
  376.         );
  377.     }

  378.     private static final TerminalLatentState ClientHazard (
  379.         final String currency,
  380.         final String client,
  381.         final List<LatentStateLabel> latentStateLabelList)
  382.         throws Exception
  383.     {
  384.         double clientHazardDrift = 0.0002;
  385.         double clientHazardVolatility = 0.02;

  386.         LatentStateLabel clientHazardLabel = EntityHazardLabel.Standard (
  387.             client,
  388.             currency
  389.         );

  390.         latentStateLabelList.add (clientHazardLabel);

  391.         return new TerminalLatentState (
  392.             clientHazardLabel,
  393.             new DiffusionEvolver (
  394.                 DiffusionEvaluatorLogarithmic.Standard (
  395.                     clientHazardDrift,
  396.                     clientHazardVolatility
  397.                 )
  398.             )
  399.         );
  400.     }

  401.     private static final TerminalLatentState ClientRecovery (
  402.         final String currency,
  403.         final String client,
  404.         final List<LatentStateLabel> latentStateLabelList)
  405.         throws Exception
  406.     {
  407.         double clientRecoveryDrift = 0.0002;
  408.         double clientRecoveryVolatility = 0.02;

  409.         LatentStateLabel clientRecoveryLabel = EntityRecoveryLabel.Senior (
  410.             client,
  411.             currency
  412.         );

  413.         latentStateLabelList.add (clientRecoveryLabel);

  414.         return new TerminalLatentState (
  415.             clientRecoveryLabel,
  416.             new DiffusionEvolver (
  417.                 DiffusionEvaluatorLogarithmic.Standard (
  418.                     clientRecoveryDrift,
  419.                     clientRecoveryVolatility
  420.                 )
  421.             )
  422.         );
  423.     }

  424.     private static final EntityDynamicsContainer EntityEvolver (
  425.         final String currency,
  426.         final String dealer,
  427.         final String client,
  428.         final List<LatentStateLabel> latentStateLabelList)
  429.         throws Exception
  430.     {
  431.         return new EntityDynamicsContainer (
  432.             DealerHazard (
  433.                 currency,
  434.                 dealer,
  435.                 latentStateLabelList
  436.             ),
  437.             DealerRecovery (
  438.                 currency,
  439.                 dealer,
  440.                 latentStateLabelList
  441.             ),
  442.             null,
  443.             ClientHazard (
  444.                 currency,
  445.                 client,
  446.                 latentStateLabelList
  447.             ),
  448.             ClientRecovery (
  449.                 currency,
  450.                 client,
  451.                 latentStateLabelList
  452.             )
  453.         );
  454.     }

  455.     private static final LatentStateDynamicsContainer LatentStateEvolver (
  456.         final EntityEquityLabel equityLabel,
  457.         final List<LatentStateLabel> latentStateLabelList)
  458.         throws Exception
  459.     {
  460.         double assetValueReplicatorDrift = 0.0025;
  461.         double assetValueReplicatorVolatility = 0.10;

  462.         latentStateLabelList.add (equityLabel);

  463.         LatentStateDynamicsContainer latentStateDynamicsContainer = new LatentStateDynamicsContainer();

  464.         latentStateDynamicsContainer.addEntityEquity (
  465.             new TerminalLatentState (
  466.                 equityLabel,
  467.                 new DiffusionEvolver (
  468.                     DiffusionEvaluatorLinear.Standard (
  469.                         assetValueReplicatorDrift,
  470.                         assetValueReplicatorVolatility
  471.                     )
  472.                 )
  473.             )
  474.         );

  475.         return latentStateDynamicsContainer;
  476.     }

  477.     private static final MarketVertexGenerator ConstructMarketVertexGenerator (
  478.         final JulianDate spotDate,
  479.         final int[] eventVertexArray,
  480.         final String currency,
  481.         final String dealer,
  482.         final String client,
  483.         final EntityEquityLabel equityLabel,
  484.         final List<LatentStateLabel> latentStateLabelList)
  485.         throws Exception
  486.     {
  487.         return new MarketVertexGenerator (
  488.             spotDate.julian(),
  489.             eventVertexArray,
  490.             EntityEvolver (
  491.                 currency,
  492.                 dealer,
  493.                 client,
  494.                 latentStateLabelList
  495.             ),
  496.             PrimarySecurityEvolver (
  497.                 currency,
  498.                 dealer,
  499.                 client,
  500.                 latentStateLabelList
  501.             ),
  502.             LatentStateEvolver (
  503.                 equityLabel,
  504.                 latentStateLabelList
  505.             )
  506.         );
  507.     }

  508.     private static final MarketVertex[] MarketVertexArray (
  509.         final Map<Integer, MarketVertex> marketVertexMap)
  510.         throws Exception
  511.     {
  512.         int marketVertexCount = marketVertexMap.size();

  513.         int marketVertexIndex = 0;
  514.         MarketVertex[] marketVertexArray = new MarketVertex[marketVertexCount];

  515.         for (Map.Entry<Integer, MarketVertex> marketVertexMapEntry : marketVertexMap.entrySet())
  516.         {
  517.             marketVertexArray[marketVertexIndex++] = marketVertexMapEntry.getValue();
  518.         }

  519.         return marketVertexArray;
  520.     }

  521.     private static final EvolutionTrajectoryVertex RunStep (
  522.         final TrajectoryEvolutionScheme tes,
  523.         final BurgardKjaerOperator bko,
  524.         final EvolutionTrajectoryVertex etvStart,
  525.         final MarketVertex mvStart,
  526.         final MarketVertex mvFinish,
  527.         final LatentStateLabel equityLabel)
  528.         throws Exception
  529.     {
  530.         PositionGreekVertex agvStart = etvStart.positionGreekVertex();

  531.         ReplicationPortfolioVertex rpvStart = etvStart.replicationPortfolioVertex();

  532.         double dblDerivativeXVAValueStart = agvStart.derivativeXVAValue();

  533.         double dblTimeWidth = (mvFinish.anchorDate().julian() - mvStart.anchorDate().julian()) / 365.;

  534.         double dblTimeStart = etvStart.time();

  535.         double dblTime = dblTimeStart + dblTimeWidth;

  536.         PrimarySecurityDynamicsContainer tc = tes.tradeablesContainer();

  537.         double dblCollateralSchemeNumeraire = mvStart.csaReplicator();

  538.         BurgardKjaerEdgeRun bker = bko.edgeRun (
  539.             new MarketEdge (
  540.                 mvStart,
  541.                 mvFinish
  542.             ),
  543.             etvStart,
  544.             0.
  545.         );

  546.         double dblTheta = bker.theta();

  547.         double dblAssetNumeraireBump = bker.positionValueBump();

  548.         double dblThetaAssetNumeraireUp = bker.thetaPositionValueUp();

  549.         double dblThetaAssetNumeraireDown = bker.thetaPositionValueDown();

  550.         double dblDerivativeXVAValueDeltaFinish = agvStart.derivativeXVAValueDelta() -
  551.             0.5 * (dblThetaAssetNumeraireUp - dblThetaAssetNumeraireDown) * dblTimeWidth / dblAssetNumeraireBump;

  552.         double dblDerivativeXVAValueGammaFinish = agvStart.derivativeXVAValueGamma() -
  553.             (dblThetaAssetNumeraireUp + dblThetaAssetNumeraireDown - 2. * dblTheta) * dblTimeWidth /
  554.                 (dblAssetNumeraireBump * dblAssetNumeraireBump);

  555.         double dblDerivativeXVAValueFinish = dblDerivativeXVAValueStart + dblTheta * dblTimeWidth;

  556.         CloseOut cog = new CloseOutBilateral (
  557.             mvStart.dealer().seniorRecoveryRate(),
  558.             mvStart.client().seniorRecoveryRate()
  559.         );

  560.         double dblGainOnBankDefaultFinish = -1. * (dblDerivativeXVAValueFinish - cog.dealerDefault
  561.             (dblDerivativeXVAValueFinish));

  562.         double dblGainOnCounterPartyDefaultFinish = -1. * (dblDerivativeXVAValueFinish - cog.clientDefault
  563.             (dblDerivativeXVAValueFinish));

  564.         org.drip.xva.derivative.CashAccountEdge cae = tes.rebalanceCash (
  565.                 etvStart,
  566.                 new MarketEdge (
  567.                     mvStart,
  568.                     mvFinish
  569.                 )
  570.             ).cashAccountEdge();

  571.         double dblCashAccountAccumulationFinish = cae.accumulation();

  572.         double dblAssetNumeraireFinish = mvFinish.latentStateValue (equityLabel);

  573.         double dblBankSeniorFundingNumeraireFinish = mvFinish.dealer().seniorFundingReplicator();

  574.         double dblCounterPartyFundingNumeraireFinish = mvFinish.client().seniorFundingReplicator();

  575.         ReplicationPortfolioVertex rpvFinish = ReplicationPortfolioVertex.Standard (
  576.             -1. * dblDerivativeXVAValueDeltaFinish,
  577.             dblGainOnBankDefaultFinish / dblBankSeniorFundingNumeraireFinish,
  578.             dblGainOnCounterPartyDefaultFinish / dblCounterPartyFundingNumeraireFinish,
  579.             rpvStart.cashAccount() + dblCashAccountAccumulationFinish
  580.         );

  581.         System.out.println ("\t||" +
  582.             FormatUtil.FormatDouble (dblTime, 1, 6, 1.) + " | " +
  583.             FormatUtil.FormatDouble (dblDerivativeXVAValueFinish, 1, 6, 1.) + " | " +
  584.             FormatUtil.FormatDouble (dblAssetNumeraireFinish, 1, 6, 1.) + " | " +
  585.             FormatUtil.FormatDouble (dblBankSeniorFundingNumeraireFinish, 1, 6, 1.) + " | " +
  586.             FormatUtil.FormatDouble (dblCounterPartyFundingNumeraireFinish, 1, 6, 1.) + " | " +
  587.             FormatUtil.FormatDouble (dblCollateralSchemeNumeraire, 1, 6, 1.) + " | " +
  588.             FormatUtil.FormatDouble (rpvFinish.positionHoldings(), 1, 6, 1.) + " | " +
  589.             FormatUtil.FormatDouble (rpvFinish.dealerSeniorNumeraireHoldings(), 1, 6, 1.) + " | " +
  590.             FormatUtil.FormatDouble (rpvFinish.clientNumeraireHoldings(), 1, 6, 1.) + " | " +
  591.             FormatUtil.FormatDouble (rpvFinish.cashAccount(), 1, 6, 1.) + " | " +
  592.             FormatUtil.FormatDouble (dblCashAccountAccumulationFinish, 1, 6, 1.) + " | " +
  593.             FormatUtil.FormatDouble (cae.assetAccumulation(), 1, 6, 1.) + " | " +
  594.             FormatUtil.FormatDouble (cae.dealerAccumulation(), 1, 6, 1.) + " | " +
  595.             FormatUtil.FormatDouble (cae.clientAccumulation(), 1, 6, 1.) + " ||"
  596.         );

  597.         return new EvolutionTrajectoryVertex (
  598.             dblTimeStart + dblTimeWidth,
  599.             rpvFinish,
  600.             new PositionGreekVertex (
  601.                 dblDerivativeXVAValueFinish,
  602.                 dblDerivativeXVAValueDeltaFinish,
  603.                 dblDerivativeXVAValueGammaFinish,
  604.                 agvStart.derivativeFairValue() * Math.exp (
  605.                     -1. * dblTimeWidth * tc.csa().evolver().evaluator().drift().value (
  606.                         new JumpDiffusionVertex (
  607.                             dblTime,
  608.                             dblCollateralSchemeNumeraire,
  609.                             0.,
  610.                             false
  611.                         )
  612.                     )
  613.                 )
  614.             ),
  615.             dblGainOnBankDefaultFinish,
  616.             dblGainOnCounterPartyDefaultFinish,
  617.             0.,
  618.             0.
  619.         );
  620.     }

  621.     public static void main (
  622.         final String[] astrArgs)
  623.         throws Exception
  624.     {
  625.         EnvManager.InitEnv ("");

  626.         String dealer = "WFC";
  627.         String client = "BAC";
  628.         int vertexCount = 24;
  629.         String currency = "USD";
  630.         int simulationDuration = 365;

  631.         double dealerHazardRateInitial = 0.03;
  632.         double clientHazardRateInitial = 0.05;
  633.         double dealerSeniorRecoveryRateInitial = 0.40;
  634.         double clientRecoveryRateInitial = 0.40;

  635.         double[][] latentStateCorrelationMatrix = new double[][]
  636.         {
  637.             {1.00, 0.00, 0.20, 0.15, 0.05, 0.00, 0.00, 0.00, 0.00, 0.00}, // #0  DEALER HAZARD
  638.             {0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #1  DEALER SENIOR RECOVERY
  639.             {0.20, 0.00, 1.00, 0.13, 0.25, 0.00, 0.00, 0.00, 0.00, 0.00}, // #2  CLIENT HAZARD
  640.             {0.15, 0.00, 0.13, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #3  CLIENT RECOVERY
  641.             {0.05, 0.00, 0.25, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #4  OVERNIGHT REPLICATOR
  642.             {0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00}, // #5  CSA REPLICATOR
  643.             {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00}, // #6  DEALER SENIOR FUNDING REPLICATOR
  644.             {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00}, // #7  DEALER SUBORDINATE FUNDING REPLICATOR
  645.             {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00}, // #8  CLIENT FUNDING REPLICATOR
  646.             {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00}, // #9  EQUITY REPLICATOR
  647.         };

  648.         double dblTerminalXVADerivativeValue = 1.;

  649.         double dblSensitivityShiftFactor = 0.001;

  650.         JulianDate spotDateJulian = DateUtil.Today();

  651.         int spotDate = spotDateJulian.julian();

  652.         int[] eventVertexArray = VertexDateBuilder.EqualWidth (
  653.             spotDate,
  654.             spotDate + simulationDuration,
  655.             vertexCount
  656.         );

  657.         List<LatentStateLabel> latentStateLabelList = new ArrayList<LatentStateLabel>();

  658.         EntityEquityLabel equityLabel = EntityEquityLabel.Standard (
  659.             "AAPL",
  660.             currency
  661.         );

  662.         MarketVertexGenerator marketVertexGenerator = ConstructMarketVertexGenerator (
  663.             spotDateJulian,
  664.             eventVertexArray,
  665.             currency,
  666.             dealer,
  667.             client,
  668.             equityLabel,
  669.             latentStateLabelList
  670.         );

  671.         System.out.println ("marketVertexGenerator = " + marketVertexGenerator);

  672.         LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

  673.         latentStateVertexContainer.add (
  674.             equityLabel,
  675.             1.
  676.         );

  677.         MarketVertex initialMarketVertex = MarketVertex.Epochal (
  678.             spotDateJulian,
  679.             1.000,
  680.             1.000,
  681.             dealerHazardRateInitial,
  682.             dealerSeniorRecoveryRateInitial,
  683.             dealerHazardRateInitial / (1 - dealerSeniorRecoveryRateInitial),
  684.             clientHazardRateInitial,
  685.             clientRecoveryRateInitial,
  686.             clientHazardRateInitial / (1 - clientRecoveryRateInitial),
  687.             latentStateVertexContainer
  688.         );

  689.         CorrelatedPathVertexDimension correlatedPathVertexDimension = new CorrelatedPathVertexDimension (
  690.             new RandomNumberGenerator(),
  691.             latentStateCorrelationMatrix,
  692.             vertexCount,
  693.             1,
  694.             true,
  695.             null
  696.         );

  697.         MarketVertex[] aMV = MarketVertexArray (
  698.             marketVertexGenerator.marketVertex (
  699.                 initialMarketVertex,
  700.                 LatentStateWeiner.FromUnitRandom (
  701.                     latentStateLabelList,
  702.                     Matrix.Transpose (correlatedPathVertexDimension.straightPathVertexRd().flatform())
  703.                 )
  704.             )
  705.         );

  706.         System.out.println ("aMV = " + aMV);

  707.         double dblDerivativeValue = dblTerminalXVADerivativeValue;
  708.         double dblDerivativeXVAValue = dblTerminalXVADerivativeValue;

  709.         PDEEvolutionControl pdeec = new PDEEvolutionControl (
  710.             PDEEvolutionControl.CLOSEOUT_GREGORY_LI_TANG,
  711.             dblSensitivityShiftFactor
  712.         );

  713.         CloseOutBilateral cob = new CloseOutBilateral (
  714.             dealerSeniorRecoveryRateInitial,
  715.             clientRecoveryRateInitial
  716.         );

  717.         TrajectoryEvolutionScheme tes = new TrajectoryEvolutionScheme (
  718.             marketVertexGenerator.primarySecurityDynamicsContainer(),
  719.             pdeec
  720.         );

  721.         BurgardKjaerOperator bko = new BurgardKjaerOperator (
  722.             marketVertexGenerator.primarySecurityDynamicsContainer(),
  723.             pdeec
  724.         );

  725.         PositionGreekVertex agvInitial = new PositionGreekVertex (
  726.             dblDerivativeXVAValue,
  727.             -1.,
  728.             0.,
  729.             dblDerivativeValue
  730.         );

  731.         double dblGainOnBankDefaultInitial = -1. * (dblDerivativeXVAValue - cob.dealerDefault
  732.             (dblDerivativeXVAValue));

  733.         double dblGainOnCounterPartyDefaultInitial = -1. * (dblDerivativeXVAValue - cob.clientDefault
  734.             (dblDerivativeXVAValue));

  735.         ReplicationPortfolioVertex rpvInitial = ReplicationPortfolioVertex.Standard (
  736.             1.,
  737.             dblGainOnBankDefaultInitial,
  738.             dblGainOnCounterPartyDefaultInitial,
  739.             0.
  740.         );

  741.         System.out.println();

  742.         System.out.println ("\t||-----------------------------------------------------------------------------------------------------------------------------------------------------------------------||");

  743.         System.out.println ("\t||                                            BILATERAL XVA EVOLVER - BURGARD & KJAER (2011) REPLICATION PORTFOLIO EVOLUTION                                             ||");

  744.         System.out.println ("\t||-----------------------------------------------------------------------------------------------------------------------------------------------------------------------||");

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

  746.         System.out.println ("\t||            - Time                                                                                                                                                     ||");

  747.         System.out.println ("\t||            - Derivative XVA Value                                                                                                                                     ||");

  748.         System.out.println ("\t||            - Asset Price Realization                                                                                                                                  ||");

  749.         System.out.println ("\t||            - Realization of the Zero Coupon Bank Bond Price                                                                                                           ||");

  750.         System.out.println ("\t||            - Realization of the Zero Coupon Counter Party Bond Price                                                                                                  ||");

  751.         System.out.println ("\t||            - Realization of the Zero Coupon Collateral Bond Price                                                                                                     ||");

  752.         System.out.println ("\t||            - Derivative XVA Asset Replication Units                                                                                                                   ||");

  753.         System.out.println ("\t||            - Derivative XVA Value Bank Bond Replication Units                                                                                                         ||");

  754.         System.out.println ("\t||            - Derivative XVA Value Counter Party Bond Replication Units                                                                                                ||");

  755.         System.out.println ("\t||            - Derivative XVA Value Cash Account Replication Units                                                                                                      ||");

  756.         System.out.println ("\t||            - Derivative Cash Account Accumulation Component                                                                                                           ||");

  757.         System.out.println ("\t||            - Asset Cash Account Accumulation Component                                                                                                                ||");

  758.         System.out.println ("\t||            - Bank Cash Account Accumulation Component                                                                                                                 ||");

  759.         System.out.println ("\t||            - Counter Party Cash Account Accumulation Component                                                                                                        ||");

  760.         System.out.println ("\t||-----------------------------------------------------------------------------------------------------------------------------------------------------------------------||");

  761.         System.out.println ("\t||" +
  762.             FormatUtil.FormatDouble (1., 1, 6, 1.) + " | " +
  763.             FormatUtil.FormatDouble (agvInitial.derivativeXVAValue(), 1, 6, 1.) + " | " +
  764.             FormatUtil.FormatDouble (aMV[aMV.length - 1].latentStateValue (equityLabel), 1, 6, 1.) + " | " +
  765.             FormatUtil.FormatDouble (1., 1, 6, 1.) + " | " +
  766.             FormatUtil.FormatDouble (1., 1, 6, 1.) + " | " +
  767.             FormatUtil.FormatDouble (1., 1, 6, 1.) + " | " +
  768.             FormatUtil.FormatDouble (rpvInitial.positionHoldings(), 1, 6, 1.) + " | " +
  769.             FormatUtil.FormatDouble (rpvInitial.dealerSeniorNumeraireHoldings(), 1, 6, 1.) + " | " +
  770.             FormatUtil.FormatDouble (rpvInitial.clientNumeraireHoldings(), 1, 6, 1.) + " | " +
  771.             FormatUtil.FormatDouble (rpvInitial.cashAccount(), 1, 6, 1.) + " | " +
  772.             FormatUtil.FormatDouble (0., 1, 6, 1.) + " | " +
  773.             FormatUtil.FormatDouble (0., 1, 6, 1.) + " | " +
  774.             FormatUtil.FormatDouble (0., 1, 6, 1.) + " | " +
  775.             FormatUtil.FormatDouble (0., 1, 6, 1.) + " ||"
  776.         );

  777.         EvolutionTrajectoryVertex etv = new EvolutionTrajectoryVertex (
  778.             1.,
  779.             ReplicationPortfolioVertex.Standard (
  780.                 1.,
  781.                 0.,
  782.                 0.,
  783.                 0.
  784.             ),
  785.             agvInitial,
  786.             dblGainOnBankDefaultInitial,
  787.             dblGainOnCounterPartyDefaultInitial,
  788.             0.,
  789.             0.
  790.         );

  791.         for (int i = vertexCount - 1; i >= 0; --i)
  792.             etv = RunStep (
  793.                 tes,
  794.                 bko,
  795.                 etv,
  796.                 aMV[i + 1],
  797.                 aMV[i],
  798.                 equityLabel
  799.             );

  800.         System.out.println ("\t||-----------------------------------------------------------------------------------------------------------------------------------------------------------------------||");

  801.         System.out.println();

  802.         EnvManager.TerminateEnv();
  803.     }
  804. }