OTCPayerClassicalPlusTimeline.java

  1. package org.drip.sample.mporfixfloat;

  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.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) 2019 Lakshmi Krishnamurthy
  47.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  48.  *
  49.  *  This file is part of DROP, an open-source library targeting risk, transaction costs, exposure, margin
  50.  *      calculations, valuation adjustment, and portfolio construction within and across fixed income,
  51.  *      credit, commodity, equity, FX, and structured products.
  52.  *  
  53.  *      https://lakshmidrip.github.io/DROP/
  54.  *  
  55.  *  DROP is composed of three modules:
  56.  *  
  57.  *  - DROP Analytics Core - https://lakshmidrip.github.io/DROP-Analytics-Core/
  58.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  59.  *  - DROP Numerical Core - https://lakshmidrip.github.io/DROP-Numerical-Core/
  60.  *
  61.  *  DROP Analytics Core implements libraries for the following:
  62.  *  - Fixed Income Analytics
  63.  *  - Asset Backed Analytics
  64.  *  - XVA Analytics
  65.  *  - Exposure and Margin Analytics
  66.  *
  67.  *  DROP Portfolio Core implements libraries for the following:
  68.  *  - Asset Allocation Analytics
  69.  *  - Transaction Cost Analytics
  70.  *
  71.  *  DROP Numerical Core implements libraries for the following:
  72.  *  - Statistical Learning
  73.  *  - Numerical Optimizer
  74.  *  - Spline Builder
  75.  *  - Algorithm Support
  76.  *
  77.  *  Documentation for DROP is Spread Over:
  78.  *
  79.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  80.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  81.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  82.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  83.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  84.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  85.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  86.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  87.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  88.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  89.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  90.  *
  91.  *  Licensed under the Apache License, Version 2.0 (the "License");
  92.  *      you may not use this file except in compliance with the License.
  93.  *  
  94.  *  You may obtain a copy of the License at
  95.  *      http://www.apache.org/licenses/LICENSE-2.0
  96.  *  
  97.  *  Unless required by applicable law or agreed to in writing, software
  98.  *      distributed under the License is distributed on an "AS IS" BASIS,
  99.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  100.  *  
  101.  *  See the License for the specific language governing permissions and
  102.  *      limitations under the License.
  103.  */

  104. /**
  105.  * <i>OTCPayerClassicalPlusTimeline</i> displays the MPoR-related Exposure Metrics Suite for the given OTC
  106.  * Payer Swap on a Daily Grid using the "Classical+" CSA Timeline of Andersen, Pykhtin, and Sokol (2017). The
  107.  * References are:
  108.  *  
  109.  * <br><br>
  110.  *  <ul>
  111.  *      <li>
  112.  *          Andersen, L. B. G., M. Pykhtin, and A. Sokol (2017a): Re-thinking Margin Period of Risk
  113.  *              https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2902737 <b>eSSRN</b>
  114.  *      </li>
  115.  *      <li>
  116.  *          Albanese, C., and L. Andersen (2014): Accounting for OTC Derivatives: Funding Adjustments and the
  117.  *              Re-Hypothecation Option https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2482955
  118.  *              <b>eSSRN</b>
  119.  *      </li>
  120.  *      <li>
  121.  *          Anfuso, F., D. Aziz, P. Giltinan, and K Loukopoulus (2017): A Sound Modeling and Back-testing
  122.  *              Framework for Forecasting Initial Margin Requirements
  123.  *              https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2716279 <b>eSSRN</b>
  124.  *      </li>
  125.  *      <li>
  126.  *          Burgard, C., and M. Kjaer (2017): Derivatives Funding, Netting, and Accounting
  127.  *              https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2534011 <b>eSSRN</b>
  128.  *      </li>
  129.  *      <li>
  130.  *          Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives Pricing
  131.  *              <i>Risk</i> <b>21 (2)</b> 97-102
  132.  *      </li>
  133.  *  </ul>
  134.  *  
  135.  * <br><br>
  136.  *  <ul>
  137.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  138.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ExposureAnalyticsLibrary.md">Exposure Analytics Library</a></li>
  139.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  140.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/mporfixfloat/README.md">OTC Fix-Float MPoR Estimate</a></li>
  141.  *  </ul>
  142.  * <br><br>
  143.  *
  144.  * @author Lakshmi Krishnamurthy
  145.  */

  146. public class OTCPayerClassicalPlusTimeline
  147. {

  148.     private static final FixFloatComponent OTCIRS (
  149.         final JulianDate spotDate,
  150.         final String currency,
  151.         final String maturityTenor,
  152.         final double coupon)
  153.     {
  154.         FixedFloatSwapConvention ffConv = IBORFixedFloatContainer.ConventionFromJurisdiction (
  155.             currency,
  156.             "ALL",
  157.             maturityTenor,
  158.             "MAIN"
  159.         );

  160.         return ffConv.createFixFloatComponent (
  161.             spotDate,
  162.             maturityTenor,
  163.             coupon,
  164.             0.,
  165.             1.
  166.         );
  167.     }

  168.     private static final PrimarySecurity OvernightReplicator (
  169.         final String currency,
  170.         final List<LatentStateLabel> latentStateLabelList)
  171.         throws Exception
  172.     {
  173.         double overnightReplicatorDrift = 0.0025;
  174.         double overnightReplicatorVolatility = 0.001;
  175.         double overnightReplicatorRepo = 0.0;

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

  177.         latentStateLabelList.add (overnightLabel);

  178.         return new PrimarySecurity (
  179.             currency + "_OVERNIGHT",
  180.             overnightLabel,
  181.             new DiffusionEvolver (
  182.                 DiffusionEvaluatorLogarithmic.Standard (
  183.                     overnightReplicatorDrift,
  184.                     overnightReplicatorVolatility
  185.                 )
  186.             ),
  187.             overnightReplicatorRepo
  188.         );
  189.     }

  190.     private static final PrimarySecurity CSAReplicator (
  191.         final String currency,
  192.         final List<LatentStateLabel> latentStateLabelList)
  193.         throws Exception
  194.     {
  195.         double csaReplicatorDrift = 0.01;
  196.         double csaReplicatorVolatility = 0.002;
  197.         double csaReplicatorRepo = 0.005;

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

  199.         latentStateLabelList.add (csaLabel);

  200.         return new PrimarySecurity (
  201.             currency + "_CSA",
  202.             csaLabel,
  203.             new DiffusionEvolver (
  204.                 DiffusionEvaluatorLogarithmic.Standard (
  205.                     csaReplicatorDrift,
  206.                     csaReplicatorVolatility
  207.                 )
  208.             ),
  209.             csaReplicatorRepo
  210.         );
  211.     }

  212.     private static final PrimarySecurity DealerSeniorFundingReplicator (
  213.         final String currency,
  214.         final String dealer,
  215.         final List<LatentStateLabel> latentStateLabelList)
  216.         throws Exception
  217.     {
  218.         double dealerSeniorFundingReplicatorDrift = 0.03;
  219.         double dealerSeniorFundingReplicatorVolatility = 0.002;
  220.         double dealerSeniorFundingReplicatorRepo = 0.028;

  221.         LatentStateLabel dealerSeniorFundingLabel = EntityFundingLabel.Senior (
  222.             dealer,
  223.             currency
  224.         );

  225.         latentStateLabelList.add (dealerSeniorFundingLabel);

  226.         return new PrimarySecurity (
  227.             dealer + "_" + currency + "_SENIOR_ZERO",
  228.             dealerSeniorFundingLabel,
  229.             new JumpDiffusionEvolver (
  230.                 DiffusionEvaluatorLogarithmic.Standard (
  231.                     dealerSeniorFundingReplicatorDrift,
  232.                     dealerSeniorFundingReplicatorVolatility
  233.                 ),
  234.                 HazardJumpEvaluator.Standard (
  235.                     0.3,
  236.                     0.45
  237.                 )
  238.             ),
  239.             dealerSeniorFundingReplicatorRepo
  240.         );
  241.     }

  242.     private static final PrimarySecurity DealerSubordinateFundingReplicator (
  243.         final String currency,
  244.         final String dealer,
  245.         final List<LatentStateLabel> latentStateLabelList)
  246.         throws Exception
  247.     {
  248.         double dealerSubordinateFundingReplicatorDrift = 0.045;
  249.         double dealerSubordinateFundingReplicatorVolatility = 0.002;
  250.         double dealerSubordinateFundingReplicatorRepo = 0.028;

  251.         LatentStateLabel dealerSubordinateFundingLabel = EntityFundingLabel.Subordinate (
  252.             dealer,
  253.             currency
  254.         );

  255.         latentStateLabelList.add (dealerSubordinateFundingLabel);

  256.         return new PrimarySecurity (
  257.             dealer + "_" + currency + "_SUBORDINATE_ZERO",
  258.             dealerSubordinateFundingLabel,
  259.             new JumpDiffusionEvolver (
  260.                 DiffusionEvaluatorLogarithmic.Standard (
  261.                     dealerSubordinateFundingReplicatorDrift,
  262.                     dealerSubordinateFundingReplicatorVolatility
  263.                 ),
  264.                 HazardJumpEvaluator.Standard (
  265.                     0.3,
  266.                     0.25
  267.                 )
  268.             ),
  269.             dealerSubordinateFundingReplicatorRepo
  270.         );
  271.     }

  272.     private static final PrimarySecurity ClientFundingReplicator (
  273.         final String currency,
  274.         final String client,
  275.         final List<LatentStateLabel> latentStateLabelList)
  276.         throws Exception
  277.     {
  278.         double clientFundingReplicatorDrift = 0.03;
  279.         double clientFundingReplicatorVolatility = 0.003;
  280.         double clientFundingReplicatorRepo = 0.028;

  281.         LatentStateLabel clientFundingLabel = EntityFundingLabel.Senior (
  282.             client,
  283.             currency
  284.         );

  285.         latentStateLabelList.add (clientFundingLabel);

  286.         return new PrimarySecurity (
  287.             client + "_" + currency + "_SENIOR_ZERO",
  288.             clientFundingLabel,
  289.             new JumpDiffusionEvolver (
  290.                 DiffusionEvaluatorLogarithmic.Standard (
  291.                     clientFundingReplicatorDrift,
  292.                     clientFundingReplicatorVolatility
  293.                 ),
  294.                 HazardJumpEvaluator.Standard (
  295.                     0.5,
  296.                     0.30
  297.                 )
  298.             ),
  299.             clientFundingReplicatorRepo
  300.         );
  301.     }

  302.     private static final TerminalLatentState DealerHazard (
  303.         final String currency,
  304.         final String dealer,
  305.         final List<LatentStateLabel> latentStateLabelList)
  306.         throws Exception
  307.     {
  308.         double dealerHazardDrift = 0.0002;
  309.         double dealerHazardVolatility = 0.02;

  310.         LatentStateLabel dealerHazardLabel = EntityHazardLabel.Standard (
  311.             dealer,
  312.             currency
  313.         );

  314.         latentStateLabelList.add (dealerHazardLabel);

  315.         return new TerminalLatentState (
  316.             dealerHazardLabel,
  317.             new DiffusionEvolver (
  318.                 DiffusionEvaluatorLogarithmic.Standard (
  319.                     dealerHazardDrift,
  320.                     dealerHazardVolatility
  321.                 )
  322.             )
  323.         );
  324.     }

  325.     private static final TerminalLatentState DealerRecovery (
  326.         final String currency,
  327.         final String dealer,
  328.         final List<LatentStateLabel> latentStateLabelList)
  329.         throws Exception
  330.     {
  331.         double dealerRecoveryDrift = 0.0002;
  332.         double dealerRecoveryVolatility = 0.02;

  333.         LatentStateLabel dealerRecoveryLabel = EntityRecoveryLabel.Senior (
  334.             dealer,
  335.             currency
  336.         );

  337.         latentStateLabelList.add (dealerRecoveryLabel);

  338.         return new TerminalLatentState (
  339.             dealerRecoveryLabel,
  340.             new DiffusionEvolver (
  341.                 DiffusionEvaluatorLogarithmic.Standard (
  342.                     dealerRecoveryDrift,
  343.                     dealerRecoveryVolatility
  344.                 )
  345.             )
  346.         );
  347.     }

  348.     private static final TerminalLatentState ClientHazard (
  349.         final String currency,
  350.         final String client,
  351.         final List<LatentStateLabel> latentStateLabelList)
  352.         throws Exception
  353.     {
  354.         double clientHazardDrift = 0.0002;
  355.         double clientHazardVolatility = 0.02;

  356.         LatentStateLabel clientHazardLabel = EntityHazardLabel.Standard (
  357.             client,
  358.             currency
  359.         );

  360.         latentStateLabelList.add (clientHazardLabel);

  361.         return new TerminalLatentState (
  362.             clientHazardLabel,
  363.             new DiffusionEvolver (
  364.                 DiffusionEvaluatorLogarithmic.Standard (
  365.                     clientHazardDrift,
  366.                     clientHazardVolatility
  367.                 )
  368.             )
  369.         );
  370.     }

  371.     private static final TerminalLatentState ClientRecovery (
  372.         final String currency,
  373.         final String client,
  374.         final List<LatentStateLabel> latentStateLabelList)
  375.         throws Exception
  376.     {
  377.         double clientRecoveryDrift = 0.0002;
  378.         double clientRecoveryVolatility = 0.02;

  379.         LatentStateLabel clientRecoveryLabel = EntityRecoveryLabel.Senior (
  380.             client,
  381.             currency
  382.         );

  383.         latentStateLabelList.add (clientRecoveryLabel);

  384.         return new TerminalLatentState (
  385.             clientRecoveryLabel,
  386.             new DiffusionEvolver (
  387.                 DiffusionEvaluatorLogarithmic.Standard (
  388.                     clientRecoveryDrift,
  389.                     clientRecoveryVolatility
  390.                 )
  391.             )
  392.         );
  393.     }

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

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

  459.     private static final LatentStateDynamicsContainer LatentStateEvolver (
  460.         final ForwardLabel forwardLabel,
  461.         final List<LatentStateLabel> latentStateLabelList)
  462.         throws Exception
  463.     {
  464.         double otcFixFloatNumeraireDrift = 0.0;
  465.         double otcFixFloatNumeraireVolatility = 0.25;

  466.         latentStateLabelList.add (forwardLabel);

  467.         LatentStateDynamicsContainer latentStateDynamicsContainer = new LatentStateDynamicsContainer();

  468.         latentStateDynamicsContainer.addForward (
  469.             new TerminalLatentState (
  470.                 forwardLabel,
  471.                 new DiffusionEvolver (
  472.                     DiffusionEvaluatorLinear.Standard (
  473.                         otcFixFloatNumeraireDrift,
  474.                         otcFixFloatNumeraireVolatility
  475.                     )
  476.                 )
  477.             )
  478.         );

  479.         return latentStateDynamicsContainer;
  480.     }

  481.     private static final MarketVertexGenerator ConstructMarketVertexGenerator (
  482.         final JulianDate spotDate,
  483.         final String exposureSamplingTenor,
  484.         final int exposureSamplingNodeCount,
  485.         final String currency,
  486.         final String dealer,
  487.         final String client,
  488.         final ForwardLabel forwardLabel,
  489.         final List<LatentStateLabel> latentStateLabelList)
  490.         throws Exception
  491.     {
  492.         JulianDate terminationDate = spotDate;
  493.         int[] eventVertexArray = new int[exposureSamplingNodeCount];

  494.         for (int i = 0; i < exposureSamplingNodeCount; ++i)
  495.         {
  496.             terminationDate = terminationDate.addTenor (exposureSamplingTenor);

  497.             eventVertexArray[i] = terminationDate.julian();
  498.         }

  499.         return new MarketVertexGenerator (
  500.             spotDate.julian(),
  501.             eventVertexArray,
  502.             EntityEvolver (
  503.                 currency,
  504.                 dealer,
  505.                 client,
  506.                 latentStateLabelList
  507.             ),
  508.             PrimarySecurityEvolver (
  509.                 currency,
  510.                 dealer,
  511.                 client,
  512.                 latentStateLabelList
  513.             ),
  514.             LatentStateEvolver (
  515.                 forwardLabel,
  516.                 latentStateLabelList
  517.             )
  518.         );
  519.     }

  520.     public static final void main (
  521.         final String[] args)
  522.         throws Exception
  523.     {
  524.         EnvManager.InitEnv ("");

  525.         JulianDate spotDate = DateUtil.CreateFromYMD (
  526.             2018,
  527.             DateUtil.APRIL,
  528.             19
  529.         );

  530.         int pathCount = 1000;
  531.         String exposurePeriodTenor = "1D";
  532.         int exposurePeriodCount = 390;
  533.         int vertexGenerationPeriodCount = exposurePeriodCount + 10;
  534.         String currency = "USD";
  535.         String dealer = "NOM";
  536.         String client = "SSGA";
  537.         double[][] correlationMatrix = new double[][]
  538.         {
  539.             {1.00, 0.00, 0.20, 0.15, 0.05, 0.00, 0.00, 0.00, 0.00, 0.00}, // #0  DEALER HAZARD
  540.             {0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #1  DEALER SENIOR RECOVERY
  541.             {0.20, 0.00, 1.00, 0.13, 0.25, 0.00, 0.00, 0.00, 0.00, 0.00}, // #2  CLIENT HAZARD
  542.             {0.15, 0.00, 0.13, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #3  CLIENT RECOVERY
  543.             {0.05, 0.00, 0.25, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #4  OVERNIGHT REPLICATOR
  544.             {0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00}, // #5  CSA REPLICATOR
  545.             {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
  546.             {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
  547.             {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00}, // #8  CLIENT FUNDING REPLICATOR
  548.             {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00}, // #9  FORWARD NUMERAIRE
  549.         };
  550.         String fixFloatMaturityTenor = "1Y";
  551.         double fixFloatCoupon = 0.02;
  552.         double fixFloatNotional = -1.e+06;

  553.         ForwardLabel forwardLabel = ForwardLabel.Create (
  554.             currency,
  555.             "3M"
  556.         );

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

  558.         MarketVertexGenerator marketVertexGenerator = ConstructMarketVertexGenerator (
  559.             spotDate,
  560.             exposurePeriodTenor,
  561.             vertexGenerationPeriodCount,
  562.             currency,
  563.             dealer,
  564.             client,
  565.             forwardLabel,
  566.             latentStateLabelList
  567.         );

  568.         LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

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

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

  588.         AndersenPykhtinSokolLag andersenPykhtinSokolLag = AndersenPykhtinSokolLag.ClassicalPlus();

  589.         FixFloatComponent fixFloatComponent = OTCIRS (
  590.             spotDate,
  591.             currency,
  592.             fixFloatMaturityTenor,
  593.             fixFloatCoupon
  594.         );

  595.         FixFloatMPoR fixFloatMPoR = new FixFloatMPoR (
  596.             fixFloatComponent,
  597.             fixFloatNotional
  598.         );

  599.         CorrelatedPathVertexDimension correlatedPathVertexDimension = new CorrelatedPathVertexDimension (
  600.             new RandomNumberGenerator(),
  601.             correlationMatrix,
  602.             vertexGenerationPeriodCount,
  603.             1,
  604.             true,
  605.             null
  606.         );

  607.         JulianDate exposureDate = spotDate;
  608.         int[] exposureDateArray = new int[exposurePeriodCount + 1];
  609.         int[] variationMarginGapEndDateArray = new int[exposurePeriodCount + 1];
  610.         int[] variationMarginGapStartDateArray = new int[exposurePeriodCount + 1];
  611.         double[] tradePaymentGapArray = new double[exposurePeriodCount + 1];
  612.         double[] variationMarginGapArray = new double[exposurePeriodCount + 1];
  613.         double[] clientTradePaymentGapArray = new double[exposurePeriodCount + 1];
  614.         double[] collateralizedExposureArray = new double[exposurePeriodCount + 1];
  615.         double[] variationMarginPostingArray = new double[exposurePeriodCount + 1];
  616.         double[] variationMarginEstimateArray = new double[exposurePeriodCount + 1];
  617.         double[] clientDealerTradePaymentGapArray = new double[exposurePeriodCount + 1];
  618.         double[] collateralizedPositiveExposureArray = new double[exposurePeriodCount + 1];
  619.         int[] clientTradePaymentGapEndDateArray = new int[exposurePeriodCount + 1];
  620.         int[] clientTradePaymentGapStartDateArray = new int[exposurePeriodCount + 1];
  621.         int[] clientDealerTradePaymentGapEndDateArray = new int[exposurePeriodCount + 1];
  622.         int[] clientDealerTradePaymentGapStartDateArray = new int[exposurePeriodCount + 1];

  623.         for (int i = 0; i <= exposurePeriodCount; ++i)
  624.         {
  625.             tradePaymentGapArray[i] = 0.;
  626.             variationMarginGapArray[i] = 0.;
  627.             clientTradePaymentGapArray[i] = 0.;
  628.             collateralizedExposureArray[i] = 0.;
  629.             variationMarginPostingArray[i] = 0.;
  630.             variationMarginEstimateArray[i] = 0.;
  631.             clientDealerTradePaymentGapArray[i] = 0.;
  632.             collateralizedPositiveExposureArray[i] = 0.;

  633.             exposureDateArray[i] = exposureDate.julian();

  634.             exposureDate = exposureDate.addTenor (exposurePeriodTenor);
  635.         }

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

  647.             PathVariationMarginTrajectoryEstimator marginTradeFlowTrajectory =
  648.                 PathVariationMarginTrajectoryEstimator.Standard (
  649.                     exposureDateArray,
  650.                     currency,
  651.                     fixFloatMPoR,
  652.                     marketPath,
  653.                     andersenPykhtinSokolLag
  654.                 );

  655.             Map<Integer, VariationMarginTradeVertexExposure> mapMarginTradeFlowEntry =
  656.                 marginTradeFlowTrajectory.trajectory();

  657.             for (int i = 0; i <= exposurePeriodCount; ++i)
  658.             {
  659.                 VariationMarginTradeVertexExposure marginTradeFlowEntry = mapMarginTradeFlowEntry.get (exposureDateArray[i]);

  660.                 LastFlowDates lastFlowDates = marginTradeFlowEntry.lastFlowDates();

  661.                 tradePaymentGapArray[i] += marginTradeFlowEntry.tradePaymentGap();

  662.                 clientTradePaymentGapArray[i] += marginTradeFlowEntry.clientTradePaymentGap();

  663.                 clientDealerTradePaymentGapArray[i] += marginTradeFlowEntry.clientDealerTradePaymentGap();

  664.                 collateralizedExposureArray[i] += marginTradeFlowEntry.collateralizedExposure();

  665.                 collateralizedPositiveExposureArray[i] += marginTradeFlowEntry.collateralizedPositiveExposure();

  666.                 variationMarginEstimateArray[i] += marginTradeFlowEntry.variationMarginEstimate();

  667.                 variationMarginPostingArray[i] += marginTradeFlowEntry.variationMarginPosting();

  668.                 variationMarginGapArray[i] += marginTradeFlowEntry.variationMarginGap();

  669.                 variationMarginGapStartDateArray[i] = lastFlowDates.clientVariationMarginPosting().julian();

  670.                 variationMarginGapEndDateArray[i] = lastFlowDates.dealerVariationMarginPosting().julian();

  671.                 clientTradePaymentGapStartDateArray[i] = lastFlowDates.clientTradePayment().julian();

  672.                 clientTradePaymentGapEndDateArray[i] = lastFlowDates.dealerTradePayment().julian();

  673.                 clientDealerTradePaymentGapStartDateArray[i] = lastFlowDates.dealerTradePayment().julian();

  674.                 clientDealerTradePaymentGapEndDateArray[i] = lastFlowDates.variationMarginPeriodEnd().julian();
  675.             }
  676.         }

  677.         System.out.println();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  701.         for (int i = 0; i <= exposurePeriodCount; ++i)
  702.         {
  703.             System.out.println (
  704.                 "\t| [" +
  705.                 new JulianDate (exposureDateArray[i]) + "] => [" +
  706.                 new JulianDate (variationMarginGapStartDateArray[i]) + " -> " +
  707.                 new JulianDate (variationMarginGapEndDateArray[i]) + "] | " +
  708.                 FormatUtil.FormatDouble (variationMarginEstimateArray[i] / pathCount, 5, 2, 1) + " | " +
  709.                 FormatUtil.FormatDouble (variationMarginPostingArray[i] / pathCount, 5, 2, 1) + " | " +
  710.                 FormatUtil.FormatDouble (variationMarginGapArray[i] / pathCount, 5, 2, 1) + " | [" +
  711.                 new JulianDate (clientTradePaymentGapStartDateArray[i]) + " -> " +
  712.                 new JulianDate (clientTradePaymentGapEndDateArray[i]) + "] | " +
  713.                 FormatUtil.FormatDouble (clientTradePaymentGapArray[i] / pathCount, 5, 2, 1) + " | [" +
  714.                 new JulianDate (clientDealerTradePaymentGapStartDateArray[i]) + " -> " +
  715.                 new JulianDate (clientDealerTradePaymentGapEndDateArray[i]) + "] | " +
  716.                 FormatUtil.FormatDouble (clientDealerTradePaymentGapArray[i] / pathCount, 5, 2, 1) + " | " +
  717.                 FormatUtil.FormatDouble (tradePaymentGapArray[i] / pathCount, 5, 2, 1) + " | " +
  718.                 FormatUtil.FormatDouble (collateralizedExposureArray[i] / pathCount, 5, 2, 1) + " | " +
  719.                 FormatUtil.FormatDouble (collateralizedPositiveExposureArray[i] / pathCount, 5, 2, 1) + " ||"
  720.             );
  721.         }

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

  723.         EnvManager.TerminateEnv();
  724.     }
  725. }