EquityClassMargin20.java

  1. package org.drip.sample.simmeq;

  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.TreeMap;

  5. import org.drip.analytics.support.CaseInsensitiveHashMap;
  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.RiskClassAggregate;
  10. import org.drip.simm.margin.RiskMeasureAggregate;
  11. import org.drip.simm.parameters.RiskClassSensitivitySettings;
  12. import org.drip.simm.product.BucketSensitivity;
  13. import org.drip.simm.product.RiskClassSensitivity;
  14. import org.drip.simm.product.RiskMeasureSensitivity;

  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.  * EquityClassMargin20 illustrates the Computation of the ISDA 2.0 Aggregate Margin for across a Group of
  60.  *  Equity Bucket Exposure Sensitivities. 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 EquityClassMargin20
  81. {

  82.     private static final void AddBucketRiskFactorSensitivity (
  83.         final Map<String, Map<String, Double>> bucketRiskFactorSensitivityMap,
  84.         final int bucketIndex,
  85.         final double notional,
  86.         final String[] equityArray)
  87.     {
  88.         Map<String, Double> riskFactorSensitivityMap = new CaseInsensitiveHashMap<Double>();

  89.         for (String equity : equityArray)
  90.         {
  91.             riskFactorSensitivityMap.put (
  92.                 equity,
  93.                 notional * (Math.random() - 0.5)
  94.             );
  95.         }

  96.         bucketRiskFactorSensitivityMap.put (
  97.             "" + bucketIndex,
  98.             riskFactorSensitivityMap
  99.         );
  100.     }

  101.     private static final Map<String, Map<String, Double>> BucketRiskFactorSensitivityMap (
  102.         final double notional)
  103.         throws Exception
  104.     {
  105.         Map<String, Map<String, Double>> bucketRiskFactorSensitivityMap =
  106.             new TreeMap<String, Map<String, Double>>();

  107.         AddBucketRiskFactorSensitivity (
  108.             bucketRiskFactorSensitivityMap,
  109.             -1,
  110.             notional,
  111.             new String[]
  112.             {
  113.                 "BOEING  ",
  114.                 "LOCKHEED",
  115.                 "RAND    ",
  116.                 "RAYTHEON",
  117.             }
  118.         );

  119.         AddBucketRiskFactorSensitivity (
  120.             bucketRiskFactorSensitivityMap,
  121.             1,
  122.             notional,
  123.             new String[]
  124.             {
  125.                 "ADP     ",
  126.                 "PSEANDG ",
  127.                 "STAPLES ",
  128.                 "U-HAUL  ",
  129.             }
  130.         );

  131.         AddBucketRiskFactorSensitivity (
  132.             bucketRiskFactorSensitivityMap,
  133.             2,
  134.             notional,
  135.             new String[]
  136.             {
  137.                 "CISCO   ",
  138.                 "DEERE   ",
  139.                 "HALIBTN ",
  140.                 "VERIZON ",
  141.             }
  142.         );

  143.         AddBucketRiskFactorSensitivity (
  144.             bucketRiskFactorSensitivityMap,
  145.             3,
  146.             notional,
  147.             new String[]
  148.             {
  149.                 "DUKE    ",
  150.                 "MONSANTO",
  151.                 "MMM     ",
  152.                 "VEDANTA ",
  153.             }
  154.         );

  155.         AddBucketRiskFactorSensitivity (
  156.             bucketRiskFactorSensitivityMap,
  157.             4,
  158.             notional,
  159.             new String[]
  160.             {
  161.                 "AMAZON  ",
  162.                 "GOLDMAN ",
  163.                 "MORGAN  ",
  164.                 "REMAX   ",
  165.             }
  166.         );

  167.         AddBucketRiskFactorSensitivity (
  168.             bucketRiskFactorSensitivityMap,
  169.             5,
  170.             notional,
  171.             new String[]
  172.             {
  173.                 "ALDI    ",
  174.                 "INFOSYS ",
  175.                 "OLLA    ",
  176.                 "RELIANCE",
  177.             }
  178.         );

  179.         AddBucketRiskFactorSensitivity (
  180.             bucketRiskFactorSensitivityMap,
  181.             6,
  182.             notional,
  183.             new String[]
  184.             {
  185.                 "GCC     ",
  186.                 "NOKIA   ",
  187.                 "SIEMENS ",
  188.                 "VODAFONE",
  189.             }
  190.         );

  191.         AddBucketRiskFactorSensitivity (
  192.             bucketRiskFactorSensitivityMap,
  193.             7,
  194.             notional,
  195.             new String[]
  196.             {
  197.                 "ADIDAS  ",
  198.                 "BAYER   ",
  199.                 "BILLERTN",
  200.                 "DE BEER ",
  201.             }
  202.         );

  203.         AddBucketRiskFactorSensitivity (
  204.             bucketRiskFactorSensitivityMap,
  205.             8,
  206.             notional,
  207.             new String[]
  208.             {
  209.                 "NOKIA   ",
  210.                 "NOMURA  ",
  211.                 "QATARSOV",
  212.                 "SOTHEBY ",
  213.             }
  214.         );

  215.         AddBucketRiskFactorSensitivity (
  216.             bucketRiskFactorSensitivityMap,
  217.             9,
  218.             notional,
  219.             new String[]
  220.             {
  221.                 "AUTODESK",
  222.                 "CALYPSO ",
  223.                 "NUMERIX ",
  224.                 "WEBLOGIC",
  225.             }
  226.         );

  227.         AddBucketRiskFactorSensitivity (
  228.             bucketRiskFactorSensitivityMap,
  229.             10,
  230.             notional,
  231.             new String[]
  232.             {
  233.                 "COGNIZAN",
  234.                 "TATAMOTO",
  235.                 "TOBLERON",
  236.                 "TVS     ",
  237.             }
  238.         );

  239.         AddBucketRiskFactorSensitivity (
  240.             bucketRiskFactorSensitivityMap,
  241.             11,
  242.             notional,
  243.             new String[]
  244.             {
  245.                 "DJIA    ",
  246.                 "LEHMAN  ",
  247.                 "RUSSELL ",
  248.                 "SANDP   ",
  249.             }
  250.         );

  251.         AddBucketRiskFactorSensitivity (
  252.             bucketRiskFactorSensitivityMap,
  253.             12,
  254.             notional,
  255.             new String[]
  256.             {
  257.                 "CBOE    ",
  258.                 "CITI    ",
  259.                 "RUSSELL ",
  260.                 "VIX     ",
  261.             }
  262.         );

  263.         return bucketRiskFactorSensitivityMap;
  264.     }

  265.     public static final void main (
  266.         final String[] inputArray)
  267.         throws Exception
  268.     {
  269.         EnvManager.InitEnv ("");

  270.         double notional = 100.;
  271.         int vegaDurationDays = 365;

  272.         MarginEstimationSettings marginEstimationSettings = MarginEstimationSettings.CornishFischer
  273.             (MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_ISDA);

  274.         RiskClassSensitivitySettings riskClassSensitivitySettings = RiskClassSensitivitySettings.ISDA_EQ_20
  275.             (vegaDurationDays);

  276.         Map<String, Map<String, Double>> bucketDeltaMap = BucketRiskFactorSensitivityMap (notional);

  277.         Map<String, BucketSensitivity> bucketDeltaSensitivityMap = new HashMap<String, BucketSensitivity>();

  278.         for (Map.Entry<String, Map<String, Double>> bucketDeltaMapEntry : bucketDeltaMap.entrySet())
  279.         {
  280.             bucketDeltaSensitivityMap.put (
  281.                 bucketDeltaMapEntry.getKey(),
  282.                 new BucketSensitivity (bucketDeltaMapEntry.getValue())
  283.             );
  284.         }

  285.         Map<String, Map<String, Double>> bucketVegaMap = BucketRiskFactorSensitivityMap (notional);

  286.         Map<String, BucketSensitivity> bucketVegaSensitivityMap = new HashMap<String, BucketSensitivity>();

  287.         for (Map.Entry<String, Map<String, Double>> bucketVegaMapEntry : bucketVegaMap.entrySet())
  288.         {
  289.             bucketVegaSensitivityMap.put (
  290.                 bucketVegaMapEntry.getKey(),
  291.                 new BucketSensitivity (bucketVegaMapEntry.getValue())
  292.             );
  293.         }

  294.         RiskClassAggregate riskClassAggregate = new RiskClassSensitivity (
  295.             new RiskMeasureSensitivity (bucketDeltaSensitivityMap),
  296.             new RiskMeasureSensitivity (bucketVegaSensitivityMap),
  297.             new RiskMeasureSensitivity (bucketVegaSensitivityMap)
  298.         ).aggregate (
  299.             riskClassSensitivitySettings,
  300.             marginEstimationSettings
  301.         );

  302.         RiskMeasureAggregate deltaRiskMeasureAggregate = riskClassAggregate.deltaMargin();

  303.         RiskMeasureAggregate vegaRiskMeasureAggregate = riskClassAggregate.vegaMargin();

  304.         RiskMeasureAggregate curvatureRiskMeasureAggregate = riskClassAggregate.curvatureMargin();

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

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

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

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

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

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

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

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

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

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

  315.         System.out.println ("\t| DELTA MARGIN COMPONENTS => " +
  316.             FormatUtil.FormatDouble (Math.sqrt (deltaRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  317.                 " | " +
  318.             FormatUtil.FormatDouble (Math.sqrt (deltaRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  319.                 " | " +
  320.             FormatUtil.FormatDouble (deltaRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  321.         );

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

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

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

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

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

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

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

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

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

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

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

  333.         System.out.println ("\t| VEGA MARGIN COMPONENTS  => " +
  334.             FormatUtil.FormatDouble (Math.sqrt (vegaRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  335.                 " | " +
  336.             FormatUtil.FormatDouble (Math.sqrt (vegaRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  337.                 " | " +
  338.             FormatUtil.FormatDouble (vegaRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  339.         );

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

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

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

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

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

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

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

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

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

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

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

  351.         System.out.println ("\t| CURVATURE MARGIN COMPONENTS => " +
  352.             FormatUtil.FormatDouble (Math.sqrt (curvatureRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  353.                 " | " +
  354.             FormatUtil.FormatDouble (Math.sqrt (curvatureRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  355.                 " | " +
  356.             FormatUtil.FormatDouble (curvatureRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  357.         );

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

  359.         System.out.println();

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

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

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

  365.         System.out.println();

  366.         EnvManager.TerminateEnv();
  367.     }
  368. }