PortfolioCollateralEstimate.java

  1. package org.drip.sample.xva;

  2. import org.drip.analytics.date.*;
  3. import org.drip.exposure.mpor.CollateralAmountEstimator;
  4. import org.drip.exposure.mpor.CollateralAmountEstimatorOutput;
  5. import org.drip.measure.bridge.BrokenDateInterpolatorLinearT;
  6. import org.drip.measure.discrete.SequenceGenerator;
  7. import org.drip.measure.dynamics.DiffusionEvaluatorLinear;
  8. import org.drip.measure.process.DiffusionEvolver;
  9. import org.drip.measure.realization.*;
  10. import org.drip.numerical.common.FormatUtil;
  11. import org.drip.service.env.EnvManager;
  12. import org.drip.xva.proto.*;
  13. import org.drip.xva.settings.*;

  14. /*
  15.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  16.  */

  17. /*!
  18.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  19.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  20.  *
  21.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  22.  *      libraries targeting analysts and developers
  23.  *      https://lakshmidrip.github.io/DRIP/
  24.  *  
  25.  *  DRIP is composed of four main libraries:
  26.  *  
  27.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  28.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  29.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  30.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  31.  *
  32.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  33.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  34.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  35.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  36.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  37.  *
  38.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  39.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  40.  *
  41.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  42.  *
  43.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  44.  *
  45.  *  Licensed under the Apache License, Version 2.0 (the "License");
  46.  *      you may not use this file except in compliance with the License.
  47.  *  
  48.  *  You may obtain a copy of the License at
  49.  *      http://www.apache.org/licenses/LICENSE-2.0
  50.  *  
  51.  *  Unless required by applicable law or agreed to in writing, software
  52.  *      distributed under the License is distributed on an "AS IS" BASIS,
  53.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  54.  *  
  55.  *  See the License for the specific language governing permissions and
  56.  *      limitations under the License.
  57.  */

  58. /**
  59.  * PortfolioCollateralEstimate illustrates the Estimation of the Collateral Amount on a Single Trade Collateral
  60.  *  Portfolio. The References are:
  61.  *  
  62.  *  - Burgard, C., and M. Kjaer (2014): PDE Representations of Derivatives with Bilateral Counter-party Risk
  63.  *      and Funding Costs, Journal of Credit Risk, 7 (3) 1-19.
  64.  *  
  65.  *  - Burgard, C., and M. Kjaer (2014): In the Balance, Risk, 24 (11) 72-75.
  66.  *  
  67.  *  - Gregory, J. (2009): Being Two-faced over Counter-party Credit Risk, Risk 20 (2) 86-90.
  68.  *  
  69.  *  - Li, B., and Y. Tang (2007): Quantitative Analysis, Derivatives Modeling, and Trading Strategies in the
  70.  *      Presence of Counter-party Credit Risk for the Fixed Income Market, World Scientific Publishing,
  71.  *      Singapore.
  72.  *
  73.  *  - Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives Pricing, Risk
  74.  *      21 (2) 97-102.
  75.  *
  76.  * @author Lakshmi Krishnamurthy
  77.  */

  78. public class PortfolioCollateralEstimate {

  79.     public static final void main (
  80.         final String[] astrArgs)
  81.         throws Exception
  82.     {
  83.         EnvManager.InitEnv ("");

  84.         int iNumStep = 40;
  85.         double dblTime = 10.;
  86.         double dblPortfolioDrift = 0.0;
  87.         double dblPortfolioVolatility = 0.15;
  88.         double dblPortfolioValueStart = 0.;
  89.         double dblBankThreshold = -0.1;
  90.         double dblCounterPartyThreshold = 0.1;

  91.         JulianDate dtSpot = DateUtil.Today();

  92.         JulianDate dtStart = dtSpot;
  93.         double dblTimeWidth = dblTime / iNumStep;
  94.         double[] adblTimeWidth = new double[iNumStep];

  95.         for (int i = 0; i < iNumStep; ++i)
  96.             adblTimeWidth[i] = dblTimeWidth;

  97.         PositionGroupSpecification positionGroupSpecification = PositionGroupSpecification.FixedThreshold (
  98.             "FIXEDTHRESHOLD",
  99.             dblCounterPartyThreshold,
  100.             dblBankThreshold,
  101.             PositionReplicationScheme.ALBANESE_ANDERSEN_VERTEX,
  102.             BrokenDateScheme.SQUARE_ROOT_OF_TIME,
  103.             0.,
  104.             CloseOutScheme.ISDA_92
  105.         );

  106.         DiffusionEvolver dePortfolio = new DiffusionEvolver (
  107.             DiffusionEvaluatorLinear.Standard (
  108.                 dblPortfolioDrift,
  109.                 dblPortfolioVolatility
  110.             )
  111.         );

  112.         JumpDiffusionEdge[] aJDESwapRate = dePortfolio.incrementSequence (
  113.             new JumpDiffusionVertex (
  114.                 dblTime,
  115.                 dblPortfolioValueStart,
  116.                 0.,
  117.                 false
  118.             ),
  119.             JumpDiffusionEdgeUnit.Diffusion (
  120.                 adblTimeWidth,
  121.                 SequenceGenerator.Gaussian (iNumStep)
  122.             ),
  123.             dblTimeWidth
  124.         );

  125.         System.out.println();

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

  127.         System.out.println ("\t||                                       COLLATERAL AMOUNT ESTIMATION OUTPUT METRICS                                        ||");

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

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

  130.         System.out.println ("\t||            - Forward Date                                                                                                ||");

  131.         System.out.println ("\t||            - Forward Value                                                                                               ||");

  132.         System.out.println ("\t||            - Bank Margin Date                                                                                            ||");

  133.         System.out.println ("\t||            - Counter Party Margin Date                                                                                   ||");

  134.         System.out.println ("\t||            - Bank Window Margin Value                                                                                    ||");

  135.         System.out.println ("\t||            - Counter Party Window Margin Value                                                                           ||");

  136.         System.out.println ("\t||            - Bank Collateral Threshold                                                                                   ||");

  137.         System.out.println ("\t||            - Counter Party Collateral Threshold                                                                          ||");

  138.         System.out.println ("\t||            - Bank Posting Requirement                                                                                    ||");

  139.         System.out.println ("\t||            - Counter Party Posting Requirement                                                                           ||");

  140.         System.out.println ("\t||            - Gross Posting Requirement                                                                                   ||");

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

  142.         for (int i = 0; i < iNumStep; ++i) {
  143.             JulianDate dtEnd = dtStart.addMonths (3);

  144.             double dblPortfolioValueFinish = dblTimeWidth * (iNumStep - i) * aJDESwapRate[i].finish();

  145.             CollateralAmountEstimator hae = new CollateralAmountEstimator (
  146.                 positionGroupSpecification,
  147.                 new BrokenDateInterpolatorLinearT (
  148.                     dtStart.julian(),
  149.                     dtEnd.julian(),
  150.                     dblPortfolioValueStart,
  151.                     dblPortfolioValueFinish
  152.                 ),
  153.                 Double.NaN
  154.             );

  155.             CollateralAmountEstimatorOutput haeo = hae.output (dtEnd);

  156.             System.out.println (
  157.                 "\t|| " +
  158.                 dtEnd + " => " +
  159.                 FormatUtil.FormatDouble (dblPortfolioValueFinish, 1, 4, 1.) + " | " +
  160.                 haeo.dealerMarginDate() + " | " +
  161.                 haeo.clientMarginDate() + " | " +
  162.                 FormatUtil.FormatDouble (haeo.dealerWindowMarginValue(), 1, 4, 1.) + " | " +
  163.                 FormatUtil.FormatDouble (haeo.clientWindowMarginValue(), 1, 4, 1.) + " | " +
  164.                 FormatUtil.FormatDouble (haeo.dealerCollateralThreshold(), 1, 4, 1.) + " | " +
  165.                 FormatUtil.FormatDouble (haeo.clientCollateralThreshold(), 1, 4, 1.) + " | " +
  166.                 FormatUtil.FormatDouble (haeo.dealerPostingRequirement(), 1, 4, 1.) + " | " +
  167.                 FormatUtil.FormatDouble (haeo.clientPostingRequirement(), 1, 4, 1.) + " | " +
  168.                 FormatUtil.FormatDouble (haeo.postingRequirement(), 1, 4, 1.) + " ||"
  169.             );

  170.             dtStart = dtEnd;
  171.             dblPortfolioValueStart = dblPortfolioValueFinish;
  172.         }

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

  174.         System.out.println();

  175.         EnvManager.TerminateEnv();
  176.     }
  177. }