FixFloatClassicalPlusShort.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.csatimeline.AndersenPykhtinSokolLag;
  8. import org.drip.exposure.csatimeline.LastFlowDates;
  9. import org.drip.exposure.evolver.EntityDynamicsContainer;
  10. import org.drip.exposure.evolver.LatentStateDynamicsContainer;
  11. import org.drip.exposure.evolver.LatentStateVertexContainer;
  12. import org.drip.exposure.evolver.PrimarySecurity;
  13. import org.drip.exposure.evolver.PrimarySecurityDynamicsContainer;
  14. import org.drip.exposure.evolver.TerminalLatentState;
  15. import org.drip.exposure.generator.FixFloatMPoR;
  16. import org.drip.exposure.mpor.PathVariationMarginTrajectoryEstimator;
  17. import org.drip.exposure.mpor.VariationMarginTradeVertexExposure;
  18. import org.drip.exposure.regression.LocalVolatilityGenerationControl;
  19. import org.drip.exposure.regressiontrade.AndersenPykhtinSokolEnsemble;
  20. import org.drip.exposure.regressiontrade.AndersenPykhtinSokolTrajectory;
  21. import org.drip.exposure.universe.LatentStateWeiner;
  22. import org.drip.exposure.universe.MarketPath;
  23. import org.drip.exposure.universe.MarketVertex;
  24. import org.drip.exposure.universe.MarketVertexGenerator;
  25. import org.drip.market.otc.FixedFloatSwapConvention;
  26. import org.drip.market.otc.IBORFixedFloatContainer;
  27. import org.drip.measure.crng.RandomNumberGenerator;
  28. import org.drip.measure.discrete.CorrelatedPathVertexDimension;
  29. import org.drip.measure.dynamics.DiffusionEvaluatorLinear;
  30. import org.drip.measure.dynamics.DiffusionEvaluatorLogarithmic;
  31. import org.drip.measure.dynamics.HazardJumpEvaluator;
  32. import org.drip.measure.gaussian.NormalQuadrature;
  33. import org.drip.measure.process.DiffusionEvolver;
  34. import org.drip.measure.process.JumpDiffusionEvolver;
  35. import org.drip.numerical.common.FormatUtil;
  36. import org.drip.numerical.linearalgebra.Matrix;
  37. import org.drip.product.rates.FixFloatComponent;
  38. import org.drip.service.env.EnvManager;
  39. import org.drip.state.identifier.CSALabel;
  40. import org.drip.state.identifier.EntityFundingLabel;
  41. import org.drip.state.identifier.EntityHazardLabel;
  42. import org.drip.state.identifier.EntityRecoveryLabel;
  43. import org.drip.state.identifier.ForwardLabel;
  44. import org.drip.state.identifier.LatentStateLabel;
  45. import org.drip.state.identifier.OvernightLabel;

  46. /*
  47.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  48.  */

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

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

  159. public class FixFloatClassicalPlusShort
  160. {

  161.     private static final FixFloatComponent OTCIRS (
  162.         final JulianDate spotDate,
  163.         final String currency,
  164.         final String maturityTenor,
  165.         final double coupon)
  166.     {
  167.         FixedFloatSwapConvention ffConv = IBORFixedFloatContainer.ConventionFromJurisdiction (
  168.             currency,
  169.             "ALL",
  170.             maturityTenor,
  171.             "MAIN"
  172.         );

  173.         return ffConv.createFixFloatComponent (
  174.             spotDate,
  175.             maturityTenor,
  176.             coupon,
  177.             0.,
  178.             1.
  179.         );
  180.     }

  181.     private static final PrimarySecurity OvernightReplicator (
  182.         final String currency,
  183.         final List<LatentStateLabel> latentStateLabelList)
  184.         throws Exception
  185.     {
  186.         double overnightReplicatorDrift = 0.0025;
  187.         double overnightReplicatorVolatility = 0.001;
  188.         double overnightReplicatorRepo = 0.0;

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

  190.         latentStateLabelList.add (overnightLabel);

  191.         return new PrimarySecurity (
  192.             currency + "_OVERNIGHT",
  193.             overnightLabel,
  194.             new DiffusionEvolver (
  195.                 DiffusionEvaluatorLogarithmic.Standard (
  196.                     overnightReplicatorDrift,
  197.                     overnightReplicatorVolatility
  198.                 )
  199.             ),
  200.             overnightReplicatorRepo
  201.         );
  202.     }

  203.     private static final PrimarySecurity CSAReplicator (
  204.         final String currency,
  205.         final List<LatentStateLabel> latentStateLabelList)
  206.         throws Exception
  207.     {
  208.         double csaReplicatorDrift = 0.01;
  209.         double csaReplicatorVolatility = 0.002;
  210.         double csaReplicatorRepo = 0.005;

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

  212.         latentStateLabelList.add (csaLabel);

  213.         return new PrimarySecurity (
  214.             currency + "_CSA",
  215.             csaLabel,
  216.             new DiffusionEvolver (
  217.                 DiffusionEvaluatorLogarithmic.Standard (
  218.                     csaReplicatorDrift,
  219.                     csaReplicatorVolatility
  220.                 )
  221.             ),
  222.             csaReplicatorRepo
  223.         );
  224.     }

  225.     private static final PrimarySecurity DealerSeniorFundingReplicator (
  226.         final String currency,
  227.         final String dealer,
  228.         final List<LatentStateLabel> latentStateLabelList)
  229.         throws Exception
  230.     {
  231.         double dealerSeniorFundingReplicatorDrift = 0.03;
  232.         double dealerSeniorFundingReplicatorVolatility = 0.002;
  233.         double dealerSeniorFundingReplicatorRepo = 0.028;

  234.         LatentStateLabel dealerSeniorFundingLabel = EntityFundingLabel.Senior (
  235.             dealer,
  236.             currency
  237.         );

  238.         latentStateLabelList.add (dealerSeniorFundingLabel);

  239.         return new PrimarySecurity (
  240.             dealer + "_" + currency + "_SENIOR_ZERO",
  241.             dealerSeniorFundingLabel,
  242.             new JumpDiffusionEvolver (
  243.                 DiffusionEvaluatorLogarithmic.Standard (
  244.                     dealerSeniorFundingReplicatorDrift,
  245.                     dealerSeniorFundingReplicatorVolatility
  246.                 ),
  247.                 HazardJumpEvaluator.Standard (
  248.                     0.3,
  249.                     0.45
  250.                 )
  251.             ),
  252.             dealerSeniorFundingReplicatorRepo
  253.         );
  254.     }

  255.     private static final PrimarySecurity DealerSubordinateFundingReplicator (
  256.         final String currency,
  257.         final String dealer,
  258.         final List<LatentStateLabel> latentStateLabelList)
  259.         throws Exception
  260.     {
  261.         double dealerSubordinateFundingReplicatorDrift = 0.045;
  262.         double dealerSubordinateFundingReplicatorVolatility = 0.002;
  263.         double dealerSubordinateFundingReplicatorRepo = 0.028;

  264.         LatentStateLabel dealerSubordinateFundingLabel = EntityFundingLabel.Subordinate (
  265.             dealer,
  266.             currency
  267.         );

  268.         latentStateLabelList.add (dealerSubordinateFundingLabel);

  269.         return new PrimarySecurity (
  270.             dealer + "_" + currency + "_SUBORDINATE_ZERO",
  271.             dealerSubordinateFundingLabel,
  272.             new JumpDiffusionEvolver (
  273.                 DiffusionEvaluatorLogarithmic.Standard (
  274.                     dealerSubordinateFundingReplicatorDrift,
  275.                     dealerSubordinateFundingReplicatorVolatility
  276.                 ),
  277.                 HazardJumpEvaluator.Standard (
  278.                     0.3,
  279.                     0.25
  280.                 )
  281.             ),
  282.             dealerSubordinateFundingReplicatorRepo
  283.         );
  284.     }

  285.     private static final PrimarySecurity ClientFundingReplicator (
  286.         final String currency,
  287.         final String client,
  288.         final List<LatentStateLabel> latentStateLabelList)
  289.         throws Exception
  290.     {
  291.         double clientFundingReplicatorDrift = 0.03;
  292.         double clientFundingReplicatorVolatility = 0.003;
  293.         double clientFundingReplicatorRepo = 0.028;

  294.         LatentStateLabel clientFundingLabel = EntityFundingLabel.Senior (
  295.             client,
  296.             currency
  297.         );

  298.         latentStateLabelList.add (clientFundingLabel);

  299.         return new PrimarySecurity (
  300.             client + "_" + currency + "_SENIOR_ZERO",
  301.             clientFundingLabel,
  302.             new JumpDiffusionEvolver (
  303.                 DiffusionEvaluatorLogarithmic.Standard (
  304.                     clientFundingReplicatorDrift,
  305.                     clientFundingReplicatorVolatility
  306.                 ),
  307.                 HazardJumpEvaluator.Standard (
  308.                     0.5,
  309.                     0.30
  310.                 )
  311.             ),
  312.             clientFundingReplicatorRepo
  313.         );
  314.     }

  315.     private static final TerminalLatentState DealerHazard (
  316.         final String currency,
  317.         final String dealer,
  318.         final List<LatentStateLabel> latentStateLabelList)
  319.         throws Exception
  320.     {
  321.         double dealerHazardDrift = 0.0002;
  322.         double dealerHazardVolatility = 0.02;

  323.         LatentStateLabel dealerHazardLabel = EntityHazardLabel.Standard (
  324.             dealer,
  325.             currency
  326.         );

  327.         latentStateLabelList.add (dealerHazardLabel);

  328.         return new TerminalLatentState (
  329.             dealerHazardLabel,
  330.             new DiffusionEvolver (
  331.                 DiffusionEvaluatorLogarithmic.Standard (
  332.                     dealerHazardDrift,
  333.                     dealerHazardVolatility
  334.                 )
  335.             )
  336.         );
  337.     }

  338.     private static final TerminalLatentState DealerRecovery (
  339.         final String currency,
  340.         final String dealer,
  341.         final List<LatentStateLabel> latentStateLabelList)
  342.         throws Exception
  343.     {
  344.         double dealerRecoveryDrift = 0.0002;
  345.         double dealerRecoveryVolatility = 0.02;

  346.         LatentStateLabel dealerRecoveryLabel = EntityRecoveryLabel.Senior (
  347.             dealer,
  348.             currency
  349.         );

  350.         latentStateLabelList.add (dealerRecoveryLabel);

  351.         return new TerminalLatentState (
  352.             dealerRecoveryLabel,
  353.             new DiffusionEvolver (
  354.                 DiffusionEvaluatorLogarithmic.Standard (
  355.                     dealerRecoveryDrift,
  356.                     dealerRecoveryVolatility
  357.                 )
  358.             )
  359.         );
  360.     }

  361.     private static final TerminalLatentState ClientHazard (
  362.         final String currency,
  363.         final String client,
  364.         final List<LatentStateLabel> latentStateLabelList)
  365.         throws Exception
  366.     {
  367.         double clientHazardDrift = 0.0002;
  368.         double clientHazardVolatility = 0.02;

  369.         LatentStateLabel clientHazardLabel = EntityHazardLabel.Standard (
  370.             client,
  371.             currency
  372.         );

  373.         latentStateLabelList.add (clientHazardLabel);

  374.         return new TerminalLatentState (
  375.             clientHazardLabel,
  376.             new DiffusionEvolver (
  377.                 DiffusionEvaluatorLogarithmic.Standard (
  378.                     clientHazardDrift,
  379.                     clientHazardVolatility
  380.                 )
  381.             )
  382.         );
  383.     }

  384.     private static final TerminalLatentState ClientRecovery (
  385.         final String currency,
  386.         final String client,
  387.         final List<LatentStateLabel> latentStateLabelList)
  388.         throws Exception
  389.     {
  390.         double clientRecoveryDrift = 0.0002;
  391.         double clientRecoveryVolatility = 0.02;

  392.         LatentStateLabel clientRecoveryLabel = EntityRecoveryLabel.Senior (
  393.             client,
  394.             currency
  395.         );

  396.         latentStateLabelList.add (clientRecoveryLabel);

  397.         return new TerminalLatentState (
  398.             clientRecoveryLabel,
  399.             new DiffusionEvolver (
  400.                 DiffusionEvaluatorLogarithmic.Standard (
  401.                     clientRecoveryDrift,
  402.                     clientRecoveryVolatility
  403.                 )
  404.             )
  405.         );
  406.     }

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

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

  472.     private static final LatentStateDynamicsContainer LatentStateEvolver (
  473.         final ForwardLabel forwardLabel,
  474.         final List<LatentStateLabel> latentStateLabelList)
  475.         throws Exception
  476.     {
  477.         double otcFixFloatNumeraireDrift = 0.0;
  478.         double otcFixFloatNumeraireVolatility = 0.10;

  479.         latentStateLabelList.add (forwardLabel);

  480.         LatentStateDynamicsContainer latentStateDynamicsContainer = new LatentStateDynamicsContainer();

  481.         latentStateDynamicsContainer.addForward (
  482.             new TerminalLatentState (
  483.                 forwardLabel,
  484.                 new DiffusionEvolver (
  485.                     DiffusionEvaluatorLinear.Standard (
  486.                         otcFixFloatNumeraireDrift,
  487.                         otcFixFloatNumeraireVolatility
  488.                     )
  489.                 )
  490.             )
  491.         );

  492.         return latentStateDynamicsContainer;
  493.     }

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

  507.         for (int i = 0; i < exposureSamplingNodeCount; ++i)
  508.         {
  509.             terminationDate = terminationDate.addTenor (exposureSamplingTenor);

  510.             eventVertexArray[i] = terminationDate.julian();
  511.         }

  512.         return new MarketVertexGenerator (
  513.             spotDate.julian(),
  514.             eventVertexArray,
  515.             EntityEvolver (
  516.                 currency,
  517.                 dealer,
  518.                 client,
  519.                 latentStateLabelList
  520.             ),
  521.             PrimarySecurityEvolver (
  522.                 currency,
  523.                 dealer,
  524.                 client,
  525.                 latentStateLabelList
  526.             ),
  527.             LatentStateEvolver (
  528.                 forwardLabel,
  529.                 latentStateLabelList
  530.             )
  531.         );
  532.     }

  533.     public static final void main (
  534.         final String[] args)
  535.         throws Exception
  536.     {
  537.         EnvManager.InitEnv ("");

  538.         JulianDate spotDate = DateUtil.CreateFromYMD (
  539.             2018,
  540.             DateUtil.APRIL,
  541.             19
  542.         );

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

  568.         LocalVolatilityGenerationControl localVolatilityGenerationControl =
  569.             LocalVolatilityGenerationControl.Standard (pathCount);

  570.         LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

  571.         latentStateVertexContainer.add (
  572.             ForwardLabel.Create (
  573.                 currency,
  574.                 "3M"
  575.             ),
  576.             0.02
  577.         );

  578.         MarketVertex initialMarketVertex = MarketVertex.Epochal (
  579.             spotDate,
  580.             1.000,              // dblOvernightNumeraireInitial
  581.             1.000,              // dblCSANumeraire
  582.             0.015,              // dblBankHazardRate
  583.             0.400,              // dblBankRecoveryRate
  584.             0.015 / (1 - 0.40), // dblBankFundingSpread
  585.             0.030,              // dblCounterPartyHazardRate
  586.             0.300,              // dblCounterPartyRecoveryRate
  587.             0.030 / (1 - 0.30), // dblCounterPartyFundingSpread
  588.             latentStateVertexContainer
  589.         );

  590.         ForwardLabel forwardLabel = ForwardLabel.Create (
  591.             currency,
  592.             "3M"
  593.         );

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

  595.         MarketVertexGenerator marketVertexGenerator = ConstructMarketVertexGenerator (
  596.             spotDate,
  597.             latentStateGenerationTenor,
  598.             latentStateVertexCount,
  599.             currency,
  600.             dealer,
  601.             client,
  602.             forwardLabel,
  603.             latentStateLabelList
  604.         );

  605.         FixFloatComponent fixFloatComponent = OTCIRS (
  606.             spotDate,
  607.             currency,
  608.             fixFloatMaturityTenor,
  609.             fixFloatCoupon
  610.         );

  611.         FixFloatMPoR fixFloatMPoR = new FixFloatMPoR (
  612.             fixFloatComponent,
  613.             fixFloatNotional
  614.         );

  615.         CorrelatedPathVertexDimension correlatedPathVertexDimension = new CorrelatedPathVertexDimension (
  616.             new RandomNumberGenerator(),
  617.             correlationMatrix,
  618.             latentStateVertexCount,
  619.             1,
  620.             true,
  621.             null
  622.         );

  623.         JulianDate sparseFixFloatExposureDate = spotDate;
  624.         int[] sparseFixFloatExposureDateArray = new int[sparseFixFloatExposureDateCount + 1];
  625.         MarketPath[] marketPathArray = new MarketPath[pathCount];

  626.         for (int sparseFixFloatExposureDateIndex = 0;
  627.             sparseFixFloatExposureDateIndex <= sparseFixFloatExposureDateCount;
  628.             ++sparseFixFloatExposureDateIndex)
  629.         {
  630.             sparseFixFloatExposureDateArray[sparseFixFloatExposureDateIndex] =
  631.                 sparseFixFloatExposureDate.julian();

  632.             sparseFixFloatExposureDate = sparseFixFloatExposureDate.addTenor (sparseFixFloatExposureTenor);
  633.         }

  634.         int denseExposureDateCount = sparseFixFloatExposureDateArray[sparseFixFloatExposureDateCount] -
  635.             sparseFixFloatExposureDateArray[0] + 1;
  636.         double[][] wanderEnsemble = new double[pathCount][denseExposureDateCount];

  637.         for (int pathIndex = 0; pathIndex < pathCount; ++pathIndex)
  638.         {
  639.             marketPathArray[pathIndex] = new MarketPath (
  640.                 marketVertexGenerator.marketVertex (
  641.                     initialMarketVertex,
  642.                     LatentStateWeiner.FromUnitRandom (
  643.                         latentStateLabelList,
  644.                         Matrix.Transpose (correlatedPathVertexDimension.straightPathVertexRd().flatform())
  645.                     )
  646.                 )
  647.             );

  648.             for (int denseExposureDateIndex = 0;
  649.                 denseExposureDateIndex < denseExposureDateCount;
  650.                 ++denseExposureDateIndex)
  651.             {
  652.                 wanderEnsemble[pathIndex][denseExposureDateIndex] = NormalQuadrature.Random();
  653.             }
  654.         }

  655.         AndersenPykhtinSokolEnsemble andersenPykhtinSokolEnsemble = new AndersenPykhtinSokolEnsemble (
  656.             fixFloatMPoR,
  657.             marketPathArray,
  658.             sparseFixFloatExposureDateArray
  659.         );

  660.         AndersenPykhtinSokolTrajectory[] andersenPykhtinSokolTrajectoryArray =
  661.             andersenPykhtinSokolEnsemble.denseTrajectory (
  662.                 localVolatilityGenerationControl,
  663.                 wanderEnsemble
  664.             );

  665.         AndersenPykhtinSokolLag andersenPykhtinSokolLag = AndersenPykhtinSokolLag.ClassicalPlus();

  666.         int[] exposureDateArray = new int[denseExposureDateCount];
  667.         int[] variationMarginGapEndDateArray = new int[denseExposureDateCount];
  668.         int[] variationMarginGapStartDateArray = new int[denseExposureDateCount];
  669.         double[] tradePaymentGapArray = new double[denseExposureDateCount];
  670.         double[] variationMarginGapArray = new double[denseExposureDateCount];
  671.         double[] clientTradePaymentGapArray = new double[denseExposureDateCount];
  672.         double[] collateralizedExposureArray = new double[denseExposureDateCount];
  673.         double[] variationMarginPostingArray = new double[denseExposureDateCount];
  674.         double[] variationMarginEstimateArray = new double[denseExposureDateCount];
  675.         double[] clientDealerTradePaymentGapArray = new double[denseExposureDateCount];
  676.         double[] collateralizedPositiveExposureArray = new double[denseExposureDateCount];
  677.         int[] clientTradePaymentGapEndDateArray = new int[denseExposureDateCount];
  678.         int[] clientTradePaymentGapStartDateArray = new int[denseExposureDateCount];
  679.         int[] clientDealerTradePaymentGapEndDateArray = new int[denseExposureDateCount];
  680.         int[] clientDealerTradePaymentGapStartDateArray = new int[denseExposureDateCount];

  681.         for (int i = 0; i < denseExposureDateCount; ++i)
  682.         {
  683.             tradePaymentGapArray[i] = 0.;
  684.             variationMarginGapArray[i] = 0.;
  685.             clientTradePaymentGapArray[i] = 0.;
  686.             collateralizedExposureArray[i] = 0.;
  687.             variationMarginPostingArray[i] = 0.;
  688.             variationMarginEstimateArray[i] = 0.;
  689.             clientDealerTradePaymentGapArray[i] = 0.;
  690.             collateralizedPositiveExposureArray[i] = 0.;

  691.             exposureDateArray[i] = spotDate.julian() + i;
  692.         }

  693.         for (int pathIndex = 0; pathIndex < pathCount; ++pathIndex)
  694.         {
  695.             PathVariationMarginTrajectoryEstimator marginTradeFlowTrajectory =
  696.                 new PathVariationMarginTrajectoryEstimator (
  697.                     exposureDateArray,
  698.                     currency,
  699.                     andersenPykhtinSokolTrajectoryArray[pathIndex].variationMarginEstimateTrajectory(),
  700.                     andersenPykhtinSokolTrajectoryArray[pathIndex].tradePaymentTrajectory(),
  701.                     andersenPykhtinSokolLag
  702.                 );

  703.             Map<Integer, VariationMarginTradeVertexExposure> mapMarginTradeFlowEntry =
  704.                 marginTradeFlowTrajectory.trajectory();

  705.             for (int denseExposureDateIndex = 0;
  706.                 denseExposureDateIndex < denseExposureDateCount;
  707.                 ++denseExposureDateIndex)
  708.             {
  709.                 VariationMarginTradeVertexExposure marginTradeFlowEntry = mapMarginTradeFlowEntry.get
  710.                     (exposureDateArray[denseExposureDateIndex]);

  711.                 LastFlowDates lastFlowDates = marginTradeFlowEntry.lastFlowDates();

  712.                 tradePaymentGapArray[denseExposureDateIndex] += marginTradeFlowEntry.tradePaymentGap();

  713.                 clientTradePaymentGapArray[denseExposureDateIndex] +=
  714.                     marginTradeFlowEntry.clientTradePaymentGap();

  715.                 clientDealerTradePaymentGapArray[denseExposureDateIndex] +=
  716.                     marginTradeFlowEntry.clientDealerTradePaymentGap();

  717.                 collateralizedExposureArray[denseExposureDateIndex] +=
  718.                     marginTradeFlowEntry.collateralizedExposure();

  719.                 collateralizedPositiveExposureArray[denseExposureDateIndex] +=
  720.                     marginTradeFlowEntry.collateralizedPositiveExposure();

  721.                 variationMarginEstimateArray[denseExposureDateIndex] +=
  722.                     marginTradeFlowEntry.variationMarginEstimate();

  723.                 variationMarginPostingArray[denseExposureDateIndex] +=
  724.                     marginTradeFlowEntry.variationMarginPosting();

  725.                 variationMarginGapArray[denseExposureDateIndex] += marginTradeFlowEntry.variationMarginGap();

  726.                 variationMarginGapStartDateArray[denseExposureDateIndex] =
  727.                     lastFlowDates.clientVariationMarginPosting().julian();

  728.                 variationMarginGapEndDateArray[denseExposureDateIndex] =
  729.                     lastFlowDates.dealerVariationMarginPosting().julian();

  730.                 clientTradePaymentGapStartDateArray[denseExposureDateIndex] =
  731.                     lastFlowDates.clientTradePayment().julian();

  732.                 clientTradePaymentGapEndDateArray[denseExposureDateIndex] =
  733.                     lastFlowDates.dealerTradePayment().julian();

  734.                 clientDealerTradePaymentGapStartDateArray[denseExposureDateIndex] =
  735.                     lastFlowDates.dealerTradePayment().julian();

  736.                 clientDealerTradePaymentGapEndDateArray[denseExposureDateIndex] =
  737.                     lastFlowDates.variationMarginPeriodEnd().julian();
  738.             }
  739.         }

  740.         System.out.println();

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

  742.         System.out.println ("\t|                                                                            FIXED STREAM MARGIN/TRADE FLOW EXPOSURES AND DATES                                                                               ||");

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

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

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

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

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

  748.         System.out.println ("\t|    - Variation Margin Gap Start Date                                                                                                                                                                        ||");

  749.         System.out.println ("\t|    - Variation Margin Gap End Date                                                                                                                                                                          ||");

  750.         System.out.println ("\t|    - Variation Margin Estimate                                                                                                                                                                              ||");

  751.         System.out.println ("\t|    - Variation Margin Posting                                                                                                                                                                               ||");

  752.         System.out.println ("\t|    - Variation Margin Gap                                                                                                                                                                                   ||");

  753.         System.out.println ("\t|    - Client Trade Payment Gap Start Date                                                                                                                                                                    ||");

  754.         System.out.println ("\t|    - Client Trade Payment Gap End Date                                                                                                                                                                      ||");

  755.         System.out.println ("\t|    - Client Trade Payment Gap                                                                                                                                                                               ||");

  756.         System.out.println ("\t|    - Net Trade Payment Gap Start Date                                                                                                                                                                       ||");

  757.         System.out.println ("\t|    - Net Trade Payment Gap End Date                                                                                                                                                                         ||");

  758.         System.out.println ("\t|    - Net Trade Payment Gap                                                                                                                                                                                  ||");

  759.         System.out.println ("\t|    - Trade Payment Gap                                                                                                                                                                                      ||");

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

  761.         System.out.println ("\t|    - Positive Exposure                                                                                                                                                                                      ||");

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

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

  764.         for (int denseExposureDateIndex = 0;
  765.             denseExposureDateIndex < denseExposureDateCount;
  766.             ++denseExposureDateIndex)
  767.         {
  768.             System.out.println (
  769.                 "\t| [" +
  770.                 new JulianDate (exposureDateArray[denseExposureDateIndex]) + "] => [" +
  771.                 new JulianDate (variationMarginGapStartDateArray[denseExposureDateIndex]) + " -> " +
  772.                 new JulianDate (variationMarginGapEndDateArray[denseExposureDateIndex]) + "] | " +
  773.                 FormatUtil.FormatDouble (variationMarginEstimateArray[denseExposureDateIndex] / pathCount, 5, 2, 1) + " | " +
  774.                 FormatUtil.FormatDouble (variationMarginPostingArray[denseExposureDateIndex] / pathCount, 5, 2, 1) + " | " +
  775.                 FormatUtil.FormatDouble (variationMarginGapArray[denseExposureDateIndex] / pathCount, 5, 2, 1) + " | [" +
  776.                 new JulianDate (clientTradePaymentGapStartDateArray[denseExposureDateIndex]) + " -> " +
  777.                 new JulianDate (clientTradePaymentGapEndDateArray[denseExposureDateIndex]) + "] | " +
  778.                 FormatUtil.FormatDouble (clientTradePaymentGapArray[denseExposureDateIndex] / pathCount, 5, 2, 1) + " | [" +
  779.                 new JulianDate (clientDealerTradePaymentGapStartDateArray[denseExposureDateIndex]) + " -> " +
  780.                 new JulianDate (clientDealerTradePaymentGapEndDateArray[denseExposureDateIndex]) + "] | " +
  781.                 FormatUtil.FormatDouble (clientDealerTradePaymentGapArray[denseExposureDateIndex] / pathCount, 5, 2, 1) + " | " +
  782.                 FormatUtil.FormatDouble (tradePaymentGapArray[denseExposureDateIndex] / pathCount, 5, 2, 1) + " | " +
  783.                 FormatUtil.FormatDouble (collateralizedExposureArray[denseExposureDateIndex] / pathCount, 5, 2, 1) + " | " +
  784.                 FormatUtil.FormatDouble (collateralizedPositiveExposureArray[denseExposureDateIndex] / pathCount, 5, 2, 1) + " ||"
  785.             );
  786.         }

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

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