TreasuryBondExplainProcessor.java

  1. package org.drip.historical.engine;

  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>TreasuryBondExplainProcessor</i> contains the Functionality associated with the Horizon Analysis of the
  79.  * Treasury 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/historical/README.md">Historical State Processing Utilities</a></li>
  86.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/historical/engine/README.md">Product Horizon Change Explain Engine</a></li>
  87.  *  </ul>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class TreasuryBondExplainProcessor extends org.drip.historical.engine.HorizonChangeExplainProcessor {

  92.     /**
  93.      * TreasuryBondExplainProcessor Constructor
  94.      *
  95.      * @param tsyComponent The Treasury Component
  96.      * @param strMarketMeasureName The Market Measure Name
  97.      * @param dblMarketMeasureValue The Market Measure Value
  98.      * @param dtFirst First Date
  99.      * @param dtSecond Second Date
  100.      * @param csqcFirst First Market Parameters
  101.      * @param csqcSecond Second Market Parameters
  102.      * @param mapCSQCRollDown Map of the Roll Down Market Parameters
  103.      *
  104.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  105.      */

  106.     public TreasuryBondExplainProcessor (
  107.         final org.drip.product.govvie.TreasuryComponent tsyComponent,
  108.         final java.lang.String strMarketMeasureName,
  109.         final double dblMarketMeasureValue,
  110.         final org.drip.analytics.date.JulianDate dtFirst,
  111.         final org.drip.analytics.date.JulianDate dtSecond,
  112.         final org.drip.param.market.CurveSurfaceQuoteContainer csqcFirst,
  113.         final org.drip.param.market.CurveSurfaceQuoteContainer csqcSecond,
  114.         final
  115.             org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.param.market.CurveSurfaceQuoteContainer>
  116.             mapCSQCRollDown)
  117.         throws java.lang.Exception
  118.     {
  119.         super (tsyComponent, 0, strMarketMeasureName, dblMarketMeasureValue, dtFirst, dtSecond, csqcFirst,
  120.             csqcSecond, mapCSQCRollDown);
  121.     }

  122.     @Override public org.drip.historical.engine.MarketMeasureRollDown rollDownMeasureMap()
  123.     {
  124.         org.drip.product.definition.Component comp = component();

  125.         int iMaturityDate = comp.maturityDate().julian();

  126.         org.drip.historical.engine.MarketMeasureRollDown mmrd = null;

  127.         org.drip.state.identifier.GovvieLabel govvieLabel = comp.govvieLabel();

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

  130.         for (java.lang.String strRollDownTenor : mapCSQCRollDown.keySet()) {
  131.             org.drip.state.govvie.GovvieCurve gc = mapCSQCRollDown.get (strRollDownTenor).govvieState
  132.                 (govvieLabel);

  133.             try {
  134.                 double dblMarketMeasureRollDown = gc.yield (iMaturityDate);

  135.                 if ("Native".equalsIgnoreCase (strRollDownTenor))
  136.                     mmrd = new org.drip.historical.engine.MarketMeasureRollDown (dblMarketMeasureRollDown);
  137.                 else
  138.                     mmrd.add (strRollDownTenor, dblMarketMeasureRollDown);
  139.             } catch (java.lang.Exception e) {
  140.                 e.printStackTrace();

  141.                 return null;
  142.             }
  143.         }

  144.         return mmrd;
  145.     }

  146.     @Override public org.drip.historical.attribution.PositionMarketSnap snapFirstMarketValue()
  147.     {
  148.         org.drip.analytics.date.JulianDate dtValuation = firstDate();

  149.         org.drip.product.govvie.TreasuryComponent tsyComponent = (org.drip.product.govvie.TreasuryComponent)
  150.             component();

  151.         org.drip.param.market.CurveSurfaceQuoteContainer csqc = firstMarketParameters();

  152.         org.drip.state.identifier.GovvieLabel govvieLabel = tsyComponent.govvieLabel();

  153.         org.drip.analytics.date.JulianDate dtEffective = tsyComponent.effectiveDate();

  154.         org.drip.analytics.date.JulianDate dtMaturity = tsyComponent.maturityDate();

  155.         org.drip.state.govvie.GovvieCurve gc = csqc.govvieState (govvieLabel);

  156.         double dblFixedCoupon = tsyComponent.couponSetting().couponRate();

  157.         java.lang.String strCurrency = tsyComponent.currency();

  158.         int iValuationDate = dtValuation.julian();

  159.         org.drip.param.valuation.ValuationParams valParams = org.drip.param.valuation.ValuationParams.Spot
  160.             (iValuationDate);

  161.         org.drip.historical.engine.MarketMeasureRollDown mmrd = rollDownMeasureMap();

  162.         if (null == mmrd) return null;

  163.         double dblRollDownInnate = mmrd.innate();

  164.         org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double> mapHorizonMetric =
  165.             mmrd.horizon();

  166.         java.lang.String strAccrualDC = tsyComponent.accrualDC();

  167.         try {
  168.             double dblYield = gc.yield (dtMaturity.julian());

  169.             double dblAccrued = tsyComponent.accrued (iValuationDate, csqc);

  170.             double dblCleanPrice = tsyComponent.priceFromYield (valParams, csqc, null, dblYield);

  171.             double dblYieldSensitivity = 10000. * tsyComponent.modifiedDurationFromYield (valParams, csqc,
  172.                 null, dblYield);

  173.             double dblCumulativeCouponDCF = org.drip.analytics.daycount.Convention.YearFraction
  174.                 (dtEffective.julian(), iValuationDate, strAccrualDC, false,
  175.                     org.drip.analytics.daycount.ActActDCParams.FromFrequency (gc.freq()), strCurrency);

  176.             org.drip.historical.attribution.PositionMarketSnap pms = new
  177.                 org.drip.historical.attribution.PositionMarketSnap (dtValuation, dblCleanPrice);

  178.             if (!pms.setR1 ("Accrued", dblAccrued)) return null;

  179.             if (!pms.setC1 ("AccruedDC", strAccrualDC)) return null;

  180.             if (!pms.setR1 ("CleanPrice", dblCleanPrice)) return null;

  181.             if (!pms.setR1 ("CumulativeCouponAmount", dblCumulativeCouponDCF * dblFixedCoupon)) return null;

  182.             if (!pms.setR1 ("CumulativeCouponDCF", dblCumulativeCouponDCF)) return null;

  183.             if (!pms.setC1 ("Currency", strCurrency)) return null;

  184.             if (!pms.setR1 ("DirtyPrice", dblCleanPrice + dblAccrued)) return null;

  185.             if (!pms.setDate ("EffectiveDate", dtEffective)) return null;

  186.             if (!pms.setC1 ("FixedAccrualDayCount", strAccrualDC)) return null;

  187.             if (!pms.setR1 ("FixedCoupon", dblFixedCoupon)) return null;

  188.             if (!pms.setDate ("MaturityDate", dtMaturity)) return null;

  189.             if (!pms.setC1 ("MaturityTenor", tsyComponent.tenor())) return null;

  190.             if (!pms.setR1 ("ModifiedDuration", dblYieldSensitivity)) return null;

  191.             if (!pms.setR1 ("Yield", dblYield)) return null;

  192.             if (!pms.setR1 ("YieldRollDown", dblRollDownInnate)) return null;

  193.             for (java.lang.String strRollDownTenor : mapHorizonMetric.keySet()) {
  194.                 if (!pms.setR1 ("YieldRollDown" + strRollDownTenor, mapHorizonMetric.get (strRollDownTenor)))
  195.                     return null;
  196.             }

  197.             if (!pms.addManifestMeasureSnap ("Yield", dblYield, -1. * dblYieldSensitivity,
  198.                 dblRollDownInnate))
  199.                 return null;

  200.             return pms;
  201.         } catch (java.lang.Exception e) {
  202.             e.printStackTrace();
  203.         }

  204.         return null;
  205.     }

  206.     @Override public boolean updateFixings()
  207.     {
  208.         return true;
  209.     }

  210.     @Override public org.drip.historical.attribution.PositionMarketSnap snapSecondMarketValue()
  211.     {
  212.         org.drip.product.govvie.TreasuryComponent tsyComponent = (org.drip.product.govvie.TreasuryComponent)
  213.             component();

  214.         org.drip.analytics.date.JulianDate dtValuation = secondDate();

  215.         int iValuationDate = dtValuation.julian();

  216.         org.drip.param.market.CurveSurfaceQuoteContainer csqc = secondMarketParameters();

  217.         org.drip.param.valuation.ValuationParams valParams = org.drip.param.valuation.ValuationParams.Spot
  218.             (iValuationDate);

  219.         try {
  220.             org.drip.state.govvie.GovvieCurve gc = csqc.govvieState (tsyComponent.govvieLabel());

  221.             double dblYield = gc.yield (tsyComponent.maturityDate().julian());

  222.             double dblCumulativeCouponDCF = org.drip.analytics.daycount.Convention.YearFraction
  223.                 (tsyComponent.effectiveDate().julian(), iValuationDate, tsyComponent.accrualDC(), false,
  224.                     org.drip.analytics.daycount.ActActDCParams.FromFrequency (gc.freq()),
  225.                         tsyComponent.currency());

  226.             org.drip.historical.attribution.PositionMarketSnap pms = new
  227.                 org.drip.historical.attribution.PositionMarketSnap (dtValuation, tsyComponent.priceFromYield
  228.                     (valParams, csqc, null, dblYield));

  229.             if (!pms.setR1 ("CumulativeCouponAmount", dblCumulativeCouponDCF *
  230.                 tsyComponent.couponSetting().couponRate()))
  231.                 return null;

  232.             if (!pms.setR1 ("CumulativeCouponDCF", dblCumulativeCouponDCF)) return null;

  233.             if (!pms.setR1 ("Yield", dblYield)) return null;

  234.             if (!pms.addManifestMeasureSnap ("Yield", dblYield, -10000. *
  235.                 tsyComponent.modifiedDurationFromYield (valParams, csqc, null, dblYield), 0.))
  236.                 return null;

  237.             return pms;
  238.         } catch (java.lang.Exception e) {
  239.             e.printStackTrace();
  240.         }

  241.         return null;
  242.     }

  243.     @Override public org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double>
  244.         crossHorizonDifferentialMetrics (
  245.             final org.drip.historical.attribution.PositionMarketSnap pmsFirst,
  246.             final org.drip.historical.attribution.PositionMarketSnap pmsSecond)
  247.     {
  248.         if (null == pmsFirst || null == pmsSecond) return null;

  249.         org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double> mapDifferentialMetric = new
  250.             org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.Double>();

  251.         org.drip.analytics.date.JulianDate dtEffective = pmsFirst.date ("EffectiveDate");

  252.         java.lang.String strAccrualDC = pmsFirst.c1 ("AccruedDC");

  253.         java.lang.String strCalendar = pmsFirst.c1 ("Currency");

  254.         int iDate1M = dtEffective.addTenor ("1M").julian();

  255.         int iDate3M = dtEffective.addTenor ("3M").julian();

  256.         int iEffectiveDate = dtEffective.julian();

  257.         org.drip.analytics.daycount.ActActDCParams aap =
  258.             org.drip.analytics.daycount.ActActDCParams.FromFrequency
  259.                 (((org.drip.product.govvie.TreasuryComponent) component()).freq());

  260.         try {
  261.             mapDifferentialMetric.put ("CumulativeCouponAmount", pmsSecond.r1 ("CumulativeCouponAmount") -
  262.                 pmsFirst.r1 ("CumulativeCouponAmount"));

  263.             mapDifferentialMetric.put ("CumulativeCouponDCF", pmsSecond.r1 ("CumulativeCouponDCF") -
  264.                 pmsFirst.r1 ("CumulativeCouponDCF"));

  265.             mapDifferentialMetric.put ("CumulativeCouponDCF1M",
  266.                 org.drip.analytics.daycount.Convention.YearFraction (iEffectiveDate, iDate1M, strAccrualDC,
  267.                     false, aap, strCalendar));

  268.             mapDifferentialMetric.put ("CumulativeCouponDCF3M",
  269.                 org.drip.analytics.daycount.Convention.YearFraction (iEffectiveDate, iDate3M, strAccrualDC,
  270.                     false, aap, strCalendar));

  271.             return mapDifferentialMetric;
  272.         } catch (java.lang.Exception e) {
  273.             e.printStackTrace();
  274.         }

  275.         return null;
  276.     }
  277. }