CreditNonQualifyingBucketCurvatureMarginFlow21.java

  1. package org.drip.sample.simmcrnq;

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

  79. public class CreditNonQualifyingBucketCurvatureMarginFlow21
  80. {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  224.         BucketSensitivityCR bucketSensitivityCR = new BucketSensitivityCR (tenorSensitivityMap);

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

  229.         BucketSensitivitySettingsCR bucketSensitivitySettingsCR =
  230.             BucketCurvatureSettingsCR.ISDA_CRNQ_21 (bucketIndex);

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

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

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

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

  242.         DisplayRiskMeasureAggregate (riskMeasureAggregateCR);

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