CreditNonQualifyingBucketDeltaMarginFlow21.java

  1. package org.drip.sample.simmcrnq;

  2. import java.util.HashMap;
  3. import java.util.Map;

  4. import org.drip.analytics.support.CaseInsensitiveHashMap;
  5. import org.drip.numerical.common.FormatUtil;
  6. import org.drip.service.env.EnvManager;
  7. import org.drip.simm.margin.BucketAggregateCR;
  8. import org.drip.simm.margin.RiskMeasureAggregateCR;
  9. import org.drip.simm.parameters.BucketSensitivitySettingsCR;
  10. import org.drip.simm.product.BucketSensitivityCR;
  11. import org.drip.simm.product.RiskFactorTenorSensitivity;

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

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

  55. /**
  56.  * CreditNonQualifyingBucketDeltaMarginFlow21 illustrates the Steps in the Computation of the SIMM 2.1 Credit
  57.  *  Non-Qualifying Delta Margin for a single CR Bucket's Exposure Sensitivities. The References are:
  58.  *  
  59.  *  - Andersen, L. B. G., M. Pykhtin, and A. Sokol (2017): Credit Exposure in the Presence of Initial Margin,
  60.  *      https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2806156, eSSRN.
  61.  *  
  62.  *  - Albanese, C., S. Caenazzo, and O. Frankel (2017): Regression Sensitivities for Initial Margin
  63.  *      Calculations, https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2763488, eSSRN.
  64.  *  
  65.  *  - Anfuso, F., D. Aziz, P. Giltinan, and K. Loukopoulus (2017): A Sound Modeling and Back-testing
  66.  *      Framework for Forecasting Initial Margin Requirements,
  67.  *      https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2716279, eSSRN.
  68.  *  
  69.  *  - Caspers, P., P. Giltinan, R. Lichters, and N. Nowaczyk (2017): Forecasting Initial Margin Requirements
  70.  *      - A Model Evaluation https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2911167, eSSRN.
  71.  *  
  72.  *  - International Swaps and Derivatives Association (2017): SIMM v2.0 Methodology,
  73.  *      https://www.isda.org/a/oFiDE/isda-simm-v2.pdf.
  74.  *
  75.  * @author Lakshmi Krishnamurthy
  76.  */

  77. public class CreditNonQualifyingBucketDeltaMarginFlow21
  78. {

  79.     private static final void AddTenorSensitivity (
  80.         final Map<String, Double> tenorSensitivityMap,
  81.         final double notional,
  82.         final String tenor)
  83.         throws Exception
  84.     {
  85.         if (tenorSensitivityMap.containsKey (tenor))
  86.         {
  87.             tenorSensitivityMap.put (
  88.                 tenor,
  89.                 tenorSensitivityMap.get (tenor) + notional * (Math.random() - 0.5)
  90.             );
  91.         }
  92.         else
  93.         {
  94.             tenorSensitivityMap.put (
  95.                 tenor,
  96.                 notional * (Math.random() - 0.5)
  97.             );
  98.         }
  99.     }

  100.     private static final RiskFactorTenorSensitivity CurveTenorSensitivityMap (
  101.         final double notional)
  102.         throws Exception
  103.     {
  104.         Map<String, Double> tenorSensitivityMap = new HashMap<String, Double>();

  105.         AddTenorSensitivity (
  106.             tenorSensitivityMap,
  107.             notional,
  108.             "1Y"
  109.         );

  110.         AddTenorSensitivity (
  111.             tenorSensitivityMap,
  112.             notional,
  113.             "2Y"
  114.         );

  115.         AddTenorSensitivity (
  116.             tenorSensitivityMap,
  117.             notional,
  118.             "3Y"
  119.         );

  120.         AddTenorSensitivity (
  121.             tenorSensitivityMap,
  122.             notional,
  123.             "5Y"
  124.         );

  125.         AddTenorSensitivity (
  126.             tenorSensitivityMap,
  127.             notional,
  128.             "10Y"
  129.         );

  130.         return new RiskFactorTenorSensitivity (tenorSensitivityMap);
  131.     }

  132.     private static final void DisplayComponentTenorSensitivity (
  133.         final String componentName,
  134.         final RiskFactorTenorSensitivity tenorSensitivityMap)
  135.         throws Exception
  136.     {
  137.         System.out.println();

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

  139.         System.out.println ("\t|  " + componentName + " DELTA   ||");

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

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

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

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

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

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

  146.         for (Map.Entry<String, Double> tenorSensitivityEntry :
  147.             tenorSensitivityMap.sensitivityMap().entrySet())
  148.         {
  149.             System.out.println (
  150.                 "\t| " +
  151.                 tenorSensitivityEntry.getKey() + " => " +
  152.                 FormatUtil.FormatDouble (tenorSensitivityEntry.getValue(), 2, 2, 1.) + " ||"
  153.             );
  154.         }

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

  156.         System.out.println();
  157.     }

  158.     private static final void ComponentRiskFactorTenorSensitivity (
  159.         final Map<String, RiskFactorTenorSensitivity> tenorSensitivityMap,
  160.         final double notional,
  161.         final String componentName)
  162.         throws Exception
  163.     {
  164.         RiskFactorTenorSensitivity ustRiskFactorSensitivity = CurveTenorSensitivityMap (notional);

  165.         tenorSensitivityMap.put (
  166.             componentName,
  167.             ustRiskFactorSensitivity
  168.         );

  169.         DisplayComponentTenorSensitivity (
  170.             componentName,
  171.             ustRiskFactorSensitivity
  172.         );
  173.     }

  174.     private static final void DisplayRiskMeasureAggregate (
  175.         final RiskMeasureAggregateCR riskMeasureAggregateCR)
  176.         throws Exception
  177.     {
  178.         System.out.println ("\t||--------------------------------------------||");

  179.         System.out.println ("\t||   CR RISK CLASS AGGREGATE MARGIN METRICS   ||");

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

  181.         System.out.println (
  182.             "\t|| Core Delta SBA Variance     => " +
  183.             FormatUtil.FormatDouble (riskMeasureAggregateCR.coreSBAVariance(), 10, 0, 1.) + " ||"
  184.         );

  185.         System.out.println (
  186.             "\t|| Residual Delta SBA Variance => " +
  187.             FormatUtil.FormatDouble (riskMeasureAggregateCR.residualSBAVariance(), 10, 0, 1.) + " ||"
  188.         );

  189.         System.out.println (
  190.             "\t|| Delta SBA                   => " +
  191.             FormatUtil.FormatDouble (riskMeasureAggregateCR.sba(), 10, 0, 1.) + " ||"
  192.         );

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

  194.         System.out.println();
  195.     }

  196.     public static final void main (
  197.         final String[] inputs)
  198.         throws Exception
  199.     {
  200.         EnvManager.InitEnv ("");

  201.         int bucketIndex = 1;
  202.         double notional = 100.;
  203.         String[] componentNameArray = new String[]
  204.         {
  205.             "UST",
  206.             "BND",
  207.             "FRT",
  208.             "ITA",
  209.             "ESP",
  210.             "GLT",
  211.         };

  212.         Map<String, RiskFactorTenorSensitivity> tenorSensitivityMap = new
  213.             CaseInsensitiveHashMap<RiskFactorTenorSensitivity>();

  214.         for (String componentName : componentNameArray)
  215.         {
  216.             ComponentRiskFactorTenorSensitivity (
  217.                 tenorSensitivityMap,
  218.                 notional,
  219.                 componentName
  220.             );
  221.         }

  222.         BucketSensitivityCR bucketSensitivityCR = new BucketSensitivityCR (tenorSensitivityMap);

  223.         DisplayComponentTenorSensitivity (
  224.             "NET",
  225.             bucketSensitivityCR.cumulativeTenorSensitivityMap()
  226.         );

  227.         BucketSensitivitySettingsCR bucketSensitivitySettingsCR =
  228.             BucketSensitivitySettingsCR.ISDA_CRNQ_DELTA_21 (bucketIndex);

  229.         BucketAggregateCR bucketAggregateCR = bucketSensitivityCR.aggregate (bucketSensitivitySettingsCR);

  230.         Map<String, BucketAggregateCR> bucketAggregateCRMap = new HashMap<String, BucketAggregateCR>();

  231.         bucketAggregateCRMap.put (
  232.             "" + bucketIndex,
  233.             bucketAggregateCR
  234.         );

  235.         RiskMeasureAggregateCR riskMeasureAggregateCR = new RiskMeasureAggregateCR (
  236.             bucketAggregateCRMap,
  237.             bucketAggregateCR.sensitivityMarginVariance(),
  238.             0.
  239.         );

  240.         DisplayRiskMeasureAggregate (riskMeasureAggregateCR);

  241.         EnvManager.TerminateEnv();
  242.     }
  243. }