CreditNonQualifyingDeltaMargin20.java

  1. package org.drip.sample.simmcrnq;

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

  5. import org.drip.analytics.support.CaseInsensitiveHashMap;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.service.env.EnvManager;
  8. import org.drip.simm.foundation.MarginEstimationSettings;
  9. import org.drip.simm.margin.RiskMeasureAggregateCR;
  10. import org.drip.simm.margin.SensitivityAggregateCR;
  11. import org.drip.simm.parameters.RiskMeasureSensitivitySettingsCR;
  12. import org.drip.simm.product.BucketSensitivityCR;
  13. import org.drip.simm.product.RiskFactorTenorSensitivity;
  14. import org.drip.simm.product.RiskMeasureSensitivityCR;

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

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

  80. public class CreditNonQualifyingDeltaMargin20
  81. {

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

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

  108.         AddTenorSensitivity (
  109.             tenorSensitivityMap,
  110.             notional,
  111.             "1Y"
  112.         );

  113.         AddTenorSensitivity (
  114.             tenorSensitivityMap,
  115.             notional,
  116.             "2Y"
  117.         );

  118.         AddTenorSensitivity (
  119.             tenorSensitivityMap,
  120.             notional,
  121.             "3Y"
  122.         );

  123.         AddTenorSensitivity (
  124.             tenorSensitivityMap,
  125.             notional,
  126.             "5Y"
  127.         );

  128.         AddTenorSensitivity (
  129.             tenorSensitivityMap,
  130.             notional,
  131.             "10Y"
  132.         );

  133.         return new RiskFactorTenorSensitivity (tenorSensitivityMap);
  134.     }

  135.     private static final void DisplayComponentTenorSensitivity (
  136.         final String componentName,
  137.         final RiskFactorTenorSensitivity tenorSensitivityMap)
  138.         throws Exception
  139.     {
  140.         System.out.println();

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

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

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

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

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

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

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

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

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

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

  159.         System.out.println();
  160.     }

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

  168.         tenorSensitivityMap.put (
  169.             componentName,
  170.             ustRiskFactorSensitivity
  171.         );

  172.         DisplayComponentTenorSensitivity (
  173.             componentName,
  174.             ustRiskFactorSensitivity
  175.         );
  176.     }

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

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

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

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

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

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

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

  197.         System.out.println();
  198.     }

  199.     private static final void DeltaMarginCovarianceEntry (
  200.         final int bucketIndex,
  201.         final SensitivityAggregateCR crDeltaAggregate)
  202.         throws Exception
  203.     {
  204.         System.out.println ("\t||-------------------------------------||");

  205.         System.out.println (
  206.             "\t|| " + FormatUtil.FormatDouble (bucketIndex, 2, 0, 1.) +
  207.             "  RISK FACTOR MARGIN COVARIANCE  ||"
  208.         );

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

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

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

  212.         System.out.println ("\t||        - Component Pair             ||");

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

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

  215.         Map<String, Double> componentMarginCovarianceMap = crDeltaAggregate.componentMarginCovarianceMap();

  216.         Set<String> componentPairSet = componentMarginCovarianceMap.keySet();

  217.         for (String componentPair : componentPairSet)
  218.         {
  219.             System.out.println (
  220.                 "\t|| " + componentPair + " => " +
  221.                 FormatUtil.FormatDouble (componentMarginCovarianceMap.get (componentPair), 9, 0, 1.) +
  222.                     "               ||"
  223.             );
  224.         }

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

  226.         System.out.println();
  227.     }

  228.     public static final void main (
  229.         final String[] argumentArray)
  230.         throws Exception
  231.     {
  232.         EnvManager.InitEnv ("");

  233.         double notional = 100.;
  234.         int[] bucketIndexArray = {
  235.              1,
  236.              2,
  237.         };
  238.         String[][] bucketComponentGrid = {
  239.             {"01a", "01b", "01c", "01d", "01e", "01f"},
  240.             {"02a", "02b", "02c", "02d", "02e", "02f"},
  241.         };

  242.         Map<String, BucketSensitivityCR> bucketSensitivityMap = new HashMap<String, BucketSensitivityCR>();

  243.         for (int bucketIndex : bucketIndexArray)
  244.         {
  245.             Map<String, RiskFactorTenorSensitivity> tenorSensitivityMap = new
  246.                 CaseInsensitiveHashMap<RiskFactorTenorSensitivity>();

  247.             for (String componentName : bucketComponentGrid[bucketIndex - 1])
  248.             {
  249.                 ComponentRiskFactorTenorSensitivity (
  250.                     tenorSensitivityMap,
  251.                     notional,
  252.                     componentName
  253.                 );
  254.             }

  255.             bucketSensitivityMap.put (
  256.                 "" + bucketIndex,
  257.                 new BucketSensitivityCR (tenorSensitivityMap)
  258.             );
  259.         }

  260.         RiskMeasureSensitivityCR riskClassSensitivity = new RiskMeasureSensitivityCR (bucketSensitivityMap);

  261.         MarginEstimationSettings marginEstimationSettings = MarginEstimationSettings.CornishFischer
  262.             (MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_ISDA);

  263.         RiskMeasureSensitivitySettingsCR riskMeasureSensitivitySettings =
  264.             RiskMeasureSensitivitySettingsCR.ISDA_CRNQ_DELTA_20();

  265.         RiskMeasureAggregateCR riskMeasureAggregate = riskClassSensitivity.linearAggregate (
  266.             riskMeasureSensitivitySettings,
  267.             marginEstimationSettings
  268.         );

  269.         for (int bucketIndex : bucketIndexArray)
  270.         {
  271.             DeltaMarginCovarianceEntry (
  272.                 bucketIndex,
  273.                 riskMeasureAggregate.bucketAggregateMap().get ("" + bucketIndex).sensitivityAggregate()
  274.             );
  275.         }

  276.         DisplayRiskMeasureAggregate (riskMeasureAggregate);

  277.         EnvManager.TerminateEnv();
  278.     }
  279. }