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

  120. public class OTCPayerCSAClassicalPlus
  121. {

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

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

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

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

  151.         latentStateLabelList.add (overnightLabel);

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

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

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

  173.         latentStateLabelList.add (csaLabel);

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

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

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

  199.         latentStateLabelList.add (dealerSeniorFundingLabel);

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

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

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

  229.         latentStateLabelList.add (dealerSubordinateFundingLabel);

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

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

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

  259.         latentStateLabelList.add (clientFundingLabel);

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

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

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

  288.         latentStateLabelList.add (dealerHazardLabel);

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

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

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

  311.         latentStateLabelList.add (dealerRecoveryLabel);

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

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

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

  334.         latentStateLabelList.add (clientHazardLabel);

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

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

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

  357.         latentStateLabelList.add (clientRecoveryLabel);

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

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

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

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

  440.         latentStateLabelList.add (forwardLabel);

  441.         LatentStateDynamicsContainer latentStateDynamicsContainer = new LatentStateDynamicsContainer();

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

  453.         return latentStateDynamicsContainer;
  454.     }

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

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

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

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

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

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

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

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

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

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

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

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

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

  562.         String fixFloatMaturityTenor = "1Y";
  563.         double fixFloatCoupon = 0.02;
  564.         double fixFloatNotional = -10.e+06;

  565.         double eadMultiplier = 1.;

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

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

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

  581.         LatentStateVertexContainer latentStateVertexContainer = new LatentStateVertexContainer();

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

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

  598.         AndersenPykhtinSokolLag andersenPykhtinSokolLag = AndersenPykhtinSokolLag.ClassicalPlus();

  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.             vertexGenerationPeriodCount,
  613.             1,
  614.             true,
  615.             null
  616.         );

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  676.         ExposureAdjustmentDigest exposureAdjustmentDigest = exposureAdjustmentAggregator.digest();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  705.         System.out.println();

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

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

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

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

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

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