FXFoundationMarginComparison.java

  1. package org.drip.sample.simmcurvature;

  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.fx.FXRiskThresholdContainer20;
  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.  * FXFoundationMarginComparison illustrates the Comparison of the FX Margin Estimates using different Schemes
  60.  *  for Calculating the Position-Bucket Curvature Margin. 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 FXFoundationMarginComparison
  81. {

  82.     private static final Map<String, Map<String, Double>> CategorySensitivityMap (
  83.         final String[] currencyArray,
  84.         final double notional)
  85.         throws Exception
  86.     {
  87.         Map<String, Map<String, Double>> currencySentivityMap = new TreeMap<String, Map<String, Double>>();

  88.         for (String currency : currencyArray)
  89.         {
  90.             int categoryIndex = FXRiskThresholdContainer20.CurrencyCategory (currency);

  91.             if (currencySentivityMap.containsKey ("" + categoryIndex))
  92.             {
  93.                 Map<String, Double> riskFactorSensitivityMap = currencySentivityMap.get ("" + categoryIndex);

  94.                 riskFactorSensitivityMap.put (
  95.                     currency,
  96.                     notional * (Math.random() - 0.5)
  97.                 );
  98.             }
  99.             else
  100.             {
  101.                 Map<String, Double> riskFactorSensitivityMap = new CaseInsensitiveHashMap<Double>();

  102.                 riskFactorSensitivityMap.put (
  103.                     currency,
  104.                     notional * (Math.random() - 0.5)
  105.                 );

  106.                 currencySentivityMap.put (
  107.                     "" + categoryIndex,
  108.                     riskFactorSensitivityMap
  109.                 );
  110.             }
  111.         }

  112.         return currencySentivityMap;
  113.     }

  114.     private static final void AddBucketRiskFactorSensitivity (
  115.         final Map<String, Map<String, Double>> bucketRiskFactorSensitivityMap,
  116.         final String bucketKey,
  117.         final double notional,
  118.         final String[] fxPairArray)
  119.     {
  120.         Map<String, Double> riskFactorSensitivityMap = new CaseInsensitiveHashMap<Double>();

  121.         for (String fxPair : fxPairArray)
  122.         {
  123.             riskFactorSensitivityMap.put (
  124.                 fxPair,
  125.                 notional * (Math.random() - 0.5)
  126.             );
  127.         }

  128.         bucketRiskFactorSensitivityMap.put (
  129.             bucketKey,
  130.             riskFactorSensitivityMap
  131.         );
  132.     }

  133.     private static final Map<String, Map<String, Double>> BucketRiskFactorSensitivityMap (
  134.         final double notional)
  135.         throws Exception
  136.     {
  137.         Map<String, Map<String, Double>> bucketRiskFactorSensitivityMap =
  138.             new TreeMap<String, Map<String, Double>>();

  139.         AddBucketRiskFactorSensitivity (
  140.             bucketRiskFactorSensitivityMap,
  141.             "1__1",
  142.             notional,
  143.             new String[]
  144.             {
  145.                 "USD_EUR",
  146.                 "USD_JPY",
  147.                 "USD_GBP",
  148.                 "USD_AUD",
  149.             }
  150.         );

  151.         AddBucketRiskFactorSensitivity (
  152.             bucketRiskFactorSensitivityMap,
  153.             "1__2",
  154.             notional,
  155.             new String[]
  156.             {
  157.                 "USD_BRL",
  158.                 "USD_CNY",
  159.                 "USD_HKD",
  160.                 "USD_INR",
  161.             }
  162.         );

  163.         AddBucketRiskFactorSensitivity (
  164.             bucketRiskFactorSensitivityMap,
  165.             "2__1",
  166.             notional,
  167.             new String[]
  168.             {
  169.                 "BRL_USD",
  170.                 "CNY_USD",
  171.                 "HKD_USD",
  172.                 "INR_USD",
  173.             }
  174.         );

  175.         AddBucketRiskFactorSensitivity (
  176.             bucketRiskFactorSensitivityMap,
  177.             "2__2",
  178.             notional,
  179.             new String[]
  180.             {
  181.                 "BRL_CNY",
  182.                 "BRL_KDD",
  183.                 "BRL_INR",
  184.                 "BRL_KRW",
  185.             }
  186.         );

  187.         AddBucketRiskFactorSensitivity (
  188.             bucketRiskFactorSensitivityMap,
  189.             "1__3",
  190.             notional,
  191.             new String[]
  192.             {
  193.                 "USD_IDR",
  194.                 "USD_PKR",
  195.                 "USD_SRL",
  196.                 "USD_BNT",
  197.             }
  198.         );

  199.         AddBucketRiskFactorSensitivity (
  200.             bucketRiskFactorSensitivityMap,
  201.             "2__3",
  202.             notional,
  203.             new String[]
  204.             {
  205.                 "BRL_IDR",
  206.                 "BRL_PKR",
  207.                 "BRL_SRL",
  208.                 "BRL_BNT",
  209.             }
  210.         );

  211.         AddBucketRiskFactorSensitivity (
  212.             bucketRiskFactorSensitivityMap,
  213.             "3__1",
  214.             notional,
  215.             new String[]
  216.             {
  217.                 "IDR_USD",
  218.                 "PKR_USD",
  219.                 "SRL_USD",
  220.                 "BNT_USD",
  221.             }
  222.         );

  223.         AddBucketRiskFactorSensitivity (
  224.             bucketRiskFactorSensitivityMap,
  225.             "3__2",
  226.             notional,
  227.             new String[]
  228.             {
  229.                 "IDR_BRL",
  230.                 "PKR_BRL",
  231.                 "SRL_BRL",
  232.                 "BNT_BRL",
  233.             }
  234.         );

  235.         AddBucketRiskFactorSensitivity (
  236.             bucketRiskFactorSensitivityMap,
  237.             "3__3",
  238.             notional,
  239.             new String[]
  240.             {
  241.                 "IDR_PKR",
  242.                 "PKR_SRL",
  243.                 "SRL_IDR",
  244.                 "BNT_SRL",
  245.             }
  246.         );

  247.         return bucketRiskFactorSensitivityMap;
  248.     }

  249.     private static final void ISDABucketCovarianceMargin (
  250.         final String positionBucketCovarianceScheme,
  251.         final Map<String, BucketSensitivity> bucketDeltaSensitivityMap,
  252.         final Map<String, BucketSensitivity> bucketVegaSensitivityMap,
  253.         final RiskClassSensitivitySettings riskClassSensitivitySettings,
  254.         final MarginEstimationSettings marginEstimationSettings)
  255.         throws Exception
  256.     {
  257.         RiskClassAggregate riskClassAggregate = new RiskClassSensitivity (
  258.             new RiskMeasureSensitivity (bucketDeltaSensitivityMap),
  259.             new RiskMeasureSensitivity (bucketVegaSensitivityMap),
  260.             new RiskMeasureSensitivity (bucketVegaSensitivityMap)
  261.         ).aggregate (
  262.             riskClassSensitivitySettings,
  263.             marginEstimationSettings
  264.         );

  265.         RiskMeasureAggregate deltaRiskMeasureAggregate = riskClassAggregate.deltaMargin();

  266.         RiskMeasureAggregate vegaRiskMeasureAggregate = riskClassAggregate.vegaMargin();

  267.         RiskMeasureAggregate curvatureRiskMeasureAggregate = riskClassAggregate.curvatureMargin();

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

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

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

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

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

  273.         System.out.println ("\t|   DELTA   => " +
  274.             FormatUtil.FormatDouble (Math.sqrt (deltaRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  275.                 " |  " +
  276.             FormatUtil.FormatDouble (Math.sqrt (deltaRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  277.                 "  |" +
  278.             FormatUtil.FormatDouble (deltaRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  279.         );

  280.         System.out.println ("\t|   VEGA    => " +
  281.             FormatUtil.FormatDouble (Math.sqrt (vegaRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  282.                 " |  " +
  283.             FormatUtil.FormatDouble (Math.sqrt (vegaRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  284.                 "  |" +
  285.             FormatUtil.FormatDouble (vegaRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  286.         );

  287.         System.out.println ("\t| CURVATURE => " +
  288.             FormatUtil.FormatDouble (Math.sqrt (curvatureRiskMeasureAggregate.coreSBAVariance()), 5, 0, 1.) +
  289.                 " |  " +
  290.             FormatUtil.FormatDouble (Math.sqrt (curvatureRiskMeasureAggregate.residualSBAVariance()), 5, 0, 1.) +
  291.                 "  |" +
  292.             FormatUtil.FormatDouble (curvatureRiskMeasureAggregate.sba(), 5, 0, 1.) + " ||"
  293.         );

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

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

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

  302.         double notional = 100.;
  303.         int vegaDurationDays = 365;

  304.         String[] currencyArray =
  305.         {
  306.             "USD",
  307.             "EUR",
  308.             "JPY",
  309.             "GBP",
  310.             "AUD",
  311.             "CHF",
  312.             "CAD",
  313.             "BRL",
  314.             "CNY",
  315.             "HKD",
  316.             "INR",
  317.             "KRW",
  318.             "MXN",
  319.             "NOK",
  320.             "NZD",
  321.             "RUB",
  322.             "SEK",
  323.             "SGD",
  324.             "TRY",
  325.             "ZAR",
  326.             "PKR",
  327.             "IDR"
  328.         };

  329.         RiskClassSensitivitySettings riskClassSensitivitySettings = RiskClassSensitivitySettings.ISDA_FX_20
  330.             (vegaDurationDays);

  331.         Map<String, Map<String, Double>> bucketDeltaMap = CategorySensitivityMap (
  332.             currencyArray,
  333.             notional
  334.         );

  335.         Map<String, BucketSensitivity> bucketDeltaSensitivityMap = new TreeMap<String, BucketSensitivity>();

  336.         for (Map.Entry<String, Map<String, Double>> deltaCategoryMapEntry : bucketDeltaMap.entrySet())
  337.         {
  338.             bucketDeltaSensitivityMap.put (
  339.                 deltaCategoryMapEntry.getKey(),
  340.                 new BucketSensitivity (deltaCategoryMapEntry.getValue())
  341.             );
  342.         }

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

  344.         Map<String, BucketSensitivity> bucketVegaSensitivityMap = new TreeMap<String, BucketSensitivity>();

  345.         for (Map.Entry<String, Map<String, Double>> bucketVegaMapEntry : bucketVegaMap.entrySet())
  346.         {
  347.             bucketVegaSensitivityMap.put (
  348.                 bucketVegaMapEntry.getKey(),
  349.                 new BucketSensitivity (bucketVegaMapEntry.getValue())
  350.             );
  351.         }

  352.         ISDABucketCovarianceMargin (
  353.             "           FRTB",
  354.             bucketDeltaSensitivityMap,
  355.             bucketVegaSensitivityMap,
  356.             riskClassSensitivitySettings,
  357.             MarginEstimationSettings.FRTB
  358.                 (MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_ISDA)
  359.         );

  360.         ISDABucketCovarianceMargin (
  361.             "     ISDA DELTA",
  362.             bucketDeltaSensitivityMap,
  363.             bucketVegaSensitivityMap,
  364.             riskClassSensitivitySettings,
  365.             MarginEstimationSettings.ISDADelta
  366.                 (MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_ISDA)
  367.         );

  368.         ISDABucketCovarianceMargin (
  369.             "CORNISH FISCHER",
  370.             bucketDeltaSensitivityMap,
  371.             bucketVegaSensitivityMap,
  372.             riskClassSensitivitySettings,
  373.             MarginEstimationSettings.CornishFischer
  374.                 (MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_FRTB)
  375.         );

  376.         EnvManager.TerminateEnv();
  377.     }
  378. }