FXDeltaMargin20.java

  1. package org.drip.sample.simmfx;

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

  14. /*
  15.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  16.  */

  17. /*!
  18.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  19.  *
  20.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  21.  *      libraries targeting analysts and developers
  22.  *      https://lakshmidrip.github.io/DRIP/
  23.  *  
  24.  *  DRIP is composed of four main libraries:
  25.  *  
  26.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  27.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  28.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  29.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  30.  *
  31.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  32.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  33.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  34.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  35.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  36.  *
  37.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  38.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  39.  *
  40.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  41.  *
  42.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  43.  *
  44.  *  Licensed under the Apache License, Version 2.0 (the "License");
  45.  *      you may not use this file except in compliance with the License.
  46.  *  
  47.  *  You may obtain a copy of the License at
  48.  *      http://www.apache.org/licenses/LICENSE-2.0
  49.  *  
  50.  *  Unless required by applicable law or agreed to in writing, software
  51.  *      distributed under the License is distributed on an "AS IS" BASIS,
  52.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  53.  *  
  54.  *  See the License for the specific language governing permissions and
  55.  *      limitations under the License.
  56.  */

  57. /**
  58.  * FXDeltaMargin20 demonstrates the Construction of a Portfolio of FX Delta Sensitivities and their eventual
  59.  *  SIMM 2.0 Margin Computation. The References 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 FXDeltaMargin20
  80. {

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

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

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

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

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

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

  111.         return currencySentivityMap;
  112.     }

  113.     private static final void CategoryRiskFactorSensitivity (
  114.         final Map<String, Map<String, Double>> categorySensitivityMap)
  115.         throws Exception
  116.     {
  117.         System.out.println ("\t|-------------------||");

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

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

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

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

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

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

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

  125.         for (Map.Entry<String, Map<String, Double>> categorySensitivityMapEntry :
  126.             categorySensitivityMap.entrySet())
  127.         {
  128.             String categoryIndex = categorySensitivityMapEntry.getKey();

  129.             Map<String, Double> riskFactorSensitivityMap = categorySensitivityMapEntry.getValue();

  130.             for (Map.Entry<String, Double> riskFactorSensitivityMapEntry :
  131.                 riskFactorSensitivityMap.entrySet())
  132.             {
  133.                 String currency = riskFactorSensitivityMapEntry.getKey();

  134.                 double riskFactorSensitivity = riskFactorSensitivityMapEntry.getValue();

  135.                 System.out.println (
  136.                     "\t| " +
  137.                     currency + " => " +
  138.                     categoryIndex + " | " +
  139.                     FormatUtil.FormatDouble(riskFactorSensitivity, 2, 2, 1.) + " ||"
  140.                 );
  141.             }
  142.         }

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

  144.         System.out.println();
  145.     }

  146.     public static final void main (
  147.         final String[] inputs)
  148.         throws Exception
  149.     {
  150.         EnvManager.InitEnv ("");

  151.         double notional = 100.;

  152.         String[] currencyArray =
  153.         {
  154.             "USD",
  155.             "EUR",
  156.             "JPY",
  157.             "GBP",
  158.             "AUD",
  159.             "CHF",
  160.             "CAD",
  161.             "BRL",
  162.             "CNY",
  163.             "HKD",
  164.             "INR",
  165.             "KRW",
  166.             "MXN",
  167.             "NOK",
  168.             "NZD",
  169.             "RUB",
  170.             "SEK",
  171.             "SGD",
  172.             "TRY",
  173.             "ZAR",
  174.             "PKR",
  175.             "IDR"
  176.         };

  177.         MarginEstimationSettings marginEstimationSettings = MarginEstimationSettings.CornishFischer
  178.             (MarginEstimationSettings.POSITION_PRINCIPAL_COMPONENT_COVARIANCE_ESTIMATOR_ISDA);

  179.         RiskMeasureSensitivitySettings riskMeasureSensitivitySettings =
  180.             RiskMeasureSensitivitySettings.ISDA_FX_DELTA_20();

  181.         Map<String, Map<String, Double>> categorySensitivityMap = CategorySensitivityMap (
  182.             currencyArray,
  183.             notional
  184.         );

  185.         CategoryRiskFactorSensitivity (categorySensitivityMap);

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

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

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

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

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

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

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

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

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

  195.         for (Map.Entry<String, Map<String, Double>> categorySensitivityMapEntry :
  196.             categorySensitivityMap.entrySet())
  197.         {
  198.             String bucketIndex = categorySensitivityMapEntry.getKey();

  199.             BucketSensitivity bucketSensitivity = new BucketSensitivity
  200.                 (categorySensitivityMapEntry.getValue());

  201.             bucketSensitivityMap.put (
  202.                 "" + bucketIndex,
  203.                 bucketSensitivity
  204.             );

  205.             BucketAggregate bucketAggregate = bucketSensitivity.aggregate
  206.                 (riskMeasureSensitivitySettings.bucketSettingsMap().get (bucketIndex));

  207.             System.out.println ("\t| " +
  208.                 bucketIndex + " => " +
  209.                 FormatUtil.FormatDouble (Math.sqrt (bucketAggregate.sensitivityMarginVariance()), 4, 0, 1.) + " | " +
  210.                 FormatUtil.FormatDouble (bucketAggregate.cumulativeSensitivityMargin(), 4, 0, 1.) + " ||"
  211.             );
  212.         }

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

  214.         System.out.println();

  215.         RiskMeasureAggregate riskMeasureAggregate = new
  216.             RiskMeasureSensitivity (bucketSensitivityMap).linearAggregate (
  217.                 riskMeasureSensitivitySettings,
  218.                 marginEstimationSettings
  219.             );

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

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

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

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

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

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

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

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

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

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

  230.         System.out.println ("\t| DELTA MARGIN COMPONENTS => " +
  231.             FormatUtil.FormatDouble (Math.sqrt (riskMeasureAggregate.coreSBAVariance()), 4, 0, 1.) +
  232.                 " | " +
  233.             FormatUtil.FormatDouble (Math.sqrt (riskMeasureAggregate.residualSBAVariance()), 4, 0, 1.) +
  234.                 " | " +
  235.             FormatUtil.FormatDouble (riskMeasureAggregate.sba(), 4, 0, 1.) + " ||"
  236.         );

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

  238.         System.out.println();

  239.         EnvManager.TerminateEnv();
  240.     }
  241. }