CreditQualifyingCurvatureMargin20.java

  1. package org.drip.sample.simmcrq;

  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.  * CreditQualifyingCurvatureMargin20 illustrates the Computation of the CR SIMM 2.0 Curvature Margin for a
  60.  *  Bucket of 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 CreditQualifyingCurvatureMargin20
  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.     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 Vega SBA Variance      => " +
  186.             FormatUtil.FormatDouble (riskMeasureAggregateCR.coreSBAVariance(), 10, 0, 1.) + " ||"
  187.         );

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

  192.         System.out.println (
  193.             "\t|| Vega 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 VegaMarginCovarianceEntry (
  200.         final int bucketIndex,
  201.         final SensitivityAggregateCR crVegaAggregate)
  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 = crVegaAggregate.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.              3,
  238.              4,
  239.              5,
  240.              6,
  241.              7,
  242.              8,
  243.              9,
  244.             10,
  245.             11,
  246.             12,
  247.         };
  248.         String[][] bucketComponentGrid = {
  249.             {"01a", "01b", "01c", "01d", "01e", "01f"},
  250.             {"02a", "02b", "02c", "02d", "02e", "02f"},
  251.             {"03a", "03b", "03c", "03d", "03e", "03f"},
  252.             {"04a", "04b", "04c", "04d", "04e", "04f"},
  253.             {"05a", "05b", "05c", "05d", "05e", "05f"},
  254.             {"06a", "06b", "06c", "06d", "06e", "06f"},
  255.             {"07a", "07b", "07c", "07d", "07e", "07f"},
  256.             {"08a", "08b", "08c", "08d", "08e", "08f"},
  257.             {"09a", "09b", "09c", "09d", "09e", "09f"},
  258.             {"10a", "10b", "10c", "10d", "10e", "10f"},
  259.             {"11a", "11b", "11c", "11d", "11e", "11f"},
  260.             {"12a", "12b", "12c", "12d", "12e", "12f"},
  261.         };

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

  263.         for (int bucketIndex : bucketIndexArray)
  264.         {
  265.             Map<String, RiskFactorTenorSensitivity> tenorSensitivityMap = new
  266.                 CaseInsensitiveHashMap<RiskFactorTenorSensitivity>();

  267.             for (String componentName : bucketComponentGrid[bucketIndex - 1])
  268.             {
  269.                 ComponentRiskFactorTenorSensitivity (
  270.                     tenorSensitivityMap,
  271.                     notional,
  272.                     componentName
  273.                 );
  274.             }

  275.             bucketSensitivityMap.put (
  276.                 "" + bucketIndex,
  277.                 new BucketSensitivityCR (tenorSensitivityMap)
  278.             );
  279.         }

  280.         RiskMeasureSensitivityCR riskClassSensitivity = new RiskMeasureSensitivityCR (bucketSensitivityMap);

  281.         MarginEstimationSettings marginEstimationSettings = MarginEstimationSettings.CornishFischer
  282.             (MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_ISDA);

  283.         RiskMeasureSensitivitySettingsCR riskMeasureSensitivitySettings =
  284.             RiskMeasureSensitivitySettingsCR.ISDA_CRQ_CURVATURE_20();

  285.         RiskMeasureAggregateCR riskMeasureAggregate = riskClassSensitivity.curvatureAggregate (
  286.             riskMeasureSensitivitySettings,
  287.             marginEstimationSettings
  288.         );

  289.         for (int bucketIndex : bucketIndexArray)
  290.         {
  291.             VegaMarginCovarianceEntry (
  292.                 bucketIndex,
  293.                 riskMeasureAggregate.bucketAggregateMap().get ("" + bucketIndex).sensitivityAggregate()
  294.             );
  295.         }

  296.         DisplayRiskMeasureAggregate (riskMeasureAggregate);

  297.         EnvManager.TerminateEnv();
  298.     }
  299. }