ProductClassMargin.java

  1. package org.drip.simm.estimator;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  9.  *
  10.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  11.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  12.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  13.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  14.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  15.  *      and computational support.
  16.  *  
  17.  *      https://lakshmidrip.github.io/DROP/
  18.  *  
  19.  *  DROP is composed of three modules:
  20.  *  
  21.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  22.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  23.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  24.  *
  25.  *  DROP Product Core implements libraries for the following:
  26.  *  - Fixed Income Analytics
  27.  *  - Loan Analytics
  28.  *  - Transaction Cost Analytics
  29.  *
  30.  *  DROP Portfolio Core implements libraries for the following:
  31.  *  - Asset Allocation Analytics
  32.  *  - Asset Liability Management Analytics
  33.  *  - Capital Estimation Analytics
  34.  *  - Exposure Analytics
  35.  *  - Margin Analytics
  36.  *  - XVA Analytics
  37.  *
  38.  *  DROP Computational Core implements libraries for the following:
  39.  *  - Algorithm Support
  40.  *  - Computation Support
  41.  *  - Function Analysis
  42.  *  - Model Validation
  43.  *  - Numerical Analysis
  44.  *  - Numerical Optimizer
  45.  *  - Spline Builder
  46.  *  - Statistical Learning
  47.  *
  48.  *  Documentation for DROP is Spread Over:
  49.  *
  50.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  51.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  52.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  53.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  54.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  55.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  56.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  57.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  58.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  59.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  60.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  61.  *
  62.  *  Licensed under the Apache License, Version 2.0 (the "License");
  63.  *      you may not use this file except in compliance with the License.
  64.  *  
  65.  *  You may obtain a copy of the License at
  66.  *      http://www.apache.org/licenses/LICENSE-2.0
  67.  *  
  68.  *  Unless required by applicable law or agreed to in writing, software
  69.  *      distributed under the License is distributed on an "AS IS" BASIS,
  70.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  71.  *  
  72.  *  See the License for the specific language governing permissions and
  73.  *      limitations under the License.
  74.  */

  75. /**
  76.  * <i>ProductClassMargin</i> holds the Initial Margin Estimates for a Single Product Class across the Six
  77.  * Risk Factors - Interest Rate, Credit Qualifying, Credit Non-Qualifying, Equity, Commodity, and FX. The
  78.  * References are:
  79.  *
  80.  * <br><br>
  81.  *  <ul>
  82.  *      <li>
  83.  *          Andersen, L. B. G., M. Pykhtin, and A. Sokol (2017): Credit Exposure in the Presence of Initial
  84.  *              Margin https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2806156 <b>eSSRN</b>
  85.  *      </li>
  86.  *      <li>
  87.  *          Albanese, C., S. Caenazzo, and O. Frankel (2017): Regression Sensitivities for Initial Margin
  88.  *              Calculations https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2763488 <b>eSSRN</b>
  89.  *      </li>
  90.  *      <li>
  91.  *          Anfuso, F., D. Aziz, P. Giltinan, and K. Loukopoulus (2017): A Sound Modeling and Back-testing
  92.  *              Framework for Forecasting Initial Margin Requirements
  93.  *                  https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2716279 <b>eSSRN</b>
  94.  *      </li>
  95.  *      <li>
  96.  *          Caspers, P., P. Giltinan, R. Lichters, and N. Nowaczyk (2017): Forecasting Initial Margin
  97.  *              Requirements - A Model Evaluation https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2911167
  98.  *                  <b>eSSRN</b>
  99.  *      </li>
  100.  *      <li>
  101.  *          International Swaps and Derivatives Association (2017): SIMM v2.0 Methodology
  102.  *              https://www.isda.org/a/oFiDE/isda-simm-v2.pdf
  103.  *      </li>
  104.  *  </ul>
  105.  *
  106.  * <br><br>
  107.  *  <ul>
  108.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  109.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/MarginAnalyticsLibrary.md">Initial and Variation Margin Analytics</a></li>
  110.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/simm/README.md">Initial Margin Analytics based on ISDA SIMM and its Variants</a></li>
  111.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/simm/estimator/README.md">ISDA SIMM Core + Add-On Estimator</a></li>
  112.  *  </ul>
  113.  * <br><br>
  114.  *
  115.  * @author Lakshmi Krishnamurthy
  116.  */

  117. public class ProductClassMargin
  118. {
  119.     private org.drip.simm.margin.RiskClassAggregate _fxRiskClassAggregate = null;
  120.     private org.drip.simm.margin.RiskClassAggregateIR _irRiskClassAggregate = null;
  121.     private org.drip.simm.margin.RiskClassAggregate _equityRiskClassAggregate = null;
  122.     private org.drip.simm.margin.RiskClassAggregate _commodityRiskClassAggregate = null;
  123.     private org.drip.simm.margin.RiskClassAggregateCR _creditQualifyingRiskClassAggregate = null;
  124.     private org.drip.simm.margin.RiskClassAggregateCR _creditNonQualifyingRiskClassAggregate = null;

  125.     /**
  126.      * ProductClassMargin Constructor
  127.      *
  128.      * @param irRiskClassAggregate IR Risk Class Aggregate
  129.      * @param creditQualifyingRiskClassAggregate Credit Qualifying Risk Class Aggregate
  130.      * @param creditNonQualifyingRiskClassAggregate Credit Non-Qualifying Risk Class Aggregate
  131.      * @param equityRiskClassAggregate Equity Risk Class Aggregate
  132.      * @param fxRiskClassAggregate FX Risk Class Aggregate
  133.      * @param commodityRiskClassAggregate Commodity Risk Class Aggregate
  134.      *
  135.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  136.      */

  137.     public ProductClassMargin (
  138.         final org.drip.simm.margin.RiskClassAggregateIR irRiskClassAggregate,
  139.         final org.drip.simm.margin.RiskClassAggregateCR creditQualifyingRiskClassAggregate,
  140.         final org.drip.simm.margin.RiskClassAggregateCR creditNonQualifyingRiskClassAggregate,
  141.         final org.drip.simm.margin.RiskClassAggregate equityRiskClassAggregate,
  142.         final org.drip.simm.margin.RiskClassAggregate fxRiskClassAggregate,
  143.         final org.drip.simm.margin.RiskClassAggregate commodityRiskClassAggregate)
  144.         throws java.lang.Exception
  145.     {
  146.         _irRiskClassAggregate = irRiskClassAggregate;
  147.         _fxRiskClassAggregate = fxRiskClassAggregate;
  148.         _equityRiskClassAggregate = equityRiskClassAggregate;
  149.         _commodityRiskClassAggregate = commodityRiskClassAggregate;
  150.         _creditQualifyingRiskClassAggregate = creditQualifyingRiskClassAggregate;
  151.         _creditNonQualifyingRiskClassAggregate = creditNonQualifyingRiskClassAggregate;

  152.         if ((null == _equityRiskClassAggregate &&
  153.             null == _commodityRiskClassAggregate &&
  154.             null == _fxRiskClassAggregate &&
  155.             null == _irRiskClassAggregate &&
  156.             null == _creditQualifyingRiskClassAggregate &&
  157.             null == _creditNonQualifyingRiskClassAggregate))
  158.         {
  159.             throw new java.lang.Exception ("ProductClassMargin => Invalid Inputs");
  160.         }
  161.     }

  162.     /**
  163.      * Retrieve the Interest Rate Risk Class Aggregate
  164.      *
  165.      * @return The Interest Rate Risk Class Aggregate
  166.      */

  167.     public org.drip.simm.margin.RiskClassAggregateIR irRiskClassAggregate()
  168.     {
  169.         return _irRiskClassAggregate;
  170.     }

  171.     /**
  172.      * Retrieve the Credit Qualifying Risk Class Aggregate
  173.      *
  174.      * @return The Credit Qualifying Risk Class Aggregate
  175.      */

  176.     public org.drip.simm.margin.RiskClassAggregateCR creditQualifyingRiskClassAggregate()
  177.     {
  178.         return _creditQualifyingRiskClassAggregate;
  179.     }

  180.     /**
  181.      * Retrieve the Credit Non-Qualifying Risk Class Aggregate
  182.      *
  183.      * @return The Credit Non-Qualifying Risk Class Aggregate
  184.      */

  185.     public org.drip.simm.margin.RiskClassAggregateCR creditNonQualifyingRiskClassAggregate()
  186.     {
  187.         return _creditNonQualifyingRiskClassAggregate;
  188.     }

  189.     /**
  190.      * Retrieve the Equity Risk Class Aggregate
  191.      *
  192.      * @return The Equity Risk Class Aggregate
  193.      */

  194.     public org.drip.simm.margin.RiskClassAggregate equityRiskClassAggregate()
  195.     {
  196.         return _equityRiskClassAggregate;
  197.     }

  198.     /**
  199.      * Retrieve the FX Risk Class Aggregate
  200.      *
  201.      * @return The FX Risk Class Aggregate
  202.      */

  203.     public org.drip.simm.margin.RiskClassAggregate fxRiskClassAggregate()
  204.     {
  205.         return _fxRiskClassAggregate;
  206.     }

  207.     /**
  208.      * Retrieve the Commodity Risk Class Aggregate
  209.      *
  210.      * @return The Commodity Risk Class Aggregate
  211.      */

  212.     public org.drip.simm.margin.RiskClassAggregate commodityRiskClassAggregate()
  213.     {
  214.         return _commodityRiskClassAggregate;
  215.     }

  216.     /**
  217.      * Compute the Total IM
  218.      *
  219.      * @param labelCorrelation Cross Risk Class Label Correlation
  220.      *
  221.      * @return The Total IM
  222.      *
  223.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  224.      */

  225.     public double total (
  226.         final org.drip.measure.stochastic.LabelCorrelation labelCorrelation)
  227.         throws java.lang.Exception
  228.     {
  229.         if (null == labelCorrelation)
  230.         {
  231.             throw new java.lang.Exception ("ProductClassMargin::total => Invalid Inputs");
  232.         }

  233.         double irIM = null == _irRiskClassAggregate ? 0. : _irRiskClassAggregate.margin();

  234.         double fxIM = null == _fxRiskClassAggregate ? 0. : _fxRiskClassAggregate.margin();

  235.         double equityIM = null == _equityRiskClassAggregate ? 0. : _equityRiskClassAggregate.margin();

  236.         double commodityIM = null == _commodityRiskClassAggregate ? 0. :
  237.             _commodityRiskClassAggregate.margin();

  238.         double creditQualifyingIM = null == _creditQualifyingRiskClassAggregate ? 0. :
  239.             _creditQualifyingRiskClassAggregate.margin();

  240.         double creditNonQualifyingIM = null == _creditNonQualifyingRiskClassAggregate ? 0. :
  241.             _creditNonQualifyingRiskClassAggregate.margin();

  242.         double totalIM = 0.;
  243.         totalIM = totalIM + irIM *irIM;
  244.         totalIM = totalIM + creditQualifyingIM * creditQualifyingIM;
  245.         totalIM = totalIM + creditNonQualifyingIM * creditNonQualifyingIM;
  246.         totalIM = totalIM + equityIM * equityIM;
  247.         totalIM = totalIM + fxIM * fxIM;
  248.         totalIM = totalIM + commodityIM * commodityIM;

  249.         totalIM = totalIM + labelCorrelation.entry (
  250.             org.drip.simm.common.Chargram.IR,
  251.             org.drip.simm.common.Chargram.CRQ
  252.         ) * irIM * creditQualifyingIM;

  253.         totalIM = totalIM + labelCorrelation.entry (
  254.             org.drip.simm.common.Chargram.IR,
  255.             org.drip.simm.common.Chargram.CRNQ
  256.         ) * irIM * creditNonQualifyingIM;

  257.         totalIM = totalIM + labelCorrelation.entry (
  258.             org.drip.simm.common.Chargram.IR,
  259.             org.drip.simm.common.Chargram.EQ
  260.         ) * irIM * equityIM;

  261.         totalIM = totalIM + labelCorrelation.entry (
  262.             org.drip.simm.common.Chargram.IR,
  263.             org.drip.simm.common.Chargram.FX
  264.         ) * irIM * fxIM;

  265.         totalIM = totalIM + labelCorrelation.entry (
  266.             org.drip.simm.common.Chargram.IR,
  267.             org.drip.simm.common.Chargram.CT
  268.         ) * irIM * commodityIM;

  269.         totalIM = totalIM + labelCorrelation.entry (
  270.             org.drip.simm.common.Chargram.CRQ,
  271.             org.drip.simm.common.Chargram.CRNQ
  272.         ) * creditQualifyingIM * creditNonQualifyingIM;

  273.         totalIM = totalIM + labelCorrelation.entry (
  274.             org.drip.simm.common.Chargram.CRQ,
  275.             org.drip.simm.common.Chargram.EQ
  276.         ) * creditQualifyingIM * equityIM;

  277.         totalIM = totalIM + labelCorrelation.entry (
  278.             org.drip.simm.common.Chargram.CRQ,
  279.             org.drip.simm.common.Chargram.FX
  280.         ) * creditQualifyingIM * fxIM;

  281.         totalIM = totalIM + labelCorrelation.entry (
  282.             org.drip.simm.common.Chargram.CRQ,
  283.             org.drip.simm.common.Chargram.CT
  284.         ) * creditQualifyingIM * commodityIM;

  285.         totalIM = totalIM + labelCorrelation.entry (
  286.             org.drip.simm.common.Chargram.CRNQ,
  287.             org.drip.simm.common.Chargram.EQ
  288.         ) * creditNonQualifyingIM * equityIM;

  289.         totalIM = totalIM + labelCorrelation.entry (
  290.             org.drip.simm.common.Chargram.CRNQ,
  291.             org.drip.simm.common.Chargram.FX
  292.         ) * creditNonQualifyingIM * fxIM;

  293.         totalIM = totalIM + labelCorrelation.entry (
  294.             org.drip.simm.common.Chargram.CRNQ,
  295.             org.drip.simm.common.Chargram.CT
  296.         ) * creditNonQualifyingIM * commodityIM;

  297.         totalIM = totalIM + labelCorrelation.entry (
  298.             org.drip.simm.common.Chargram.EQ,
  299.             org.drip.simm.common.Chargram.FX
  300.         ) * equityIM * fxIM;

  301.         totalIM = totalIM + labelCorrelation.entry (
  302.             org.drip.simm.common.Chargram.EQ,
  303.             org.drip.simm.common.Chargram.CT
  304.         ) * equityIM * commodityIM;

  305.         totalIM = totalIM + labelCorrelation.entry (
  306.             org.drip.simm.common.Chargram.FX,
  307.             org.drip.simm.common.Chargram.CT
  308.         ) * fxIM * commodityIM;

  309.         return java.lang.Math.sqrt (totalIM);
  310.     }
  311. }