XVAGreeks.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>XVAGreeks</i> demonstrates the Bank and Counter-Party Default Based Derivative Evolution of the XVA
  97.  * Greeks and their Components. 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 XVAGreeks {

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

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

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

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

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

  170.         latentStateLabelList.add (overnightLabel);

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

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

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

  192.         latentStateLabelList.add (csaLabel);

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

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

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

  218.         latentStateLabelList.add (dealerSeniorFundingLabel);

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

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

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

  248.         latentStateLabelList.add (dealerSubordinateFundingLabel);

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

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

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

  278.         latentStateLabelList.add (clientFundingLabel);

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

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

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

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

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

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

  343.         latentStateLabelList.add (dealerHazardLabel);

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

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

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

  366.         latentStateLabelList.add (dealerRecoveryLabel);

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

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

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

  389.         latentStateLabelList.add (clientHazardLabel);

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

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

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

  412.         latentStateLabelList.add (clientRecoveryLabel);

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

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

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

  461.         latentStateLabelList.add (equityLabel);

  462.         LatentStateDynamicsContainer latentStateDynamicsContainer = new LatentStateDynamicsContainer();

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

  474.         return latentStateDynamicsContainer;
  475.     }

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

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

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

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

  518.         return marketVertexArray;
  519.     }

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

  529.         ReplicationPortfolioVertex rpvStart = etvStart.replicationPortfolioVertex();

  530.         double dblDerivativeXVAValueStart = agvStart.derivativeXVAValue();

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

  532.         double dblTimeStart = etvStart.time();

  533.         double dblTime = dblTimeStart + dblTimeWidth;

  534.         PrimarySecurityDynamicsContainer tc = tes.tradeablesContainer();

  535.         double dblCollateralSchemeNumeraire = mvStart.csaReplicator();

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

  544.         double dblTheta = bker.theta();

  545.         double dblAssetNumeraireBump = bker.positionValueBump();

  546.         double dblThetaAssetNumeraireUp = bker.thetaPositionValueUp();

  547.         double dblThetaAssetNumeraireDown = bker.thetaPositionValueDown();

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

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

  553.         double dblDerivativeXVAValueFinish = dblDerivativeXVAValueStart + dblTheta * dblTimeWidth;

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

  558.         double dblGainOnBankDefaultFinish = -1. * (dblDerivativeXVAValueFinish - cog.dealerDefault
  559.             (dblDerivativeXVAValueFinish));

  560.         double dblGainOnCounterPartyDefaultFinish = -1. * (dblDerivativeXVAValueFinish -
  561.             cog.clientDefault (dblDerivativeXVAValueFinish));

  562.         System.out.println ("\t||" +
  563.             FormatUtil.FormatDouble (dblTime, 1, 6, 1.) + " | " +
  564.             FormatUtil.FormatDouble (dblDerivativeXVAValueFinish, 1, 6, 1.) + " | " +
  565.             FormatUtil.FormatDouble (dblDerivativeXVAValueDeltaFinish, 1, 6, 1.) + " | " +
  566.             FormatUtil.FormatDouble (dblDerivativeXVAValueGammaFinish, 1, 6, 1.) + " | " +
  567.             FormatUtil.FormatDouble (dblGainOnBankDefaultFinish, 1, 6, 1.) + " | " +
  568.             FormatUtil.FormatDouble (dblGainOnCounterPartyDefaultFinish, 1, 6, 1.) + " | " +
  569.             FormatUtil.FormatDouble (bker.derivativeXVAStochasticGrowth(), 1, 6, 1.) + " | " +
  570.             FormatUtil.FormatDouble (bker.derivativeXVACollateralGrowth(), 1, 6, 1.) + " | " +
  571.             FormatUtil.FormatDouble (bker.derivativeXVAFundingGrowth(), 1, 6, 1.) + " | " +
  572.             FormatUtil.FormatDouble (bker.derivativeXVADealerDefaultGrowth(), 1, 6, 1.) + " | " +
  573.             FormatUtil.FormatDouble (bker.derivativeXVAClientDefaultGrowth(), 1, 6, 1.) + " | " +
  574.             FormatUtil.FormatDouble (dblThetaAssetNumeraireDown, 1, 6, 1.) + " | " +
  575.             FormatUtil.FormatDouble (dblTheta, 1, 6, 1.) + " | " +
  576.             FormatUtil.FormatDouble (dblThetaAssetNumeraireUp, 1, 6, 1.) + " ||"
  577.         );

  578.         org.drip.xva.derivative.CashAccountEdge cae = tes.rebalanceCash (
  579.             etvStart,
  580.             new MarketEdge (
  581.                 mvStart,
  582.                 mvFinish
  583.             )
  584.         ).cashAccountEdge();

  585.         return new EvolutionTrajectoryVertex (
  586.             dblTimeStart + dblTimeWidth,
  587.             ReplicationPortfolioVertex.Standard (
  588.                 -1. * dblDerivativeXVAValueDeltaFinish,
  589.                 dblGainOnBankDefaultFinish / mvFinish.dealer().seniorFundingReplicator(),
  590.                 dblGainOnCounterPartyDefaultFinish / mvFinish.client().seniorFundingReplicator(),
  591.                 rpvStart.cashAccount() + cae.accumulation()
  592.             ),
  593.             new PositionGreekVertex (
  594.                 dblDerivativeXVAValueFinish,
  595.                 dblDerivativeXVAValueDeltaFinish,
  596.                 dblDerivativeXVAValueGammaFinish,
  597.                 agvStart.derivativeFairValue() * Math.exp (
  598.                     -1. * dblTimeWidth * tc.csa().evolver().evaluator().drift().value (
  599.                         new JumpDiffusionVertex (
  600.                             dblTime,
  601.                             dblCollateralSchemeNumeraire,
  602.                             0.,
  603.                             false
  604.                         )
  605.                     )
  606.                 )
  607.             ),
  608.             dblGainOnBankDefaultFinish,
  609.             dblGainOnCounterPartyDefaultFinish,
  610.             0.,
  611.             0.
  612.         );
  613.     }

  614.     public static void main (
  615.         final String[] astrArgs)
  616.         throws Exception
  617.     {
  618.         EnvManager.InitEnv ("");

  619.         String dealer = "WFC";
  620.         String client = "BAC";
  621.         int vertexCount = 24;
  622.         String currency = "USD";
  623.         int simulationDuration = 365;

  624.         double dealerHazardRateInitial = 0.03;
  625.         double clientHazardRateInitial = 0.05;
  626.         double dealerSeniorRecoveryRateInitial = 0.40;
  627.         double clientRecoveryRateInitial = 0.40;

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

  641.         double dblTerminalXVADerivativeValue = 1.;

  642.         double dblSensitivityShiftFactor = 0.001;

  643.         JulianDate spotDateJulian = DateUtil.Today();

  644.         int spotDate = spotDateJulian.julian();

  645.         int[] eventVertexArray = VertexDateBuilder.EqualWidth (
  646.             spotDate,
  647.             spotDate + simulationDuration,
  648.             vertexCount
  649.         );

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

  651.         EntityEquityLabel equityLabel = EntityEquityLabel.Standard (
  652.             "AAPL",
  653.             currency
  654.         );

  655.         MarketVertexGenerator marketVertexGenerator = ConstructMarketVertexGenerator (
  656.             spotDateJulian,
  657.             eventVertexArray,
  658.             currency,
  659.             dealer,
  660.             client,
  661.             equityLabel,
  662.             latentStateLabelList
  663.         );

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

  665.         LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

  666.         latentStateVertexContainer.add (
  667.             equityLabel,
  668.             1.
  669.         );

  670.         MarketVertex initialMarketVertex = MarketVertex.Epochal (
  671.             spotDateJulian,
  672.             1.000,
  673.             1.000,
  674.             dealerHazardRateInitial,
  675.             dealerSeniorRecoveryRateInitial,
  676.             dealerHazardRateInitial / (1 - dealerSeniorRecoveryRateInitial),
  677.             clientHazardRateInitial,
  678.             clientRecoveryRateInitial,
  679.             clientHazardRateInitial / (1 - clientRecoveryRateInitial),
  680.             latentStateVertexContainer
  681.         );

  682.         CorrelatedPathVertexDimension correlatedPathVertexDimension = new CorrelatedPathVertexDimension (
  683.             new RandomNumberGenerator(),
  684.             latentStateCorrelationMatrix,
  685.             vertexCount,
  686.             1,
  687.             true,
  688.             null
  689.         );

  690.         MarketVertex[] aMV = MarketVertexArray (
  691.             marketVertexGenerator.marketVertex (
  692.                 initialMarketVertex,
  693.                 LatentStateWeiner.FromUnitRandom (
  694.                     latentStateLabelList,
  695.                     Matrix.Transpose (correlatedPathVertexDimension.straightPathVertexRd().flatform())
  696.                 )
  697.             )
  698.         );

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

  700.         double dblDerivativeValue = dblTerminalXVADerivativeValue;
  701.         double dblDerivativeXVAValue = dblTerminalXVADerivativeValue;

  702.         PDEEvolutionControl pdeec = new PDEEvolutionControl (
  703.             PDEEvolutionControl.CLOSEOUT_GREGORY_LI_TANG,
  704.             dblSensitivityShiftFactor
  705.         );

  706.         CloseOutBilateral cob = new CloseOutBilateral (
  707.             dealerSeniorRecoveryRateInitial,
  708.             clientRecoveryRateInitial
  709.         );

  710.         TrajectoryEvolutionScheme tes = new TrajectoryEvolutionScheme (
  711.             marketVertexGenerator.primarySecurityDynamicsContainer(),
  712.             pdeec
  713.         );

  714.         BurgardKjaerOperator bko = new BurgardKjaerOperator (
  715.             marketVertexGenerator.primarySecurityDynamicsContainer(),
  716.             pdeec
  717.         );

  718.         PositionGreekVertex agvInitial = new PositionGreekVertex (
  719.             dblDerivativeXVAValue,
  720.             -1.,
  721.             0.,
  722.             dblDerivativeValue
  723.         );

  724.         double dblGainOnBankDefaultInitial = -1. * (dblDerivativeXVAValue - cob.dealerDefault
  725.             (dblDerivativeXVAValue));

  726.         double dblGainOnCounterPartyDefaultInitial = -1. * (dblDerivativeXVAValue - cob.clientDefault
  727.             (dblDerivativeXVAValue));

  728.         System.out.println();

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

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

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

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

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

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

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

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

  737.         System.out.println ("\t||            - Gain at Bank Default                                                                                                                                     ||");

  738.         System.out.println ("\t||            - Gain at Counter Party Default                                                                                                                            ||");

  739.         System.out.println ("\t||            - Derivative XVA Asset Growth Theta                                                                                                                        ||");

  740.         System.out.println ("\t||            - Derivative XVA Collateral Numeraire Growth Theta                                                                                                         ||");

  741.         System.out.println ("\t||            - Derivative XVA Bank Funding Growth Theta                                                                                                                 ||");

  742.         System.out.println ("\t||            - Derivative XVA Bank Default Growth Theta                                                                                                                 ||");

  743.         System.out.println ("\t||            - Derivative XVA Counter Party Default Growth Theta                                                                                                        ||");

  744.         System.out.println ("\t||            - Derivative XVA Theta Based on Asset Numeraire Down                                                                                                       ||");

  745.         System.out.println ("\t||            - Derivative XVA Theta                                                                                                                                     ||");

  746.         System.out.println ("\t||            - Derivative XVA Theta Based on Asset Numeraire Up                                                                                                         ||");

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

  748.         System.out.println ("\t||" +
  749.             FormatUtil.FormatDouble (1., 1, 6, 1.) + " | " +
  750.             FormatUtil.FormatDouble (agvInitial.derivativeXVAValue(), 1, 6, 1.) + " | " +
  751.             FormatUtil.FormatDouble (agvInitial.derivativeXVAValueDelta(), 1, 6, 1.) + " | " +
  752.             FormatUtil.FormatDouble (agvInitial.derivativeXVAValueGamma(), 1, 6, 1.) + " | " +
  753.             FormatUtil.FormatDouble (dblGainOnBankDefaultInitial, 1, 6, 1.) + " | " +
  754.             FormatUtil.FormatDouble (dblGainOnCounterPartyDefaultInitial, 1, 6, 1.) + " | " +
  755.             FormatUtil.FormatDouble (0., 1, 6, 1.) + " | " +
  756.             FormatUtil.FormatDouble (0., 1, 6, 1.) + " | " +
  757.             FormatUtil.FormatDouble (0., 1, 6, 1.) + " | " +
  758.             FormatUtil.FormatDouble (0., 1, 6, 1.) + " | " +
  759.             FormatUtil.FormatDouble (0., 1, 6, 1.) + " | " +
  760.             FormatUtil.FormatDouble (0., 1, 6, 1.) + " | " +
  761.             FormatUtil.FormatDouble (0., 1, 6, 1.) + " | " +
  762.             FormatUtil.FormatDouble (0., 1, 6, 1.) + " ||"
  763.         );

  764.         EvolutionTrajectoryVertex etv = new EvolutionTrajectoryVertex (
  765.             1.,
  766.             ReplicationPortfolioVertex.Standard (
  767.                 1.,
  768.                 0.,
  769.                 0.,
  770.                 0.
  771.             ),
  772.             agvInitial,
  773.             dblGainOnBankDefaultInitial,
  774.             dblGainOnCounterPartyDefaultInitial,
  775.             0.,
  776.             0.
  777.         );

  778.         for (int i = vertexCount - 1; i >= 0; --i)
  779.             etv = RunStep (
  780.                 tes,
  781.                 bko,
  782.                 etv,
  783.                 aMV[i + 1],
  784.                 aMV[i]
  785.             );

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

  787.         System.out.println();

  788.         EnvManager.TerminateEnv();
  789.     }
  790. }