CollateralAmountEstimator.java

  1. package org.drip.exposure.mpor;

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

  76. /**
  77.  * <i>CollateralAmountEstimator</i> estimates the Amount of Collateral Hypothecation that is to be Posted
  78.  * during a Single Run of a Collateral Hypothecation Group Valuation. The References are:
  79.  *  
  80.  * <br><br>
  81.  *      <ul>
  82.  *          <li>
  83.  *              Burgard, C., and M. Kjaer (2014): PDE Representations of Derivatives with Bilateral Counter-
  84.  *                  party Risk and Funding Costs <i>Journal of Credit Risk</i> <b>7 (3)</b> 1-19
  85.  *          </li>
  86.  *          <li>
  87.  *              Burgard, C., and M. Kjaer (2014): In the Balance, Risk, 24 (11) 72-75
  88.  *          </li>
  89.  *          <li>
  90.  *              Gregory, J. (2009): Being Two-faced over Counter-party Credit Risk <i>Risk</i> <b>20 (2)</b>
  91.  *                  86-90
  92.  *          </li>
  93.  *          <li>
  94.  *              Li, B., and Y. Tang (2007): Quantitative Analysis, Derivatives Modeling, and Trading
  95.  *                  Strategies in the Presence of Counter-party Credit Risk for the Fixed Income Market
  96.  *                  <i>World Scientific Publishing </i> <b>Singapore</b>
  97.  *          </li>
  98.  *          <li>
  99.  *              Piterbarg, V. (2010): Funding Beyond Discounting: Collateral Agreements and Derivatives
  100.  *                  Pricing <i>Risk</i> <b>21 (2)</b> 97-102
  101.  *          </li>
  102.  *          <li>
  103.  *      </ul>
  104.  *
  105.  *  <br><br>
  106.  *  <ul>
  107.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  108.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ExposureAnalyticsLibrary.md">Exposure Analytics</a></li>
  109.  *      <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>
  110.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/exposure/mpor/README.md">Margin Period Collateral Amount Estimation</a></li>
  111.  *  </ul>
  112.  *  
  113.  * @author Lakshmi Krishnamurthy
  114.  */

  115. public class CollateralAmountEstimator
  116. {
  117.     private double _currentBalance = java.lang.Double.NaN;
  118.     private org.drip.measure.bridge.BrokenDateInterpolator _brokenDateInterpolator = null;
  119.     private org.drip.xva.proto.PositionGroupSpecification _positionGroupSpecification = null;

  120.     /**
  121.      * CollateralAmountEstimator Constructor
  122.      *
  123.      * @param positionGroupSpecification The Position Group Specification
  124.      * @param brokenDateInterpolator The Stochastic Value Broken Date Bridge Estimator
  125.      * @param currentBalance The Current Collateral Balance
  126.      *
  127.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  128.      */

  129.     public CollateralAmountEstimator (
  130.         final org.drip.xva.proto.PositionGroupSpecification positionGroupSpecification,
  131.         final org.drip.measure.bridge.BrokenDateInterpolator brokenDateInterpolator,
  132.         final double currentBalance)
  133.         throws java.lang.Exception
  134.     {
  135.         if (null == (_positionGroupSpecification = positionGroupSpecification) ||
  136.             null == (_brokenDateInterpolator = brokenDateInterpolator))
  137.         {
  138.             throw new java.lang.Exception ("CollateralAmountEstimator Constructor => Invalid Inputs");
  139.         }

  140.         _currentBalance = currentBalance;
  141.     }

  142.     /**
  143.      * Retrieve the Position Group Specification
  144.      *
  145.      * @return The Position Group Specification
  146.      */

  147.     public org.drip.xva.proto.PositionGroupSpecification positionGroupSpecification()
  148.     {
  149.         return _positionGroupSpecification;
  150.     }

  151.     /**
  152.      * Retrieve the Stochastic Value Broken Date Bridge Estimator
  153.      *
  154.      * @return The Stochastic Value Broken Date Bridge Estimator
  155.      */

  156.     public org.drip.measure.bridge.BrokenDateInterpolator brokenDateBridge()
  157.     {
  158.         return _brokenDateInterpolator;
  159.     }

  160.     /**
  161.      * Retrieve the Current Collateral Balance
  162.      *
  163.      * @return The Current Collateral Balance
  164.      */

  165.     public double currentCollateralBalance()
  166.     {
  167.         return _currentBalance;
  168.     }

  169.     /**
  170.      * Calculate the Margin Value at the Dealer Default Window
  171.      *
  172.      * @param valuationDateJulian The Valuation Date
  173.      *
  174.      * @return The Margin Value at the Dealer Default Window
  175.      *
  176.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  177.      */

  178.     public double dealerWindowMarginValue (
  179.         final org.drip.analytics.date.JulianDate valuationDateJulian)
  180.         throws java.lang.Exception
  181.     {
  182.         if (null == valuationDateJulian)
  183.         {
  184.             throw new java.lang.Exception
  185.                 ("CollateralAmountEstimator::dealerWindowMarginValue => Invalid Inputs");
  186.         }

  187.         org.drip.analytics.date.JulianDate marginDate = valuationDateJulian.subtractDays
  188.             (_positionGroupSpecification.dealerDefaultWindow());

  189.         if (null == marginDate)
  190.         {
  191.             throw new java.lang.Exception
  192.                 ("CollateralAmountEstimator::dealerWindowMarginValue => Invalid Inputs");
  193.         }

  194.         return _brokenDateInterpolator.interpolate (marginDate.julian());
  195.     }

  196.     /**
  197.      * Calculate the Dealer Margin Threshold
  198.      *
  199.      * @param valuationDateJulian The Valuation Date
  200.      *
  201.      * @return The Dealer Margin Threshold
  202.      *
  203.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  204.      */

  205.     public double dealerThreshold (
  206.         final org.drip.analytics.date.JulianDate valuationDateJulian)
  207.         throws java.lang.Exception
  208.     {
  209.         org.drip.function.definition.R1ToR1 dealerThresholdFunction =
  210.             _positionGroupSpecification.dealerThresholdFunction();

  211.         return null == dealerThresholdFunction ? 0. : dealerThresholdFunction.evaluate
  212.             (valuationDateJulian.julian());
  213.     }

  214.     /**
  215.      * Calculate the Margin Amount Required to be Posted by the Dealer
  216.      *
  217.      * @param valuationDateJulian The Valuation Date
  218.      *
  219.      * @return The Margin Amount Required to be Posted by the Dealer
  220.      *
  221.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  222.      */

  223.     public double dealerPostingRequirement (
  224.         final org.drip.analytics.date.JulianDate valuationDateJulian)
  225.         throws java.lang.Exception
  226.     {
  227.         double dealerPostingRequirement = dealerWindowMarginValue (valuationDateJulian) - dealerThreshold
  228.             (valuationDateJulian);

  229.         return 0. < dealerPostingRequirement ? 0. : dealerPostingRequirement;
  230.     }

  231.     /**
  232.      * Calculate the Margin Value at the Client Default Window
  233.      *
  234.      * @param valuationDateJulian The Valuation Date
  235.      *
  236.      * @return The Margin Value at the Client Default Window
  237.      *
  238.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  239.      */

  240.     public double clientWindowMarginValue (
  241.         final org.drip.analytics.date.JulianDate valuationDateJulian)
  242.         throws java.lang.Exception
  243.     {
  244.         if (null == valuationDateJulian)
  245.         {
  246.             throw new java.lang.Exception
  247.                 ("CollateralAmountEstimator::clientWindowMarginValue => Invalid Inputs");
  248.         }

  249.         org.drip.analytics.date.JulianDate marginDate = valuationDateJulian.subtractDays
  250.             (_positionGroupSpecification.clientDefaultWindow());

  251.         if (null == marginDate)
  252.         {
  253.             throw new java.lang.Exception
  254.                 ("CollateralAmountEstimator::clientWindowMarginValue => Invalid Inputs");
  255.         }

  256.         return _brokenDateInterpolator.interpolate (marginDate.julian());
  257.     }

  258.     /**
  259.      * Calculate the Client Margin Threshold
  260.      *
  261.      * @param valuationDateJulian The Valuation Date
  262.      *
  263.      * @return The Client Margin Threshold
  264.      *
  265.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  266.      */

  267.     public double clientThreshold (
  268.         final org.drip.analytics.date.JulianDate valuationDateJulian)
  269.         throws java.lang.Exception
  270.     {
  271.         org.drip.function.definition.R1ToR1[] clientThresholdFunctionArray =
  272.             _positionGroupSpecification.clientThresholdFunctionArray();

  273.         return null == clientThresholdFunctionArray || null == clientThresholdFunctionArray[0] ? 0. :
  274.             clientThresholdFunctionArray[0].evaluate (valuationDateJulian.julian());
  275.     }

  276.     /**
  277.      * Calculate the Margin Amount Required to be Posted by the Client
  278.      *
  279.      * @param valuationDateJulian The Valuation Date
  280.      *
  281.      * @return The Margin Amount Required to be Posted by the Client
  282.      *
  283.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  284.      */

  285.     public double clientPostingRequirement (
  286.         final org.drip.analytics.date.JulianDate valuationDateJulian)
  287.         throws java.lang.Exception
  288.     {
  289.         double clientPostingRequirement = clientWindowMarginValue (valuationDateJulian) - clientThreshold
  290.             (valuationDateJulian);

  291.         return 0. > clientPostingRequirement ? 0. : clientPostingRequirement;
  292.     }

  293.     /**
  294.      * Calculate the Gross Margin Amount Required to be Posted
  295.      *
  296.      * @param valuationDateJulian The Valuation Date
  297.      *
  298.      * @return The Gross Margin Amount Required to be Posted
  299.      *
  300.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  301.      */

  302.     public double postingRequirement (
  303.         final org.drip.analytics.date.JulianDate valuationDateJulian)
  304.         throws java.lang.Exception
  305.     {
  306.         return org.drip.numerical.common.NumberUtil.IsValid (_currentBalance) ? _currentBalance :
  307.             dealerPostingRequirement (valuationDateJulian) + clientPostingRequirement (valuationDateJulian);
  308.     }

  309.     /**
  310.      * Generate the MarginAmountEstimatorOutput Instance
  311.      *
  312.      * @param valuationDateJulian The Valuation Date
  313.      *
  314.      * @return The MarginAmountEstimatorOutput Instance
  315.      */

  316.     public org.drip.exposure.mpor.CollateralAmountEstimatorOutput output (
  317.         final org.drip.analytics.date.JulianDate valuationDateJulian)
  318.     {
  319.         if (null == valuationDateJulian)
  320.         {
  321.             return null;
  322.         }

  323.         org.drip.analytics.date.JulianDate dealerMarginDate = valuationDateJulian.subtractDays
  324.             (_positionGroupSpecification.dealerDefaultWindow());

  325.         org.drip.analytics.date.JulianDate clientMarginDate = valuationDateJulian.subtractDays
  326.             (_positionGroupSpecification.clientDefaultWindow());

  327.         if (null == dealerMarginDate ||
  328.             null == clientMarginDate)
  329.         {
  330.             return null;
  331.         }

  332.         org.drip.function.definition.R1ToR1[] clientThresholdFunctionArray =
  333.             _positionGroupSpecification.clientThresholdFunctionArray();

  334.         org.drip.function.definition.R1ToR1 dealerThresholdFunction =
  335.             _positionGroupSpecification.dealerThresholdFunction();

  336.         double valuationDate = valuationDateJulian.julian();

  337.         try
  338.         {
  339.             double dealerWindowMarginValue = _brokenDateInterpolator.interpolate (dealerMarginDate.julian());

  340.             double clientWindowMarginValue = _brokenDateInterpolator.interpolate (clientMarginDate.julian());

  341.             double dealerThresholdValue = null == dealerThresholdFunction ? 0. :
  342.                 dealerThresholdFunction.evaluate (valuationDate);

  343.             double clientThresholdValue = null == clientThresholdFunctionArray || null ==
  344.                 clientThresholdFunctionArray[0] ? 0. : clientThresholdFunctionArray[0].evaluate
  345.                     (valuationDate);

  346.             double dealerPostingRequirement = dealerWindowMarginValue - dealerThresholdValue;
  347.             dealerPostingRequirement = 0. < dealerPostingRequirement ? 0. : dealerPostingRequirement;
  348.             double clientPostingRequirement = clientWindowMarginValue - clientThresholdValue;
  349.             clientPostingRequirement = 0. > clientPostingRequirement ? 0. : clientPostingRequirement;

  350.             return new org.drip.exposure.mpor.CollateralAmountEstimatorOutput (
  351.                 dealerMarginDate,
  352.                 clientMarginDate,
  353.                 dealerWindowMarginValue,
  354.                 dealerThresholdValue,
  355.                 dealerPostingRequirement,
  356.                 clientWindowMarginValue,
  357.                 clientThresholdValue,
  358.                 clientPostingRequirement,
  359.                 org.drip.numerical.common.NumberUtil.IsValid (_currentBalance) ? _currentBalance :
  360.                     dealerPostingRequirement + clientPostingRequirement);
  361.         }
  362.         catch (java.lang.Exception e)
  363.         {
  364.             e.printStackTrace();
  365.         }

  366.         return null;
  367.     }
  368. }