MultilateralBasisCurve.java

  1. package org.drip.state.csa;

  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>MultilateralBasisCurve</i> implements the CSA Cash Rate Curve as a Basis over an Overnight Curve.
  77.  *
  78.  *  <br><br>
  79.  *  <ul>
  80.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  81.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  82.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/state/README.md">Latent State Inference and Creation Utilities</a></li>
  83.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/state/csa/README.md">Credit Support Annex Latent State</a></li>
  84.  *  </ul>
  85.  * <br><br>
  86.  *
  87.  * @author Lakshmi Krishnamurthy
  88.  */

  89. public class MultilateralBasisCurve implements org.drip.state.csa.CashFlowEstimator
  90. {
  91.     private static final int NUM_DF_QUADRATURES = 5;

  92.     private double _dblBasis = java.lang.Double.NaN;
  93.     private org.drip.state.discount.MergedDiscountForwardCurve _mdfcOvernight = null;

  94.     /**
  95.      * Retrieve the Overnight Curve
  96.      *
  97.      * @return The Overnight Curve
  98.      */

  99.     public org.drip.state.discount.MergedDiscountForwardCurve overnightCurve()
  100.     {
  101.         return _mdfcOvernight;
  102.     }

  103.     /**
  104.      * Retrieve the Basis to the Overnight Curve
  105.      *
  106.      * @return The Basis to the Overnight Curve
  107.      */

  108.     public double basis()
  109.     {
  110.         return _dblBasis;
  111.     }

  112.     @Override public org.drip.analytics.date.JulianDate epoch()
  113.     {
  114.         return _mdfcOvernight.epoch();
  115.     }

  116.     @Override public double df (
  117.         final int iDate)
  118.         throws java.lang.Exception
  119.     {
  120.         int iEpochDate = epoch().julian();

  121.         if (iEpochDate >= iDate)
  122.             throw new java.lang.Exception ("MultilateralBasisCurve::df => Invalid Inputs");

  123.         return _mdfcOvernight.df (iDate) * java.lang.Math.exp (_dblBasis * (iEpochDate - iDate) / 365.25);
  124.     }

  125.     @Override public double df (
  126.         final org.drip.analytics.date.JulianDate dt)
  127.         throws java.lang.Exception
  128.     {
  129.         if (null == dt) throw new java.lang.Exception ("MultilateralBasisCurve::df => Invalid Inputs");

  130.         return df (dt.julian());
  131.     }

  132.     @Override public double df (
  133.         final java.lang.String strTenor)
  134.         throws java.lang.Exception
  135.     {
  136.         return df (epoch().addTenor (strTenor));
  137.     }

  138.     @Override public double effectiveDF (
  139.         final int iDate1,
  140.         final int iDate2)
  141.         throws java.lang.Exception
  142.     {
  143.         if (epoch().julian() > iDate1 || iDate1 >= iDate2)
  144.             throw new java.lang.Exception ("MultilateralFlatForwardCurve::effectiveDF => Invalid Inputs");

  145.         int iNumQuadratures = 0;
  146.         double dblEffectiveDF = 0.;
  147.         int iQuadratureWidth = (iDate2 - iDate1) / NUM_DF_QUADRATURES;

  148.         if (0 == iQuadratureWidth) iQuadratureWidth = 1;

  149.         for (int iDate = iDate1; iDate <= iDate2; iDate += iQuadratureWidth) {
  150.             ++iNumQuadratures;

  151.             dblEffectiveDF += (df (iDate) + df (iDate + iQuadratureWidth));
  152.         }

  153.         return dblEffectiveDF / (2. * iNumQuadratures);
  154.     }

  155.     @Override public double effectiveDF (
  156.         final org.drip.analytics.date.JulianDate dt1,
  157.         final org.drip.analytics.date.JulianDate dt2)
  158.         throws java.lang.Exception
  159.     {
  160.         if (null == dt1 || null == dt2)
  161.             throw new java.lang.Exception ("MultilateralFlatForwardCurve::effectiveDF => Invalid Inputs");

  162.         return effectiveDF (
  163.             dt1.julian(),
  164.             dt2.julian()
  165.         );
  166.     }

  167.     @Override public double effectiveDF (
  168.         final java.lang.String strTenor1,
  169.         final java.lang.String strTenor2)
  170.         throws java.lang.Exception
  171.     {
  172.         org.drip.analytics.date.JulianDate dtEpoch = epoch();

  173.         return effectiveDF (
  174.             dtEpoch.addTenor (strTenor1),
  175.             dtEpoch.addTenor (strTenor2)
  176.         );
  177.     }

  178.     @Override public double rate (
  179.         final int iDate)
  180.         throws java.lang.Exception
  181.     {
  182.         int iEpochDate = epoch().julian();

  183.         if (iEpochDate >= iDate)
  184.             throw new java.lang.Exception ("MultilateralFlatForwardCurve::rate => Invalid Inputs");

  185.         return 365.25 * java.lang.Math.log (df (iEpochDate) / df (iDate)) / (iEpochDate - iDate);
  186.     }

  187.     @Override public double rate (
  188.         final org.drip.analytics.date.JulianDate dt)
  189.         throws java.lang.Exception
  190.     {
  191.         if (null == dt)
  192.             throw new java.lang.Exception ("MultilateralFlatForwardCurve::rate => Invalid Inputs");

  193.         return rate (dt.julian());
  194.     }

  195.     @Override public double rate (
  196.         final java.lang.String strTenor)
  197.         throws java.lang.Exception
  198.     {
  199.         return rate (epoch().addTenor (strTenor));
  200.     }

  201.     @Override public double rate (
  202.         final int iDate1,
  203.         final int iDate2)
  204.         throws java.lang.Exception
  205.     {
  206.         int iEpochDate = epoch().julian();

  207.         if (iEpochDate > iDate1 || iDate1 >= iDate2)
  208.             throw new java.lang.Exception ("MultilateralFlatForwardCurve::rate => Invalid Inputs");

  209.         return 365.25 * java.lang.Math.log (df (iDate1) / df (iDate2)) / (iDate2 - iDate1);
  210.     }

  211.     @Override public double rate (
  212.         final org.drip.analytics.date.JulianDate dt1,
  213.         final org.drip.analytics.date.JulianDate dt2)
  214.         throws java.lang.Exception
  215.     {
  216.         if (null == dt1 || null == dt2)
  217.             throw new java.lang.Exception ("MultilateralFlatForwardCurve::rate => Invalid Inputs");

  218.         return rate (
  219.             dt1.julian(),
  220.             dt2.julian()
  221.         );
  222.     }

  223.     @Override public double rate (
  224.         final java.lang.String strTenor1,
  225.         final java.lang.String strTenor2)
  226.         throws java.lang.Exception
  227.     {
  228.         org.drip.analytics.date.JulianDate dtEpoch = epoch();

  229.         return rate (
  230.             dtEpoch.addTenor (strTenor1),
  231.             dtEpoch.addTenor (strTenor2)
  232.         );
  233.     }
  234. }