EnsembleVariationMarginEstimate.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.LocalVolatilityGenerationControl;
  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.gaussian.NormalQuadrature;
  27. import org.drip.measure.process.DiffusionEvolver;
  28. import org.drip.measure.process.JumpDiffusionEvolver;
  29. import org.drip.measure.statistics.UnivariateDiscreteThin;
  30. import org.drip.numerical.common.FormatUtil;
  31. import org.drip.numerical.linearalgebra.Matrix;
  32. import org.drip.product.rates.FixFloatComponent;
  33. import org.drip.service.env.EnvManager;
  34. import org.drip.state.identifier.CSALabel;
  35. import org.drip.state.identifier.EntityFundingLabel;
  36. import org.drip.state.identifier.EntityHazardLabel;
  37. import org.drip.state.identifier.EntityRecoveryLabel;
  38. import org.drip.state.identifier.ForwardLabel;
  39. import org.drip.state.identifier.LatentStateLabel;
  40. import org.drip.state.identifier.OvernightLabel;

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

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

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

  153. public class EnsembleVariationMarginEstimate
  154. {

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

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

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

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

  184.         latentStateLabelList.add (overnightLabel);

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

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

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

  206.         latentStateLabelList.add (csaLabel);

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

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

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

  232.         latentStateLabelList.add (dealerSeniorFundingLabel);

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

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

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

  262.         latentStateLabelList.add (dealerSubordinateFundingLabel);

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

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

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

  292.         latentStateLabelList.add (clientFundingLabel);

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

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

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

  321.         latentStateLabelList.add (dealerHazardLabel);

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

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

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

  344.         latentStateLabelList.add (dealerRecoveryLabel);

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

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

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

  367.         latentStateLabelList.add (clientHazardLabel);

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

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

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

  390.         latentStateLabelList.add (clientRecoveryLabel);

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

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

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

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

  473.         latentStateLabelList.add (forwardLabel);

  474.         LatentStateDynamicsContainer latentStateDynamicsContainer = new LatentStateDynamicsContainer();

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

  486.         return latentStateDynamicsContainer;
  487.     }

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

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

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

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

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

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

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

  562.         LocalVolatilityGenerationControl localVolatilityGenerationControl =
  563.             LocalVolatilityGenerationControl.Standard (pathCount);

  564.         LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

  565.         latentStateVertexContainer.add (
  566.             ForwardLabel.Create (
  567.                 currency,
  568.                 "3M"
  569.             ),
  570.             0.02
  571.         );

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

  584.         ForwardLabel forwardLabel = ForwardLabel.Create (
  585.             currency,
  586.             "3M"
  587.         );

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

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

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

  605.         FixFloatMPoR fixFloatMPoR = new FixFloatMPoR (
  606.             fixFloatComponent,
  607.             fixFloatNotional
  608.         );

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

  617.         JulianDate sparseFixFloatExposureDate = spotDate;
  618.         int[] sparseFixFloatExposureDateArray = new int[sparseFixFloatExposureDateCount + 1];
  619.         MarketPath[] marketPathArray = new MarketPath[pathCount];

  620.         for (int sparseFixFloatExposureDateIndex = 0;
  621.             sparseFixFloatExposureDateIndex <= sparseFixFloatExposureDateCount;
  622.             ++sparseFixFloatExposureDateIndex)
  623.         {
  624.             sparseFixFloatExposureDateArray[sparseFixFloatExposureDateIndex] =
  625.                 sparseFixFloatExposureDate.julian();

  626.             sparseFixFloatExposureDate = sparseFixFloatExposureDate.addTenor (sparseFixFloatExposureTenor);
  627.         }

  628.         int denseExposureDateCount = sparseFixFloatExposureDateArray[sparseFixFloatExposureDateCount] -
  629.             sparseFixFloatExposureDateArray[0] + 1;
  630.         double[][] wanderEnsemble = new double[pathCount][denseExposureDateCount];

  631.         for (int pathIndex = 0; pathIndex < pathCount; ++pathIndex)
  632.         {
  633.             marketPathArray[pathIndex] = new MarketPath (
  634.                 marketVertexGenerator.marketVertex (
  635.                     initialMarketVertex,
  636.                     LatentStateWeiner.FromUnitRandom (
  637.                         latentStateLabelList,
  638.                         Matrix.Transpose (correlatedPathVertexDimension.straightPathVertexRd().flatform())
  639.                     )
  640.                 )
  641.             );

  642.             for (int denseExposureDate = 0; denseExposureDate < denseExposureDateCount; ++denseExposureDate)
  643.             {
  644.                 wanderEnsemble[pathIndex][denseExposureDate] = NormalQuadrature.Random();
  645.             }
  646.         }

  647.         AndersenPykhtinSokolEnsemble andersenPykhtinSokolEnsemble = new AndersenPykhtinSokolEnsemble (
  648.             fixFloatMPoR,
  649.             marketPathArray,
  650.             sparseFixFloatExposureDateArray
  651.         );

  652.         double[][] denseVariationMargin = andersenPykhtinSokolEnsemble.denseVariationMargin (
  653.             localVolatilityGenerationControl,
  654.             wanderEnsemble
  655.         );

  656.         double[][] denseVariationMarginTranspose = Matrix.Transpose (denseVariationMargin);

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

  658.         System.out.println ("\t||        ENSEMBLE VARIATION MARGIN ESTIMATE        ||");

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

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

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

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

  663.         System.out.println ("\t||          - Variation Margin Average              ||");

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

  665.         System.out.println ("\t||          - Variation Margin Maximum              ||");

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

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

  668.         for (int denseExposureDateIndex = 0;
  669.             denseExposureDateIndex < denseExposureDateCount;
  670.             ++denseExposureDateIndex)
  671.         {
  672.             UnivariateDiscreteThin denseVMThinStatistics = new UnivariateDiscreteThin
  673.                 (denseVariationMarginTranspose[denseExposureDateIndex]);

  674.             System.out.println (
  675.                 "\t|| " +
  676.                 new JulianDate (spotDate.julian() + denseExposureDateIndex) + " => " +
  677.                 FormatUtil.FormatDouble (denseVMThinStatistics.average(), 5, 0, 1.) + " | " +
  678.                 FormatUtil.FormatDouble (denseVMThinStatistics.minimum(), 5, 0, 1.) + " | " +
  679.                 FormatUtil.FormatDouble (denseVMThinStatistics.maximum(), 5, 0, 1.) + " | " +
  680.                 FormatUtil.FormatDouble (denseVMThinStatistics.error(), 5, 0, 1.) + " ||"
  681.             );
  682.         }

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

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