EnsembleTradeFlowAdjustment.java

  1. package org.drip.sample.andersen2017vm;

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

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

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

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

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

  152. public class EnsembleTradeFlowAdjustment
  153. {

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

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

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

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

  183.         latentStateLabelList.add (overnightLabel);

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

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

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

  205.         latentStateLabelList.add (csaLabel);

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

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

  227.         LatentStateLabel dealerSeniorFundingLabel = EntityFundingLabel.Senior (
  228.             dealer,
  229.             currency
  230.         );

  231.         latentStateLabelList.add (dealerSeniorFundingLabel);

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

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

  257.         LatentStateLabel dealerSubordinateFundingLabel = EntityFundingLabel.Subordinate (
  258.             dealer,
  259.             currency
  260.         );

  261.         latentStateLabelList.add (dealerSubordinateFundingLabel);

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

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

  287.         LatentStateLabel clientFundingLabel = EntityFundingLabel.Senior (
  288.             client,
  289.             currency
  290.         );

  291.         latentStateLabelList.add (clientFundingLabel);

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

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

  316.         LatentStateLabel dealerHazardLabel = EntityHazardLabel.Standard (
  317.             dealer,
  318.             currency
  319.         );

  320.         latentStateLabelList.add (dealerHazardLabel);

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

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

  339.         LatentStateLabel dealerRecoveryLabel = EntityRecoveryLabel.Senior (
  340.             dealer,
  341.             currency
  342.         );

  343.         latentStateLabelList.add (dealerRecoveryLabel);

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

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

  362.         LatentStateLabel clientHazardLabel = EntityHazardLabel.Standard (
  363.             client,
  364.             currency
  365.         );

  366.         latentStateLabelList.add (clientHazardLabel);

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

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

  385.         LatentStateLabel clientRecoveryLabel = EntityRecoveryLabel.Senior (
  386.             client,
  387.             currency
  388.         );

  389.         latentStateLabelList.add (clientRecoveryLabel);

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

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

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

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

  472.         latentStateLabelList.add (forwardLabel);

  473.         LatentStateDynamicsContainer latentStateDynamicsContainer = new LatentStateDynamicsContainer();

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

  485.         return latentStateDynamicsContainer;
  486.     }

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

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

  503.             eventVertexArray[i] = terminationDate.julian();
  504.         }

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

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

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

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

  561.         LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

  562.         latentStateVertexContainer.add (
  563.             ForwardLabel.Create (
  564.                 currency,
  565.                 "3M"
  566.             ),
  567.             0.02
  568.         );

  569.         MarketVertex initialMarketVertex = MarketVertex.Epochal (
  570.             spotDate,
  571.             1.000,              // dblOvernightNumeraireInitial
  572.             1.000,              // dblCSANumeraire
  573.             0.015,              // dblBankHazardRate
  574.             0.400,              // dblBankRecoveryRate
  575.             0.015 / (1 - 0.40), // dblBankFundingSpread
  576.             0.030,              // dblCounterPartyHazardRate
  577.             0.300,              // dblCounterPartyRecoveryRate
  578.             0.030 / (1 - 0.30), // dblCounterPartyFundingSpread
  579.             latentStateVertexContainer
  580.         );

  581.         ForwardLabel forwardLabel = ForwardLabel.Create (
  582.             currency,
  583.             "3M"
  584.         );

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

  586.         MarketVertexGenerator marketVertexGenerator = ConstructMarketVertexGenerator (
  587.             spotDate,
  588.             latentStateGenerationTenor,
  589.             latentStateVertexCount,
  590.             currency,
  591.             dealer,
  592.             client,
  593.             forwardLabel,
  594.             latentStateLabelList
  595.         );

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

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

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

  614.         JulianDate sparseFixFloatExposureDate = spotDate;
  615.         int[] sparseFixFloatExposureDateArray = new int[sparseFixFloatExposureDateCount + 1];
  616.         MarketPath[] marketPathArray = new MarketPath[pathCount];

  617.         for (int sparseFixFloatExposureDateIndex = 0;
  618.             sparseFixFloatExposureDateIndex <= sparseFixFloatExposureDateCount;
  619.             ++sparseFixFloatExposureDateIndex)
  620.         {
  621.             sparseFixFloatExposureDateArray[sparseFixFloatExposureDateIndex] =
  622.                 sparseFixFloatExposureDate.julian();

  623.             sparseFixFloatExposureDate = sparseFixFloatExposureDate.addTenor (sparseFixFloatExposureTenor);
  624.         }

  625.         for (int pathIndex = 0; pathIndex < pathCount; ++pathIndex)
  626.         {
  627.             marketPathArray[pathIndex] = new MarketPath (
  628.                 marketVertexGenerator.marketVertex (
  629.                     initialMarketVertex,
  630.                     LatentStateWeiner.FromUnitRandom (
  631.                         latentStateLabelList,
  632.                         Matrix.Transpose (correlatedPathVertexDimension.straightPathVertexRd().flatform())
  633.                     )
  634.                 )
  635.             );
  636.         }

  637.         AndersenPykhtinSokolEnsemble andersenPykhtinSokolEnsemble = new AndersenPykhtinSokolEnsemble (
  638.             fixFloatMPoR,
  639.             marketPathArray,
  640.             sparseFixFloatExposureDateArray
  641.         );

  642.         PykhtinPillarDynamics[] pillarDynamicsArray = andersenPykhtinSokolEnsemble.ensemblePillarDynamics();

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

  644.         System.out.println ("\t||        ENSEMBLE TRADE FLOW ADJUSTED VARIATION MARGIN        ||");

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

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

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

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

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

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

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

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

  653.         for (int sparseFixFloatExposureDateIndex = 0;
  654.             sparseFixFloatExposureDateIndex <= sparseFixFloatExposureDateCount;
  655.             ++sparseFixFloatExposureDateIndex)
  656.         {
  657.             UnivariateDiscreteThin univariateDiscreteThin = UnivariateDiscreteThin.FromList
  658.                 (pillarDynamicsArray[sparseFixFloatExposureDateIndex].exposureList());

  659.             System.out.println (
  660.                 "\t|| " +
  661.                 new JulianDate (sparseFixFloatExposureDateArray[sparseFixFloatExposureDateIndex]) + " => " +
  662.                 FormatUtil.FormatDouble (univariateDiscreteThin.average(), 5, 1, 1.) + " | " +
  663.                 FormatUtil.FormatDouble (univariateDiscreteThin.minimum(), 6, 1, 1.) + " | " +
  664.                 FormatUtil.FormatDouble (univariateDiscreteThin.maximum(), 6, 1, 1.) + " | " +
  665.                 FormatUtil.FormatDouble (univariateDiscreteThin.error(), 6, 1, 1.) + " ||"
  666.             );
  667.         }

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

  669.         EnvManager.TerminateEnv();
  670.     }
  671. }