EQMarginComparison.java

  1. package org.drip.sample.simmvariance;

  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.  * EQMarginComparison illustrates the Comparison of the Equity Margin Estimates using difference Schemes for
  60.  *  Calculating the Position-Bucket Principal Component Co-variance. 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 EQMarginComparison
  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.     private static final void ISDABucketCovarianceMargin (
  266.         final String positionBucketCovarianceScheme,
  267.         final Map<String, BucketSensitivity> bucketDeltaSensitivityMap,
  268.         final Map<String, BucketSensitivity> bucketVegaSensitivityMap,
  269.         final RiskClassSensitivitySettings riskClassSensitivitySettings,
  270.         final MarginEstimationSettings marginEstimationSettings)
  271.         throws Exception
  272.     {
  273.         RiskClassAggregate riskClassAggregate = new RiskClassSensitivity (
  274.             new RiskMeasureSensitivity (bucketDeltaSensitivityMap),
  275.             new RiskMeasureSensitivity (bucketVegaSensitivityMap),
  276.             new RiskMeasureSensitivity (bucketVegaSensitivityMap)
  277.         ).aggregate (
  278.             riskClassSensitivitySettings,
  279.             marginEstimationSettings
  280.         );

  281.         RiskMeasureAggregate deltaRiskMeasureAggregate = riskClassAggregate.deltaMargin();

  282.         RiskMeasureAggregate vegaRiskMeasureAggregate = riskClassAggregate.vegaMargin();

  283.         RiskMeasureAggregate curvatureRiskMeasureAggregate = riskClassAggregate.curvatureMargin();

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

  285.         System.out.println ("\t|               " + positionBucketCovarianceScheme + " SBA MARGIN          ||");

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

  287.         System.out.println ("\t|  MEASURE  =>  CORE  | RESIDUAL | TOTAL ||");

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

  289.         System.out.println ("\t|   DELTA   => " +
  290.             FormatUtil.FormatDouble (Math.sqrt (deltaRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  291.                 " |  " +
  292.             FormatUtil.FormatDouble (Math.sqrt (deltaRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  293.                 "  |" +
  294.             FormatUtil.FormatDouble (deltaRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  295.         );

  296.         System.out.println ("\t|   VEGA    => " +
  297.             FormatUtil.FormatDouble (Math.sqrt (vegaRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  298.                 " |  " +
  299.             FormatUtil.FormatDouble (Math.sqrt (vegaRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  300.                 "  |" +
  301.             FormatUtil.FormatDouble (vegaRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  302.         );

  303.         System.out.println ("\t| CURVATURE => " +
  304.             FormatUtil.FormatDouble (Math.sqrt (curvatureRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  305.                 " |  " +
  306.             FormatUtil.FormatDouble (Math.sqrt (curvatureRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  307.                 "  |" +
  308.             FormatUtil.FormatDouble (curvatureRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  309.         );

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

  311.         System.out.println();
  312.     }

  313.     public static final void main (
  314.         final String[] inputArray)
  315.         throws Exception
  316.     {
  317.         EnvManager.InitEnv ("");

  318.         double notional = 100.;
  319.         int vegaDurationDays = 365;

  320.         RiskClassSensitivitySettings riskClassSensitivitySettings = RiskClassSensitivitySettings.ISDA_EQ_20
  321.             (vegaDurationDays);

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

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

  324.         for (Map.Entry<String, Map<String, Double>> bucketDeltaMapEntry : bucketDeltaMap.entrySet())
  325.         {
  326.             bucketDeltaSensitivityMap.put (
  327.                 bucketDeltaMapEntry.getKey(),
  328.                 new BucketSensitivity (bucketDeltaMapEntry.getValue())
  329.             );
  330.         }

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

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

  333.         for (Map.Entry<String, Map<String, Double>> bucketVegaMapEntry : bucketVegaMap.entrySet())
  334.         {
  335.             bucketVegaSensitivityMap.put (
  336.                 bucketVegaMapEntry.getKey(),
  337.                 new BucketSensitivity (bucketVegaMapEntry.getValue())
  338.             );
  339.         }

  340.         ISDABucketCovarianceMargin (
  341.             MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_ISDA,
  342.             bucketDeltaSensitivityMap,
  343.             bucketVegaSensitivityMap,
  344.             riskClassSensitivitySettings,
  345.             MarginEstimationSettings.CornishFischer
  346.                 (MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_ISDA)
  347.         );

  348.         ISDABucketCovarianceMargin (
  349.             MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_FRTB,
  350.             bucketDeltaSensitivityMap,
  351.             bucketVegaSensitivityMap,
  352.             riskClassSensitivitySettings,
  353.             MarginEstimationSettings.CornishFischer
  354.                 (MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_FRTB)
  355.         );

  356.         EnvManager.TerminateEnv();
  357.     }
  358. }