CRQFoundationMarginComparison.java

  1. package org.drip.sample.simmcurvature;

  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.  * CRQFoundationMarginComparison illustrates the Comparison of the Credit (Qualifying) Margin Estimates using
  60.  *  different Schemes for Calculating the Position-Bucket Curvature Margin. 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 CRQFoundationMarginComparison
  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 ComponentRiskFactorTenorSensitivity (
  136.         final Map<String, RiskFactorTenorSensitivity> tenorSensitivityMap,
  137.         final double notional,
  138.         final String componentName)
  139.         throws Exception
  140.     {
  141.         RiskFactorTenorSensitivity ustRiskFactorSensitivity = CurveTenorSensitivityMap (notional);

  142.         tenorSensitivityMap.put (
  143.             componentName,
  144.             ustRiskFactorSensitivity
  145.         );
  146.     }

  147.     private static final void ISDABucketCovarianceMargin (
  148.         final String positionBucketCovarianceScheme,
  149.         final Map<String, BucketSensitivityCR> bucketDeltaSensitivityMap,
  150.         final Map<String, BucketSensitivityCR> bucketVegaSensitivityMap,
  151.         final RiskClassSensitivitySettingsCR riskClassSensitivitySettings,
  152.         final MarginEstimationSettings marginEstimationSettings)
  153.         throws Exception
  154.     {
  155.         RiskClassAggregateCR riskClassAggregate = new RiskClassSensitivityCR (
  156.             new RiskMeasureSensitivityCR (bucketDeltaSensitivityMap),
  157.             new RiskMeasureSensitivityCR (bucketVegaSensitivityMap),
  158.             new RiskMeasureSensitivityCR (bucketVegaSensitivityMap)
  159.         ).aggregate (
  160.             riskClassSensitivitySettings,
  161.             marginEstimationSettings
  162.         );

  163.         RiskMeasureAggregateCR deltaRiskMeasureAggregate = riskClassAggregate.deltaMargin();

  164.         RiskMeasureAggregateCR vegaRiskMeasureAggregate = riskClassAggregate.vegaMargin();

  165.         RiskMeasureAggregateCR curvatureRiskMeasureAggregate = riskClassAggregate.curvatureMargin();

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

  167.         System.out.println ("\t|       " + positionBucketCovarianceScheme + " SBA MARGIN       ||");

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

  169.         System.out.println ("\t|  MEASURE  =>  CORE  | RESIDUAL | TOTAL ||");

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

  171.         System.out.println ("\t|   DELTA   => " +
  172.             FormatUtil.FormatDouble (Math.sqrt (deltaRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  173.                 " |  " +
  174.             FormatUtil.FormatDouble (Math.sqrt (deltaRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  175.                 "  |" +
  176.             FormatUtil.FormatDouble (deltaRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  177.         );

  178.         System.out.println ("\t|   VEGA    => " +
  179.             FormatUtil.FormatDouble (Math.sqrt (vegaRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  180.                 " |  " +
  181.             FormatUtil.FormatDouble (Math.sqrt (vegaRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  182.                 "  |" +
  183.             FormatUtil.FormatDouble (vegaRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  184.         );

  185.         System.out.println ("\t| CURVATURE => " +
  186.             FormatUtil.FormatDouble (Math.sqrt (curvatureRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  187.                 " |  " +
  188.             FormatUtil.FormatDouble (Math.sqrt (curvatureRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  189.                 "  |" +
  190.             FormatUtil.FormatDouble (curvatureRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  191.         );

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

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

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

  200.         double notional = 100.;
  201.         int[] bucketIndexArray = {
  202.              1,
  203.              2,
  204.              3,
  205.              4,
  206.              5,
  207.              6,
  208.              7,
  209.              8,
  210.              9,
  211.             10,
  212.             11,
  213.             12,
  214.         };
  215.         String[][] bucketComponentGrid = {
  216.             {"01a", "01b", "01c", "01d", "01e", "01f"},
  217.             {"02a", "02b", "02c", "02d", "02e", "02f"},
  218.             {"03a", "03b", "03c", "03d", "03e", "03f"},
  219.             {"04a", "04b", "04c", "04d", "04e", "04f"},
  220.             {"05a", "05b", "05c", "05d", "05e", "05f"},
  221.             {"06a", "06b", "06c", "06d", "06e", "06f"},
  222.             {"07a", "07b", "07c", "07d", "07e", "07f"},
  223.             {"08a", "08b", "08c", "08d", "08e", "08f"},
  224.             {"09a", "09b", "09c", "09d", "09e", "09f"},
  225.             {"10a", "10b", "10c", "10d", "10e", "10f"},
  226.             {"11a", "11b", "11c", "11d", "11e", "11f"},
  227.             {"12a", "12b", "12c", "12d", "12e", "12f"},
  228.         };

  229.         Map<String, BucketSensitivityCR> bucketDeltaSensitivityMap =
  230.             new HashMap<String, BucketSensitivityCR>();

  231.         Map<String, BucketSensitivityCR> bucketVegaSensitivityMap =
  232.             new HashMap<String, BucketSensitivityCR>();

  233.         for (int bucketIndex : bucketIndexArray)
  234.         {
  235.             Map<String, RiskFactorTenorSensitivity> tenorDeltaSensitivityMap = new
  236.                 CaseInsensitiveHashMap<RiskFactorTenorSensitivity>();

  237.             Map<String, RiskFactorTenorSensitivity> tenorVegaSensitivityMap = new
  238.                 CaseInsensitiveHashMap<RiskFactorTenorSensitivity>();

  239.             for (String componentName : bucketComponentGrid[bucketIndex - 1])
  240.             {
  241.                 ComponentRiskFactorTenorSensitivity (
  242.                     tenorDeltaSensitivityMap,
  243.                     notional,
  244.                     componentName
  245.                 );

  246.                 ComponentRiskFactorTenorSensitivity (
  247.                     tenorVegaSensitivityMap,
  248.                     notional,
  249.                     componentName
  250.                 );
  251.             }

  252.             bucketDeltaSensitivityMap.put (
  253.                 "" + bucketIndex,
  254.                 new BucketSensitivityCR (tenorDeltaSensitivityMap)
  255.             );

  256.             bucketVegaSensitivityMap.put (
  257.                 "" + bucketIndex,
  258.                 new BucketSensitivityCR (tenorVegaSensitivityMap)
  259.             );
  260.         }

  261.         RiskClassSensitivitySettingsCR riskClassSensitivitySettings =
  262.             RiskClassSensitivitySettingsCR.ISDA_CRQ_20();

  263.         ISDABucketCovarianceMargin (
  264.             "           FRTB",
  265.             bucketDeltaSensitivityMap,
  266.             bucketVegaSensitivityMap,
  267.             riskClassSensitivitySettings,
  268.             MarginEstimationSettings.FRTB
  269.                 (MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_ISDA)
  270.         );

  271.         ISDABucketCovarianceMargin (
  272.             "     ISDA DELTA",
  273.             bucketDeltaSensitivityMap,
  274.             bucketVegaSensitivityMap,
  275.             riskClassSensitivitySettings,
  276.             MarginEstimationSettings.ISDADelta
  277.                 (MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_ISDA)
  278.         );

  279.         ISDABucketCovarianceMargin (
  280.             "CORNISH FISCHER",
  281.             bucketDeltaSensitivityMap,
  282.             bucketVegaSensitivityMap,
  283.             riskClassSensitivitySettings,
  284.             MarginEstimationSettings.CornishFischer
  285.                 (MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_FRTB)
  286.         );

  287.         EnvManager.TerminateEnv();
  288.     }
  289. }