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

  138. public class CorrelatedNumeraireXVAReplicationPortfolio {

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

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

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

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

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

  172.         latentStateLabelList.add (overnightLabel);

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

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

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

  194.         latentStateLabelList.add (csaLabel);

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

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

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

  220.         latentStateLabelList.add (dealerSeniorFundingLabel);

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

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

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

  250.         latentStateLabelList.add (dealerSubordinateFundingLabel);

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

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

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

  280.         latentStateLabelList.add (clientFundingLabel);

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

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

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

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

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

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

  345.         latentStateLabelList.add (dealerHazardLabel);

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

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

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

  368.         latentStateLabelList.add (dealerRecoveryLabel);

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

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

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

  391.         latentStateLabelList.add (clientHazardLabel);

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

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

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

  414.         latentStateLabelList.add (clientRecoveryLabel);

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

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

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

  463.         latentStateLabelList.add (equityLabel);

  464.         LatentStateDynamicsContainer latentStateDynamicsContainer = new LatentStateDynamicsContainer();

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

  476.         return latentStateDynamicsContainer;
  477.     }

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

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

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

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

  520.         return marketVertexArray;
  521.     }

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

  532.         ReplicationPortfolioVertex rpvStart = etvStart.replicationPortfolioVertex();

  533.         double dblDerivativeXVAValueStart = agvStart.derivativeXVAValue();

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

  535.         double dblTimeStart = etvStart.time();

  536.         double dblTime = dblTimeStart + dblTimeWidth;

  537.         PrimarySecurityDynamicsContainer tc = tes.tradeablesContainer();

  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 dblBankSeniorFundingNumeraireFinish = mvFinish.dealer().seniorFundingReplicator();

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

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

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

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

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

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

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

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

  647.         double dblTerminalXVADerivativeValue = 1.;

  648.         double dblSensitivityShiftFactor = 0.001;

  649.         JulianDate spotDateJulian = DateUtil.Today();

  650.         int spotDate = spotDateJulian.julian();

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

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

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

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

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

  671.         LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

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

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

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

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

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

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

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

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

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

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

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

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

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

  734.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  754.         EvolutionTrajectoryVertex etv = new EvolutionTrajectoryVertex (
  755.             1.,
  756.             ReplicationPortfolioVertex.Standard (
  757.                 1.,
  758.                 0.,
  759.                 0.,
  760.                 0.
  761.             ),
  762.             agvInitial,
  763.             dblGainOnBankDefaultInitial,
  764.             dblGainOnCounterPartyDefaultInitial,
  765.             0.,
  766.             0.
  767.         );

  768.         for (int i = vertexCount - 1; i >= 0; --i)
  769.             etv = RunStep (
  770.                 tes,
  771.                 bko,
  772.                 etv,
  773.                 aMV[i + 1],
  774.                 aMV[i],
  775.                 equityLabel
  776.             );

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

  778.         System.out.println();

  779.         EnvManager.TerminateEnv();
  780.     }
  781. }