RatesClassMargin21.java

  1. package org.drip.sample.simmir;

  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.  * RatesClassMargin21 illustrates the Computation of the SIMM 2.1 IR Class Margin for a Currency Bucket's IR
  61.  *  Exposure Sensitivities. 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 RatesClassMargin21
  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.     public static final void main (
  154.         final String[] inputs)
  155.         throws Exception
  156.     {
  157.         EnvManager.InitEnv ("");

  158.         String[] currencyArray = {
  159.             "USD",
  160.             "EUR",
  161.             "CNY",
  162.             "INR",
  163.             "JPY"
  164.         };

  165.         double[] notionalArray = {
  166.             100.,
  167.             108.,
  168.             119.,
  169.              49.,
  170.              28.
  171.         };

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

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

  174.         for (int currencyIndex = 0; currencyIndex < currencyArray.length; ++currencyIndex)
  175.         {
  176.             bucketDeltaSensitivityMap.put (
  177.                 currencyArray[currencyIndex],
  178.                 CurrencyBucketSensitivity (
  179.                     currencyArray[currencyIndex],
  180.                     notionalArray[currencyIndex]
  181.                 )
  182.             );

  183.             bucketVegaSensitivityMap.put (
  184.                 currencyArray[currencyIndex],
  185.                 CurrencyBucketSensitivity (
  186.                     currencyArray[currencyIndex],
  187.                     notionalArray[currencyIndex]
  188.                 )
  189.             );
  190.         }

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

  192.         for (String currency : currencyArray)
  193.         {
  194.             currencyList.add (currency);
  195.         }

  196.         MarginEstimationSettings marginEstimationSettings = MarginEstimationSettings.CornishFischer
  197.             (MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_ISDA);

  198.         RiskClassSensitivitySettingsIR riskClassSensitivitySettingsIR =
  199.             RiskClassSensitivitySettingsIR.ISDA_21 (currencyList);

  200.         RiskClassAggregateIR riskClassAggregate = new RiskClassSensitivityIR (
  201.             new RiskMeasureSensitivityIR (bucketDeltaSensitivityMap),
  202.             new RiskMeasureSensitivityIR (bucketVegaSensitivityMap),
  203.             new RiskMeasureSensitivityIR (bucketVegaSensitivityMap)
  204.         ).aggregate (
  205.             riskClassSensitivitySettingsIR,
  206.             marginEstimationSettings
  207.         );

  208.         RiskMeasureAggregateIR deltaRiskMeasureAggregate = riskClassAggregate.deltaMargin();

  209.         RiskMeasureAggregateIR vegaRiskMeasureAggregate = riskClassAggregate.vegaMargin();

  210.         RiskMeasureAggregateIR curvatureRiskMeasureAggregate = riskClassAggregate.curvatureMargin();

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

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

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

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

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

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

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

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

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

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

  221.         System.out.println ("\t| DELTA MARGIN COMPONENTS => " +
  222.             FormatUtil.FormatDouble (Math.sqrt (deltaRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  223.                 " | " +
  224.             FormatUtil.FormatDouble (Math.sqrt (deltaRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  225.                 " | " +
  226.             FormatUtil.FormatDouble (deltaRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  227.         );

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

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

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

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

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

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

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

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

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

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

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

  239.         System.out.println ("\t| VEGA MARGIN COMPONENTS  => " +
  240.             FormatUtil.FormatDouble (Math.sqrt (vegaRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  241.                 " | " +
  242.             FormatUtil.FormatDouble (Math.sqrt (vegaRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  243.                 " | " +
  244.             FormatUtil.FormatDouble (vegaRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  245.         );

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

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

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

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

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

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

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

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

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

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

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

  257.         System.out.println ("\t| CURVATURE MARGIN COMPONENTS => " +
  258.             FormatUtil.FormatDouble (Math.sqrt (curvatureRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  259.                 " | " +
  260.             FormatUtil.FormatDouble (Math.sqrt (curvatureRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  261.                 " | " +
  262.             FormatUtil.FormatDouble (curvatureRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  263.         );

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

  265.         System.out.println();

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

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

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

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