XVAMarketGeneration.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.numerical.common.FormatUtil;
  14. import org.drip.numerical.linearalgebra.Matrix;
  15. import org.drip.service.env.EnvManager;
  16. import org.drip.state.identifier.*;

  17. /*
  18.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  19.  */

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

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

  132. public class XVAMarketGeneration {

  133.     private static final PrimarySecurity AssetValueReplicator (
  134.         final String currency)
  135.         throws Exception
  136.     {
  137.         double assetValueReplicatorDrift = 0.0025;
  138.         double assetValueReplicatorVolatility = 0.10;
  139.         double assetValueReplicatorRepo = 0.03;
  140.         double assetValueReplicatorDividend = 0.02;

  141.         EntityEquityLabel equityLabel = EntityEquityLabel.Standard (
  142.             "AAPL",
  143.             currency
  144.         );

  145.         return new PrimarySecurity (
  146.             "AAPL",
  147.             equityLabel,
  148.             new DiffusionEvolver (
  149.                 DiffusionEvaluatorLogarithmic.Standard (
  150.                     assetValueReplicatorDrift - assetValueReplicatorDividend,
  151.                     assetValueReplicatorVolatility
  152.                 )
  153.             ),
  154.             assetValueReplicatorRepo
  155.         );
  156.     }

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

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

  166.         latentStateLabelList.add (overnightLabel);

  167.         return new PrimarySecurity (
  168.             currency + "_OVERNIGHT",
  169.             overnightLabel,
  170.             new DiffusionEvolver (
  171.                 DiffusionEvaluatorLogarithmic.Standard (
  172.                     overnightReplicatorDrift,
  173.                     overnightReplicatorVolatility
  174.                 )
  175.             ),
  176.             overnightReplicatorRepo
  177.         );
  178.     }

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

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

  188.         latentStateLabelList.add (csaLabel);

  189.         return new PrimarySecurity (
  190.             currency + "_CSA",
  191.             csaLabel,
  192.             new DiffusionEvolver (
  193.                 DiffusionEvaluatorLogarithmic.Standard (
  194.                     csaReplicatorDrift,
  195.                     csaReplicatorVolatility
  196.                 )
  197.             ),
  198.             csaReplicatorRepo
  199.         );
  200.     }

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

  210.         LatentStateLabel dealerSeniorFundingLabel = EntityFundingLabel.Senior (
  211.             dealer,
  212.             currency
  213.         );

  214.         latentStateLabelList.add (dealerSeniorFundingLabel);

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

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

  240.         LatentStateLabel dealerSubordinateFundingLabel = EntityFundingLabel.Subordinate (
  241.             dealer,
  242.             currency
  243.         );

  244.         latentStateLabelList.add (dealerSubordinateFundingLabel);

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

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

  270.         LatentStateLabel clientFundingLabel = EntityFundingLabel.Senior (
  271.             client,
  272.             currency
  273.         );

  274.         latentStateLabelList.add (clientFundingLabel);

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

  291.     private static final PrimarySecurityDynamicsContainer PrimarySecurityEvolver (
  292.         final String currency,
  293.         final String dealer,
  294.         final String client,
  295.         final List<LatentStateLabel> latentStateLabelList)
  296.         throws Exception
  297.     {
  298.         List<PrimarySecurity> assetList = new ArrayList<PrimarySecurity>();

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

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

  327.     private static final TerminalLatentState DealerHazard (
  328.         final String currency,
  329.         final String dealer,
  330.         final List<LatentStateLabel> latentStateLabelList)
  331.         throws Exception
  332.     {
  333.         double dealerHazardDrift = 0.0002;
  334.         double dealerHazardVolatility = 0.02;

  335.         LatentStateLabel dealerHazardLabel = EntityHazardLabel.Standard (
  336.             dealer,
  337.             currency
  338.         );

  339.         latentStateLabelList.add (dealerHazardLabel);

  340.         return new TerminalLatentState (
  341.             dealerHazardLabel,
  342.             new DiffusionEvolver (
  343.                 DiffusionEvaluatorLogarithmic.Standard (
  344.                     dealerHazardDrift,
  345.                     dealerHazardVolatility
  346.                 )
  347.             )
  348.         );
  349.     }

  350.     private static final TerminalLatentState DealerRecovery (
  351.         final String currency,
  352.         final String dealer,
  353.         final List<LatentStateLabel> latentStateLabelList)
  354.         throws Exception
  355.     {
  356.         double dealerRecoveryDrift = 0.0002;
  357.         double dealerRecoveryVolatility = 0.02;

  358.         LatentStateLabel dealerRecoveryLabel = EntityRecoveryLabel.Senior (
  359.             dealer,
  360.             currency
  361.         );

  362.         latentStateLabelList.add (dealerRecoveryLabel);

  363.         return new TerminalLatentState (
  364.             dealerRecoveryLabel,
  365.             new DiffusionEvolver (
  366.                 DiffusionEvaluatorLogarithmic.Standard (
  367.                     dealerRecoveryDrift,
  368.                     dealerRecoveryVolatility
  369.                 )
  370.             )
  371.         );
  372.     }

  373.     private static final TerminalLatentState ClientHazard (
  374.         final String currency,
  375.         final String client,
  376.         final List<LatentStateLabel> latentStateLabelList)
  377.         throws Exception
  378.     {
  379.         double clientHazardDrift = 0.0002;
  380.         double clientHazardVolatility = 0.02;

  381.         LatentStateLabel clientHazardLabel = EntityHazardLabel.Standard (
  382.             client,
  383.             currency
  384.         );

  385.         latentStateLabelList.add (clientHazardLabel);

  386.         return new TerminalLatentState (
  387.             clientHazardLabel,
  388.             new DiffusionEvolver (
  389.                 DiffusionEvaluatorLogarithmic.Standard (
  390.                     clientHazardDrift,
  391.                     clientHazardVolatility
  392.                 )
  393.             )
  394.         );
  395.     }

  396.     private static final TerminalLatentState ClientRecovery (
  397.         final String currency,
  398.         final String client,
  399.         final List<LatentStateLabel> latentStateLabelList)
  400.         throws Exception
  401.     {
  402.         double clientRecoveryDrift = 0.0002;
  403.         double clientRecoveryVolatility = 0.02;

  404.         LatentStateLabel clientRecoveryLabel = EntityRecoveryLabel.Senior (
  405.             client,
  406.             currency
  407.         );

  408.         latentStateLabelList.add (clientRecoveryLabel);

  409.         return new TerminalLatentState (
  410.             clientRecoveryLabel,
  411.             new DiffusionEvolver (
  412.                 DiffusionEvaluatorLogarithmic.Standard (
  413.                     clientRecoveryDrift,
  414.                     clientRecoveryVolatility
  415.                 )
  416.             )
  417.         );
  418.     }

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

  450.     private static final LatentStateDynamicsContainer LatentStateEvolver (
  451.         final EntityEquityLabel equityLabel,
  452.         final List<LatentStateLabel> latentStateLabelList)
  453.         throws Exception
  454.     {
  455.         double assetValueReplicatorDrift = 0.0025;
  456.         double assetValueReplicatorVolatility = 0.10;

  457.         latentStateLabelList.add (equityLabel);

  458.         LatentStateDynamicsContainer latentStateDynamicsContainer = new LatentStateDynamicsContainer();

  459.         latentStateDynamicsContainer.addEntityEquity (
  460.             new TerminalLatentState (
  461.                 equityLabel,
  462.                 new DiffusionEvolver (
  463.                     DiffusionEvaluatorLinear.Standard (
  464.                         assetValueReplicatorDrift,
  465.                         assetValueReplicatorVolatility
  466.                     )
  467.                 )
  468.             )
  469.         );

  470.         return latentStateDynamicsContainer;
  471.     }

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

  503.     private static final MarketVertex[] MarketVertexArray (
  504.         final Map<Integer, MarketVertex> marketVertexMap)
  505.         throws Exception
  506.     {
  507.         int marketVertexCount = marketVertexMap.size();

  508.         int marketVertexIndex = 0;
  509.         MarketVertex[] marketVertexArray = new MarketVertex[marketVertexCount];

  510.         for (Map.Entry<Integer, MarketVertex> marketVertexMapEntry : marketVertexMap.entrySet())
  511.         {
  512.             marketVertexArray[marketVertexIndex++] = marketVertexMapEntry.getValue();
  513.         }

  514.         return marketVertexArray;
  515.     }

  516.     public static final void main (
  517.         final String[] astrArgs)
  518.         throws Exception
  519.     {
  520.         /* EnvManager.InitEnv ("");

  521.         String bank = "WFC";
  522.         int iNumVertex = 24;
  523.         String currency = "USD";
  524.         String counterParty = "BAC";
  525.         int iSimulationDuration = 365;

  526.         double[][] aadblCorrelationMatrix = new double[][] {
  527.             {1.00, 0.00, 0.20, 0.15, 0.05, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #0 ASSET NUMERAIRE
  528.             {0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #1 OVERNIGHT POLICY INDEX NUMERAIRE
  529.             {0.20, 0.00, 1.00, 0.13, 0.25, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #2 COLLATERAL SCHEME NUMERAIRE
  530.             {0.15, 0.00, 0.13, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #3 BANK HAZARD RATE
  531.             {0.05, 0.00, 0.25, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #4 BANK SENIOR FUNDING NUMERAIRE
  532.             {0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #5 BANK SENIOR RECOVERY RATE
  533.             {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00}, // #6 BANK SUBORDINATE FUNDING NUMERAIRE
  534.             {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00}, // #7 BANK SUBORDINATE RECOVERY RATE
  535.             {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00}, // #8 COUNTER PARTY HAZARD RATE
  536.             {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00}, // #9 COUNTER PARTY FUNDING NUMERAIRE
  537.             {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00}  // #10 COUNTER PARTY RECOVERY RATE
  538.         };

  539.         double dblAssetNumeraireDrift = 0.06;
  540.         double dblAssetNumeraireVolatility = 0.010;
  541.         double dblAssetNumeraireRepo = 0.03;
  542.         double dblAssetNumeraireDividend = 0.02;
  543.         double dblAssetNumeraireInitial = 1.;

  544.         double dblOvernightIndexNumeraireDrift = 0.0025;
  545.         double dblOvernightIndexNumeraireVolatility = 0.001;
  546.         double dblOvernightIndexNumeraireRepo = 0.0;

  547.         double dblCollateralSchemeNumeraireDrift = 0.01;
  548.         double dblCollateralSchemeNumeraireVolatility = 0.002;
  549.         double dblCollateralSchemeNumeraireRepo = 0.005;

  550.         double dblBankSeniorFundingNumeraireDrift = 0.03;
  551.         double dblBankSeniorFundingNumeraireVolatility = 0.002;
  552.         double dblBankSeniorFundingNumeraireRepo = 0.028;

  553.         double dblBankSubordinateFundingNumeraireDrift = 0.045;
  554.         double dblBankSubordinateFundingNumeraireVolatility = 0.002;
  555.         double dblBankSubordinateFundingNumeraireRepo = 0.028;

  556.         double dblCounterPartyFundingNumeraireDrift = 0.03;
  557.         double dblCounterPartyFundingNumeraireVolatility = 0.003;
  558.         double dblCounterPartyFundingNumeraireRepo = 0.028;

  559.         double dblBankHazardRateDrift = 0.00;
  560.         double dblBankHazardRateVolatility = 0.005;
  561.         double dblBankHazardRateInitial = 0.03;

  562.         double dblBankSeniorRecoveryRateDrift = 0.0;
  563.         double dblBankSeniorRecoveryRateVolatility = 0.0;
  564.         double dblBankSeniorRecoveryRateInitial = 0.45;

  565.         double dblBankSubordinateRecoveryRateDrift = 0.0;
  566.         double dblBankSubordinateRecoveryRateVolatility = 0.0;
  567.         double dblBankSubordinateRecoveryRateInitial = 0.25;

  568.         double dblCounterPartyHazardRateDrift = 0.00;
  569.         double dblCounterPartyHazardRateVolatility = 0.005;
  570.         double dblCounterPartyHazardRateInitial = 0.05;

  571.         double dblCounterPartyRecoveryRateDrift = 0.0;
  572.         double dblCounterPartyRecoveryRateVolatility = 0.0;
  573.         double dblCounterPartyRecoveryRateInitial = 0.30;

  574.         EntityEquityLabel equityLabel = EntityEquityLabel.Standard (
  575.             "AAPL",
  576.             currency
  577.         );

  578.         PrimarySecurity tAsset = new PrimarySecurity (
  579.             "AAPL",
  580.             equityLabel,
  581.             new DiffusionEvolver (
  582.                 DiffusionEvaluatorLogarithmic.Standard (
  583.                     dblAssetNumeraireDrift - dblAssetNumeraireDividend,
  584.                     dblAssetNumeraireVolatility
  585.                 )
  586.             ),
  587.             dblAssetNumeraireRepo
  588.         );

  589.         PrimarySecurity tOvernightIndex = new PrimarySecurity (
  590.             currency + "_OVERNIGHT_ZERO",
  591.             OvernightLabel.Create (currency),
  592.             new DiffusionEvolver (
  593.                 DiffusionEvaluatorLogarithmic.Standard (
  594.                     dblOvernightIndexNumeraireDrift,
  595.                     dblOvernightIndexNumeraireVolatility
  596.                 )
  597.             ),
  598.             dblOvernightIndexNumeraireRepo
  599.         );

  600.         PrimarySecurity tCollateralScheme = new PrimarySecurity (
  601.             currency + "_CSA_ZERO",
  602.             CSALabel.ISDA (currency),
  603.             new DiffusionEvolver (
  604.                 DiffusionEvaluatorLogarithmic.Standard (
  605.                     dblCollateralSchemeNumeraireDrift,
  606.                     dblCollateralSchemeNumeraireVolatility
  607.                 )
  608.             ),
  609.             dblCollateralSchemeNumeraireRepo
  610.         );

  611.         PrimarySecurity tBankSeniorFunding = new PrimarySecurity (
  612.             bank + "_" + currency + "_SENIOR_ZERO",
  613.             EntityFundingLabel.Senior (
  614.                 bank,
  615.                 currency
  616.             ),
  617.             new JumpDiffusionEvolver (
  618.                 DiffusionEvaluatorLogarithmic.Standard (
  619.                     dblBankSeniorFundingNumeraireDrift,
  620.                     dblBankSeniorFundingNumeraireVolatility
  621.                 ),
  622.                 HazardJumpEvaluator.Standard (
  623.                     dblBankHazardRateInitial,
  624.                     dblBankSeniorRecoveryRateInitial
  625.                 )
  626.             ),
  627.             dblBankSeniorFundingNumeraireRepo
  628.         );

  629.         PrimarySecurity tBankSubordinateFunding = new PrimarySecurity (
  630.             bank + "_" + currency + "_SUBORDINATE_ZERO",
  631.             EntityFundingLabel.Subordinate (
  632.                 bank,
  633.                 currency
  634.             ),
  635.             new JumpDiffusionEvolver (
  636.                 DiffusionEvaluatorLogarithmic.Standard (
  637.                     dblBankSubordinateFundingNumeraireDrift,
  638.                     dblBankSubordinateFundingNumeraireVolatility
  639.                 ),
  640.                 HazardJumpEvaluator.Standard (
  641.                     dblBankHazardRateInitial,
  642.                     dblBankSubordinateRecoveryRateInitial
  643.                 )
  644.             ),
  645.             dblBankSubordinateFundingNumeraireRepo
  646.         );

  647.         PrimarySecurity tCounterPartyFunding = new PrimarySecurity (
  648.             counterParty + "_" + currency + "_SENIOR_ZERO",
  649.             EntityFundingLabel.Senior (
  650.                 counterParty,
  651.                 currency
  652.             ),
  653.             new JumpDiffusionEvolver (
  654.                 DiffusionEvaluatorLogarithmic.Standard (
  655.                     dblCounterPartyFundingNumeraireDrift,
  656.                     dblCounterPartyFundingNumeraireVolatility
  657.                 ),
  658.                 HazardJumpEvaluator.Standard (
  659.                     dblCounterPartyHazardRateInitial,
  660.                     dblCounterPartyRecoveryRateInitial
  661.                 )
  662.             ),
  663.             dblCounterPartyFundingNumeraireRepo
  664.         );

  665.         JulianDate dtSpot = DateUtil.Today();

  666.         int iSpotDate = dtSpot.julian();

  667.         int aiVertexDate[] = VertexDateBuilder.EqualWidth (
  668.             iSpotDate,
  669.             iSpotDate + iSimulationDuration,
  670.             iNumVertex
  671.         );

  672.         List<PrimarySecurity> assetList = new ArrayList<PrimarySecurity>();

  673.         assetList.add (tAsset);

  674.         MarketVertexGeneratorDeprecatione mvg = new MarketVertexGeneratorDeprecatione (
  675.             iSpotDate,
  676.             aiVertexDate,
  677.             new PrimarySecurityDynamicsContainer (
  678.                 assetList,
  679.                 tOvernightIndex,
  680.                 tCollateralScheme,
  681.                 tBankSeniorFunding,
  682.                 tBankSubordinateFunding,
  683.                 tCounterPartyFunding
  684.             ),
  685.             new EntityDynamicsContainer (
  686.                 new TerminalLatentState (
  687.                     EntityHazardLabel.Standard (
  688.                         bank,
  689.                         currency
  690.                     ),
  691.                     new DiffusionEvolver (
  692.                         DiffusionEvaluatorLogarithmic.Standard (
  693.                             dblBankHazardRateDrift,
  694.                             dblBankHazardRateVolatility
  695.                         )
  696.                     )
  697.                 ),
  698.                 new TerminalLatentState (
  699.                     EntityRecoveryLabel.Senior (
  700.                         bank,
  701.                         currency
  702.                     ),
  703.                     new DiffusionEvolver (
  704.                         DiffusionEvaluatorLogarithmic.Standard (
  705.                             dblBankSeniorRecoveryRateDrift,
  706.                             dblBankSeniorRecoveryRateVolatility
  707.                         )
  708.                     )
  709.                 ),
  710.                 new TerminalLatentState (
  711.                     EntityRecoveryLabel.Subordinate (
  712.                         bank,
  713.                         currency
  714.                     ),
  715.                     new DiffusionEvolver (
  716.                         DiffusionEvaluatorLogarithmic.Standard (
  717.                             dblBankSubordinateRecoveryRateDrift,
  718.                             dblBankSubordinateRecoveryRateVolatility
  719.                         )
  720.                     )
  721.                 ),
  722.                 new TerminalLatentState (
  723.                     EntityHazardLabel.Standard (
  724.                         counterParty,
  725.                         currency
  726.                     ),
  727.                     new DiffusionEvolver (
  728.                         DiffusionEvaluatorLogarithmic.Standard (
  729.                             dblCounterPartyHazardRateDrift,
  730.                             dblCounterPartyHazardRateVolatility
  731.                         )
  732.                     )
  733.                 ),
  734.                 new TerminalLatentState (
  735.                     EntityRecoveryLabel.Senior (
  736.                         counterParty,
  737.                         currency
  738.                     ),
  739.                     new DiffusionEvolver (
  740.                         DiffusionEvaluatorLogarithmic.Standard (
  741.                             dblCounterPartyRecoveryRateDrift,
  742.                             dblCounterPartyRecoveryRateVolatility
  743.                         )
  744.                     )
  745.                 )
  746.             )
  747.         );

  748.         LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

  749.         latentStateVertexContainer.add (
  750.             equityLabel,
  751.             dblAssetNumeraireInitial
  752.         );

  753.         MarketVertex mvInitial = MarketVertex.Nodal (
  754.             dtSpot,
  755.             dblOvernightIndexNumeraireDrift,
  756.             1.,
  757.             dblCollateralSchemeNumeraireDrift,
  758.             1.,
  759.             new MarketVertexEntity (
  760.                 1.,
  761.                 dblBankHazardRateInitial,
  762.                 dblBankSeniorRecoveryRateInitial,
  763.                 dblBankSeniorFundingNumeraireDrift,
  764.                 1.,
  765.                 dblBankSubordinateRecoveryRateInitial,
  766.                 dblBankSubordinateFundingNumeraireDrift,
  767.                 1.
  768.             ),
  769.             new MarketVertexEntity (
  770.                 1.,
  771.                 dblCounterPartyHazardRateInitial,
  772.                 dblCounterPartyRecoveryRateInitial,
  773.                 dblCounterPartyFundingNumeraireDrift,
  774.                 1.,
  775.                 Double.NaN,
  776.                 Double.NaN,
  777.                 Double.NaN
  778.             ),
  779.             latentStateVertexContainer
  780.         );

  781.         MarketVertex[] aMV = MarketVertexArray (
  782.             mvg.marketVertex (
  783.                 mvInitial,
  784.                 Matrix.Transpose (
  785.                     SequenceGenerator.GaussianJoint (
  786.                         iNumVertex,
  787.                         aadblCorrelationMatrix
  788.                     )
  789.                 )
  790.             )
  791.         ); */

  792.         EnvManager.InitEnv ("");

  793.         String dealer = "WFC";
  794.         String client = "BAC";
  795.         int vertexCount = 24;
  796.         String currency = "USD";
  797.         int simulationDuration = 365;

  798.         double dealerHazardRateInitial = 0.03;
  799.         double clientHazardRateInitial = 0.05;
  800.         double dealerSeniorRecoveryRateInitial = 0.40;
  801.         double clientRecoveryRateInitial = 0.40;

  802.         double[][] latentStateCorrelationMatrix = new double[][]
  803.         {
  804.             {1.00, 0.00, 0.20, 0.15, 0.05, 0.00, 0.00, 0.00, 0.00, 0.00}, // #0  DEALER HAZARD
  805.             {0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #1  DEALER SENIOR RECOVERY
  806.             {0.20, 0.00, 1.00, 0.13, 0.25, 0.00, 0.00, 0.00, 0.00, 0.00}, // #2  CLIENT HAZARD
  807.             {0.15, 0.00, 0.13, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #3  CLIENT RECOVERY
  808.             {0.05, 0.00, 0.25, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #4  OVERNIGHT REPLICATOR
  809.             {0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00}, // #5  CSA REPLICATOR
  810.             {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
  811.             {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
  812.             {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00}, // #8  CLIENT FUNDING REPLICATOR
  813.             {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00}, // #9  EQUITY REPLICATOR
  814.         };

  815.         JulianDate spotDateJulian = DateUtil.Today();

  816.         int spotDate = spotDateJulian.julian();

  817.         int[] eventVertexArray = VertexDateBuilder.EqualWidth (
  818.             spotDate,
  819.             spotDate + simulationDuration,
  820.             vertexCount
  821.         );

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

  823.         EntityEquityLabel equityLabel = EntityEquityLabel.Standard (
  824.             "AAPL",
  825.             currency
  826.         );

  827.         MarketVertexGenerator marketVertexGenerator = ConstructMarketVertexGenerator (
  828.             spotDateJulian,
  829.             eventVertexArray,
  830.             currency,
  831.             dealer,
  832.             client,
  833.             equityLabel,
  834.             latentStateLabelList
  835.         );

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

  837.         LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

  838.         latentStateVertexContainer.add (
  839.             equityLabel,
  840.             1.
  841.         );

  842.         MarketVertex initialMarketVertex = MarketVertex.Epochal (
  843.             spotDateJulian,
  844.             1.000,
  845.             1.000,
  846.             dealerHazardRateInitial,
  847.             dealerSeniorRecoveryRateInitial,
  848.             dealerHazardRateInitial / (1 - dealerSeniorRecoveryRateInitial),
  849.             clientHazardRateInitial,
  850.             clientRecoveryRateInitial,
  851.             clientHazardRateInitial / (1 - clientRecoveryRateInitial),
  852.             latentStateVertexContainer
  853.         );

  854.         CorrelatedPathVertexDimension correlatedPathVertexDimension = new CorrelatedPathVertexDimension (
  855.             new RandomNumberGenerator(),
  856.             latentStateCorrelationMatrix,
  857.             vertexCount,
  858.             1,
  859.             true,
  860.             null
  861.         );

  862.         MarketVertex[] aMV = MarketVertexArray (
  863.             marketVertexGenerator.marketVertex (
  864.                 initialMarketVertex,
  865.                 LatentStateWeiner.FromUnitRandom (
  866.                     latentStateLabelList,
  867.                     Matrix.Transpose (correlatedPathVertexDimension.straightPathVertexRd().flatform())
  868.                 )
  869.             )
  870.         );

  871.         System.out.println();

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

  873.         System.out.println ("\t||          ASSET, OVERNIGHT INDEX, AND COLLATERAL NUMERAIRE          ||");

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

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

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

  877.         System.out.println ("\t||            - Overnight Index Rate                                  ||");

  878.         System.out.println ("\t||            - Overnight Index Numeraire                             ||");

  879.         System.out.println ("\t||            - Collateral Scheme Rate                                ||");

  880.         System.out.println ("\t||            - Collateral Scheme Numeraire                           ||");

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

  882.         for (int i = 0; i < aMV.length; ++i)
  883.             System.out.println (
  884.                 "\t|| " + aMV[i].anchorDate() + " => " +
  885.                 FormatUtil.FormatDouble (aMV[i].latentStateValue (equityLabel), 1, 6, 1.) + " | " +
  886.                 FormatUtil.FormatDouble (aMV[i].overnightRate(), 1, 2, 100.) + "% | " +
  887.                 FormatUtil.FormatDouble (aMV[i].overnightReplicator(), 1, 6, 1.) + " | " +
  888.                 FormatUtil.FormatDouble (aMV[i].csaRate(), 1, 2, 100.) + "% | " +
  889.                 FormatUtil.FormatDouble (aMV[i].csaReplicator(), 1, 6, 1.) + " ||"
  890.             );

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

  892.         System.out.println();

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

  894.         System.out.println ("\t||             BANK REALIZATION VERTEX => SURVIVAL, SENIOR/SUBORDINATE NODE METRICS             ||");

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

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

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

  898.         System.out.println ("\t||            - Hazard Rate                                                                     ||");

  899.         System.out.println ("\t||            - Survival Probability                                                            ||");

  900.         System.out.println ("\t||            - Senior Recovery Rate                                                            ||");

  901.         System.out.println ("\t||            - Senior Funding Spread                                                           ||");

  902.         System.out.println ("\t||            - Senior Funding Numeraire                                                        ||");

  903.         System.out.println ("\t||            - Subordinate Recovery Rate                                                       ||");

  904.         System.out.println ("\t||            - Subordinate Funding Spread                                                      ||");

  905.         System.out.println ("\t||            - Subordinate Funding Numeraire                                                   ||");

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

  907.         for (int i = 0; i < aMV.length; ++i) {
  908.             MarketVertexEntity emvBank = aMV[i].dealer();

  909.             System.out.println (
  910.                 "\t|| " + aMV[i].anchorDate() + " => " +
  911.                 FormatUtil.FormatDouble (emvBank.hazardRate(), 1, 6, 1.) + " | " +
  912.                 FormatUtil.FormatDouble (emvBank.survivalProbability(), 1, 6, 1.) + " | " +
  913.                 FormatUtil.FormatDouble (emvBank.seniorRecoveryRate(), 1, 0, 100.) + "% | " +
  914.                 FormatUtil.FormatDouble (emvBank.seniorFundingSpread(), 1, 2, 100.) + "% | " +
  915.                 FormatUtil.FormatDouble (emvBank.seniorFundingReplicator(), 1, 6, 1.) + " | " +
  916.                 FormatUtil.FormatDouble (emvBank.subordinateRecoveryRate(), 1, 0, 100.) + "% | " +
  917.                 FormatUtil.FormatDouble (emvBank.subordinateFundingSpread(), 1, 2, 100.) + "% | " +
  918.                 FormatUtil.FormatDouble (emvBank.subordinateFundingReplicator(), 1, 6, 1.) + " ||"
  919.             );
  920.         }

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

  922.         System.out.println();

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

  924.         System.out.println ("\t||         COUNTER PARTY REALIZATION VERTEX => SURVIVAL, SENIOR/SUBORDINATE NODE METRICS        ||");

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

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

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

  928.         System.out.println ("\t||            - Hazard Rate                                                                     ||");

  929.         System.out.println ("\t||            - Survival Probability                                                            ||");

  930.         System.out.println ("\t||            - Senior Recovery Rate                                                            ||");

  931.         System.out.println ("\t||            - Senior Funding Spread                                                           ||");

  932.         System.out.println ("\t||            - Senior Funding Numeraire                                                        ||");

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

  934.         for (int i = 0; i < aMV.length; ++i) {
  935.             MarketVertexEntity emvCounterParty = aMV[i].client();

  936.             System.out.println (
  937.                 "\t|| " + aMV[i].anchorDate() + " => " +
  938.                 FormatUtil.FormatDouble (emvCounterParty.hazardRate(), 1, 6, 1.) + " | " +
  939.                 FormatUtil.FormatDouble (emvCounterParty.survivalProbability(), 1, 6, 1.) + " | " +
  940.                 FormatUtil.FormatDouble (emvCounterParty.seniorRecoveryRate(), 1, 0, 100.) + "% | " +
  941.                 FormatUtil.FormatDouble (emvCounterParty.seniorFundingSpread(), 1, 2, 100.) + "% | " +
  942.                 FormatUtil.FormatDouble (emvCounterParty.seniorFundingReplicator(), 1, 6, 1.) + " ||"
  943.             );
  944.         }

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

  946.         System.out.println();

  947.         EnvManager.TerminateEnv();
  948.     }
  949. }