DiscountFactorDiscountCurve.java

  1. package org.drip.state.curve;

  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>DiscountFactorDiscountCurve</i> manages the Discounting Latent State, using the Discount Factor as the
  82.  * State Response Representation. It exports the following functionality:
  83.  *  <ul>
  84.  *      <li>
  85.  *          Compute the discount factor, forward rate, or the zero rate from the Discount Factor Latent State
  86.  *      </li>
  87.  *      <li>
  88.  *          Create a ForwardRateEstimator instance for the given Index
  89.  *      </li>
  90.  *      <li>
  91.  *          Retrieve Array of the Calibration Components
  92.  *      </li>
  93.  *      <li>
  94.  *          Retrieve the Curve Construction Input Set
  95.  *      </li>
  96.  *      <li>
  97.  *          Compute the Jacobian of the Discount Factor Latent State to the input Quote
  98.  *      </li>
  99.  *      <li>
  100.  *          Synthesize scenario Latent State by parallel shifting/custom tweaking the quantification metric
  101.  *      </li>
  102.  *      <li>
  103.  *          Synthesize scenario Latent State by parallel/custom shifting/custom tweaking the manifest measure
  104.  *      </li>
  105.  *      <li>
  106.  *          Serialize into and de-serialize out of byte array
  107.  *      </li>
  108.  *  </ul>
  109.  *
  110.  *  <br><br>
  111.  *  <ul>
  112.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  113.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  114.  *      <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>
  115.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/state/curve/README.md">Basis Spline Based Latent States</a></li>
  116.  *  </ul>
  117.  * <br><br>
  118.  *
  119.  * @author Lakshmi Krishnamurthy
  120.  */

  121. public class DiscountFactorDiscountCurve extends org.drip.state.discount.MergedDiscountForwardCurve {
  122.     private org.drip.spline.grid.Span _span = null;
  123.     private double _dblRightFlatForwardRate = java.lang.Double.NaN;

  124.     private DiscountFactorDiscountCurve shiftManifestMeasure (
  125.         final double[] adblShiftedManifestMeasure)
  126.     {
  127.         return null;
  128.     }

  129.     /**
  130.      * DiscountFactorDiscountCurve constructor
  131.      *
  132.      * @param strCurrency Currency
  133.      * @param span The Span Instance
  134.      *
  135.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  136.      */

  137.     public DiscountFactorDiscountCurve (
  138.         final java.lang.String strCurrency,
  139.         final org.drip.spline.grid.Span span)
  140.         throws java.lang.Exception
  141.     {
  142.         super ((int) span.left(), strCurrency, null);

  143.         _dblRightFlatForwardRate = -365.25 * java.lang.Math.log ((_span = span).calcResponseValue
  144.             (_span.right())) / (_span.right() - _span.left());
  145.     }

  146.     @Override public double df (
  147.         final int iDate)
  148.         throws java.lang.Exception
  149.     {
  150.         int iEpochDate = epoch().julian();

  151.         if (iDate <= iEpochDate) return 1.;

  152.         return (iDate <= _span.right() ? _span.calcResponseValue (iDate) : java.lang.Math.exp (-1. *
  153.             _dblRightFlatForwardRate * (iDate - iEpochDate) / 365.25)) * turnAdjust (iEpochDate, iDate);
  154.     }

  155.     @Override public double forward (
  156.         final int iDate1,
  157.         final int iDate2)
  158.         throws java.lang.Exception
  159.     {
  160.         int iEpochDate = epoch().julian();

  161.         if (iDate1 < iEpochDate || iDate2 < iEpochDate) return 0.;

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

  164.     @Override public double zero (
  165.         final int iDate)
  166.         throws java.lang.Exception
  167.     {
  168.         int iEpochDate = epoch().julian();

  169.         if (iDate < iEpochDate) return 0.;

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

  172.     @Override public org.drip.state.forward.ForwardRateEstimator forwardRateEstimator (
  173.         final int iDate,
  174.         final org.drip.state.identifier.ForwardLabel fri)
  175.     {
  176.         if (null == _span || !_span.isMergeState (iDate, fri)) return null;

  177.         return new org.drip.state.forward.ForwardRateEstimator() {
  178.             @Override public org.drip.state.identifier.ForwardLabel index()
  179.             {
  180.                 return fri;
  181.             }

  182.             @Override public java.lang.String tenor()
  183.             {
  184.                 return fri.tenor();
  185.             }

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

  193.                 java.lang.String strTenor = fri.tenor();

  194.                 return libor (dt.subtractTenor (strTenor).julian(), strTenor);
  195.             }

  196.             @Override public double forward (
  197.                 final int iDate)
  198.                 throws java.lang.Exception
  199.             {
  200.                 return forward (new org.drip.analytics.date.JulianDate (iDate));
  201.             }

  202.             @Override public double forward (
  203.                 final java.lang.String strTenor)
  204.                 throws java.lang.Exception
  205.             {
  206.                 if (null == strTenor || strTenor.isEmpty())
  207.                     throw new java.lang.Exception
  208.                         ("DiscountFactorDiscountCurve::ForwardEstimator::forward => Invalid Inputs!");

  209.                 return forward (epoch().addTenor (strTenor));
  210.             }
  211.         };
  212.     }

  213.     @Override public java.lang.String latentStateQuantificationMetric()
  214.     {
  215.         return org.drip.analytics.definition.LatentStateStatic.DISCOUNT_QM_DISCOUNT_FACTOR;
  216.     }

  217.     @Override public DiscountFactorDiscountCurve parallelShiftManifestMeasure (
  218.         final java.lang.String strManifestMeasure,
  219.         final double dblShift)
  220.     {
  221.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblShift)) return null;

  222.         org.drip.product.definition.CalibratableComponent[] aCC = calibComp();

  223.         if (null == aCC) return null;

  224.         int iNumComp = aCC.length;
  225.         double[] adblShiftedManifestMeasure = new double[iNumComp];

  226.         for (int i = 0; i < iNumComp; ++i)
  227.             adblShiftedManifestMeasure[i] += dblShift;

  228.         return shiftManifestMeasure (adblShiftedManifestMeasure);
  229.     }

  230.     @Override public DiscountFactorDiscountCurve shiftManifestMeasure (
  231.         final int iSpanIndex,
  232.         final java.lang.String strManifestMeasure,
  233.         final double dblShift)
  234.     {
  235.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblShift)) return null;

  236.         org.drip.product.definition.CalibratableComponent[] aCC = calibComp();

  237.         if (null == aCC) return null;

  238.         int iNumComp = aCC.length;
  239.         double[] adblShiftedManifestMeasure = new double[iNumComp];

  240.         if (iSpanIndex >= iNumComp) return null;

  241.         for (int i = 0; i < iNumComp; ++i)
  242.             adblShiftedManifestMeasure[i] += (i == iSpanIndex ? dblShift : 0.);

  243.         return shiftManifestMeasure (adblShiftedManifestMeasure);
  244.     }

  245.     @Override public org.drip.state.discount.MergedDiscountForwardCurve customTweakManifestMeasure (
  246.         final java.lang.String strManifestMeasure,
  247.         final org.drip.param.definition.ManifestMeasureTweak rvtp)
  248.     {
  249.         if (null == rvtp) return null;

  250.         org.drip.product.definition.CalibratableComponent[] aCC = calibComp();

  251.         if (null == aCC) return null;

  252.         org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double>>
  253.             mapQuote = _ccis.quoteMap();

  254.         int iNumComp = aCC.length;
  255.         double[] adblQuote = new double[iNumComp];

  256.         for (int i = 0; i < iNumComp; ++i)
  257.             adblQuote[i] = mapQuote.get (aCC[i].primaryCode()).get (strManifestMeasure);

  258.         double[] adblShiftedManifestMeasure = org.drip.analytics.support.Helper.TweakManifestMeasure
  259.             (adblQuote, rvtp);

  260.         return shiftManifestMeasure (adblShiftedManifestMeasure);
  261.     }

  262.     @Override public DiscountFactorDiscountCurve parallelShiftQuantificationMetric (
  263.         final double dblShift)
  264.     {
  265.         return null;
  266.     }

  267.     @Override public org.drip.analytics.definition.Curve customTweakQuantificationMetric (
  268.         final org.drip.param.definition.ManifestMeasureTweak rvtp)
  269.     {
  270.         return null;
  271.     }

  272.     @Override public org.drip.numerical.differentiation.WengertJacobian jackDDFDManifestMeasure (
  273.         final int iDate,
  274.         final java.lang.String strManifestMeasure)
  275.     {
  276.         return null == _span ? null : _span.jackDResponseDManifestMeasure (strManifestMeasure, iDate, 1);
  277.     }

  278.     @Override public org.drip.product.definition.CalibratableComponent[] calibComp()
  279.     {
  280.         return null == _ccis ? null : _ccis.components();
  281.     }

  282.     @Override public org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double> manifestMeasure (
  283.         final java.lang.String strInstrumentCode)
  284.     {
  285.         if (null == _ccis) return null;

  286.         org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double>>
  287.             mapQuote = _ccis.quoteMap();

  288.         if (null == mapQuote || !mapQuote.containsKey (strInstrumentCode)) return null;

  289.         return mapQuote.get (strInstrumentCode);
  290.     }
  291. }