IRFoundationMarginComparison.java

  1. package org.drip.sample.simmcurvature;

  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;

  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.RiskClassAggregateIR;
  10. import org.drip.simm.margin.RiskMeasureAggregateIR;
  11. import org.drip.simm.parameters.RiskClassSensitivitySettingsIR;
  12. import org.drip.simm.product.BucketSensitivityIR;
  13. import org.drip.simm.product.RiskClassSensitivityIR;
  14. import org.drip.simm.product.RiskFactorTenorSensitivity;
  15. import org.drip.simm.product.RiskMeasureSensitivityIR;

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

  19. /*!
  20.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  21.  *
  22.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  23.  *      libraries targeting analysts and developers
  24.  *      https://lakshmidrip.github.io/DRIP/
  25.  *  
  26.  *  DRIP is composed of four main libraries:
  27.  *  
  28.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  29.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  30.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  31.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  32.  *
  33.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  34.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  35.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  36.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  37.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  38.  *
  39.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  40.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  41.  *
  42.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  43.  *
  44.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  45.  *
  46.  *  Licensed under the Apache License, Version 2.0 (the "License");
  47.  *      you may not use this file except in compliance with the License.
  48.  *  
  49.  *  You may obtain a copy of the License at
  50.  *      http://www.apache.org/licenses/LICENSE-2.0
  51.  *  
  52.  *  Unless required by applicable law or agreed to in writing, software
  53.  *      distributed under the License is distributed on an "AS IS" BASIS,
  54.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  55.  *  
  56.  *  See the License for the specific language governing permissions and
  57.  *      limitations under the License.
  58.  */

  59. /**
  60.  * IRFoundationMarginComparison illustrates the Comparison of the IR Margin Estimates using different Schemes
  61.  *  for Calculating the Position-Bucket Curvature Margin. The References are:
  62.  *  
  63.  *  - Andersen, L. B. G., M. Pykhtin, and A. Sokol (2017): Credit Exposure in the Presence of Initial Margin,
  64.  *      https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2806156, eSSRN.
  65.  *  
  66.  *  - Albanese, C., S. Caenazzo, and O. Frankel (2017): Regression Sensitivities for Initial Margin
  67.  *      Calculations, https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2763488, eSSRN.
  68.  *  
  69.  *  - Anfuso, F., D. Aziz, P. Giltinan, and K. Loukopoulus (2017): A Sound Modeling and Back-testing
  70.  *      Framework for Forecasting Initial Margin Requirements,
  71.  *      https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2716279, eSSRN.
  72.  *  
  73.  *  - Caspers, P., P. Giltinan, R. Lichters, and N. Nowaczyk (2017): Forecasting Initial Margin Requirements
  74.  *      - A Model Evaluation https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2911167, eSSRN.
  75.  *  
  76.  *  - International Swaps and Derivatives Association (2017): SIMM v2.0 Methodology,
  77.  *      https://www.isda.org/a/oFiDE/isda-simm-v2.pdf.
  78.  *
  79.  * @author Lakshmi Krishnamurthy
  80.  */

  81. public class IRFoundationMarginComparison
  82. {

  83.     private static final RiskFactorTenorSensitivity CurveTenorSensitivityMap (
  84.         final double notional)
  85.         throws Exception
  86.     {
  87.         Map<String, Double> tenorSensitivityMap = new HashMap<String, Double>();

  88.         tenorSensitivityMap.put (
  89.             "2W",
  90.             notional * (Math.random() - 0.5)
  91.         );

  92.         tenorSensitivityMap.put (
  93.             "1M",
  94.             notional * (Math.random() - 0.5)
  95.         );

  96.         tenorSensitivityMap.put (
  97.             "3M",
  98.             notional * (Math.random() - 0.5)
  99.         );

  100.         tenorSensitivityMap.put (
  101.             "6M",
  102.             notional * (Math.random() - 0.5)
  103.         );

  104.         tenorSensitivityMap.put (
  105.             "1Y",
  106.             notional * (Math.random() - 0.5)
  107.         );

  108.         tenorSensitivityMap.put (
  109.             "2Y",
  110.             notional * (Math.random() - 0.5)
  111.         );

  112.         tenorSensitivityMap.put (
  113.             "3Y",
  114.             notional * (Math.random() - 0.5)
  115.         );

  116.         tenorSensitivityMap.put (
  117.             "5Y",
  118.             notional * (Math.random() - 0.5)
  119.         );

  120.         tenorSensitivityMap.put (
  121.             "10Y",
  122.             notional * (Math.random() - 0.5)
  123.         );

  124.         tenorSensitivityMap.put (
  125.             "15Y",
  126.             notional * (Math.random() - 0.5)
  127.         );

  128.         tenorSensitivityMap.put (
  129.             "20Y",
  130.             notional * (Math.random() - 0.5)
  131.         );

  132.         tenorSensitivityMap.put (
  133.             "30Y",
  134.             notional * (Math.random() - 0.5)
  135.         );

  136.         return new RiskFactorTenorSensitivity (tenorSensitivityMap);
  137.     }

  138.     private static final BucketSensitivityIR CurrencyBucketSensitivity (
  139.         final String currency,
  140.         final double notional)
  141.         throws Exception
  142.     {
  143.         return new BucketSensitivityIR (
  144.             CurveTenorSensitivityMap (notional),
  145.             CurveTenorSensitivityMap (notional),
  146.             CurveTenorSensitivityMap (notional),
  147.             CurveTenorSensitivityMap (notional),
  148.             CurveTenorSensitivityMap (notional),
  149.             CurveTenorSensitivityMap (notional),
  150.             CurveTenorSensitivityMap (notional)
  151.         );
  152.     }

  153.     private static final void ISDABucketCovarianceMargin (
  154.         final String positionBucketCovarianceScheme,
  155.         final Map<String, BucketSensitivityIR> bucketDeltaSensitivityMap,
  156.         final Map<String, BucketSensitivityIR> bucketVegaSensitivityMap,
  157.         final RiskClassSensitivitySettingsIR riskClassSensitivitySettings,
  158.         final MarginEstimationSettings marginEstimationSettings)
  159.         throws Exception
  160.     {
  161.         RiskClassAggregateIR riskClassAggregate = new RiskClassSensitivityIR (
  162.             new RiskMeasureSensitivityIR (bucketDeltaSensitivityMap),
  163.             new RiskMeasureSensitivityIR (bucketVegaSensitivityMap),
  164.             new RiskMeasureSensitivityIR (bucketVegaSensitivityMap)
  165.         ).aggregate (
  166.             riskClassSensitivitySettings,
  167.             marginEstimationSettings
  168.         );

  169.         RiskMeasureAggregateIR deltaRiskMeasureAggregate = riskClassAggregate.deltaMargin();

  170.         RiskMeasureAggregateIR vegaRiskMeasureAggregate = riskClassAggregate.vegaMargin();

  171.         RiskMeasureAggregateIR curvatureRiskMeasureAggregate = riskClassAggregate.curvatureMargin();

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

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

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

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

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

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

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

  191.         System.out.println ("\t| CURVATURE => " +
  192.             FormatUtil.FormatDouble (Math.sqrt (curvatureRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  193.                 " |  " +
  194.             FormatUtil.FormatDouble (Math.sqrt (curvatureRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  195.                 "  |" +
  196.             FormatUtil.FormatDouble (curvatureRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  197.         );

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

  199.         System.out.println();
  200.     }

  201.     public static final void main (
  202.         final String[] inputs)
  203.         throws Exception
  204.     {
  205.         EnvManager.InitEnv ("");

  206.         String[] currencyArray = {
  207.             "USD",
  208.             "EUR",
  209.             "CNY",
  210.             "INR",
  211.             "JPY"
  212.         };

  213.         double[] notionalArray = {
  214.             100.,
  215.             108.,
  216.             119.,
  217.              49.,
  218.              28.
  219.         };

  220.         Map<String, BucketSensitivityIR> bucketDeltaSensitivityMap = new HashMap<String, BucketSensitivityIR>();

  221.         Map<String, BucketSensitivityIR> bucketVegaSensitivityMap = new HashMap<String, BucketSensitivityIR>();

  222.         for (int currencyIndex = 0; currencyIndex < currencyArray.length; ++currencyIndex)
  223.         {
  224.             bucketDeltaSensitivityMap.put (
  225.                 currencyArray[currencyIndex],
  226.                 CurrencyBucketSensitivity (
  227.                     currencyArray[currencyIndex],
  228.                     notionalArray[currencyIndex]
  229.                 )
  230.             );

  231.             bucketVegaSensitivityMap.put (
  232.                 currencyArray[currencyIndex],
  233.                 CurrencyBucketSensitivity (
  234.                     currencyArray[currencyIndex],
  235.                     notionalArray[currencyIndex]
  236.                 )
  237.             );
  238.         }

  239.         List<String> currencyList = new ArrayList<String>();

  240.         for (String currency : currencyArray)
  241.         {
  242.             currencyList.add (currency);
  243.         }

  244.         RiskClassSensitivitySettingsIR riskClassSensitivitySettings =
  245.             RiskClassSensitivitySettingsIR.ISDA_20 (currencyList);

  246.         ISDABucketCovarianceMargin (
  247.             "           FRTB",
  248.             bucketDeltaSensitivityMap,
  249.             bucketVegaSensitivityMap,
  250.             riskClassSensitivitySettings,
  251.             MarginEstimationSettings.FRTB
  252.                 (MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_ISDA)
  253.         );

  254.         ISDABucketCovarianceMargin (
  255.             "     ISDA DELTA",
  256.             bucketDeltaSensitivityMap,
  257.             bucketVegaSensitivityMap,
  258.             riskClassSensitivitySettings,
  259.             MarginEstimationSettings.ISDADelta
  260.                 (MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_ISDA)
  261.         );

  262.         ISDABucketCovarianceMargin (
  263.             "CORNISH FISCHER",
  264.             bucketDeltaSensitivityMap,
  265.             bucketVegaSensitivityMap,
  266.             riskClassSensitivitySettings,
  267.             MarginEstimationSettings.CornishFischer
  268.                 (MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_FRTB)
  269.         );

  270.         EnvManager.TerminateEnv();
  271.     }
  272. }