MultilateralFlatForwardCurve.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>MultilateralFlatForwardCurve</i> implements the CSA Cash Rate Curve using a Flat Forward CSA Rate.
  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 MultilateralFlatForwardCurve extends org.drip.state.nonlinear.FlatForwardDiscountCurve
  90.     implements org.drip.state.csa.CashFlowEstimator
  91. {

  92.     /**
  93.      * MultilateralFlatForwardCurve Constructor
  94.      *
  95.      * @param dtEpoch Epoch Date
  96.      * @param strCurrency Currency
  97.      * @param aiDate Array of Dates
  98.      * @param adblForwardRate Array of Forward Rates
  99.      * @param bDiscreteCompounding TRUE - Compounding is Discrete
  100.      * @param strCompoundingDayCount Day Count Convention to be used for Discrete Compounding
  101.      * @param iCompoundingFreq Frequency to be used for Discrete Compounding
  102.      *
  103.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  104.      */

  105.     public MultilateralFlatForwardCurve (
  106.         final org.drip.analytics.date.JulianDate dtEpoch,
  107.         final java.lang.String strCurrency,
  108.         final int[] aiDate,
  109.         final double[] adblForwardRate,
  110.         final boolean bDiscreteCompounding,
  111.         final java.lang.String strCompoundingDayCount,
  112.         final int iCompoundingFreq)
  113.         throws java.lang.Exception
  114.     {
  115.         super (
  116.             dtEpoch,
  117.             strCurrency,
  118.             aiDate,
  119.             adblForwardRate,
  120.             bDiscreteCompounding,
  121.             strCompoundingDayCount,
  122.             iCompoundingFreq
  123.         );
  124.     }

  125.     @Override public double rate (
  126.         final int iDate)
  127.         throws java.lang.Exception
  128.     {
  129.         int iEpochDate = epoch().julian();

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

  132.         return discreteCompounding() ? ((1. / df (iDate)) - 1.) / yearFract (
  133.             iEpochDate,
  134.             iDate
  135.         ) : org.drip.analytics.support.Helper.DF2Yield (
  136.             compoundingFrequency(),
  137.             df (iDate),
  138.             yearFract (
  139.                 iEpochDate,
  140.                 iDate
  141.             )
  142.         );
  143.     }

  144.     @Override public double rate (
  145.         final org.drip.analytics.date.JulianDate dt)
  146.         throws java.lang.Exception
  147.     {
  148.         if (null == dt)
  149.             throw new java.lang.Exception ("MultilateralFlatForwardCurve::rate => Invalid Inputs");

  150.         return rate (dt.julian());
  151.     }

  152.     @Override public double rate (
  153.         final java.lang.String strTenor)
  154.         throws java.lang.Exception
  155.     {
  156.         return rate (epoch().addTenor (strTenor));
  157.     }

  158.     @Override public double rate (
  159.         final int iDate1,
  160.         final int iDate2)
  161.         throws java.lang.Exception
  162.     {
  163.         int iEpochDate = epoch().julian();

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

  166.         return discreteCompounding() ? ((df (iDate1) / df (iDate2)) - 1.) / yearFract (
  167.             iDate1,
  168.             iDate2
  169.         ) : org.drip.analytics.support.Helper.DF2Yield (
  170.             compoundingFrequency(),
  171.             df (iDate1) / df (iDate2),
  172.             yearFract (
  173.                 iDate1,
  174.                 iDate2
  175.             )
  176.         );
  177.     }

  178.     @Override public double rate (
  179.         final org.drip.analytics.date.JulianDate dt1,
  180.         final org.drip.analytics.date.JulianDate dt2)
  181.         throws java.lang.Exception
  182.     {
  183.         if (null == dt1 || null == dt2)
  184.             throw new java.lang.Exception ("MultilateralFlatForwardCurve::rate => Invalid Inputs");

  185.         return rate (
  186.             dt1.julian(),
  187.             dt2.julian()
  188.         );
  189.     }

  190.     @Override public double rate (
  191.         final java.lang.String strTenor1,
  192.         final java.lang.String strTenor2)
  193.         throws java.lang.Exception
  194.     {
  195.         org.drip.analytics.date.JulianDate dtEpoch = epoch();

  196.         return rate (
  197.             dtEpoch.addTenor (strTenor1),
  198.             dtEpoch.addTenor (strTenor2)
  199.         );
  200.     }
  201. }