GovvieCurve.java

  1. package org.drip.state.govvie;

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

  77. /**
  78.  * <i>GovvieCurve</i> is the Stub for the Govvie Curve for the specified Govvie/Treasury.
  79.  *
  80.  *  <br><br>
  81.  *  <ul>
  82.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  83.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  84.  *      <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>
  85.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/state/govvie/README.md">Govvie Latent State Curve Estimator</a></li>
  86.  *  </ul>
  87.  * <br><br>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public abstract class GovvieCurve extends org.drip.state.discount.DiscountCurve implements
  92.     org.drip.state.govvie.YieldEstimator {
  93.     private static final int NUM_DF_QUADRATURES = 5;

  94.     private int _iFreq = 2;
  95.     private java.lang.String _strCurrency = "";
  96.     private java.lang.String _strTreasuryCode = "";
  97.     private java.lang.String _strDayCount = "DCAct_Act_UST";

  98.     protected int _iEpochDate = java.lang.Integer.MIN_VALUE;
  99.     protected org.drip.analytics.input.CurveConstructionInputSet _ccis = null;

  100.     protected GovvieCurve (
  101.         final int iEpochDate,
  102.         final java.lang.String strTreasuryCode,
  103.         final java.lang.String strCurrency)
  104.         throws java.lang.Exception
  105.     {
  106.         if (null == (_strTreasuryCode = strTreasuryCode) || _strTreasuryCode.isEmpty() || null ==
  107.             (_strCurrency = strCurrency) || _strCurrency.isEmpty())
  108.             throw new java.lang.Exception ("GovvieCurve ctr: Invalid Inputs");

  109.         _iEpochDate = iEpochDate;
  110.     }

  111.     @Override public org.drip.analytics.date.JulianDate epoch()
  112.     {
  113.         return new org.drip.analytics.date.JulianDate (_iEpochDate);
  114.     }

  115.     @Override public java.lang.String currency()
  116.     {
  117.         return _strCurrency;
  118.     }

  119.     @Override public org.drip.state.identifier.LatentStateLabel label()
  120.     {
  121.         return org.drip.state.identifier.GovvieLabel.Standard (_strTreasuryCode);
  122.     }

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

  128.         return yield (dt.julian());
  129.     }

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

  136.     @Override public double forwardYield (
  137.         final int iDate1,
  138.         final int iDate2)
  139.         throws java.lang.Exception
  140.     {
  141.         if (iDate1 >= iDate2) throw new java.lang.Exception ("GovvieCurve::forwardYield => Invalid Inputs");

  142.         org.drip.analytics.daycount.ActActDCParams aadp =
  143.             org.drip.analytics.daycount.ActActDCParams.FromFrequency (_iFreq);

  144.         double dblYearFraction = org.drip.analytics.daycount.Convention.YearFraction (iDate1, iDate2,
  145.             _strDayCount, false, aadp, _strCurrency);

  146.         double dblDF1 = org.drip.analytics.support.Helper.Yield2DF  (_iFreq, yield (iDate1),
  147.             org.drip.analytics.daycount.Convention.YearFraction (_iEpochDate, iDate1, _strDayCount, false,
  148.                 aadp, _strCurrency));

  149.         double dblDF2 = org.drip.analytics.support.Helper.Yield2DF  (_iFreq, yield (iDate2),
  150.             org.drip.analytics.daycount.Convention.YearFraction (_iEpochDate, iDate2, _strDayCount, false,
  151.                 aadp, _strCurrency));

  152.         return org.drip.analytics.support.Helper.DF2Yield (
  153.             _iFreq,
  154.             dblDF2 / dblDF1,
  155.             dblYearFraction
  156.         );
  157.     }

  158.     @Override public double df (
  159.         final int iDate)
  160.         throws java.lang.Exception
  161.     {
  162.         return org.drip.analytics.support.Helper.Yield2DF  (_iFreq, yield (iDate),
  163.             org.drip.analytics.daycount.Convention.YearFraction (_iEpochDate, iDate, _strDayCount, false,
  164.                 org.drip.analytics.daycount.ActActDCParams.FromFrequency (_iFreq), _strCurrency));
  165.     }

  166.     @Override public double df (
  167.         final org.drip.analytics.date.JulianDate dt)
  168.         throws java.lang.Exception
  169.     {
  170.         if (null == dt) throw new java.lang.Exception ("GovvieCurve::df => Invalid Inputs");

  171.         return df (dt.julian());
  172.     }

  173.     @Override public double df (
  174.         final java.lang.String strTenor)
  175.         throws java.lang.Exception
  176.     {
  177.         return df (new org.drip.analytics.date.JulianDate (_iEpochDate).addTenor (strTenor));
  178.     }

  179.     @Override public double effectiveDF (
  180.         final int iDate1,
  181.         final int iDate2)
  182.         throws java.lang.Exception
  183.     {
  184.         if (iDate1 == iDate2) return df (iDate1);

  185.         int iNumQuadratures = 0;
  186.         double dblEffectiveDF = 0.;
  187.         int iQuadratureWidth = (iDate2 - iDate1) / NUM_DF_QUADRATURES;

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

  189.         for (int iDate = iDate1; iDate <= iDate2; iDate += iQuadratureWidth) {
  190.             ++iNumQuadratures;

  191.             dblEffectiveDF += (df (iDate) + df (iDate + iQuadratureWidth));
  192.         }

  193.         return dblEffectiveDF / (2. * iNumQuadratures);
  194.     }

  195.     @Override public double effectiveDF (
  196.         final org.drip.analytics.date.JulianDate dt1,
  197.         final org.drip.analytics.date.JulianDate dt2)
  198.         throws java.lang.Exception
  199.     {
  200.         if (null == dt1 || null == dt2)
  201.             throw new java.lang.Exception ("GovvieCurve::effectiveDF => Got null for date");

  202.         return effectiveDF (dt1.julian(), dt2.julian());
  203.     }

  204.     @Override public double effectiveDF (
  205.         final java.lang.String strTenor1,
  206.         final java.lang.String strTenor2)
  207.         throws java.lang.Exception
  208.     {
  209.         if (null == strTenor1 || strTenor1.isEmpty() || null == strTenor2 || strTenor2.isEmpty())
  210.             throw new java.lang.Exception ("GovvieCurve::effectiveDF => Got bad tenor");

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

  212.         return effectiveDF (dtStart.addTenor (strTenor1), dtStart.addTenor (strTenor2));
  213.     }

  214.     @Override public double yieldDF (
  215.         final int iDate,
  216.         final double dblDCF)
  217.         throws java.lang.Exception
  218.     {
  219.         return org.drip.analytics.support.Helper.Yield2DF (_iFreq, yield (iDate), dblDCF);
  220.     }

  221.     @Override public boolean setCCIS (
  222.         final org.drip.analytics.input.CurveConstructionInputSet ccis)
  223.     {
  224.         _ccis = ccis;
  225.         return true;
  226.     }

  227.     @Override public org.drip.product.definition.CalibratableComponent[] calibComp()
  228.     {
  229.         return null;
  230.     }

  231.     @Override public org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double> manifestMeasure (
  232.         final java.lang.String strInstr)
  233.     {
  234.         return null;
  235.     }

  236.     @Override public org.drip.state.representation.LatentState parallelShiftManifestMeasure (
  237.         final java.lang.String strManifestMeasure,
  238.         final double dblShift)
  239.     {
  240.         return null;
  241.     }

  242.     @Override public org.drip.state.representation.LatentState shiftManifestMeasure (
  243.         final int iSpanIndex,
  244.         final java.lang.String strManifestMeasure,
  245.         final double dblShift)
  246.     {
  247.         return null;
  248.     }

  249.     @Override public org.drip.state.representation.LatentState customTweakManifestMeasure (
  250.         final java.lang.String strManifestMeasure,
  251.         final org.drip.param.definition.ManifestMeasureTweak rvtp)
  252.     {
  253.         return null;
  254.     }

  255.     @Override public org.drip.state.representation.LatentState parallelShiftQuantificationMetric (
  256.         final double dblShift)
  257.     {
  258.         return null;
  259.     }

  260.     @Override public org.drip.state.representation.LatentState customTweakQuantificationMetric (
  261.         final org.drip.param.definition.ManifestMeasureTweak rvtp)
  262.     {
  263.         return null;
  264.     }

  265.     /**
  266.      * Retrieve the Yield Frequency
  267.      *
  268.      * @return The Yield Frequency
  269.      */

  270.     public int freq()
  271.     {
  272.         return _iFreq;
  273.     }

  274.     /**
  275.      * Retrieve the Yield Day Count
  276.      *
  277.      * @return The Yield Day Count
  278.      */

  279.     public java.lang.String dayCount()
  280.     {
  281.         return _strDayCount;
  282.     }

  283.     /**
  284.      * Retrieve the Manifest Measure Jacobian of the Forward Rate to the given date
  285.      *
  286.      * @param strManifestMeasure Manifest Measure
  287.      * @param iDate Date
  288.      *
  289.      * @return The Manifest Measure Jacobian of the Forward Rate to the given date
  290.      */

  291.     public abstract org.drip.numerical.differentiation.WengertJacobian jackDForwardDManifestMeasure (
  292.         final java.lang.String strManifestMeasure,
  293.         final int iDate);

  294.     /**
  295.      * Retrieve the Manifest Measure Jacobian of the Forward Rate to the given date
  296.      *
  297.      * @param strManifestMeasure Manifest Measure
  298.      * @param dt Date
  299.      *
  300.      * @return The Manifest Measure Jacobian of the Forward Rate to the given date
  301.      */

  302.     public org.drip.numerical.differentiation.WengertJacobian jackDForwardDManifestMeasure (
  303.         final java.lang.String strManifestMeasure,
  304.         final org.drip.analytics.date.JulianDate dt)
  305.     {
  306.         if (null == dt) return null;

  307.         return jackDForwardDManifestMeasure (strManifestMeasure, dt.julian());
  308.     }

  309.     /**
  310.      * Retrieve the Manifest Measure Jacobian of the Forward Rate to the date implied by the given Tenor
  311.      *
  312.      * @param strManifestMeasure Manifest Measure
  313.      * @param strTenor Tenor
  314.      *
  315.      * @return The Manifest Measure Jacobian of the Forward Rate to the date implied by the given Tenor
  316.      */

  317.     public org.drip.numerical.differentiation.WengertJacobian jackDForwardDManifestMeasure (
  318.         final java.lang.String strManifestMeasure,
  319.         final java.lang.String strTenor)
  320.     {
  321.         if (null == strTenor || strTenor.isEmpty()) return null;

  322.         try {
  323.             return jackDForwardDManifestMeasure (strManifestMeasure, epoch().addTenor (strTenor));
  324.         } catch (java.lang.Exception e) {
  325.             e.printStackTrace();
  326.         }

  327.         return null;
  328.     }
  329. }