EquityDeltaMargin21.java

  1. package org.drip.sample.simmeq;

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

  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.foundation.MarginEstimationSettings;
  8. import org.drip.simm.margin.BucketAggregate;
  9. import org.drip.simm.margin.RiskMeasureAggregate;
  10. import org.drip.simm.parameters.RiskMeasureSensitivitySettings;
  11. import org.drip.simm.product.BucketSensitivity;
  12. import org.drip.simm.product.RiskMeasureSensitivity;

  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.  * EquityDeltaMargin21 illustrates the Computation of the SIMM 2.1 Delta Margin across a Group of Equity
  58.  *  Bucket 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 EquityDeltaMargin21
  79. {

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

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

  94.         bucketRiskFactorSensitivityMap.put (
  95.             "" + bucketIndex,
  96.             riskFactorSensitivityMap
  97.         );
  98.     }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  261.         return bucketRiskFactorSensitivityMap;
  262.     }

  263.     private static final void DisplayBucketRiskFactorSensitivity (
  264.         final Map<String, Map<String, Double>> bucketRiskFactorSensitivityMap)
  265.         throws Exception
  266.     {
  267.         System.out.println ("\t|--------------------------||");

  268.         System.out.println ("\t|    RISK FACTOR DELTA     ||");

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

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

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

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

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

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

  275.         for (Map.Entry<String, Map<String, Double>> bucketSensitivityMapEntry :
  276.             bucketRiskFactorSensitivityMap.entrySet())
  277.         {
  278.             String bucketIndex = bucketSensitivityMapEntry.getKey();

  279.             Map<String, Double> riskFactorSensitivityMap = bucketSensitivityMapEntry.getValue();

  280.             for (Map.Entry<String, Double> riskFactorSensitivityMapEntry :
  281.                 riskFactorSensitivityMap.entrySet())
  282.             {
  283.                 String currency = riskFactorSensitivityMapEntry.getKey();

  284.                 double riskFactorSensitivity = riskFactorSensitivityMapEntry.getValue();

  285.                 System.out.println (
  286.                     "\t| " +
  287.                     currency + " => " +
  288.                     FormatUtil.FormatDouble (Integer.parseInt (bucketIndex), 2, 0, 1.) + " | " +
  289.                     FormatUtil.FormatDouble (riskFactorSensitivity, 2, 2, 1.) + " ||"
  290.                 );
  291.             }
  292.         }

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

  294.         System.out.println();
  295.     }

  296.     public static final void main (
  297.         final String[] inputs)
  298.         throws Exception
  299.     {
  300.         EnvManager.InitEnv ("");

  301.         double notional = 100.;

  302.         MarginEstimationSettings marginEstimationSettings = MarginEstimationSettings.CornishFischer
  303.             (MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_ISDA);

  304.         RiskMeasureSensitivitySettings riskMeasureSensitivitySettings =
  305.             RiskMeasureSensitivitySettings.ISDA_EQ_DELTA_21();

  306.         Map<String, Map<String, Double>> bucketRiskFactorSensitivityMap = BucketRiskFactorSensitivityMap
  307.             (notional);

  308.         DisplayBucketRiskFactorSensitivity (bucketRiskFactorSensitivityMap);

  309.         Map<String, BucketSensitivity> bucketSensitivityMap = new TreeMap<String, BucketSensitivity>();

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

  311.         System.out.println ("\t|    BUCKET AGGREGATE    ||");

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

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

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

  315.         System.out.println ("\t|    - Bucket Margin     ||");

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

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

  318.         for (Map.Entry<String, Map<String, Double>> bucketSensitivityMapEntry :
  319.             bucketRiskFactorSensitivityMap.entrySet())
  320.         {
  321.             String bucketIndex = bucketSensitivityMapEntry.getKey();

  322.             BucketSensitivity bucketSensitivity = new BucketSensitivity
  323.                 (bucketSensitivityMapEntry.getValue());

  324.             bucketSensitivityMap.put (
  325.                 "" + bucketIndex,
  326.                 bucketSensitivity
  327.             );

  328.             BucketAggregate bucketAggregate = bucketSensitivity.aggregate
  329.                 (riskMeasureSensitivitySettings.bucketSettingsMap().get (bucketIndex));

  330.             System.out.println ("\t| " +
  331.                 FormatUtil.FormatDouble (Integer.parseInt (bucketIndex), 2, 0, 1.) + " => " +
  332.                 FormatUtil.FormatDouble (Math.sqrt (bucketAggregate.sensitivityMarginVariance()), 5, 0, 1.) + " | " +
  333.                 FormatUtil.FormatDouble (bucketAggregate.cumulativeSensitivityMargin(), 5, 0, 1.) + " ||"
  334.             );
  335.         }

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

  337.         System.out.println();

  338.         RiskMeasureAggregate riskMeasureAggregate = new RiskMeasureSensitivity
  339.             (bucketSensitivityMap).linearAggregate (
  340.                 riskMeasureSensitivitySettings,
  341.                 marginEstimationSettings
  342.             );

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

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

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

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

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

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

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

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

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

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

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

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

  361.         EnvManager.TerminateEnv();
  362.     }
  363. }