CreditNonQualifyingClassMargin21.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.foundation.MarginEstimationSettings;
  8. import org.drip.simm.margin.RiskClassAggregateCR;
  9. import org.drip.simm.margin.RiskMeasureAggregateCR;
  10. import org.drip.simm.parameters.RiskClassSensitivitySettingsCR;
  11. import org.drip.simm.product.BucketSensitivityCR;
  12. import org.drip.simm.product.RiskClassSensitivityCR;
  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.  * CreditNonQualifyingClassMargin21 illustrates the Computation of the SIMM 2.1 CR Class Margin for a
  60.  *  Bucket's 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 CreditNonQualifyingClassMargin21
  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 + " VEGA    ||");

  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.     public static final void main (
  178.         final String[] argumentArray)
  179.         throws Exception
  180.     {
  181.         EnvManager.InitEnv ("");

  182.         double notional = 100.;
  183.         int[] bucketIndexArray = {
  184.              1,
  185.              2,
  186.         };
  187.         String[][] bucketComponentGrid = {
  188.             {"01a", "01b", "01c", "01d", "01e", "01f"},
  189.             {"02a", "02b", "02c", "02d", "02e", "02f"},
  190.         };

  191.         Map<String, BucketSensitivityCR> bucketDeltaSensitivityMap =
  192.             new HashMap<String, BucketSensitivityCR>();

  193.         Map<String, BucketSensitivityCR> bucketVegaSensitivityMap =
  194.             new HashMap<String, BucketSensitivityCR>();

  195.         for (int bucketIndex : bucketIndexArray)
  196.         {
  197.             Map<String, RiskFactorTenorSensitivity> tenorDeltaSensitivityMap = new
  198.                 CaseInsensitiveHashMap<RiskFactorTenorSensitivity>();

  199.             Map<String, RiskFactorTenorSensitivity> tenorVegaSensitivityMap = new
  200.                 CaseInsensitiveHashMap<RiskFactorTenorSensitivity>();

  201.             for (String componentName : bucketComponentGrid[bucketIndex - 1])
  202.             {
  203.                 ComponentRiskFactorTenorSensitivity (
  204.                     tenorDeltaSensitivityMap,
  205.                     notional,
  206.                     componentName
  207.                 );

  208.                 ComponentRiskFactorTenorSensitivity (
  209.                     tenorVegaSensitivityMap,
  210.                     notional,
  211.                     componentName
  212.                 );
  213.             }

  214.             bucketDeltaSensitivityMap.put (
  215.                 "" + bucketIndex,
  216.                 new BucketSensitivityCR (tenorDeltaSensitivityMap)
  217.             );

  218.             bucketVegaSensitivityMap.put (
  219.                 "" + bucketIndex,
  220.                 new BucketSensitivityCR (tenorVegaSensitivityMap)
  221.             );
  222.         }

  223.         RiskClassSensitivityCR riskClassSensitivity = new RiskClassSensitivityCR (
  224.             new RiskMeasureSensitivityCR (bucketDeltaSensitivityMap),
  225.             new RiskMeasureSensitivityCR (bucketVegaSensitivityMap),
  226.             new RiskMeasureSensitivityCR (bucketVegaSensitivityMap)
  227.         );

  228.         MarginEstimationSettings marginEstimationSettings = MarginEstimationSettings.CornishFischer
  229.             (MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_ISDA);

  230.         RiskClassSensitivitySettingsCR riskClassSensitivitySettings =
  231.             RiskClassSensitivitySettingsCR.ISDA_CRNQ_21();

  232.         RiskClassAggregateCR riskClassAggregate = riskClassSensitivity.aggregate (
  233.             riskClassSensitivitySettings,
  234.             marginEstimationSettings
  235.         );

  236.         RiskMeasureAggregateCR deltaRiskMeasureAggregate = riskClassAggregate.deltaMargin();

  237.         RiskMeasureAggregateCR vegaRiskMeasureAggregate = riskClassAggregate.vegaMargin();

  238.         RiskMeasureAggregateCR curvatureRiskMeasureAggregate = riskClassAggregate.curvatureMargin();

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

  240.         System.out.println ("\t|               SBA BASED DELTA MARGIN                ||");

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

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

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

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

  245.         System.out.println ("\t|            - Core Delta SBA Margin                  ||");

  246.         System.out.println ("\t|            - Residual Delta SBA Margin              ||");

  247.         System.out.println ("\t|            - SBA Delta Margin                       ||");

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

  249.         System.out.println ("\t| DELTA MARGIN COMPONENTS => " +
  250.             FormatUtil.FormatDouble (Math.sqrt (deltaRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  251.                 " | " +
  252.             FormatUtil.FormatDouble (Math.sqrt (deltaRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  253.                 " | " +
  254.             FormatUtil.FormatDouble (deltaRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  255.         );

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

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

  258.         System.out.println ("\t|               SBA BASED VEGA MARGIN                 ||");

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

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

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

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

  263.         System.out.println ("\t|            - Core Vega SBA Margin                   ||");

  264.         System.out.println ("\t|            - Residual Vega SBA Margin               ||");

  265.         System.out.println ("\t|            - SBA Vega Margin                        ||");

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

  267.         System.out.println ("\t| VEGA MARGIN COMPONENTS  => " +
  268.             FormatUtil.FormatDouble (Math.sqrt (vegaRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  269.                 " | " +
  270.             FormatUtil.FormatDouble (Math.sqrt (vegaRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  271.                 " | " +
  272.             FormatUtil.FormatDouble (vegaRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  273.         );

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

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

  276.         System.out.println ("\t|                 SBA BASED CURVATURE MARGIN              ||");

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

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

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

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

  281.         System.out.println ("\t|            - Core Curvature SBA Margin                  ||");

  282.         System.out.println ("\t|            - Residual Curvature SBA Margin              ||");

  283.         System.out.println ("\t|            - SBA Curvature Margin                       ||");

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

  285.         System.out.println ("\t| CURVATURE MARGIN COMPONENTS => " +
  286.             FormatUtil.FormatDouble (Math.sqrt (curvatureRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  287.                 " | " +
  288.             FormatUtil.FormatDouble (Math.sqrt (curvatureRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  289.                 " | " +
  290.             FormatUtil.FormatDouble (curvatureRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  291.         );

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

  293.         System.out.println();

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

  295.         System.out.println (
  296.             "\t| TOTAL MARGIN => " +
  297.             FormatUtil.FormatDouble (riskClassAggregate.margin(), 5, 0, 1.) + " ||");

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

  299.         EnvManager.TerminateEnv();
  300.     }
  301. }