FixedStreamMPoR.java

  1. package org.drip.exposure.generator;

  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>FixedStreamMPoR</i> estimates the MPoR Variation Margin and the Trade Payments for the given Fixed
  77.  * Coupon Stream off of the Realized Market Path. 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.  *              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/generator/README.md">Rates Stream Margin Period Exposure</a></li>
  111.  *  </ul>
  112.  *
  113.  * @author Lakshmi Krishnamurthy
  114.  */

  115. public class FixedStreamMPoR extends org.drip.exposure.generator.StreamMPoR
  116. {

  117.     /**
  118.      * FixedStreamMPoR Constructor
  119.      *
  120.      * @param stream The Fixed Coupon Stream Instance
  121.      * @param notional The Fixed Coupon Stream Notional
  122.      *
  123.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  124.      */

  125.     public FixedStreamMPoR (
  126.         final org.drip.product.rates.Stream stream,
  127.         final double notional)
  128.         throws java.lang.Exception
  129.     {
  130.         super (
  131.             stream,
  132.             notional
  133.         );
  134.     }

  135.     @Override public double variationMarginEstimate (
  136.         final int forwardDate,
  137.         final org.drip.exposure.universe.MarketPath marketPath)
  138.         throws java.lang.Exception
  139.     {
  140.         if (null == marketPath)
  141.         {
  142.             throw new java.lang.Exception ("FixedStreamMPoR::variationMarginEstimate => Invalid Inputs");
  143.         }

  144.         double variationMarginEstimate = 0.;

  145.         double overnightReplicatorForward = marketPath.marketVertex (forwardDate).overnightReplicator();

  146.         for (org.drip.analytics.cashflow.CompositePeriod period : stream().periods())
  147.         {
  148.             int periodEndDate = period.endDate();

  149.             if (periodEndDate < forwardDate)
  150.             {
  151.                 continue;
  152.             }

  153.             org.drip.analytics.output.CompositePeriodCouponMetrics compositePeriodCouponMetrics =
  154.                 period.couponMetrics (
  155.                     forwardDate,
  156.                     null
  157.                 );

  158.             if (null == compositePeriodCouponMetrics)
  159.             {
  160.                 throw new java.lang.Exception
  161.                     ("FixedStreamMPoR::variationMarginEstimate => Cannot Compute CompositePeriodCouponMetrics");
  162.             }

  163.             variationMarginEstimate += period.couponDCF() *
  164.                 period.notional (periodEndDate) *
  165.                 compositePeriodCouponMetrics.rate() *
  166.                 period.couponFactor (periodEndDate) *
  167.                 overnightReplicatorForward /
  168.                 marketPath.marketVertex (period.payDate()).overnightReplicator();
  169.         }

  170.         return variationMarginEstimate * notional();
  171.     }

  172.     @Override public org.drip.exposure.mpor.TradePayment tradePayment (
  173.         final int forwardDate,
  174.         final org.drip.exposure.universe.MarketPath marketPath)
  175.     {
  176.         if (null == marketPath)
  177.         {
  178.             return null;
  179.         }

  180.         for (org.drip.analytics.cashflow.CompositePeriod period : stream().periods())
  181.         {
  182.             int periodPayDate = period.payDate();

  183.             if (periodPayDate == forwardDate)
  184.             {
  185.                 int periodEndDate = period.endDate();

  186.                 org.drip.analytics.output.CompositePeriodCouponMetrics compositePeriodCouponMetrics =
  187.                     period.couponMetrics (
  188.                         periodEndDate,
  189.                         null
  190.                     );

  191.                 if (null == compositePeriodCouponMetrics)
  192.                 {
  193.                     return null;
  194.                 }

  195.                 try
  196.                 {
  197.                     return org.drip.exposure.mpor.TradePayment.Standard (
  198.                         notional() * period.couponDCF() *
  199.                         period.notional (periodEndDate) *
  200.                         compositePeriodCouponMetrics.rate() *
  201.                         period.couponFactor (periodEndDate)
  202.                     );
  203.                 }
  204.                 catch (java.lang.Exception e)
  205.                 {
  206.                     e.printStackTrace();

  207.                     return null;
  208.                 }
  209.             }
  210.         }

  211.         return org.drip.exposure.mpor.TradePayment.Standard (0.);
  212.     }

  213.     @Override public org.drip.exposure.mpor.TradePayment[] denseTradePaymentArray (
  214.         final int startDate,
  215.         final int endDate,
  216.         final org.drip.exposure.universe.MarketPath marketPath)
  217.     {
  218.         if (endDate < startDate ||
  219.             null == marketPath)
  220.         {
  221.             return null;
  222.         }

  223.         int denseDateCount = endDate - startDate + 1;
  224.         org.drip.exposure.mpor.TradePayment[] denseTradePaymentArray = new
  225.             org.drip.exposure.mpor.TradePayment[denseDateCount];

  226.         for (org.drip.analytics.cashflow.CompositePeriod period : stream().periods())
  227.         {
  228.             int periodPayDate = period.payDate();

  229.             if (periodPayDate < startDate)
  230.             {
  231.                 continue;
  232.             }

  233.             if (periodPayDate > endDate)
  234.             {
  235.                 break;
  236.             }

  237.             int periodEndDate = period.endDate();

  238.             org.drip.analytics.output.CompositePeriodCouponMetrics compositePeriodCouponMetrics =
  239.                 period.couponMetrics (
  240.                     periodEndDate,
  241.                     null
  242.                 );

  243.             if (null == compositePeriodCouponMetrics)
  244.             {
  245.                 return null;
  246.             }

  247.             try
  248.             {
  249.                 denseTradePaymentArray[periodPayDate - startDate] =
  250.                     org.drip.exposure.mpor.TradePayment.Standard (
  251.                         notional() * period.couponDCF() *
  252.                         period.notional (periodEndDate) *
  253.                         compositePeriodCouponMetrics.rate() *
  254.                         period.couponFactor (periodEndDate)
  255.                     );
  256.             }
  257.             catch (java.lang.Exception e)
  258.             {
  259.                 e.printStackTrace();

  260.                 return null;
  261.             }
  262.         }

  263.         org.drip.exposure.mpor.TradePayment zeroTradePayment = org.drip.exposure.mpor.TradePayment.Standard
  264.             (0.);

  265.         for (int denseDateIndex = 0; denseDateIndex < denseDateCount; ++denseDateIndex)
  266.         {
  267.             if (null == denseTradePaymentArray[denseDateIndex])
  268.             {
  269.                 denseTradePaymentArray[denseDateIndex] = zeroTradePayment;
  270.             }
  271.         }

  272.         return denseTradePaymentArray;
  273.     }
  274. }