PathTradeFlowAdjustment.java

  1. package org.drip.sample.andersen2017vm;

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

  5. import org.drip.analytics.date.DateUtil;
  6. import org.drip.analytics.date.JulianDate;
  7. import org.drip.exposure.evolver.EntityDynamicsContainer;
  8. import org.drip.exposure.evolver.LatentStateDynamicsContainer;
  9. import org.drip.exposure.evolver.LatentStateVertexContainer;
  10. import org.drip.exposure.evolver.PrimarySecurity;
  11. import org.drip.exposure.evolver.PrimarySecurityDynamicsContainer;
  12. import org.drip.exposure.evolver.TerminalLatentState;
  13. import org.drip.exposure.generator.FixFloatMPoR;
  14. import org.drip.exposure.mpor.TradePayment;
  15. import org.drip.exposure.regressiontrade.AndersenPykhtinSokolPath;
  16. import org.drip.exposure.regressiontrade.AdjustedVariationMarginEstimator;
  17. import org.drip.exposure.regressiontrade.VariationMarginEstimateVertex;
  18. import org.drip.exposure.universe.LatentStateWeiner;
  19. import org.drip.exposure.universe.MarketPath;
  20. import org.drip.exposure.universe.MarketVertex;
  21. import org.drip.exposure.universe.MarketVertexGenerator;
  22. import org.drip.market.otc.FixedFloatSwapConvention;
  23. import org.drip.market.otc.IBORFixedFloatContainer;
  24. import org.drip.measure.crng.RandomNumberGenerator;
  25. import org.drip.measure.discrete.CorrelatedPathVertexDimension;
  26. import org.drip.measure.dynamics.DiffusionEvaluatorLinear;
  27. import org.drip.measure.dynamics.DiffusionEvaluatorLogarithmic;
  28. import org.drip.measure.dynamics.HazardJumpEvaluator;
  29. import org.drip.measure.process.DiffusionEvolver;
  30. import org.drip.measure.process.JumpDiffusionEvolver;
  31. import org.drip.numerical.common.FormatUtil;
  32. import org.drip.numerical.linearalgebra.Matrix;
  33. import org.drip.product.rates.FixFloatComponent;
  34. import org.drip.service.env.EnvManager;
  35. import org.drip.state.identifier.CSALabel;
  36. import org.drip.state.identifier.EntityFundingLabel;
  37. import org.drip.state.identifier.EntityHazardLabel;
  38. import org.drip.state.identifier.EntityRecoveryLabel;
  39. import org.drip.state.identifier.ForwardLabel;
  40. import org.drip.state.identifier.LatentStateLabel;
  41. import org.drip.state.identifier.OvernightLabel;

  42. /*
  43.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  44.  */

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

  115. /**
  116.  * <i>PathTradeFlowAdjustment</i> generates the Trade Flow Adjusted Variation Margin from Sparse Nodes for a
  117.  * Fix-Float Swap. The References are:
  118.  *
  119.  * <br><br>
  120.  *  <ul>
  121.  *      <li>
  122.  *          Albanese, C., and L. Andersen (2014): Accounting for OTC Derivatives: Funding Adjustments and the
  123.  *              Re-Hypothecation Option, eSSRN, https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2482955
  124.  *              <b>eSSRN</b>
  125.  *      </li>
  126.  *      <li>
  127.  *          Andersen, L. B. G., M. Pykhtin, and A. Sokol (2017): Re-thinking Margin Period of Risk
  128.  *              https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2902737 <b>eSSRN</b>
  129.  *      </li>
  130.  *      <li>
  131.  *          Andersen, L. B. G., M. Pykhtin, and A. Sokol (2017): Credit Exposure in the Presence of Initial
  132.  *              Margin https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2806156 <b>eSSRN</b>
  133.  *      </li>
  134.  *      <li>
  135.  *          Burgard, C., and M. Kjaer (2017): Derivatives Funding, Netting, and Accounting
  136.  *              https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2534011 <b>eSSRN</b>
  137.  *      </li>
  138.  *      <li>
  139.  *          Pykhtin, M. (2009): Modeling Counter-party Credit Exposure in the Presence of Margin Agreements
  140.  *              http://www.risk-europe.com/protected/michael-pykhtin.pdf
  141.  *      </li>
  142.  *  </ul>
  143.  * <br><br>
  144.  *  <ul>
  145.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  146.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ExposureAnalyticsLibrary.md">Exposure Analytics</a></li>
  147.  *      <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>
  148.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/andersen2017vm/README.md">Andersen Pykhtin Sokol Regression VM</a></li>
  149.  *  </ul>
  150.  * <br><br>
  151.  *
  152.  * @author Lakshmi Krishnamurthy
  153.  */

  154. public class PathTradeFlowAdjustment
  155. {

  156.     private static final FixFloatComponent OTCIRS (
  157.         final JulianDate spotDate,
  158.         final String currency,
  159.         final String maturityTenor,
  160.         final double coupon)
  161.     {
  162.         FixedFloatSwapConvention ffConv = IBORFixedFloatContainer.ConventionFromJurisdiction (
  163.             currency,
  164.             "ALL",
  165.             maturityTenor,
  166.             "MAIN"
  167.         );

  168.         return ffConv.createFixFloatComponent (
  169.             spotDate,
  170.             maturityTenor,
  171.             coupon,
  172.             0.,
  173.             1.
  174.         );
  175.     }

  176.     private static final PrimarySecurity OvernightReplicator (
  177.         final String currency,
  178.         final List<LatentStateLabel> latentStateLabelList)
  179.         throws Exception
  180.     {
  181.         double overnightReplicatorDrift = 0.0025;
  182.         double overnightReplicatorVolatility = 0.001;
  183.         double overnightReplicatorRepo = 0.0;

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

  185.         latentStateLabelList.add (overnightLabel);

  186.         return new PrimarySecurity (
  187.             currency + "_OVERNIGHT",
  188.             overnightLabel,
  189.             new DiffusionEvolver (
  190.                 DiffusionEvaluatorLogarithmic.Standard (
  191.                     overnightReplicatorDrift,
  192.                     overnightReplicatorVolatility
  193.                 )
  194.             ),
  195.             overnightReplicatorRepo
  196.         );
  197.     }

  198.     private static final PrimarySecurity CSAReplicator (
  199.         final String currency,
  200.         final List<LatentStateLabel> latentStateLabelList)
  201.         throws Exception
  202.     {
  203.         double csaReplicatorDrift = 0.01;
  204.         double csaReplicatorVolatility = 0.002;
  205.         double csaReplicatorRepo = 0.005;

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

  207.         latentStateLabelList.add (csaLabel);

  208.         return new PrimarySecurity (
  209.             currency + "_CSA",
  210.             csaLabel,
  211.             new DiffusionEvolver (
  212.                 DiffusionEvaluatorLogarithmic.Standard (
  213.                     csaReplicatorDrift,
  214.                     csaReplicatorVolatility
  215.                 )
  216.             ),
  217.             csaReplicatorRepo
  218.         );
  219.     }

  220.     private static final PrimarySecurity DealerSeniorFundingReplicator (
  221.         final String currency,
  222.         final String dealer,
  223.         final List<LatentStateLabel> latentStateLabelList)
  224.         throws Exception
  225.     {
  226.         double dealerSeniorFundingReplicatorDrift = 0.03;
  227.         double dealerSeniorFundingReplicatorVolatility = 0.002;
  228.         double dealerSeniorFundingReplicatorRepo = 0.028;

  229.         LatentStateLabel dealerSeniorFundingLabel = EntityFundingLabel.Senior (
  230.             dealer,
  231.             currency
  232.         );

  233.         latentStateLabelList.add (dealerSeniorFundingLabel);

  234.         return new PrimarySecurity (
  235.             dealer + "_" + currency + "_SENIOR_ZERO",
  236.             dealerSeniorFundingLabel,
  237.             new JumpDiffusionEvolver (
  238.                 DiffusionEvaluatorLogarithmic.Standard (
  239.                     dealerSeniorFundingReplicatorDrift,
  240.                     dealerSeniorFundingReplicatorVolatility
  241.                 ),
  242.                 HazardJumpEvaluator.Standard (
  243.                     0.3,
  244.                     0.45
  245.                 )
  246.             ),
  247.             dealerSeniorFundingReplicatorRepo
  248.         );
  249.     }

  250.     private static final PrimarySecurity DealerSubordinateFundingReplicator (
  251.         final String currency,
  252.         final String dealer,
  253.         final List<LatentStateLabel> latentStateLabelList)
  254.         throws Exception
  255.     {
  256.         double dealerSubordinateFundingReplicatorDrift = 0.045;
  257.         double dealerSubordinateFundingReplicatorVolatility = 0.002;
  258.         double dealerSubordinateFundingReplicatorRepo = 0.028;

  259.         LatentStateLabel dealerSubordinateFundingLabel = EntityFundingLabel.Subordinate (
  260.             dealer,
  261.             currency
  262.         );

  263.         latentStateLabelList.add (dealerSubordinateFundingLabel);

  264.         return new PrimarySecurity (
  265.             dealer + "_" + currency + "_SUBORDINATE_ZERO",
  266.             dealerSubordinateFundingLabel,
  267.             new JumpDiffusionEvolver (
  268.                 DiffusionEvaluatorLogarithmic.Standard (
  269.                     dealerSubordinateFundingReplicatorDrift,
  270.                     dealerSubordinateFundingReplicatorVolatility
  271.                 ),
  272.                 HazardJumpEvaluator.Standard (
  273.                     0.3,
  274.                     0.25
  275.                 )
  276.             ),
  277.             dealerSubordinateFundingReplicatorRepo
  278.         );
  279.     }

  280.     private static final PrimarySecurity ClientFundingReplicator (
  281.         final String currency,
  282.         final String client,
  283.         final List<LatentStateLabel> latentStateLabelList)
  284.         throws Exception
  285.     {
  286.         double clientFundingReplicatorDrift = 0.03;
  287.         double clientFundingReplicatorVolatility = 0.003;
  288.         double clientFundingReplicatorRepo = 0.028;

  289.         LatentStateLabel clientFundingLabel = EntityFundingLabel.Senior (
  290.             client,
  291.             currency
  292.         );

  293.         latentStateLabelList.add (clientFundingLabel);

  294.         return new PrimarySecurity (
  295.             client + "_" + currency + "_SENIOR_ZERO",
  296.             clientFundingLabel,
  297.             new JumpDiffusionEvolver (
  298.                 DiffusionEvaluatorLogarithmic.Standard (
  299.                     clientFundingReplicatorDrift,
  300.                     clientFundingReplicatorVolatility
  301.                 ),
  302.                 HazardJumpEvaluator.Standard (
  303.                     0.5,
  304.                     0.30
  305.                 )
  306.             ),
  307.             clientFundingReplicatorRepo
  308.         );
  309.     }

  310.     private static final TerminalLatentState DealerHazard (
  311.         final String currency,
  312.         final String dealer,
  313.         final List<LatentStateLabel> latentStateLabelList)
  314.         throws Exception
  315.     {
  316.         double dealerHazardDrift = 0.0002;
  317.         double dealerHazardVolatility = 0.02;

  318.         LatentStateLabel dealerHazardLabel = EntityHazardLabel.Standard (
  319.             dealer,
  320.             currency
  321.         );

  322.         latentStateLabelList.add (dealerHazardLabel);

  323.         return new TerminalLatentState (
  324.             dealerHazardLabel,
  325.             new DiffusionEvolver (
  326.                 DiffusionEvaluatorLogarithmic.Standard (
  327.                     dealerHazardDrift,
  328.                     dealerHazardVolatility
  329.                 )
  330.             )
  331.         );
  332.     }

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

  341.         LatentStateLabel dealerRecoveryLabel = EntityRecoveryLabel.Senior (
  342.             dealer,
  343.             currency
  344.         );

  345.         latentStateLabelList.add (dealerRecoveryLabel);

  346.         return new TerminalLatentState (
  347.             dealerRecoveryLabel,
  348.             new DiffusionEvolver (
  349.                 DiffusionEvaluatorLogarithmic.Standard (
  350.                     dealerRecoveryDrift,
  351.                     dealerRecoveryVolatility
  352.                 )
  353.             )
  354.         );
  355.     }

  356.     private static final TerminalLatentState ClientHazard (
  357.         final String currency,
  358.         final String client,
  359.         final List<LatentStateLabel> latentStateLabelList)
  360.         throws Exception
  361.     {
  362.         double clientHazardDrift = 0.0002;
  363.         double clientHazardVolatility = 0.02;

  364.         LatentStateLabel clientHazardLabel = EntityHazardLabel.Standard (
  365.             client,
  366.             currency
  367.         );

  368.         latentStateLabelList.add (clientHazardLabel);

  369.         return new TerminalLatentState (
  370.             clientHazardLabel,
  371.             new DiffusionEvolver (
  372.                 DiffusionEvaluatorLogarithmic.Standard (
  373.                     clientHazardDrift,
  374.                     clientHazardVolatility
  375.                 )
  376.             )
  377.         );
  378.     }

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

  387.         LatentStateLabel clientRecoveryLabel = EntityRecoveryLabel.Senior (
  388.             client,
  389.             currency
  390.         );

  391.         latentStateLabelList.add (clientRecoveryLabel);

  392.         return new TerminalLatentState (
  393.             clientRecoveryLabel,
  394.             new DiffusionEvolver (
  395.                 DiffusionEvaluatorLogarithmic.Standard (
  396.                     clientRecoveryDrift,
  397.                     clientRecoveryVolatility
  398.                 )
  399.             )
  400.         );
  401.     }

  402.     private static final EntityDynamicsContainer EntityEvolver (
  403.         final String currency,
  404.         final String dealer,
  405.         final String client,
  406.         final List<LatentStateLabel> latentStateLabelList)
  407.         throws Exception
  408.     {
  409.         return new EntityDynamicsContainer (
  410.             DealerHazard (
  411.                 currency,
  412.                 dealer,
  413.                 latentStateLabelList
  414.             ),
  415.             DealerRecovery (
  416.                 currency,
  417.                 dealer,
  418.                 latentStateLabelList
  419.             ),
  420.             null,
  421.             ClientHazard (
  422.                 currency,
  423.                 client,
  424.                 latentStateLabelList
  425.             ),
  426.             ClientRecovery (
  427.                 currency,
  428.                 client,
  429.                 latentStateLabelList
  430.             )
  431.         );
  432.     }

  433.     private static final PrimarySecurityDynamicsContainer PrimarySecurityEvolver (
  434.         final String currency,
  435.         final String dealer,
  436.         final String client,
  437.         final List<LatentStateLabel> latentStateLabelList)
  438.         throws Exception
  439.     {
  440.         return new PrimarySecurityDynamicsContainer (
  441.             null,
  442.             OvernightReplicator (
  443.                 currency,
  444.                 latentStateLabelList
  445.             ),
  446.             CSAReplicator (
  447.                 currency,
  448.                 latentStateLabelList
  449.             ),
  450.             DealerSeniorFundingReplicator (
  451.                 currency,
  452.                 dealer,
  453.                 latentStateLabelList
  454.             ),
  455.             DealerSubordinateFundingReplicator (
  456.                 currency,
  457.                 dealer,
  458.                 latentStateLabelList
  459.             ),
  460.             ClientFundingReplicator (
  461.                 currency,
  462.                 client,
  463.                 latentStateLabelList
  464.             )
  465.         );
  466.     }

  467.     private static final LatentStateDynamicsContainer LatentStateEvolver (
  468.         final ForwardLabel forwardLabel,
  469.         final List<LatentStateLabel> latentStateLabelList)
  470.         throws Exception
  471.     {
  472.         double otcFixFloatNumeraireDrift = 0.0;
  473.         double otcFixFloatNumeraireVolatility = 0.10;

  474.         latentStateLabelList.add (forwardLabel);

  475.         LatentStateDynamicsContainer latentStateDynamicsContainer = new LatentStateDynamicsContainer();

  476.         latentStateDynamicsContainer.addForward (
  477.             new TerminalLatentState (
  478.                 forwardLabel,
  479.                 new DiffusionEvolver (
  480.                     DiffusionEvaluatorLinear.Standard (
  481.                         otcFixFloatNumeraireDrift,
  482.                         otcFixFloatNumeraireVolatility
  483.                     )
  484.                 )
  485.             )
  486.         );

  487.         return latentStateDynamicsContainer;
  488.     }

  489.     private static final MarketVertexGenerator ConstructMarketVertexGenerator (
  490.         final JulianDate spotDate,
  491.         final String exposureSamplingTenor,
  492.         final int exposureSamplingNodeCount,
  493.         final String currency,
  494.         final String dealer,
  495.         final String client,
  496.         final ForwardLabel forwardLabel,
  497.         final List<LatentStateLabel> latentStateLabelList)
  498.         throws Exception
  499.     {
  500.         JulianDate terminationDate = spotDate;
  501.         int[] eventVertexArray = new int[exposureSamplingNodeCount];

  502.         for (int i = 0; i < exposureSamplingNodeCount; ++i)
  503.         {
  504.             terminationDate = terminationDate.addTenor (exposureSamplingTenor);

  505.             eventVertexArray[i] = terminationDate.julian();
  506.         }

  507.         return new MarketVertexGenerator (
  508.             spotDate.julian(),
  509.             eventVertexArray,
  510.             EntityEvolver (
  511.                 currency,
  512.                 dealer,
  513.                 client,
  514.                 latentStateLabelList
  515.             ),
  516.             PrimarySecurityEvolver (
  517.                 currency,
  518.                 dealer,
  519.                 client,
  520.                 latentStateLabelList
  521.             ),
  522.             LatentStateEvolver (
  523.                 forwardLabel,
  524.                 latentStateLabelList
  525.             )
  526.         );
  527.     }

  528.     public static final void main (
  529.         final String[] args)
  530.         throws Exception
  531.     {
  532.         EnvManager.InitEnv ("");

  533.         JulianDate spotDate = DateUtil.CreateFromYMD (
  534.             2018,
  535.             DateUtil.APRIL,
  536.             19
  537.         );

  538.         String latentStateGenerationTenor = "1D";
  539.         int latentStateGenerationCount = 930;
  540.         int latentStateVertexCount = latentStateGenerationCount + 10;
  541.         String currency = "USD";
  542.         String dealer = "NOM";
  543.         String client = "SSGA";
  544.         double[][] correlationMatrix = new double[][]
  545.         {
  546.             {1.00, 0.00, 0.20, 0.15, 0.05, 0.00, 0.00, 0.00, 0.00, 0.00}, // #0  DEALER HAZARD
  547.             {0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #1  DEALER SENIOR RECOVERY
  548.             {0.20, 0.00, 1.00, 0.13, 0.25, 0.00, 0.00, 0.00, 0.00, 0.00}, // #2  CLIENT HAZARD
  549.             {0.15, 0.00, 0.13, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #3  CLIENT RECOVERY
  550.             {0.05, 0.00, 0.25, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #4  OVERNIGHT REPLICATOR
  551.             {0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00}, // #5  CSA REPLICATOR
  552.             {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
  553.             {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
  554.             {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00}, // #8  CLIENT FUNDING REPLICATOR
  555.             {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00}, // #9  FORWARD NUMERAIRE
  556.         };
  557.         String sparseFixFloatExposureTenor = "3M";
  558.         int sparseFixFloatExposureCount = 8;
  559.         String fixFloatMaturityTenor = "2Y";
  560.         double fixFloatCoupon = 0.02;
  561.         double fixFloatNotional = -1.e+06;

  562.         ForwardLabel forwardLabel = ForwardLabel.Create (
  563.             currency,
  564.             "3M"
  565.         );

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

  567.         MarketVertexGenerator marketVertexGenerator = ConstructMarketVertexGenerator (
  568.             spotDate,
  569.             latentStateGenerationTenor,
  570.             latentStateVertexCount,
  571.             currency,
  572.             dealer,
  573.             client,
  574.             forwardLabel,
  575.             latentStateLabelList
  576.         );

  577.         LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

  578.         latentStateVertexContainer.add (
  579.             ForwardLabel.Create (
  580.                 currency,
  581.                 "3M"
  582.             ),
  583.             0.02
  584.         );

  585.         MarketVertex initialMarketVertex = MarketVertex.Epochal (
  586.             spotDate,
  587.             1.000,              // dblOvernightNumeraireInitial
  588.             1.000,              // dblCSANumeraire
  589.             0.015,              // dblBankHazardRate
  590.             0.400,              // dblBankRecoveryRate
  591.             0.015 / (1 - 0.40), // dblBankFundingSpread
  592.             0.030,              // dblCounterPartyHazardRate
  593.             0.300,              // dblCounterPartyRecoveryRate
  594.             0.030 / (1 - 0.30), // dblCounterPartyFundingSpread
  595.             latentStateVertexContainer
  596.         );

  597.         FixFloatComponent fixFloatComponent = OTCIRS (
  598.             spotDate,
  599.             currency,
  600.             fixFloatMaturityTenor,
  601.             fixFloatCoupon
  602.         );

  603.         FixFloatMPoR fixFloatMPoR = new FixFloatMPoR (
  604.             fixFloatComponent,
  605.             fixFloatNotional
  606.         );

  607.         CorrelatedPathVertexDimension correlatedPathVertexDimension = new CorrelatedPathVertexDimension (
  608.             new RandomNumberGenerator(),
  609.             correlationMatrix,
  610.             latentStateVertexCount,
  611.             1,
  612.             true,
  613.             null
  614.         );

  615.         JulianDate sparseFixFloatExposureDate = spotDate;
  616.         int[] sparseFixFloatExposureDateArray = new int[sparseFixFloatExposureCount + 1];

  617.         for (int i = 0; i <= sparseFixFloatExposureCount; ++i)
  618.         {
  619.             sparseFixFloatExposureDateArray[i] = sparseFixFloatExposureDate.julian();

  620.             sparseFixFloatExposureDate = sparseFixFloatExposureDate.addTenor (sparseFixFloatExposureTenor);
  621.         }

  622.         MarketPath marketPath = new MarketPath (
  623.             marketVertexGenerator.marketVertex (
  624.                 initialMarketVertex,
  625.                 LatentStateWeiner.FromUnitRandom (
  626.                     latentStateLabelList,
  627.                     Matrix.Transpose (correlatedPathVertexDimension.straightPathVertexRd().flatform())
  628.                 )
  629.             )
  630.         );

  631.         AdjustedVariationMarginEstimator pathCoordinator = new AdjustedVariationMarginEstimator (
  632.             fixFloatMPoR,
  633.             marketPath
  634.         );

  635.         AndersenPykhtinSokolPath andersenPykhtinSokolPath = pathCoordinator.andersenPykhtinSokolPath
  636.             (sparseFixFloatExposureDateArray);

  637.         TradePayment[] tradePaymentArray = andersenPykhtinSokolPath.denseTradePaymentArray();

  638.         JulianDate denseExposureDate = spotDate;

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

  640.         System.out.println ("\t||  Non-zero Dealer/Client Flows  ||");

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

  642.         System.out.println ("\t||                                ||");

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

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

  645.         System.out.println ("\t||        - Dealer Trade Flow     ||");

  646.         System.out.println ("\t||        - Client Trade Flow     ||");

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

  648.         for (TradePayment tradePayment : tradePaymentArray)
  649.         {
  650.             if (0 != tradePayment.dealer() || 0 != tradePayment.client())
  651.             {
  652.                 System.out.println ("\t|| " +
  653.                     denseExposureDate + " => " +
  654.                     FormatUtil.FormatDouble (tradePayment.dealer(), 5, 0, 1.) + " | " +
  655.                     FormatUtil.FormatDouble (tradePayment.client(), 5, 0, 1.) + " ||"
  656.                 );
  657.             }

  658.             denseExposureDate = denseExposureDate.addDays (1);
  659.         }

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

  661.         System.out.println();

  662.         Map<Integer, VariationMarginEstimateVertex> variationMarginEstimateVertexTrajectory =
  663.             andersenPykhtinSokolPath.variationMarginEstimateTrajectory();

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

  665.         System.out.println ("\t||      Unadjusted/Adjusted VM      ||");

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

  667.         System.out.println ("\t||                                  ||");

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

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

  670.         System.out.println ("\t||        - Unadjusted VM           ||");

  671.         System.out.println ("\t||        - Adjusted VM             ||");

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

  673.         for (Map.Entry<Integer, VariationMarginEstimateVertex> variationMarginEstimateVertexEntry :
  674.             variationMarginEstimateVertexTrajectory.entrySet())
  675.         {
  676.             VariationMarginEstimateVertex variationMarginEstimateVertex =
  677.                 variationMarginEstimateVertexEntry.getValue();

  678.             System.out.println ("\t|| " +
  679.                 new JulianDate (variationMarginEstimateVertexEntry.getKey()) + " => " +
  680.                 FormatUtil.FormatDouble (variationMarginEstimateVertex.unadjusted(), 6, 0, 1.) + " | " +
  681.                 FormatUtil.FormatDouble (variationMarginEstimateVertex.adjusted(), 6, 0, 1.) + " ||"
  682.             );
  683.         }

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

  685.         EnvManager.TerminateEnv();
  686.     }
  687. }