AdjustedVariationMarginEstimator.java

  1. package org.drip.exposure.regressiontrade;

  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>AdjustedVariationMarginEstimator</i> coordinates the Generation of the Path-specific Trade Payment
  77.  * Adjusted Variation Margin Flows. The References are:
  78.  *  
  79.  * <br><br>
  80.  *      <ul>
  81.  *          <li>
  82.  *              Andersen, L. B. G., M. Pykhtin, and A. Sokol (2017): Re-thinking Margin Period of Risk
  83.  *                  https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2902737 <b>eSSRN</b>
  84.  *          </li>
  85.  *          <li>
  86.  *              Andersen, L. B. G., M. Pykhtin, and A. Sokol (2017): Credit Exposure in the Presence of
  87.  *                  Initial Margin https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2806156 <b>eSSRN</b>
  88.  *          </li>
  89.  *          <li>
  90.  *              Albanese, C., and L. Andersen (2014): Accounting for OTC Derivatives: Funding Adjustments and
  91.  *                  the Re-Hypothecation Option https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2482955
  92.  *                  <b>eSSRN</b>
  93.  *          </li>
  94.  *          <li>
  95.  *              Burgard, C., and M. Kjaer (2017): Derivatives Funding, Netting, and Accounting
  96.  *                  https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2534011 <b>eSSRN</b>
  97.  *          </li>
  98.  *          <li>
  99.  *              Pykhtin, M. (2009): Modeling Counter-party Credit Exposure in the Presence of Margin
  100.  *                  Agreements http://www.risk-europe.com/protected/michael-pykhtin.pdf
  101.  *          </li>
  102.  *      </ul>
  103.  *
  104.  *  <br><br>
  105.  *  <ul>
  106.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  107.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ExposureAnalyticsLibrary.md">Exposure Analytics</a></li>
  108.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/exposure/README.md">Exposure Group Level Collateralized/Uncollateralized Exposure</a></li>
  109.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/exposure/regressiontrade/README.md">Exposure Regression under Margin and Trade Payments</a></li>
  110.  *  </ul>
  111.  *
  112.  * @author Lakshmi Krishnamurthy
  113.  */

  114. public class AdjustedVariationMarginEstimator
  115. {
  116.     private org.drip.exposure.universe.MarketPath _marketPath = null;
  117.     private org.drip.exposure.mpor.VariationMarginTradePaymentVertex _marginTradePaymentGenerator = null;

  118.     private static final double CumulativeTradePayment (
  119.         final org.drip.exposure.mpor.TradePayment[] denseTradePaymentArray,
  120.         final int startIndex,
  121.         final int endIndex)
  122.     {
  123.         double cumulativeTradePayment = 0.;

  124.         for (int index = startIndex + 1; index <= endIndex; ++index)
  125.         {
  126.             cumulativeTradePayment += (denseTradePaymentArray[index].dealer() +
  127.                 denseTradePaymentArray[index].client());
  128.         }

  129.         return cumulativeTradePayment;
  130.     }

  131.     /**
  132.      * AdjustedVariationMarginEstimator Constructor
  133.      *
  134.      * @param marginTradePaymentGenerator The Path-wise Variation Margin/Trade Payment Generator
  135.      * @param marketPath The Market Path
  136.      *
  137.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  138.      */

  139.     public AdjustedVariationMarginEstimator (
  140.         final org.drip.exposure.mpor.VariationMarginTradePaymentVertex marginTradePaymentGenerator,
  141.         final org.drip.exposure.universe.MarketPath marketPath)
  142.         throws java.lang.Exception
  143.     {
  144.         if (null == (_marginTradePaymentGenerator = marginTradePaymentGenerator) ||
  145.             null == (_marketPath = marketPath))
  146.         {
  147.             throw new java.lang.Exception ("AdjustedVariationMarginEstimator Constructor => Invalid Inputs");
  148.         }
  149.     }

  150.     /**
  151.      * Retrieve the Path-wise Variation Margin/Trade Payment Generator
  152.      *
  153.      * @return The Path-wise Variation Margin/Trade Payment Generator
  154.      */

  155.     public org.drip.exposure.mpor.VariationMarginTradePaymentVertex marginTradePaymentGenerator()
  156.     {
  157.         return _marginTradePaymentGenerator;
  158.     }

  159.     /**
  160.      * Retrieve the Path-wise Market Path
  161.      *
  162.      * @return The Path-wise Market Path
  163.      */

  164.     public org.drip.exposure.universe.MarketPath marketPath()
  165.     {
  166.         return _marketPath;
  167.     }

  168.     /**
  169.      * Generate the Path-wise Variation Margin Estimate on the Exposure Dates
  170.      *
  171.      * @param exposureDateArray The Path-wise Exposure Dates
  172.      *
  173.      * @return The Path-wise Variation Margin Estimate on the Exposure Dates
  174.      */

  175.     public double[] variationMarginEstimate (
  176.         final int[] exposureDateArray)
  177.     {
  178.         if (null == exposureDateArray)
  179.         {
  180.             return null;
  181.         }

  182.         int exposureDateCount = exposureDateArray.length;
  183.         double[] variationMarginEstimateArray = 0 == exposureDateCount ? null : new double[exposureDateCount];

  184.         if (0 == exposureDateCount)
  185.         {
  186.             return null;
  187.         }

  188.         for (int exposureDateIndex = 0; exposureDateIndex < exposureDateCount; ++exposureDateIndex)
  189.         {
  190.             try
  191.             {
  192.                 variationMarginEstimateArray[exposureDateIndex] =
  193.                     _marginTradePaymentGenerator.variationMarginEstimate (
  194.                         exposureDateArray[exposureDateIndex],
  195.                         _marketPath
  196.                     );
  197.             }
  198.             catch (java.lang.Exception e)
  199.             {
  200.                 e.printStackTrace();

  201.                 return null;
  202.             }
  203.         }

  204.         return variationMarginEstimateArray;
  205.     }

  206.     /**
  207.      * Retrieve the Dense Trade Payment Array across the Exposure Date Range
  208.      *
  209.      * @param startDate The Exposure Range Start Date
  210.      * @param endDate The Exposure Range End Date
  211.      *
  212.      * @return The Dense Trade Payment Array
  213.      */

  214.     public org.drip.exposure.mpor.TradePayment[] denseTradePayment (
  215.         final int startDate,
  216.         final int endDate)
  217.     {
  218.         return _marginTradePaymentGenerator.denseTradePaymentArray (
  219.             startDate,
  220.             endDate,
  221.             _marketPath
  222.         );
  223.     }

  224.     /**
  225.      * Generate the Path-wise Andersen Pykhtin Sokol (2017) Variation Margin Estimates on the Exposure Dates
  226.      *
  227.      * @param exposureDateArray The Path-wise Exposure Dates
  228.      *
  229.      * @return The Path-wise Andersen Pykhtin Sokol (2017) Variation Margin Estimates on the Exposure Dates
  230.      */

  231.     public org.drip.exposure.regressiontrade.AndersenPykhtinSokolPath andersenPykhtinSokolPath (
  232.         final int[] exposureDateArray)
  233.     {
  234.         double[] variationMarginEstimateArray = variationMarginEstimate (exposureDateArray);

  235.         if (null == variationMarginEstimateArray)
  236.         {
  237.             return null;
  238.         }

  239.         int exposureDateCount = variationMarginEstimateArray.length;
  240.         org.drip.exposure.regressiontrade.AndersenPykhtinSokolPath andersenPykhtinSokolPath = null;

  241.         org.drip.exposure.mpor.TradePayment[] denseTradePaymentArray = denseTradePayment (
  242.             exposureDateArray[0],
  243.             exposureDateArray[exposureDateArray.length - 1]
  244.         );

  245.         try
  246.         {
  247.             andersenPykhtinSokolPath = new org.drip.exposure.regressiontrade.AndersenPykhtinSokolPath
  248.                 (denseTradePaymentArray);
  249.         }
  250.         catch (java.lang.Exception e)
  251.         {
  252.             e.printStackTrace();

  253.             return null;
  254.         }

  255.         for (int exposureDateIndex = 0; exposureDateIndex < exposureDateCount; ++exposureDateIndex)
  256.         {
  257.             double periodCumulativeTradePayment = 0 == exposureDateIndex ? 0. : CumulativeTradePayment (
  258.                 denseTradePaymentArray,
  259.                 exposureDateArray[exposureDateIndex - 1] - exposureDateArray[0],
  260.                 exposureDateArray[exposureDateIndex] - exposureDateArray[0]
  261.             );

  262.             if (!andersenPykhtinSokolPath.addVariationMarginEstimateVertex (
  263.                 exposureDateArray[exposureDateIndex],
  264.                 variationMarginEstimateArray[exposureDateIndex],
  265.                 variationMarginEstimateArray[exposureDateIndex] - periodCumulativeTradePayment
  266.             ))
  267.             {
  268.                 return null;
  269.             }
  270.         }

  271.         return andersenPykhtinSokolPath;
  272.     }

  273.     /**
  274.      * Generate the Path-wise Andersen Pykhtin Sokol (2017) Adjusted Variation Margin Estimates
  275.      *
  276.      * @param exposureDateArray The Path-wise Exposure Dates
  277.      *
  278.      * @return The Path-wise Andersen Pykhtin Sokol (2017) Adjusted Variation Margin Estimates
  279.      */

  280.     public org.drip.exposure.regressiontrade.AdjustedVariationMarginEstimate adjustedVariationMarginEstimate (
  281.         final int[] exposureDateArray)
  282.     {
  283.         double[] variationMarginEstimateArray = variationMarginEstimate (exposureDateArray);

  284.         if (null == variationMarginEstimateArray)
  285.         {
  286.             return null;
  287.         }

  288.         int exposureDateCount = variationMarginEstimateArray.length;
  289.         double[] adjustedVariationMarginEstimateArray = new double[exposureDateCount];

  290.         org.drip.exposure.mpor.TradePayment[] denseTradePaymentArray = denseTradePayment (
  291.             exposureDateArray[0],
  292.             exposureDateArray[exposureDateArray.length - 1]
  293.         );

  294.         if (null == denseTradePaymentArray)
  295.         {
  296.             return null;
  297.         }

  298.         for (int exposureDateIndex = 0; exposureDateIndex < exposureDateCount; ++exposureDateIndex)
  299.         {
  300.             adjustedVariationMarginEstimateArray[exposureDateIndex] =
  301.                 variationMarginEstimateArray[exposureDateIndex] - (0 == exposureDateIndex ? 0. :
  302.                     CumulativeTradePayment (
  303.                         denseTradePaymentArray,
  304.                         exposureDateArray[exposureDateIndex - 1] - exposureDateArray[0],
  305.                         exposureDateArray[exposureDateIndex] - exposureDateArray[0]
  306.                     )
  307.                 );
  308.         }

  309.         try
  310.         {
  311.             return new org.drip.exposure.regressiontrade.AdjustedVariationMarginEstimate (
  312.                 adjustedVariationMarginEstimateArray,
  313.                 denseTradePaymentArray
  314.             );
  315.         }
  316.         catch (java.lang.Exception e)
  317.         {
  318.             e.printStackTrace();
  319.         }

  320.         return null;
  321.     }
  322. }