DiscountCurve.java

  1. package org.drip.state.discount;

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

  80. /**
  81.  * <i>DiscountCurve</i> Interface combines the Interfaces of Latent State Curve Representation and Discount
  82.  * Factor Estimator.
  83.  *
  84.  *  <br><br>
  85.  *  <ul>
  86.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  87.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  88.  *      <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>
  89.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/state/discount/README.md">Discount Curve Spline Latent State</a></li>
  90.  *  </ul>
  91.  * <br><br>
  92.  *
  93.  * @author Lakshmi Krishnamurthy
  94.  */

  95. public abstract class DiscountCurve implements org.drip.analytics.definition.Curve,
  96.     org.drip.state.discount.DiscountFactorEstimator {

  97.     /**
  98.      * Construct a Flat Forward Instance of the Curve at the specified Date Nodes
  99.      *
  100.      * @param strDayCount Forward Curve Day Count
  101.      * @param iFreq Forward Curve Frequency
  102.      * @param aiDate Array of Date Nodes
  103.      *
  104.      * @return The Flat Forward Instance
  105.      */

  106.     public org.drip.state.nonlinear.FlatForwardDiscountCurve flatForward (
  107.         final java.lang.String strDayCount,
  108.         final int iFreq,
  109.         final int[] aiDate)
  110.     {
  111.         if (null == aiDate) return null;

  112.         int iNumNode = aiDate.length;
  113.         double[] adblForwardRate = 0 == iNumNode ? null : new double [iNumNode];

  114.         if (0 == iNumNode) return null;

  115.         java.lang.String strCurrency = currency();

  116.         org.drip.analytics.date.JulianDate dtStart = epoch();

  117.         org.drip.analytics.daycount.ActActDCParams aap =
  118.             org.drip.analytics.daycount.ActActDCParams.FromFrequency (iFreq);

  119.         try {
  120.             for (int i = 0; i < iNumNode; ++i) {
  121.                 int iStartDate = 0 == i ? dtStart.julian() : aiDate[i - 1];

  122.                 adblForwardRate[i] = ((df (iStartDate) / df (aiDate[i])) - 1.) /
  123.                     org.drip.analytics.daycount.Convention.YearFraction (iStartDate, aiDate[i], strDayCount,
  124.                         false, aap, strCurrency);
  125.             }

  126.             return new org.drip.state.nonlinear.FlatForwardDiscountCurve (dtStart, strCurrency, aiDate,
  127.                 adblForwardRate, true, strDayCount, iFreq);
  128.         } catch (java.lang.Exception e) {
  129.             e.printStackTrace();
  130.         }

  131.         return null;
  132.     }

  133.     /**
  134.      * Construct Flat Native Forward Instance of the Curve at the specified Date Nodes
  135.      *
  136.      * @param aiDate Array of Date Nodes
  137.      * @param dblBump The Bump Amount
  138.      *
  139.      * @return The Flat Forward Instance
  140.      */

  141.     public org.drip.state.nonlinear.FlatForwardDiscountCurve flatNativeForward (
  142.         final int[] aiDate,
  143.         final double dblBump)
  144.     {
  145.         if (null == aiDate || !org.drip.numerical.common.NumberUtil.IsValid (dblBump)) return null;

  146.         int iNumNode = aiDate.length;
  147.         double[] adblForwardRate = 0 == iNumNode ? null : new double [iNumNode];

  148.         if (0 == iNumNode) return null;

  149.         java.lang.String strCurrency = currency();

  150.         org.drip.market.otc.FixedFloatSwapConvention ffsc =
  151.             org.drip.market.otc.IBORFixedFloatContainer.ConventionFromJurisdiction (strCurrency);

  152.         if (null == ffsc) return null;

  153.         org.drip.param.period.UnitCouponAccrualSetting ucas =
  154.             ffsc.floatStreamConvention().floaterIndex().ucas();

  155.         org.drip.analytics.date.JulianDate dtStart = epoch();

  156.         int iSpotDate = dtStart.julian();

  157.         int iFreq = ucas.freq();

  158.         java.lang.String strDayCount = ucas.couponDC();

  159.         org.drip.analytics.daycount.ActActDCParams aap =
  160.             org.drip.analytics.daycount.ActActDCParams.FromFrequency (iFreq);

  161.         org.drip.product.definition.CalibratableComponent[] aCalibComp = calibComp();

  162.         int iNumComp = aCalibComp.length;
  163.         double[] adblCompCalibValue = new double[iNumComp];
  164.         java.lang.String[] astrCalibMeasure = new java.lang.String[iNumComp];

  165.         org.drip.param.market.CurveSurfaceQuoteContainer csqcNative =
  166.             org.drip.param.creator.MarketParamsBuilder.Create
  167.                 ((org.drip.state.discount.MergedDiscountForwardCurve) this, null, null, null, null, null,
  168.                     null);

  169.         org.drip.param.valuation.ValuationParams valParams = org.drip.param.valuation.ValuationParams.Spot
  170.             (iSpotDate);

  171.         for (int i = 0; i < iNumComp; ++i)
  172.         {
  173.             astrCalibMeasure[i] = "Rate";

  174.             try {
  175.                 adblCompCalibValue[i] = aCalibComp[i].measureValue (valParams, null, csqcNative, null,
  176.                     astrCalibMeasure[i]);
  177.             } catch (java.lang.Exception e) {
  178.                 e.printStackTrace();

  179.                 return null;
  180.             }
  181.         }

  182.         org.drip.state.discount.MergedDiscountForwardCurve mdfcNonlinear =
  183.             org.drip.state.creator.ScenarioDiscountCurveBuilder.NonlinearBuild (
  184.                 dtStart,
  185.                 strCurrency,
  186.                 aCalibComp,
  187.                 adblCompCalibValue,
  188.                 astrCalibMeasure,
  189.                 null
  190.             );

  191.         try {
  192.             for (int i = 0; i < iNumNode; ++i) {
  193.                 int iStartDate = 0 == i ? iSpotDate : aiDate[i - 1];

  194.                 adblForwardRate[i] = ((mdfcNonlinear.df (iStartDate) / mdfcNonlinear.df (aiDate[i])) - 1.) /
  195.                     org.drip.analytics.daycount.Convention.YearFraction (iStartDate, aiDate[i], strDayCount,
  196.                         false, aap, strCurrency) + dblBump;
  197.             }

  198.             return new org.drip.state.nonlinear.FlatForwardDiscountCurve (dtStart, strCurrency, aiDate,
  199.                 adblForwardRate, true, strDayCount, iFreq);
  200.         } catch (java.lang.Exception e) {
  201.             e.printStackTrace();
  202.         }

  203.         return null;
  204.     }

  205.     /**
  206.      * Construct Flat Native Forward Instance of the Curve at the specified Date Node Tenors
  207.      *
  208.      * @param astrTenor Array of Date Tenors
  209.      * @param dblBump The Bump Amount
  210.      *
  211.      * @return The Flat Forward Instance
  212.      */

  213.     public org.drip.state.nonlinear.FlatForwardDiscountCurve flatNativeForward (
  214.         final java.lang.String[] astrTenor,
  215.         final double dblBump)
  216.     {
  217.         if (null == astrTenor || !org.drip.numerical.common.NumberUtil.IsValid (dblBump)) return null;

  218.         int iNumNode = astrTenor.length;
  219.         double[] adblForwardRate = 0 == iNumNode ? null : new double [iNumNode];

  220.         if (0 == iNumNode) return null;

  221.         java.lang.String strCurrency = currency();

  222.         org.drip.market.otc.FixedFloatSwapConvention ffsc =
  223.             org.drip.market.otc.IBORFixedFloatContainer.ConventionFromJurisdiction (strCurrency);

  224.         if (null == ffsc) return null;

  225.         org.drip.param.period.UnitCouponAccrualSetting ucas =
  226.             ffsc.floatStreamConvention().floaterIndex().ucas();

  227.         org.drip.analytics.date.JulianDate dtStart = epoch();

  228.         int iFreq = ucas.freq();

  229.         java.lang.String strDayCount = ucas.couponDC();

  230.         org.drip.analytics.daycount.ActActDCParams aap =
  231.             org.drip.analytics.daycount.ActActDCParams.FromFrequency (iFreq);

  232.         int[] aiDate = new int[iNumNode];

  233.         try {
  234.             for (int i = 0; i < iNumNode; ++i) {
  235.                 org.drip.analytics.date.JulianDate dtTenor = dtStart.addTenor (astrTenor[i]);

  236.                 if (null == dtTenor) return null;

  237.                 aiDate[i] = dtTenor.julian();

  238.                 int iStartDate = 0 == i ? dtStart.julian() : aiDate[i - 1];

  239.                 adblForwardRate[i] = ((df (iStartDate) / df (aiDate[i])) - 1.) /
  240.                     org.drip.analytics.daycount.Convention.YearFraction (iStartDate, aiDate[i], strDayCount,
  241.                         false, aap, strCurrency) + dblBump;
  242.             }

  243.             return new org.drip.state.nonlinear.FlatForwardDiscountCurve (dtStart, strCurrency, aiDate,
  244.                 adblForwardRate, true, strDayCount, iFreq);
  245.         } catch (java.lang.Exception e) {
  246.             e.printStackTrace();
  247.         }

  248.         return null;
  249.     }

  250.     /**
  251.      * Construct Flat Native Forward Instance of the Curve at the specified Date Nodes with
  252.      *  (Exclusive/Inclusive) Bumps applied within the Tenors
  253.      *
  254.      * @param aiDate Array of Date Nodes
  255.      * @param iBumpNode The Node to be Bumped
  256.      * @param dblBump The Bump Amount
  257.      *
  258.      * @return The Flat Forward Instance
  259.      */

  260.     public org.drip.state.nonlinear.FlatForwardDiscountCurve flatNativeForwardEI (
  261.         final int[] aiDate,
  262.         final int iBumpNode,
  263.         final double dblBump)
  264.     {
  265.         if (null == aiDate || !org.drip.numerical.common.NumberUtil.IsValid (dblBump)) return null;

  266.         int iNumNode = aiDate.length;
  267.         double[] adblForwardRate = 0 == iNumNode ? null : new double [iNumNode];

  268.         if (0 == iNumNode) return null;

  269.         java.lang.String strCurrency = currency();

  270.         org.drip.market.otc.FixedFloatSwapConvention ffsc =
  271.             org.drip.market.otc.IBORFixedFloatContainer.ConventionFromJurisdiction (strCurrency);

  272.         if (null == ffsc) return null;

  273.         org.drip.param.period.UnitCouponAccrualSetting ucas =
  274.             ffsc.floatStreamConvention().floaterIndex().ucas();

  275.         org.drip.analytics.date.JulianDate dtStart = epoch();

  276.         int iFreq = ucas.freq();

  277.         java.lang.String strDayCount = ucas.couponDC();

  278.         org.drip.analytics.daycount.ActActDCParams aap =
  279.             org.drip.analytics.daycount.ActActDCParams.FromFrequency (iFreq);

  280.         try {
  281.             for (int i = 0; i < iNumNode; ++i) {
  282.                 int iStartDate = 0 == i ? dtStart.julian() : aiDate[i - 1];

  283.                 adblForwardRate[i] = ((df (iStartDate) / df (aiDate[i])) - 1.) /
  284.                     org.drip.analytics.daycount.Convention.YearFraction (iStartDate, aiDate[i], strDayCount,
  285.                         false, aap, strCurrency) + (i == iBumpNode ? dblBump : 0.);
  286.             }

  287.             return new org.drip.state.nonlinear.FlatForwardDiscountCurve (dtStart, strCurrency, aiDate,
  288.                 adblForwardRate, true, strDayCount, iFreq);
  289.         } catch (java.lang.Exception e) {
  290.             e.printStackTrace();
  291.         }

  292.         return null;
  293.     }
  294. }