CreditQualifyingClassMargin21.java

  1. package org.drip.sample.simmcrq;

  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.  * CreditQualifyingClassMargin21 illustrates the Computation of the SIMM 2.1 CR Class Margin for a Bucket's
  60.  *  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 CreditQualifyingClassMargin21
  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.              3,
  187.              4,
  188.              5,
  189.              6,
  190.              7,
  191.              8,
  192.              9,
  193.             10,
  194.             11,
  195.             12,
  196.         };
  197.         String[][] bucketComponentGrid = {
  198.             {"01a", "01b", "01c", "01d", "01e", "01f"},
  199.             {"02a", "02b", "02c", "02d", "02e", "02f"},
  200.             {"03a", "03b", "03c", "03d", "03e", "03f"},
  201.             {"04a", "04b", "04c", "04d", "04e", "04f"},
  202.             {"05a", "05b", "05c", "05d", "05e", "05f"},
  203.             {"06a", "06b", "06c", "06d", "06e", "06f"},
  204.             {"07a", "07b", "07c", "07d", "07e", "07f"},
  205.             {"08a", "08b", "08c", "08d", "08e", "08f"},
  206.             {"09a", "09b", "09c", "09d", "09e", "09f"},
  207.             {"10a", "10b", "10c", "10d", "10e", "10f"},
  208.             {"11a", "11b", "11c", "11d", "11e", "11f"},
  209.             {"12a", "12b", "12c", "12d", "12e", "12f"},
  210.         };

  211.         Map<String, BucketSensitivityCR> bucketDeltaSensitivityMap =
  212.             new HashMap<String, BucketSensitivityCR>();

  213.         Map<String, BucketSensitivityCR> bucketVegaSensitivityMap =
  214.             new HashMap<String, BucketSensitivityCR>();

  215.         for (int bucketIndex : bucketIndexArray)
  216.         {
  217.             Map<String, RiskFactorTenorSensitivity> tenorDeltaSensitivityMap = new
  218.                 CaseInsensitiveHashMap<RiskFactorTenorSensitivity>();

  219.             Map<String, RiskFactorTenorSensitivity> tenorVegaSensitivityMap = new
  220.                 CaseInsensitiveHashMap<RiskFactorTenorSensitivity>();

  221.             for (String componentName : bucketComponentGrid[bucketIndex - 1])
  222.             {
  223.                 ComponentRiskFactorTenorSensitivity (
  224.                     tenorDeltaSensitivityMap,
  225.                     notional,
  226.                     componentName
  227.                 );

  228.                 ComponentRiskFactorTenorSensitivity (
  229.                     tenorVegaSensitivityMap,
  230.                     notional,
  231.                     componentName
  232.                 );
  233.             }

  234.             bucketDeltaSensitivityMap.put (
  235.                 "" + bucketIndex,
  236.                 new BucketSensitivityCR (tenorDeltaSensitivityMap)
  237.             );

  238.             bucketVegaSensitivityMap.put (
  239.                 "" + bucketIndex,
  240.                 new BucketSensitivityCR (tenorVegaSensitivityMap)
  241.             );
  242.         }

  243.         RiskClassSensitivityCR riskClassSensitivity = new RiskClassSensitivityCR (
  244.             new RiskMeasureSensitivityCR (bucketDeltaSensitivityMap),
  245.             new RiskMeasureSensitivityCR (bucketVegaSensitivityMap),
  246.             new RiskMeasureSensitivityCR (bucketVegaSensitivityMap)
  247.         );

  248.         MarginEstimationSettings marginEstimationSettings = MarginEstimationSettings.CornishFischer
  249.             (MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_ISDA);

  250.         RiskClassSensitivitySettingsCR riskClassSensitivitySettings =
  251.             RiskClassSensitivitySettingsCR.ISDA_CRQ_21();

  252.         RiskClassAggregateCR riskClassAggregate = riskClassSensitivity.aggregate (
  253.             riskClassSensitivitySettings,
  254.             marginEstimationSettings
  255.         );

  256.         RiskMeasureAggregateCR deltaRiskMeasureAggregate = riskClassAggregate.deltaMargin();

  257.         RiskMeasureAggregateCR vegaRiskMeasureAggregate = riskClassAggregate.vegaMargin();

  258.         RiskMeasureAggregateCR curvatureRiskMeasureAggregate = riskClassAggregate.curvatureMargin();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  305.         System.out.println ("\t| CURVATURE MARGIN COMPONENTS => " +
  306.             FormatUtil.FormatDouble (Math.sqrt (curvatureRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  307.                 " | " +
  308.             FormatUtil.FormatDouble (Math.sqrt (curvatureRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  309.                 " | " +
  310.             FormatUtil.FormatDouble (curvatureRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  311.         );

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

  313.         System.out.println();

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

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

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

  319.         EnvManager.TerminateEnv();
  320.     }
  321. }