OTCPayerCSAAggressive.java

  1. package org.drip.sample.mporfixfloatxva;

  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.evolver.EntityDynamicsContainer;
  9. import org.drip.exposure.evolver.LatentStateDynamicsContainer;
  10. import org.drip.exposure.evolver.LatentStateVertexContainer;
  11. import org.drip.exposure.evolver.PrimarySecurity;
  12. import org.drip.exposure.evolver.PrimarySecurityDynamicsContainer;
  13. import org.drip.exposure.evolver.TerminalLatentState;
  14. import org.drip.exposure.generator.FixFloatMPoR;
  15. import org.drip.exposure.mpor.PathVariationMarginTrajectoryEstimator;
  16. import org.drip.exposure.mpor.VariationMarginTradeVertexExposure;
  17. import org.drip.exposure.universe.LatentStateWeiner;
  18. import org.drip.exposure.universe.MarketPath;
  19. import org.drip.exposure.universe.MarketVertex;
  20. import org.drip.exposure.universe.MarketVertexGenerator;
  21. import org.drip.market.otc.FixedFloatSwapConvention;
  22. import org.drip.market.otc.IBORFixedFloatContainer;
  23. import org.drip.measure.crng.RandomNumberGenerator;
  24. import org.drip.measure.discrete.CorrelatedPathVertexDimension;
  25. import org.drip.measure.dynamics.DiffusionEvaluatorLinear;
  26. import org.drip.measure.dynamics.DiffusionEvaluatorLogarithmic;
  27. import org.drip.measure.dynamics.HazardJumpEvaluator;
  28. import org.drip.measure.process.DiffusionEvolver;
  29. import org.drip.measure.process.JumpDiffusionEvolver;
  30. import org.drip.measure.statistics.UnivariateDiscreteThin;
  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. import org.drip.xva.gross.BaselExposureDigest;
  43. import org.drip.xva.gross.ExposureAdjustmentAggregator;
  44. import org.drip.xva.gross.ExposureAdjustmentDigest;
  45. import org.drip.xva.gross.MonoPathExposureAdjustment;
  46. import org.drip.xva.gross.PathExposureAdjustment;
  47. import org.drip.xva.hypothecation.CollateralGroupVertex;
  48. import org.drip.xva.netting.CollateralGroupPath;
  49. import org.drip.xva.netting.CreditDebtGroupPath;
  50. import org.drip.xva.netting.FundingGroupPath;
  51. import org.drip.xva.settings.StandardizedExposureGeneratorScheme;
  52. import org.drip.xva.strategy.AlbaneseAndersenFundingGroupPath;
  53. import org.drip.xva.strategy.AlbaneseAndersenNettingGroupPath;
  54. import org.drip.xva.vertex.AlbaneseAndersen;

  55. /*
  56.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  57.  */

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

  98. /**
  99.  * OTCPayerCSAAggressive displays the MPoR-related XVA Metrics Suite for the given OTC Payer Swap on a Daily
  100.  *  Grid using the "Aggressive" CSA Timeline of Andersen, Pykhtin, and Sokol (2017). The References are:
  101.  *  
  102.  *  - Andersen, L. B. G., M. Pykhtin, and A. Sokol (2017): Re-thinking Margin Period of Risk,
  103.  *      https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2902737, eSSRN.
  104.  *  
  105.  *  - Andersen, L. B. G., M. Pykhtin, and A. Sokol (2017): Credit Exposure in the Presence of Initial Margin,
  106.  *      https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2806156, eSSRN.
  107.  *  
  108.  *  - Albanese, C., and L. Andersen (2014): Accounting for OTC Derivatives: Funding Adjustments and the
  109.  *      Re-Hypothecation Option, eSSRN, https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2482955.
  110.  *  
  111.  *  - Burgard, C., and M. Kjaer (2017): Derivatives Funding, Netting, and Accounting, eSSRN,
  112.  *      https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2534011.
  113.  *
  114.  *  - Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives Pricing, Risk
  115.  *      21 (2) 97-102.
  116.  *
  117.  * @author Lakshmi Krishnamurthy
  118.  */

  119. public class OTCPayerCSAAggressive
  120. {

  121.     private static final FixFloatComponent OTCIRS (
  122.         final JulianDate spotDate,
  123.         final String currency,
  124.         final String maturityTenor,
  125.         final double coupon)
  126.     {
  127.         FixedFloatSwapConvention ffConv = IBORFixedFloatContainer.ConventionFromJurisdiction (
  128.             currency,
  129.             "ALL",
  130.             maturityTenor,
  131.             "MAIN"
  132.         );

  133.         return ffConv.createFixFloatComponent (
  134.             spotDate,
  135.             maturityTenor,
  136.             coupon,
  137.             0.,
  138.             1.
  139.         );
  140.     }

  141.     private static final PrimarySecurity OvernightReplicator (
  142.         final String currency,
  143.         final List<LatentStateLabel> latentStateLabelList)
  144.         throws Exception
  145.     {
  146.         double overnightReplicatorDrift = 0.0025;
  147.         double overnightReplicatorVolatility = 0.001;
  148.         double overnightReplicatorRepo = 0.0;

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

  150.         latentStateLabelList.add (overnightLabel);

  151.         return new PrimarySecurity (
  152.             currency + "_OVERNIGHT",
  153.             overnightLabel,
  154.             new DiffusionEvolver (
  155.                 DiffusionEvaluatorLogarithmic.Standard (
  156.                     overnightReplicatorDrift,
  157.                     overnightReplicatorVolatility
  158.                 )
  159.             ),
  160.             overnightReplicatorRepo
  161.         );
  162.     }

  163.     private static final PrimarySecurity CSAReplicator (
  164.         final String currency,
  165.         final List<LatentStateLabel> latentStateLabelList)
  166.         throws Exception
  167.     {
  168.         double csaReplicatorDrift = 0.01;
  169.         double csaReplicatorVolatility = 0.002;
  170.         double csaReplicatorRepo = 0.005;

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

  172.         latentStateLabelList.add (csaLabel);

  173.         return new PrimarySecurity (
  174.             currency + "_CSA",
  175.             csaLabel,
  176.             new DiffusionEvolver (
  177.                 DiffusionEvaluatorLogarithmic.Standard (
  178.                     csaReplicatorDrift,
  179.                     csaReplicatorVolatility
  180.                 )
  181.             ),
  182.             csaReplicatorRepo
  183.         );
  184.     }

  185.     private static final PrimarySecurity DealerSeniorFundingReplicator (
  186.         final String currency,
  187.         final String dealer,
  188.         final List<LatentStateLabel> latentStateLabelList)
  189.         throws Exception
  190.     {
  191.         double dealerSeniorFundingReplicatorDrift = 0.03;
  192.         double dealerSeniorFundingReplicatorVolatility = 0.002;
  193.         double dealerSeniorFundingReplicatorRepo = 0.028;

  194.         LatentStateLabel dealerSeniorFundingLabel = EntityFundingLabel.Senior (
  195.             dealer,
  196.             currency
  197.         );

  198.         latentStateLabelList.add (dealerSeniorFundingLabel);

  199.         return new PrimarySecurity (
  200.             dealer + "_" + currency + "_SENIOR_ZERO",
  201.             dealerSeniorFundingLabel,
  202.             new JumpDiffusionEvolver (
  203.                 DiffusionEvaluatorLogarithmic.Standard (
  204.                     dealerSeniorFundingReplicatorDrift,
  205.                     dealerSeniorFundingReplicatorVolatility
  206.                 ),
  207.                 HazardJumpEvaluator.Standard (
  208.                     0.3,
  209.                     0.45
  210.                 )
  211.             ),
  212.             dealerSeniorFundingReplicatorRepo
  213.         );
  214.     }

  215.     private static final PrimarySecurity DealerSubordinateFundingReplicator (
  216.         final String currency,
  217.         final String dealer,
  218.         final List<LatentStateLabel> latentStateLabelList)
  219.         throws Exception
  220.     {
  221.         double dealerSubordinateFundingReplicatorDrift = 0.045;
  222.         double dealerSubordinateFundingReplicatorVolatility = 0.002;
  223.         double dealerSubordinateFundingReplicatorRepo = 0.028;

  224.         LatentStateLabel dealerSubordinateFundingLabel = EntityFundingLabel.Subordinate (
  225.             dealer,
  226.             currency
  227.         );

  228.         latentStateLabelList.add (dealerSubordinateFundingLabel);

  229.         return new PrimarySecurity (
  230.             dealer + "_" + currency + "_SUBORDINATE_ZERO",
  231.             dealerSubordinateFundingLabel,
  232.             new JumpDiffusionEvolver (
  233.                 DiffusionEvaluatorLogarithmic.Standard (
  234.                     dealerSubordinateFundingReplicatorDrift,
  235.                     dealerSubordinateFundingReplicatorVolatility
  236.                 ),
  237.                 HazardJumpEvaluator.Standard (
  238.                     0.3,
  239.                     0.25
  240.                 )
  241.             ),
  242.             dealerSubordinateFundingReplicatorRepo
  243.         );
  244.     }

  245.     private static final PrimarySecurity ClientFundingReplicator (
  246.         final String currency,
  247.         final String client,
  248.         final List<LatentStateLabel> latentStateLabelList)
  249.         throws Exception
  250.     {
  251.         double clientFundingReplicatorDrift = 0.03;
  252.         double clientFundingReplicatorVolatility = 0.003;
  253.         double clientFundingReplicatorRepo = 0.028;

  254.         LatentStateLabel clientFundingLabel = EntityFundingLabel.Senior (
  255.             client,
  256.             currency
  257.         );

  258.         latentStateLabelList.add (clientFundingLabel);

  259.         return new PrimarySecurity (
  260.             client + "_" + currency + "_SENIOR_ZERO",
  261.             clientFundingLabel,
  262.             new JumpDiffusionEvolver (
  263.                 DiffusionEvaluatorLogarithmic.Standard (
  264.                     clientFundingReplicatorDrift,
  265.                     clientFundingReplicatorVolatility
  266.                 ),
  267.                 HazardJumpEvaluator.Standard (
  268.                     0.5,
  269.                     0.30
  270.                 )
  271.             ),
  272.             clientFundingReplicatorRepo
  273.         );
  274.     }

  275.     private static final TerminalLatentState DealerHazard (
  276.         final String currency,
  277.         final String dealer,
  278.         final List<LatentStateLabel> latentStateLabelList)
  279.         throws Exception
  280.     {
  281.         double dealerHazardDrift = 0.0002;
  282.         double dealerHazardVolatility = 0.02;

  283.         LatentStateLabel dealerHazardLabel = EntityHazardLabel.Standard (
  284.             dealer,
  285.             currency
  286.         );

  287.         latentStateLabelList.add (dealerHazardLabel);

  288.         return new TerminalLatentState (
  289.             dealerHazardLabel,
  290.             new DiffusionEvolver (
  291.                 DiffusionEvaluatorLogarithmic.Standard (
  292.                     dealerHazardDrift,
  293.                     dealerHazardVolatility
  294.                 )
  295.             )
  296.         );
  297.     }

  298.     private static final TerminalLatentState DealerRecovery (
  299.         final String currency,
  300.         final String dealer,
  301.         final List<LatentStateLabel> latentStateLabelList)
  302.         throws Exception
  303.     {
  304.         double dealerRecoveryDrift = 0.0002;
  305.         double dealerRecoveryVolatility = 0.02;

  306.         LatentStateLabel dealerRecoveryLabel = EntityRecoveryLabel.Senior (
  307.             dealer,
  308.             currency
  309.         );

  310.         latentStateLabelList.add (dealerRecoveryLabel);

  311.         return new TerminalLatentState (
  312.             dealerRecoveryLabel,
  313.             new DiffusionEvolver (
  314.                 DiffusionEvaluatorLogarithmic.Standard (
  315.                     dealerRecoveryDrift,
  316.                     dealerRecoveryVolatility
  317.                 )
  318.             )
  319.         );
  320.     }

  321.     private static final TerminalLatentState ClientHazard (
  322.         final String currency,
  323.         final String client,
  324.         final List<LatentStateLabel> latentStateLabelList)
  325.         throws Exception
  326.     {
  327.         double clientHazardDrift = 0.0002;
  328.         double clientHazardVolatility = 0.02;

  329.         LatentStateLabel clientHazardLabel = EntityHazardLabel.Standard (
  330.             client,
  331.             currency
  332.         );

  333.         latentStateLabelList.add (clientHazardLabel);

  334.         return new TerminalLatentState (
  335.             clientHazardLabel,
  336.             new DiffusionEvolver (
  337.                 DiffusionEvaluatorLogarithmic.Standard (
  338.                     clientHazardDrift,
  339.                     clientHazardVolatility
  340.                 )
  341.             )
  342.         );
  343.     }

  344.     private static final TerminalLatentState ClientRecovery (
  345.         final String currency,
  346.         final String client,
  347.         final List<LatentStateLabel> latentStateLabelList)
  348.         throws Exception
  349.     {
  350.         double clientRecoveryDrift = 0.0002;
  351.         double clientRecoveryVolatility = 0.02;

  352.         LatentStateLabel clientRecoveryLabel = EntityRecoveryLabel.Senior (
  353.             client,
  354.             currency
  355.         );

  356.         latentStateLabelList.add (clientRecoveryLabel);

  357.         return new TerminalLatentState (
  358.             clientRecoveryLabel,
  359.             new DiffusionEvolver (
  360.                 DiffusionEvaluatorLogarithmic.Standard (
  361.                     clientRecoveryDrift,
  362.                     clientRecoveryVolatility
  363.                 )
  364.             )
  365.         );
  366.     }

  367.     private static final EntityDynamicsContainer EntityEvolver (
  368.         final String currency,
  369.         final String dealer,
  370.         final String client,
  371.         final List<LatentStateLabel> latentStateLabelList)
  372.         throws Exception
  373.     {
  374.         return new EntityDynamicsContainer (
  375.             DealerHazard (
  376.                 currency,
  377.                 dealer,
  378.                 latentStateLabelList
  379.             ),
  380.             DealerRecovery (
  381.                 currency,
  382.                 dealer,
  383.                 latentStateLabelList
  384.             ),
  385.             null,
  386.             ClientHazard (
  387.                 currency,
  388.                 client,
  389.                 latentStateLabelList
  390.             ),
  391.             ClientRecovery (
  392.                 currency,
  393.                 client,
  394.                 latentStateLabelList
  395.             )
  396.         );
  397.     }

  398.     private static final PrimarySecurityDynamicsContainer PrimarySecurityEvolver (
  399.         final String currency,
  400.         final String dealer,
  401.         final String client,
  402.         final List<LatentStateLabel> latentStateLabelList)
  403.         throws Exception
  404.     {
  405.         return new PrimarySecurityDynamicsContainer (
  406.             null,
  407.             OvernightReplicator (
  408.                 currency,
  409.                 latentStateLabelList
  410.             ),
  411.             CSAReplicator (
  412.                 currency,
  413.                 latentStateLabelList
  414.             ),
  415.             DealerSeniorFundingReplicator (
  416.                 currency,
  417.                 dealer,
  418.                 latentStateLabelList
  419.             ),
  420.             DealerSubordinateFundingReplicator (
  421.                 currency,
  422.                 dealer,
  423.                 latentStateLabelList
  424.             ),
  425.             ClientFundingReplicator (
  426.                 currency,
  427.                 client,
  428.                 latentStateLabelList
  429.             )
  430.         );
  431.     }

  432.     private static final LatentStateDynamicsContainer LatentStateEvolver (
  433.         final ForwardLabel forwardLabel,
  434.         final List<LatentStateLabel> latentStateLabelList)
  435.         throws Exception
  436.     {
  437.         double otcFixFloatNumeraireDrift = 0.0;
  438.         double otcFixFloatNumeraireVolatility = 0.25;

  439.         latentStateLabelList.add (forwardLabel);

  440.         LatentStateDynamicsContainer latentStateDynamicsContainer = new LatentStateDynamicsContainer();

  441.         latentStateDynamicsContainer.addForward (
  442.             new TerminalLatentState (
  443.                 forwardLabel,
  444.                 new DiffusionEvolver (
  445.                     DiffusionEvaluatorLinear.Standard (
  446.                         otcFixFloatNumeraireDrift,
  447.                         otcFixFloatNumeraireVolatility
  448.                     )
  449.                 )
  450.             )
  451.         );

  452.         return latentStateDynamicsContainer;
  453.     }

  454.     private static final MarketVertexGenerator ConstructMarketVertexGenerator (
  455.         final JulianDate spotDate,
  456.         final String exposureSamplingTenor,
  457.         final int exposureSamplingNodeCount,
  458.         final String currency,
  459.         final String dealer,
  460.         final String client,
  461.         final ForwardLabel forwardLabel,
  462.         final List<LatentStateLabel> latentStateLabelList)
  463.         throws Exception
  464.     {
  465.         JulianDate terminationDate = spotDate;
  466.         int[] eventVertexArray = new int[exposureSamplingNodeCount];

  467.         for (int i = 0; i < exposureSamplingNodeCount; ++i)
  468.         {
  469.             terminationDate = terminationDate.addTenor (exposureSamplingTenor);

  470.             eventVertexArray[i] = terminationDate.julian();
  471.         }

  472.         return new MarketVertexGenerator (
  473.             spotDate.julian(),
  474.             eventVertexArray,
  475.             EntityEvolver (
  476.                 currency,
  477.                 dealer,
  478.                 client,
  479.                 latentStateLabelList
  480.             ),
  481.             PrimarySecurityEvolver (
  482.                 currency,
  483.                 dealer,
  484.                 client,
  485.                 latentStateLabelList
  486.             ),
  487.             LatentStateEvolver (
  488.                 forwardLabel,
  489.                 latentStateLabelList
  490.             )
  491.         );
  492.     }

  493.     private static final void DisplayThinStatistics (
  494.         final String annotation,
  495.         final UnivariateDiscreteThin univariateDiscreteThin)
  496.         throws Exception
  497.     {
  498.         System.out.println (
  499.             annotation + " => " +
  500.             FormatUtil.FormatDouble (univariateDiscreteThin.average(), 3, 0, 1.) + " | " +
  501.             FormatUtil.FormatDouble (univariateDiscreteThin.minimum(), 3, 0, 1.) + " | " +
  502.             FormatUtil.FormatDouble (univariateDiscreteThin.maximum(), 3, 0, 1.) + " | " +
  503.             FormatUtil.FormatDouble (univariateDiscreteThin.error(), 3, 0, 1.) + " ||"
  504.         );
  505.     }

  506.     private static final void DisplayBaselMeasures (
  507.         final BaselExposureDigest baselExposureDigest)
  508.         throws Exception
  509.     {
  510.         System.out.println (
  511.             "\t| Expected Exposure                    => " +
  512.             FormatUtil.FormatDouble (baselExposureDigest.expectedExposure(), 6, 0, 1.) + " ||"
  513.         );

  514.         System.out.println (
  515.             "\t| Expected Positive Exposure           => " +
  516.             FormatUtil.FormatDouble (baselExposureDigest.expectedPositiveExposure(), 6, 0, 1.) + " ||"
  517.         );

  518.         System.out.println (
  519.             "\t| Effective Expected Exposure          => " +
  520.             FormatUtil.FormatDouble (baselExposureDigest.effectiveExpectedExposure(), 6, 0, 1.) + " ||"
  521.         );

  522.         System.out.println (
  523.             "\t| Effective Expected Positive Exposure => " +
  524.             FormatUtil.FormatDouble (baselExposureDigest.effectiveExpectedPositiveExposure(), 6, 0, 1.) + " ||"
  525.         );

  526.         System.out.println (
  527.             "\t| Exposure At Default                  => " +
  528.             FormatUtil.FormatDouble (baselExposureDigest.exposureAtDefault(), 6, 0, 1.) + " ||"
  529.         );
  530.     }

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

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

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

  564.         double eadMultiplier = 1.;

  565.         ForwardLabel forwardLabel = ForwardLabel.Create (
  566.             currency,
  567.             "3M"
  568.         );

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

  570.         MarketVertexGenerator marketVertexGenerator = ConstructMarketVertexGenerator (
  571.             spotDate,
  572.             exposurePeriodTenor,
  573.             vertexGenerationPeriodCount,
  574.             currency,
  575.             dealer,
  576.             client,
  577.             forwardLabel,
  578.             latentStateLabelList
  579.         );

  580.         LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

  581.         latentStateVertexContainer.add (
  582.             forwardLabel,
  583.             0.02
  584.         );

  585.         MarketVertex initialMarketVertex = MarketVertex.Epochal (
  586.             spotDate,
  587.             1.000,              // dblOvernightNumeraireInitial
  588.             1.000,              // dblCSANumeraire
  589.             0.015,              // dblBankHazardRate
  590.             0.400,              // dblBankRecoveryRate
  591.             0.015 / (1 - 0.40), // dblBankFundingSpread
  592.             0.030,              // dblCounterPartyHazardRate
  593.             0.300,              // dblCounterPartyRecoveryRate
  594.             0.030 / (1 - 0.30), // dblCounterPartyFundingSpread
  595.             latentStateVertexContainer
  596.         );

  597.         AndersenPykhtinSokolLag andersenPykhtinSokolLag = AndersenPykhtinSokolLag.Aggressive();

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

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

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

  616.         JulianDate exposureDate = spotDate;
  617.         int[] exposureDateArray = new int[exposurePeriodCount + 1];
  618.         PathExposureAdjustment[] pathExposureAdjustmentArray = new PathExposureAdjustment[pathCount];

  619.         for (int exposurePeriodIndex = 0; exposurePeriodIndex <= exposurePeriodCount; ++exposurePeriodIndex)
  620.         {
  621.             exposureDateArray[exposurePeriodIndex] = exposureDate.julian();

  622.             exposureDate = exposureDate.addTenor (exposurePeriodTenor);
  623.         }

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

  635.             PathVariationMarginTrajectoryEstimator variationMarginTradeTrajectoryEstimator =
  636.                 PathVariationMarginTrajectoryEstimator.Standard (
  637.                     exposureDateArray,
  638.                     currency,
  639.                     fixFloatMPoR,
  640.                     marketPath,
  641.                     andersenPykhtinSokolLag
  642.                 );

  643.             Map<Integer, VariationMarginTradeVertexExposure> mapMarginTradeFlowEntry =
  644.                 variationMarginTradeTrajectoryEstimator.trajectory();

  645.             CollateralGroupVertex[] collateralGroupVertexArray = new
  646.                 CollateralGroupVertex[exposurePeriodCount + 1];

  647.             for (int exposurePeriodIndex = 0; exposurePeriodIndex <= exposurePeriodCount; ++exposurePeriodIndex)
  648.             {
  649.                 VariationMarginTradeVertexExposure variationMarginTradeVertexExposure =
  650.                     mapMarginTradeFlowEntry.get (exposureDateArray[exposurePeriodIndex]);

  651.                 collateralGroupVertexArray[exposurePeriodIndex] = new AlbaneseAndersen (
  652.                     new JulianDate (exposureDateArray[exposurePeriodIndex]),
  653.                     variationMarginTradeVertexExposure.variationMarginEstimate(),
  654.                     variationMarginTradeVertexExposure.tradePaymentGap(),
  655.                     variationMarginTradeVertexExposure.variationMarginPosting()
  656.                 );
  657.             }

  658.             CollateralGroupPath collateralGroupPath = new CollateralGroupPath (
  659.                 collateralGroupVertexArray,
  660.                 marketPath
  661.             );

  662.             CreditDebtGroupPath creditDebtGroupPath = new AlbaneseAndersenNettingGroupPath (
  663.                 new CollateralGroupPath[] {collateralGroupPath},
  664.                 marketPath
  665.             );

  666.             FundingGroupPath fundingGroupPath = new AlbaneseAndersenFundingGroupPath (
  667.                 new CreditDebtGroupPath[] {creditDebtGroupPath},
  668.                 marketPath
  669.             );

  670.             pathExposureAdjustmentArray[pathIndex] = new MonoPathExposureAdjustment
  671.                 (new FundingGroupPath[] {fundingGroupPath});
  672.         }

  673.         ExposureAdjustmentAggregator exposureAdjustmentAggregator = new ExposureAdjustmentAggregator
  674.             (pathExposureAdjustmentArray);

  675.         ExposureAdjustmentDigest exposureAdjustmentDigest = exposureAdjustmentAggregator.digest();

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

  677.         System.out.println ("\t|    OTC FIX FLOAT MPOR XVA METRICS     ||");

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

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

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

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

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

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

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

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

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

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

  688.         DisplayThinStatistics ("\t| UCOLVA  ", exposureAdjustmentDigest.ucolva());

  689.         DisplayThinStatistics ("\t| FTDCOLVA", exposureAdjustmentDigest.ftdcolva());

  690.         DisplayThinStatistics ("\t| UCVA    ", exposureAdjustmentDigest.ucva());

  691.         DisplayThinStatistics ("\t| FTDCVA  ", exposureAdjustmentDigest.ftdcva());

  692.         DisplayThinStatistics ("\t| CVA     ", exposureAdjustmentDigest.cva());

  693.         DisplayThinStatistics ("\t| CVACL   ", exposureAdjustmentDigest.cvacl());

  694.         DisplayThinStatistics ("\t| DVA     ", exposureAdjustmentDigest.dva());

  695.         DisplayThinStatistics ("\t| FVA     ", exposureAdjustmentDigest.fva());

  696.         DisplayThinStatistics ("\t| FDA     ", exposureAdjustmentDigest.fda());

  697.         DisplayThinStatistics ("\t| DVA2    ", exposureAdjustmentDigest.dva2());

  698.         DisplayThinStatistics ("\t| FCA     ", exposureAdjustmentDigest.fca());

  699.         DisplayThinStatistics ("\t| FBA     ", exposureAdjustmentDigest.fba());

  700.         DisplayThinStatistics ("\t| SFVA    ", exposureAdjustmentDigest.sfva());

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

  702.         DisplayThinStatistics ("\t| Total VA", exposureAdjustmentDigest.totalVA());

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

  704.         System.out.println();

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

  706.         System.out.println ("\t|             BASEL EXPOSURE MEASURES             ||");

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

  708.         DisplayBaselMeasures (exposureAdjustmentAggregator.baselExposureDigest
  709.             (StandardizedExposureGeneratorScheme.Basel (eadMultiplier)));

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

  711.         EnvManager.TerminateEnv();
  712.     }
  713. }