TreasuryAPI.java

  1. package org.drip.service.product;

  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>TreasuryAPI</i> demonstrates the Details behind the Pricing and the Scenario Runs behind a Treasury
  79.  * Bond.
  80.  *
  81.  * <br><br>
  82.  *  <ul>
  83.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  84.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  85.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/service/README.md">Environment, Product/Definition Containers, and Scenario/State Manipulation APIs</a></li>
  86.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/service/product/README.md">Product Horizon PnL Attribution Decomposition</a></li>
  87.  *  </ul>
  88.  * <br><br>
  89.  *
  90.  * @author Lakshmi Krishnamurthy
  91.  */

  92. public class TreasuryAPI {

  93.     /**
  94.      * Compute the Horizon Change Attribution Details for the Specified Treasury Bond
  95.      *
  96.      * @param gcFirst First Day Govvie Curve
  97.      * @param gcSecond Second Date Govvie Curve
  98.      * @param mapRollDownGovvieCurve Map of the Roll Down Govvie Curves
  99.      * @param strMaturityTenor Treasury Bond Maturity Tenor
  100.      * @param strCode Treasury Bond Code
  101.      *
  102.      * @return The Horizon Change Attribution Instance
  103.      */

  104.     public static final org.drip.historical.attribution.PositionChangeComponents HorizonChangeAttribution (
  105.         final org.drip.state.govvie.GovvieCurve gcFirst,
  106.         final org.drip.state.govvie.GovvieCurve gcSecond,
  107.         final org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.state.govvie.GovvieCurve>
  108.             mapRollDownGovvieCurve,
  109.         final java.lang.String strMaturityTenor,
  110.         final java.lang.String strCode)
  111.     {
  112.         if (null == gcFirst || null == mapRollDownGovvieCurve || 0 == mapRollDownGovvieCurve.size())
  113.             return null;

  114.         double dblFirstGovvieCurveYield = java.lang.Double.NaN;

  115.         try {
  116.             dblFirstGovvieCurveYield = gcFirst.yield (strMaturityTenor);
  117.         } catch (java.lang.Exception e) {
  118.             e.printStackTrace();

  119.             return null;
  120.         }

  121.         org.drip.analytics.date.JulianDate dtFirst = gcFirst.epoch();

  122.         org.drip.param.market.CurveSurfaceQuoteContainer csqcFirst = new
  123.             org.drip.param.market.CurveSurfaceQuoteContainer();

  124.         if (!csqcFirst.setGovvieState (gcFirst)) return null;

  125.         org.drip.param.market.CurveSurfaceQuoteContainer csqcSecond = new
  126.             org.drip.param.market.CurveSurfaceQuoteContainer();

  127.         if (!csqcSecond.setGovvieState (gcSecond)) return null;

  128.         org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.param.market.CurveSurfaceQuoteContainer>
  129.             mapCSQCRollDown = new
  130.                 org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.param.market.CurveSurfaceQuoteContainer>();

  131.         for (java.lang.String strRollDownTenor : mapRollDownGovvieCurve.keySet()) {
  132.             org.drip.param.market.CurveSurfaceQuoteContainer csqcRollDown = new
  133.                 org.drip.param.market.CurveSurfaceQuoteContainer();

  134.             org.drip.state.govvie.GovvieCurve gcRollDown = mapRollDownGovvieCurve.get (strRollDownTenor);

  135.             if (null == gcRollDown || !csqcRollDown.setGovvieState (gcRollDown)) return null;

  136.             mapCSQCRollDown.put (strRollDownTenor, csqcRollDown);
  137.         }

  138.         try {
  139.             return org.drip.historical.engine.HorizonChangeExplainExecutor.GenerateAttribution (new
  140.                 org.drip.historical.engine.TreasuryBondExplainProcessor
  141.                     (org.drip.service.template.TreasuryBuilder.FromCode (strCode, dtFirst, dtFirst.addTenor
  142.                         (strMaturityTenor), dblFirstGovvieCurveYield), "Yield", dblFirstGovvieCurveYield,
  143.                             dtFirst, gcSecond.epoch(), csqcFirst, csqcSecond, mapCSQCRollDown));
  144.         } catch (java.lang.Exception e) {
  145.             e.printStackTrace();
  146.         }

  147.         return null;
  148.     }

  149.     /**
  150.      * Generate the Govvie Curve Horizon Metrics
  151.      *
  152.      * @param dtFirst The First Date
  153.      * @param dtSecond The Second Date
  154.      * @param astrGovvieTreasuryInstrumentTenor Array of Govvie Curve Treasury Instrument Maturity Tenors
  155.      * @param adblFirstGovvieTreasuryInstrument Array of First Date Govvie Curve Treasury Instrument Quotes
  156.      * @param adblSecondGovvieTreasuryInstrument Array of Second Date Govvie Curve Treasury Instrument Quotes
  157.      * @param strMaturityTenor Treasury Bond Maturity Tenor
  158.      * @param strCode Treasury Bond Code
  159.      * @param astrRollDownHorizon Array of the Roll Down Horizon Tenors
  160.      * @param iLatentStateType Latent State Type
  161.      *
  162.      * @return The Govvie Curve Horizon Metrics
  163.      */

  164.     public static final org.drip.historical.attribution.PositionChangeComponents HorizonChangeAttribution (
  165.         final org.drip.analytics.date.JulianDate dtFirst,
  166.         final org.drip.analytics.date.JulianDate dtSecond,
  167.         final java.lang.String[] astrGovvieTreasuryInstrumentTenor,
  168.         final double[] adblFirstGovvieTreasuryInstrument,
  169.         final double[] adblSecondGovvieTreasuryInstrument,
  170.         final java.lang.String strMaturityTenor,
  171.         final java.lang.String strCode,
  172.         final java.lang.String[] astrRollDownHorizon,
  173.         final int iLatentStateType)
  174.     {
  175.         if (null == dtFirst || null == dtSecond || dtFirst.julian() >= dtSecond.julian()) return null;

  176.         int iNumGovvieTreasuryInstrument = null == astrGovvieTreasuryInstrumentTenor ? 0 :
  177.             astrGovvieTreasuryInstrumentTenor.length;
  178.         int iNumFirstGovvieTreasuryInstrument = null == adblFirstGovvieTreasuryInstrument ? 0 :
  179.             adblFirstGovvieTreasuryInstrument.length;
  180.         int iNumSecondGovvieTreasuryInstrument = null == adblSecondGovvieTreasuryInstrument ? 0 :
  181.             adblSecondGovvieTreasuryInstrument.length;
  182.         int iNumRollDownHorizon = null == astrRollDownHorizon ? 0 : astrRollDownHorizon .length;
  183.         org.drip.analytics.date.JulianDate[] adtFirstEffective = new
  184.             org.drip.analytics.date.JulianDate[iNumGovvieTreasuryInstrument];
  185.         org.drip.analytics.date.JulianDate[] adtFirstMaturity = new
  186.             org.drip.analytics.date.JulianDate[iNumGovvieTreasuryInstrument];
  187.         org.drip.analytics.date.JulianDate[] adtSecondEffective = new
  188.             org.drip.analytics.date.JulianDate[iNumGovvieTreasuryInstrument];
  189.         org.drip.analytics.date.JulianDate[] adtSecondMaturity = new
  190.             org.drip.analytics.date.JulianDate[iNumGovvieTreasuryInstrument];
  191.         org.drip.analytics.date.JulianDate[] adtRollDownEffective = new
  192.             org.drip.analytics.date.JulianDate[iNumGovvieTreasuryInstrument];
  193.         org.drip.analytics.date.JulianDate[] adtRollDownMaturity = new
  194.             org.drip.analytics.date.JulianDate[iNumGovvieTreasuryInstrument];

  195.         org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.state.govvie.GovvieCurve>
  196.             mapRollDownGovvieCurve = 0 == iNumRollDownHorizon ? null : new
  197.                 org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.state.govvie.GovvieCurve>();

  198.         if (0 == iNumGovvieTreasuryInstrument || iNumGovvieTreasuryInstrument !=
  199.             iNumFirstGovvieTreasuryInstrument || iNumGovvieTreasuryInstrument !=
  200.                 iNumSecondGovvieTreasuryInstrument)
  201.             return null;

  202.         for (int i = 0; i < iNumGovvieTreasuryInstrument; ++i) {
  203.             adtFirstMaturity[i] = (adtFirstEffective[i] = dtFirst).addTenor
  204.                 (astrGovvieTreasuryInstrumentTenor[i]);

  205.             adtSecondMaturity[i] = (adtSecondEffective[i] = dtSecond).addTenor
  206.                 (astrGovvieTreasuryInstrumentTenor[i]);
  207.         }

  208.         org.drip.state.govvie.GovvieCurve gcFirst =
  209.             org.drip.service.template.LatentMarketStateBuilder.GovvieCurve (strCode, dtFirst,
  210.                 adtFirstEffective, adtFirstMaturity, adblFirstGovvieTreasuryInstrument,
  211.                     adblFirstGovvieTreasuryInstrument, "Yield", iLatentStateType);

  212.         org.drip.state.govvie.GovvieCurve gcSecond =
  213.             org.drip.service.template.LatentMarketStateBuilder.GovvieCurve (strCode, dtSecond,
  214.                 adtSecondEffective, adtSecondMaturity, adblSecondGovvieTreasuryInstrument,
  215.                     adblSecondGovvieTreasuryInstrument, "Yield", iLatentStateType);

  216.         org.drip.state.govvie.GovvieCurve gcRollDown =
  217.             org.drip.service.template.LatentMarketStateBuilder.GovvieCurve (strCode, dtSecond,
  218.                 adtSecondEffective, adtSecondMaturity, adblFirstGovvieTreasuryInstrument,
  219.                     adblFirstGovvieTreasuryInstrument, "Yield", iLatentStateType);

  220.         if (null == gcRollDown) return null;

  221.         mapRollDownGovvieCurve.put ("Native", gcRollDown);

  222.         for (int j = 0; j < iNumRollDownHorizon; ++j) {
  223.             org.drip.analytics.date.JulianDate dtRollDown = dtFirst.addTenor (astrRollDownHorizon[j]);

  224.             for (int i = 0; i < iNumGovvieTreasuryInstrument; ++i)
  225.                 adtRollDownMaturity[i] = (adtRollDownEffective[i] = dtRollDown).addTenor
  226.                     (astrGovvieTreasuryInstrumentTenor[i]);

  227.             org.drip.state.govvie.GovvieCurve gcHorizonRollDown =
  228.                 org.drip.service.template.LatentMarketStateBuilder.GovvieCurve (strCode, dtRollDown,
  229.                     adtRollDownEffective, adtRollDownMaturity, adblFirstGovvieTreasuryInstrument,
  230.                         adblFirstGovvieTreasuryInstrument, "Yield", iLatentStateType);

  231.             if (null == gcHorizonRollDown) return null;

  232.             mapRollDownGovvieCurve.put (astrRollDownHorizon[j], gcHorizonRollDown);
  233.         }

  234.         return HorizonChangeAttribution (gcFirst, gcSecond, mapRollDownGovvieCurve, strMaturityTenor,
  235.             strCode);
  236.     }

  237.     /**
  238.      * Generate the Govvie Curve Horizon Metrics
  239.      *
  240.      * @param adtSpot Array of the Spot Dates
  241.      * @param iHorizonGap The Horizon Gap
  242.      * @param astrGovvieTreasuryInstrumentTenor Array of Govvie Curve Treasury Instrument Maturity Tenors
  243.      * @param aadblGovvieTreasuryInstrumentQuote Array of Govvie Curve Treasury Instrument Quotes
  244.      * @param strMaturityTenor Treasury Bond Maturity Tenor
  245.      * @param strCode Treasury Bond Code
  246.      * @param astrRollDownHorizon Array of the Roll Down Horizon Tenors
  247.      * @param iLatentStateType Latent State Type
  248.      *
  249.      * @return The Govvie Curve Horizon Metrics
  250.      */

  251.     public static final java.util.List<org.drip.historical.attribution.PositionChangeComponents>
  252.         HorizonChangeAttribution (
  253.             final org.drip.analytics.date.JulianDate[] adtSpot,
  254.             final int iHorizonGap,
  255.             final java.lang.String[] astrGovvieTreasuryInstrumentTenor,
  256.             final double[][] aadblGovvieTreasuryInstrumentQuote,
  257.             final java.lang.String strMaturityTenor,
  258.             final java.lang.String strCode,
  259.             final java.lang.String[] astrRollDownHorizon,
  260.             final int iLatentStateType)
  261.     {
  262.         if (null == adtSpot || 0 >= iHorizonGap || null == aadblGovvieTreasuryInstrumentQuote) return null;

  263.         int iNumClose = adtSpot.length;
  264.         int iNumRollDownTenor = null == astrRollDownHorizon ? 0 : astrRollDownHorizon.length;

  265.         if (0 == iNumClose || iNumClose != aadblGovvieTreasuryInstrumentQuote.length || 0 ==
  266.             iNumRollDownTenor)
  267.             return null;

  268.         java.util.List<org.drip.historical.attribution.PositionChangeComponents> lsPCC = new
  269.             java.util.ArrayList<org.drip.historical.attribution.PositionChangeComponents>();

  270.         for (int i = iHorizonGap; i < iNumClose; ++i) {
  271.             org.drip.historical.attribution.PositionChangeComponents pcc = HorizonChangeAttribution
  272.                 (adtSpot[i - iHorizonGap], adtSpot[i], astrGovvieTreasuryInstrumentTenor,
  273.                     aadblGovvieTreasuryInstrumentQuote[i - iHorizonGap],
  274.                         aadblGovvieTreasuryInstrumentQuote[i], strMaturityTenor, strCode,
  275.                             astrRollDownHorizon, iLatentStateType);

  276.             if (null != pcc) lsPCC.add (pcc);
  277.         }

  278.         return lsPCC;
  279.     }
  280. }