ExposurePathBrownianBridge.java

  1. package org.drip.sample.pykhtin2009;

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

  6. import org.drip.analytics.date.DateUtil;
  7. import org.drip.analytics.date.JulianDate;
  8. import org.drip.exposure.csatimeline.AndersenPykhtinSokolLag;
  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.NumeraireMPoR;
  16. import org.drip.exposure.mpor.PathVariationMarginTrajectoryEstimator;
  17. import org.drip.exposure.regression.LocalVolatilityGenerationControl;
  18. import org.drip.exposure.regression.PykhtinBrownianBridgeStretch;
  19. import org.drip.exposure.regression.PykhtinPillarDynamics;
  20. import org.drip.exposure.universe.LatentStateWeiner;
  21. import org.drip.exposure.universe.MarketPath;
  22. import org.drip.exposure.universe.MarketVertex;
  23. import org.drip.exposure.universe.MarketVertexGenerator;
  24. import org.drip.function.definition.R1ToR1;
  25. import org.drip.measure.crng.RandomNumberGenerator;
  26. import org.drip.measure.discrete.CorrelatedPathVertexDimension;
  27. import org.drip.measure.dynamics.DiffusionEvaluatorLinear;
  28. import org.drip.measure.dynamics.DiffusionEvaluatorLogarithmic;
  29. import org.drip.measure.dynamics.HazardJumpEvaluator;
  30. import org.drip.measure.gaussian.NormalQuadrature;
  31. import org.drip.measure.process.DiffusionEvolver;
  32. import org.drip.measure.process.JumpDiffusionEvolver;
  33. import org.drip.measure.statistics.UnivariateDiscreteThin;
  34. import org.drip.numerical.common.FormatUtil;
  35. import org.drip.numerical.linearalgebra.Matrix;
  36. import org.drip.service.env.EnvManager;
  37. import org.drip.state.identifier.CSALabel;
  38. import org.drip.state.identifier.EntityEquityLabel;
  39. import org.drip.state.identifier.EntityFundingLabel;
  40. import org.drip.state.identifier.EntityHazardLabel;
  41. import org.drip.state.identifier.EntityRecoveryLabel;
  42. import org.drip.state.identifier.LatentStateLabel;
  43. import org.drip.state.identifier.OvernightLabel;

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

  47. /*!
  48.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  49.  *
  50.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  51.  *      libraries targeting analysts and developers
  52.  *      https://lakshmidrip.github.io/DRIP/
  53.  *  
  54.  *  DRIP is composed of four main libraries:
  55.  *  
  56.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  57.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  58.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  59.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  60.  *
  61.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  62.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  63.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  64.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  65.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  66.  *
  67.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  68.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  69.  *
  70.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  71.  *
  72.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  73.  *
  74.  *  Licensed under the Apache License, Version 2.0 (the "License");
  75.  *      you may not use this file except in compliance with the License.
  76.  *  
  77.  *  You may obtain a copy of the License at
  78.  *      http://www.apache.org/licenses/LICENSE-2.0
  79.  *  
  80.  *  Unless required by applicable law or agreed to in writing, software
  81.  *      distributed under the License is distributed on an "AS IS" BASIS,
  82.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  83.  *  
  84.  *  See the License for the specific language governing permissions and
  85.  *      limitations under the License.
  86.  */

  87. /**
  88.  * ExposurePathBrownianBridge sets up a Brownian Bridge Scheme base on the Pykhtin (2009) local Volatility
  89.  *  Methodology to estimate Exposures at Secondary Nodes. The References are:
  90.  *  
  91.  *  - Andersen, L. B. G., M. Pykhtin, and A. Sokol (2017): Re-thinking Margin Period of Risk,
  92.  *      https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2902737, eSSRN.
  93.  *  
  94.  *  - Andersen, L. B. G., M. Pykhtin, and A. Sokol (2017): Credit Exposure in the Presence of Initial Margin,
  95.  *      https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2806156, eSSRN.
  96.  *  
  97.  *  - Albanese, C., and L. Andersen (2014): Accounting for OTC Derivatives: Funding Adjustments and the
  98.  *      Re-Hypothecation Option, eSSRN, https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2482955.
  99.  *  
  100.  *  - Burgard, C., and M. Kjaer (2017): Derivatives Funding, Netting, and Accounting, eSSRN,
  101.  *      https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2534011.
  102.  *
  103.  *  - Pykhtin, M. (2009): Modeling Counter-party Credit Exposure in the Presence of Margin Agreements,
  104.  *      http://www.risk-europe.com/protected/michael-pykhtin.pdf.
  105.  *
  106.  * @author Lakshmi Krishnamurthy
  107.  */

  108. public class ExposurePathBrownianBridge
  109. {

  110.     private static final PrimarySecurity OvernightReplicator (
  111.         final String currency,
  112.         final List<LatentStateLabel> latentStateLabelList)
  113.         throws Exception
  114.     {
  115.         double overnightReplicatorDrift = 0.0025;
  116.         double overnightReplicatorVolatility = 0.001;
  117.         double overnightReplicatorRepo = 0.0;

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

  119.         latentStateLabelList.add (overnightLabel);

  120.         return new PrimarySecurity (
  121.             currency + "_OVERNIGHT",
  122.             overnightLabel,
  123.             new DiffusionEvolver (
  124.                 DiffusionEvaluatorLogarithmic.Standard (
  125.                     overnightReplicatorDrift,
  126.                     overnightReplicatorVolatility
  127.                 )
  128.             ),
  129.             overnightReplicatorRepo
  130.         );
  131.     }

  132.     private static final PrimarySecurity CSAReplicator (
  133.         final String currency,
  134.         final List<LatentStateLabel> latentStateLabelList)
  135.         throws Exception
  136.     {
  137.         double csaReplicatorDrift = 0.01;
  138.         double csaReplicatorVolatility = 0.002;
  139.         double csaReplicatorRepo = 0.005;

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

  141.         latentStateLabelList.add (csaLabel);

  142.         return new PrimarySecurity (
  143.             currency + "_CSA",
  144.             csaLabel,
  145.             new DiffusionEvolver (
  146.                 DiffusionEvaluatorLogarithmic.Standard (
  147.                     csaReplicatorDrift,
  148.                     csaReplicatorVolatility
  149.                 )
  150.             ),
  151.             csaReplicatorRepo
  152.         );
  153.     }

  154.     private static final PrimarySecurity DealerSeniorFundingReplicator (
  155.         final String currency,
  156.         final String dealer,
  157.         final List<LatentStateLabel> latentStateLabelList)
  158.         throws Exception
  159.     {
  160.         double dealerSeniorFundingReplicatorDrift = 0.03;
  161.         double dealerSeniorFundingReplicatorVolatility = 0.002;
  162.         double dealerSeniorFundingReplicatorRepo = 0.028;

  163.         LatentStateLabel dealerSeniorFundingLabel = EntityFundingLabel.Senior (
  164.             dealer,
  165.             currency
  166.         );

  167.         latentStateLabelList.add (dealerSeniorFundingLabel);

  168.         return new PrimarySecurity (
  169.             dealer + "_" + currency + "_SENIOR_ZERO",
  170.             dealerSeniorFundingLabel,
  171.             new JumpDiffusionEvolver (
  172.                 DiffusionEvaluatorLogarithmic.Standard (
  173.                     dealerSeniorFundingReplicatorDrift,
  174.                     dealerSeniorFundingReplicatorVolatility
  175.                 ),
  176.                 HazardJumpEvaluator.Standard (
  177.                     0.3,
  178.                     0.45
  179.                 )
  180.             ),
  181.             dealerSeniorFundingReplicatorRepo
  182.         );
  183.     }

  184.     private static final PrimarySecurity DealerSubordinateFundingReplicator (
  185.         final String currency,
  186.         final String dealer,
  187.         final List<LatentStateLabel> latentStateLabelList)
  188.         throws Exception
  189.     {
  190.         double dealerSubordinateFundingReplicatorDrift = 0.045;
  191.         double dealerSubordinateFundingReplicatorVolatility = 0.002;
  192.         double dealerSubordinateFundingReplicatorRepo = 0.028;

  193.         LatentStateLabel dealerSubordinateFundingLabel = EntityFundingLabel.Subordinate (
  194.             dealer,
  195.             currency
  196.         );

  197.         latentStateLabelList.add (dealerSubordinateFundingLabel);

  198.         return new PrimarySecurity (
  199.             dealer + "_" + currency + "_SUBORDINATE_ZERO",
  200.             dealerSubordinateFundingLabel,
  201.             new JumpDiffusionEvolver (
  202.                 DiffusionEvaluatorLogarithmic.Standard (
  203.                     dealerSubordinateFundingReplicatorDrift,
  204.                     dealerSubordinateFundingReplicatorVolatility
  205.                 ),
  206.                 HazardJumpEvaluator.Standard (
  207.                     0.3,
  208.                     0.25
  209.                 )
  210.             ),
  211.             dealerSubordinateFundingReplicatorRepo
  212.         );
  213.     }

  214.     private static final PrimarySecurity ClientFundingReplicator (
  215.         final String currency,
  216.         final String client,
  217.         final List<LatentStateLabel> latentStateLabelList)
  218.         throws Exception
  219.     {
  220.         double clientFundingReplicatorDrift = 0.03;
  221.         double clientFundingReplicatorVolatility = 0.003;
  222.         double clientFundingReplicatorRepo = 0.028;

  223.         LatentStateLabel clientFundingLabel = EntityFundingLabel.Senior (
  224.             client,
  225.             currency
  226.         );

  227.         latentStateLabelList.add (clientFundingLabel);

  228.         return new PrimarySecurity (
  229.             client + "_" + currency + "_SENIOR_ZERO",
  230.             clientFundingLabel,
  231.             new JumpDiffusionEvolver (
  232.                 DiffusionEvaluatorLogarithmic.Standard (
  233.                     clientFundingReplicatorDrift,
  234.                     clientFundingReplicatorVolatility
  235.                 ),
  236.                 HazardJumpEvaluator.Standard (
  237.                     0.5,
  238.                     0.30
  239.                 )
  240.             ),
  241.             clientFundingReplicatorRepo
  242.         );
  243.     }

  244.     private static final TerminalLatentState DealerHazard (
  245.         final String currency,
  246.         final String dealer,
  247.         final List<LatentStateLabel> latentStateLabelList)
  248.         throws Exception
  249.     {
  250.         double dealerHazardDrift = 0.0002;
  251.         double dealerHazardVolatility = 0.02;

  252.         LatentStateLabel dealerHazardLabel = EntityHazardLabel.Standard (
  253.             dealer,
  254.             currency
  255.         );

  256.         latentStateLabelList.add (dealerHazardLabel);

  257.         return new TerminalLatentState (
  258.             dealerHazardLabel,
  259.             new DiffusionEvolver (
  260.                 DiffusionEvaluatorLogarithmic.Standard (
  261.                     dealerHazardDrift,
  262.                     dealerHazardVolatility
  263.                 )
  264.             )
  265.         );
  266.     }

  267.     private static final TerminalLatentState DealerRecovery (
  268.         final String currency,
  269.         final String dealer,
  270.         final List<LatentStateLabel> latentStateLabelList)
  271.         throws Exception
  272.     {
  273.         double dealerRecoveryDrift = 0.0002;
  274.         double dealerRecoveryVolatility = 0.02;

  275.         LatentStateLabel dealerRecoveryLabel = EntityRecoveryLabel.Senior (
  276.             dealer,
  277.             currency
  278.         );

  279.         latentStateLabelList.add (dealerRecoveryLabel);

  280.         return new TerminalLatentState (
  281.             dealerRecoveryLabel,
  282.             new DiffusionEvolver (
  283.                 DiffusionEvaluatorLogarithmic.Standard (
  284.                     dealerRecoveryDrift,
  285.                     dealerRecoveryVolatility
  286.                 )
  287.             )
  288.         );
  289.     }

  290.     private static final TerminalLatentState ClientHazard (
  291.         final String currency,
  292.         final String client,
  293.         final List<LatentStateLabel> latentStateLabelList)
  294.         throws Exception
  295.     {
  296.         double clientHazardDrift = 0.0002;
  297.         double clientHazardVolatility = 0.02;

  298.         LatentStateLabel clientHazardLabel = EntityHazardLabel.Standard (
  299.             client,
  300.             currency
  301.         );

  302.         latentStateLabelList.add (clientHazardLabel);

  303.         return new TerminalLatentState (
  304.             clientHazardLabel,
  305.             new DiffusionEvolver (
  306.                 DiffusionEvaluatorLogarithmic.Standard (
  307.                     clientHazardDrift,
  308.                     clientHazardVolatility
  309.                 )
  310.             )
  311.         );
  312.     }

  313.     private static final TerminalLatentState ClientRecovery (
  314.         final String currency,
  315.         final String client,
  316.         final List<LatentStateLabel> latentStateLabelList)
  317.         throws Exception
  318.     {
  319.         double clientRecoveryDrift = 0.0002;
  320.         double clientRecoveryVolatility = 0.02;

  321.         LatentStateLabel clientRecoveryLabel = EntityRecoveryLabel.Senior (
  322.             client,
  323.             currency
  324.         );

  325.         latentStateLabelList.add (clientRecoveryLabel);

  326.         return new TerminalLatentState (
  327.             clientRecoveryLabel,
  328.             new DiffusionEvolver (
  329.                 DiffusionEvaluatorLogarithmic.Standard (
  330.                     clientRecoveryDrift,
  331.                     clientRecoveryVolatility
  332.                 )
  333.             )
  334.         );
  335.     }

  336.     private static final EntityDynamicsContainer EntityEvolver (
  337.         final String currency,
  338.         final String dealer,
  339.         final String client,
  340.         final List<LatentStateLabel> latentStateLabelList)
  341.         throws Exception
  342.     {
  343.         return new EntityDynamicsContainer (
  344.             DealerHazard (
  345.                 currency,
  346.                 dealer,
  347.                 latentStateLabelList
  348.             ),
  349.             DealerRecovery (
  350.                 currency,
  351.                 dealer,
  352.                 latentStateLabelList
  353.             ),
  354.             null,
  355.             ClientHazard (
  356.                 currency,
  357.                 client,
  358.                 latentStateLabelList
  359.             ),
  360.             ClientRecovery (
  361.                 currency,
  362.                 client,
  363.                 latentStateLabelList
  364.             )
  365.         );
  366.     }

  367.     private static final PrimarySecurityDynamicsContainer PrimarySecurityEvolver (
  368.         final String currency,
  369.         final String dealer,
  370.         final String client,
  371.         final List<LatentStateLabel> latentStateLabelList)
  372.         throws Exception
  373.     {
  374.         return new PrimarySecurityDynamicsContainer (
  375.             null,
  376.             OvernightReplicator (
  377.                 currency,
  378.                 latentStateLabelList
  379.             ),
  380.             CSAReplicator (
  381.                 currency,
  382.                 latentStateLabelList
  383.             ),
  384.             DealerSeniorFundingReplicator (
  385.                 currency,
  386.                 dealer,
  387.                 latentStateLabelList
  388.             ),
  389.             DealerSubordinateFundingReplicator (
  390.                 currency,
  391.                 dealer,
  392.                 latentStateLabelList
  393.             ),
  394.             ClientFundingReplicator (
  395.                 currency,
  396.                 client,
  397.                 latentStateLabelList
  398.             )
  399.         );
  400.     }

  401.     private static final LatentStateDynamicsContainer LatentStateEvolver (
  402.         final EntityEquityLabel equityLabel,
  403.         final List<LatentStateLabel> latentStateLabelList)
  404.         throws Exception
  405.     {
  406.         double equityNumeraireDrift = 0.05;
  407.         double equityNumeraireVolatility = 0.10;

  408.         latentStateLabelList.add (equityLabel);

  409.         LatentStateDynamicsContainer latentStateDynamicsContainer = new LatentStateDynamicsContainer();

  410.         latentStateDynamicsContainer.addEntityEquity (
  411.             new TerminalLatentState (
  412.                 equityLabel,
  413.                 new DiffusionEvolver (
  414.                     DiffusionEvaluatorLinear.Standard (
  415.                         equityNumeraireDrift,
  416.                         equityNumeraireVolatility
  417.                     )
  418.                 )
  419.             )
  420.         );

  421.         return latentStateDynamicsContainer;
  422.     }

  423.     private static final MarketVertexGenerator ConstructMarketVertexGenerator (
  424.         final JulianDate spotDate,
  425.         final String exposureSamplingTenor,
  426.         final int exposureSamplingNodeCount,
  427.         final String currency,
  428.         final String dealer,
  429.         final String client,
  430.         final EntityEquityLabel equityLabel,
  431.         final List<LatentStateLabel> latentStateLabelList)
  432.         throws Exception
  433.     {
  434.         JulianDate terminationDate = spotDate;
  435.         int[] eventVertexArray = new int[exposureSamplingNodeCount];

  436.         for (int i = 0; i < exposureSamplingNodeCount; ++i)
  437.         {
  438.             terminationDate = terminationDate.addTenor (exposureSamplingTenor);

  439.             eventVertexArray[i] = terminationDate.julian();
  440.         }

  441.         return new MarketVertexGenerator (
  442.             spotDate.julian(),
  443.             eventVertexArray,
  444.             EntityEvolver (
  445.                 currency,
  446.                 dealer,
  447.                 client,
  448.                 latentStateLabelList
  449.             ),
  450.             PrimarySecurityEvolver (
  451.                 currency,
  452.                 dealer,
  453.                 client,
  454.                 latentStateLabelList
  455.             ),
  456.             LatentStateEvolver (
  457.                 equityLabel,
  458.                 latentStateLabelList
  459.             )
  460.         );
  461.     }

  462.     public static final void main (
  463.         final String[] args)
  464.         throws Exception
  465.     {
  466.         EnvManager.InitEnv ("");

  467.         JulianDate spotDateJulian = DateUtil.CreateFromYMD (
  468.             2018,
  469.             DateUtil.APRIL,
  470.             19
  471.         );

  472.         int pathCount = 1000;
  473.         String sparseExposureTenor = "3M";
  474.         int sparseExposurePeriodCount = 20;
  475.         String currency = "USD";
  476.         String dealer = "NOM";
  477.         String client = "SSGA";
  478.         double[][] correlationMatrix = new double[][] {
  479.             {1.00, 0.00, 0.20, 0.15, 0.05, 0.00, 0.00, 0.00, 0.00, 0.00}, // #0  DEALER HAZARD
  480.             {0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #1  DEALER SENIOR RECOVERY
  481.             {0.20, 0.00, 1.00, 0.13, 0.25, 0.00, 0.00, 0.00, 0.00, 0.00}, // #2  CLIENT HAZARD
  482.             {0.15, 0.00, 0.13, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #3  CLIENT RECOVERY
  483.             {0.05, 0.00, 0.25, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00}, // #4  OVERNIGHT REPLICATOR
  484.             {0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00}, // #5  CSA REPLICATOR
  485.             {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
  486.             {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
  487.             {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00}, // #8  CLIENT FUNDING REPLICATOR
  488.             {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00}, // #9  OTC FIX FLOAT REPLICATOR
  489.         };
  490.         String referenceEntity = "HYG";
  491.         double equityNotional = 10.;

  492.         int spotDate = spotDateJulian.julian();

  493.         LocalVolatilityGenerationControl localVolatilityGenerationControl =
  494.             LocalVolatilityGenerationControl.Standard (pathCount);

  495.         EntityEquityLabel equityLabel = EntityEquityLabel.Standard (
  496.             referenceEntity,
  497.             currency
  498.         );

  499.         NumeraireMPoR numeraireMPoR = new NumeraireMPoR (
  500.             equityLabel,
  501.             equityNotional
  502.         );

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

  504.         MarketVertexGenerator marketVertexGenerator = ConstructMarketVertexGenerator (
  505.             spotDateJulian,
  506.             sparseExposureTenor,
  507.             sparseExposurePeriodCount,
  508.             currency,
  509.             dealer,
  510.             client,
  511.             equityLabel,
  512.             latentStateLabelList
  513.         );

  514.         LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

  515.         latentStateVertexContainer.add (
  516.             equityLabel,
  517.             10.
  518.         );

  519.         MarketVertex initialMarketVertex = MarketVertex.Epochal (
  520.             spotDateJulian,
  521.             1.000,              // dblOvernightNumeraireInitial
  522.             1.000,              // dblCSANumeraire
  523.             0.015,              // dblBankHazardRate
  524.             0.400,              // dblBankRecoveryRate
  525.             0.015 / (1 - 0.40), // dblBankFundingSpread
  526.             0.030,              // dblCounterPartyHazardRate
  527.             0.300,              // dblCounterPartyRecoveryRate
  528.             0.030 / (1 - 0.30), // dblCounterPartyFundingSpread
  529.             latentStateVertexContainer
  530.         );

  531.         AndersenPykhtinSokolLag andersenPykhtinSokolLag = AndersenPykhtinSokolLag.ClassicalMinus();

  532.         CorrelatedPathVertexDimension correlatedPathVertexDimension = new CorrelatedPathVertexDimension (
  533.             new RandomNumberGenerator(),
  534.             correlationMatrix,
  535.             sparseExposurePeriodCount,
  536.             1,
  537.             true,
  538.             null
  539.         );

  540.         JulianDate sparseExposureDate = spotDateJulian;
  541.         int[] sparseExposureDateArray = new int[sparseExposurePeriodCount + 1];
  542.         double[][] pathSparseExposureArray = new double[sparseExposurePeriodCount + 1][pathCount];

  543.         for (int i = 0; i <= sparseExposurePeriodCount; ++i)
  544.         {
  545.             sparseExposureDateArray[i] = sparseExposureDate.julian();

  546.             sparseExposureDate = sparseExposureDate.addTenor (sparseExposureTenor);
  547.         }

  548.         List<Map<Integer, Double>> wanderTrajectoryList = new ArrayList<Map<Integer, Double>>();

  549.         for (int pathIndex = 0; pathIndex < pathCount; ++pathIndex)
  550.         {
  551.             Map<Integer, Double> wanderTrajectory = new TreeMap<Integer, Double>();

  552.             for (int denseExposureDate = spotDate;
  553.                 denseExposureDate <= sparseExposureDateArray[sparseExposurePeriodCount];
  554.                 ++denseExposureDate)
  555.             {
  556.                 wanderTrajectory.put (
  557.                     denseExposureDate,
  558.                     NormalQuadrature.Random()
  559.                 );
  560.             }

  561.             wanderTrajectoryList.add (wanderTrajectory);
  562.         }

  563.         for (int pathIndex = 0; pathIndex < pathCount; ++pathIndex)
  564.         {
  565.             MarketPath marketPath = new MarketPath (
  566.                 marketVertexGenerator.marketVertex (
  567.                     initialMarketVertex,
  568.                     LatentStateWeiner.FromUnitRandom (
  569.                         latentStateLabelList,
  570.                         Matrix.Transpose (correlatedPathVertexDimension.straightPathVertexRd().flatform())
  571.                     )
  572.                 )
  573.             );

  574.             Map<Integer, Double> variationMarginEstimateTrajectory =
  575.                 PathVariationMarginTrajectoryEstimator.Standard (
  576.                     sparseExposureDateArray,
  577.                     currency,
  578.                     numeraireMPoR,
  579.                     marketPath,
  580.                     andersenPykhtinSokolLag
  581.                 ).variationMarginEstimateTrajectory();

  582.             int sparseExposureDateIndex = 0;

  583.             for (Map.Entry<Integer, Double> variationMarginEstimateTrajectoryEntry :
  584.                 variationMarginEstimateTrajectory.entrySet())
  585.             {
  586.                 pathSparseExposureArray[sparseExposureDateIndex++][pathIndex] =
  587.                     variationMarginEstimateTrajectoryEntry.getValue();
  588.             }
  589.         }

  590.         Map<Integer, R1ToR1> localVolatilityTrajectory = new TreeMap<Integer, R1ToR1>();

  591.         for (int exposureDateIndex = 0; exposureDateIndex <= sparseExposurePeriodCount; ++exposureDateIndex)
  592.         {
  593.             PykhtinPillarDynamics vertexRealization = PykhtinPillarDynamics.Standard
  594.                 (pathSparseExposureArray [exposureDateIndex]);

  595.             localVolatilityTrajectory.put (
  596.                 sparseExposureDateArray[exposureDateIndex],
  597.                 null == vertexRealization ? null :
  598.                     vertexRealization.localVolatilityR1ToR1 (localVolatilityGenerationControl)
  599.             );
  600.         }

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

  602.         System.out.println ("\t||           EXPOSURE DATE LOCAL VOLATILITY              ||");

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

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

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

  606.         System.out.println ("\t||            - Simulation Path Number                   ||");

  607.         System.out.println ("\t||            - The Spot/Forward Dates                   ||");

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

  609.         int denseExposureDateCount = sparseExposureDateArray[sparseExposurePeriodCount] - spotDate + 1;
  610.         double[][] pathDenseExposureDistribution = new double[denseExposureDateCount][pathCount];
  611.         UnivariateDiscreteThin[] univariateDiscreteThinArray = new
  612.             UnivariateDiscreteThin[denseExposureDateCount];

  613.         for (int pathIndex = 0; pathIndex < pathCount; ++pathIndex)
  614.         {
  615.             Map<Integer, Double> sparseExposureTrajectory = new TreeMap<Integer, Double>();

  616.             for (int sparseExposureDateIndex = 0;
  617.                 sparseExposureDateIndex <= sparseExposurePeriodCount;
  618.                 ++sparseExposureDateIndex)
  619.             {
  620.                 sparseExposureTrajectory.put (
  621.                     sparseExposureDateArray[sparseExposureDateIndex],
  622.                     pathSparseExposureArray[sparseExposureDateIndex][pathIndex]
  623.                 );
  624.             }

  625.             Map<Integer, Double> pathDenseExposureTrajectory = new PykhtinBrownianBridgeStretch (
  626.                 sparseExposureTrajectory,
  627.                 localVolatilityTrajectory
  628.             ).denseExposure (wanderTrajectoryList.get (pathIndex));

  629.             if (null != pathDenseExposureTrajectory)
  630.             {
  631.                 for (int denseExposureDate = spotDate;
  632.                     denseExposureDate <= sparseExposureDateArray[sparseExposurePeriodCount];
  633.                     ++denseExposureDate)
  634.                 {
  635.                     pathDenseExposureDistribution[denseExposureDate - spotDate][pathIndex] =
  636.                         pathDenseExposureTrajectory.get (denseExposureDate);
  637.                 }
  638.             }
  639.         }

  640.         for (int denseExposureDate = spotDate;
  641.             denseExposureDate <= sparseExposureDateArray[sparseExposurePeriodCount];
  642.             ++denseExposureDate)
  643.         {
  644.             int dateIndex = denseExposureDate - spotDate;

  645.             univariateDiscreteThinArray[dateIndex] = new UnivariateDiscreteThin
  646.                 (pathDenseExposureDistribution[dateIndex]);

  647.             System.out.println (
  648.                 "\t|| " +
  649.                 new JulianDate (denseExposureDate) + " => " +
  650.                 FormatUtil.FormatDouble (univariateDiscreteThinArray[dateIndex].average(), 3, 3, 1.) + " |" +
  651.                 FormatUtil.FormatDouble (univariateDiscreteThinArray[dateIndex].minimum(), 3, 3, 1.) + " |" +
  652.                 FormatUtil.FormatDouble (univariateDiscreteThinArray[dateIndex].maximum(), 3, 3, 1.) + " |" +
  653.                 FormatUtil.FormatDouble (univariateDiscreteThinArray[dateIndex].error(), 3, 3, 1.) + " ||"
  654.             );
  655.         }

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

  657.         EnvManager.TerminateEnv();
  658.     }
  659. }