CreditQualifyingBucketCurvatureMarginFlow21.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.margin.BucketAggregateCR;
  8. import org.drip.simm.margin.RiskMeasureAggregateCR;
  9. import org.drip.simm.parameters.BucketCurvatureSettingsCR;
  10. import org.drip.simm.parameters.BucketSensitivitySettingsCR;
  11. import org.drip.simm.product.BucketSensitivityCR;
  12. import org.drip.simm.product.RiskFactorTenorSensitivity;

  13. /*
  14.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  15.  */

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

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

  78. public class CreditQualifyingBucketCurvatureMarginFlow21
  79. {

  80.     private static final void AddTenorSensitivity (
  81.         final Map<String, Double> tenorSensitivityMap,
  82.         final double notional,
  83.         final String tenor)
  84.         throws Exception
  85.     {
  86.         if (tenorSensitivityMap.containsKey (tenor))
  87.         {
  88.             tenorSensitivityMap.put (
  89.                 tenor,
  90.                 tenorSensitivityMap.get (tenor) + notional * (Math.random() - 0.5)
  91.             );
  92.         }
  93.         else
  94.         {
  95.             tenorSensitivityMap.put (
  96.                 tenor,
  97.                 notional * (Math.random() - 0.5)
  98.             );
  99.         }
  100.     }

  101.     private static final RiskFactorTenorSensitivity CurveTenorSensitivityMap (
  102.         final double notional)
  103.         throws Exception
  104.     {
  105.         Map<String, Double> tenorSensitivityMap = new HashMap<String, Double>();

  106.         AddTenorSensitivity (
  107.             tenorSensitivityMap,
  108.             notional,
  109.             "1Y"
  110.         );

  111.         AddTenorSensitivity (
  112.             tenorSensitivityMap,
  113.             notional,
  114.             "2Y"
  115.         );

  116.         AddTenorSensitivity (
  117.             tenorSensitivityMap,
  118.             notional,
  119.             "3Y"
  120.         );

  121.         AddTenorSensitivity (
  122.             tenorSensitivityMap,
  123.             notional,
  124.             "5Y"
  125.         );

  126.         AddTenorSensitivity (
  127.             tenorSensitivityMap,
  128.             notional,
  129.             "10Y"
  130.         );

  131.         return new RiskFactorTenorSensitivity (tenorSensitivityMap);
  132.     }

  133.     private static final void DisplayComponentTenorSensitivity (
  134.         final String componentName,
  135.         final RiskFactorTenorSensitivity tenorSensitivityMap)
  136.         throws Exception
  137.     {
  138.         System.out.println();

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

  140.         System.out.println ("\t|  " + componentName + " VEGA    ||");

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

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

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

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

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

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

  147.         for (Map.Entry<String, Double> tenorSensitivityEntry :
  148.             tenorSensitivityMap.sensitivityMap().entrySet())
  149.         {
  150.             System.out.println (
  151.                 "\t| " +
  152.                 tenorSensitivityEntry.getKey() + " => " +
  153.                 FormatUtil.FormatDouble (tenorSensitivityEntry.getValue(), 2, 2, 1.) + " ||"
  154.             );
  155.         }

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

  157.         System.out.println();
  158.     }

  159.     private static final void ComponentRiskFactorTenorSensitivity (
  160.         final Map<String, RiskFactorTenorSensitivity> tenorSensitivityMap,
  161.         final double notional,
  162.         final String componentName)
  163.         throws Exception
  164.     {
  165.         RiskFactorTenorSensitivity ustRiskFactorSensitivity = CurveTenorSensitivityMap (notional);

  166.         tenorSensitivityMap.put (
  167.             componentName,
  168.             ustRiskFactorSensitivity
  169.         );

  170.         DisplayComponentTenorSensitivity (
  171.             componentName,
  172.             ustRiskFactorSensitivity
  173.         );
  174.     }

  175.     private static final void DisplayRiskMeasureAggregate (
  176.         final RiskMeasureAggregateCR riskMeasureAggregateCR)
  177.         throws Exception
  178.     {
  179.         System.out.println ("\t||--------------------------------------------||");

  180.         System.out.println ("\t||   CR RISK CLASS AGGREGATE MARGIN METRICS   ||");

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

  182.         System.out.println (
  183.             "\t|| Core Vega SBA Variance      => " +
  184.             FormatUtil.FormatDouble (riskMeasureAggregateCR.coreSBAVariance(), 10, 0, 1.) + " ||"
  185.         );

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

  190.         System.out.println (
  191.             "\t|| Vega SBA                    => " +
  192.             FormatUtil.FormatDouble (riskMeasureAggregateCR.sba(), 10, 0, 1.) + " ||"
  193.         );

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

  195.         System.out.println();
  196.     }

  197.     public static final void main (
  198.         final String[] inputs)
  199.         throws Exception
  200.     {
  201.         EnvManager.InitEnv ("");

  202.         int bucketIndex = 1;
  203.         double notional = 100.;
  204.         String[] componentNameArray = new String[]
  205.         {
  206.             "UST",
  207.             "BND",
  208.             "FRT",
  209.             "ITA",
  210.             "ESP",
  211.             "GLT",
  212.         };

  213.         Map<String, RiskFactorTenorSensitivity> tenorSensitivityMap = new
  214.             CaseInsensitiveHashMap<RiskFactorTenorSensitivity>();

  215.         for (String componentName : componentNameArray)
  216.         {
  217.             ComponentRiskFactorTenorSensitivity (
  218.                 tenorSensitivityMap,
  219.                 notional,
  220.                 componentName
  221.             );
  222.         }

  223.         BucketSensitivityCR bucketSensitivityCR = new BucketSensitivityCR (tenorSensitivityMap);

  224.         DisplayComponentTenorSensitivity (
  225.             "NET",
  226.             bucketSensitivityCR.cumulativeTenorSensitivityMap()
  227.         );

  228.         BucketSensitivitySettingsCR bucketSensitivitySettingsCR =
  229.             BucketCurvatureSettingsCR.ISDA_CRQ_21 (bucketIndex);

  230.         BucketAggregateCR bucketAggregateCR = bucketSensitivityCR.aggregate (bucketSensitivitySettingsCR);

  231.         Map<String, BucketAggregateCR> bucketAggregateCRMap = new HashMap<String, BucketAggregateCR>();

  232.         bucketAggregateCRMap.put (
  233.             "" + bucketIndex,
  234.             bucketAggregateCR
  235.         );

  236.         RiskMeasureAggregateCR riskMeasureAggregateCR = new RiskMeasureAggregateCR (
  237.             bucketAggregateCRMap,
  238.             bucketAggregateCR.sensitivityMarginVariance(),
  239.             0.
  240.         );

  241.         DisplayRiskMeasureAggregate (riskMeasureAggregateCR);

  242.         EnvManager.TerminateEnv();
  243.     }
  244. }