SingleStreamComponent.java

  1. package org.drip.product.rates;

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

  79. /**
  80.  * <i>SingleStreamComponent</i> implements fixed income component that is based off of a single stream.
  81.  *
  82.  * <br><br>
  83.  *  <ul>
  84.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  85.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  86.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/product/README.md">Product Components/Baskets for Credit, FRA, FX, Govvie, Rates, and Option AssetClasses</a></li>
  87.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/product/rates/README.md">Fixed Income Multi-Stream Components</a></li>
  88.  *  </ul>
  89.  * <br><br>
  90.  *
  91.  * @author Lakshmi Krishnamurthy
  92.  */

  93. public class SingleStreamComponent extends org.drip.product.definition.CalibratableComponent {
  94.     private java.lang.String _strCode = "";
  95.     private java.lang.String _strName = "";
  96.     private org.drip.product.rates.Stream _stream = null;
  97.     private org.drip.param.valuation.CashSettleParams _csp = null;

  98.     /**
  99.      * SingleStreamComponent constructor
  100.      *
  101.      * @param strName The Component Name
  102.      * @param stream The Single Stream Instance
  103.      * @param csp Cash Settle Parameters Instance
  104.      *
  105.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  106.      */

  107.     public SingleStreamComponent (
  108.         final java.lang.String strName,
  109.         final org.drip.product.rates.Stream stream,
  110.         final org.drip.param.valuation.CashSettleParams csp)
  111.         throws java.lang.Exception
  112.     {
  113.         if (null == (_strName = strName) || _strName.isEmpty() || null == (_stream = stream))
  114.             throw new java.lang.Exception ("SingleStreamComponent ctr: Invalid Inputs");

  115.         _csp = csp;
  116.     }

  117.     /**
  118.      * Retrieve the Stream Instance
  119.      *
  120.      * @return The Stream Instance
  121.      */

  122.     public org.drip.product.rates.Stream stream()
  123.     {
  124.         return _stream;
  125.     }

  126.     @Override public java.lang.String name()
  127.     {
  128.         return _strName;
  129.     }

  130.     @Override public org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.String> couponCurrency()
  131.     {
  132.         org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.String> mapCouponCurrency = new
  133.             org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.String>();

  134.         mapCouponCurrency.put (name(), _stream.couponCurrency());

  135.         return mapCouponCurrency;
  136.     }

  137.     @Override public java.lang.String payCurrency()
  138.     {
  139.         return _stream.payCurrency();
  140.     }

  141.     @Override public java.lang.String principalCurrency()
  142.     {
  143.         return null;
  144.     }

  145.     @Override public
  146.         org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.identifier.ForwardLabel>
  147.         forwardLabel()
  148.     {
  149.         org.drip.state.identifier.ForwardLabel forwardLabel = _stream.forwardLabel();

  150.         if (null == forwardLabel) return null;

  151.         org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.identifier.ForwardLabel>
  152.             mapForwardLabel = new
  153.                 org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.identifier.ForwardLabel>();

  154.         mapForwardLabel.put (
  155.             "DERIVED",
  156.             forwardLabel
  157.         );

  158.         return mapForwardLabel;
  159.     }

  160.     @Override public
  161.         org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.identifier.OTCFixFloatLabel>
  162.         otcFixFloatLabel()
  163.     {
  164.         org.drip.state.identifier.OTCFixFloatLabel otcFixFloatLabel = _stream.otcFixFloatLabel();

  165.         if (null == otcFixFloatLabel) return null;

  166.         org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.identifier.OTCFixFloatLabel>
  167.             mapOTCFixFloatLabel = new
  168.                 org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.identifier.OTCFixFloatLabel>();

  169.         mapOTCFixFloatLabel.put (
  170.             "DERIVED",
  171.             otcFixFloatLabel
  172.         );

  173.         return mapOTCFixFloatLabel;
  174.     }

  175.     @Override public org.drip.state.identifier.FundingLabel fundingLabel()
  176.     {
  177.         return _stream.fundingLabel();
  178.     }

  179.     @Override public org.drip.state.identifier.GovvieLabel govvieLabel()
  180.     {
  181.         return org.drip.state.identifier.GovvieLabel.Standard (payCurrency());
  182.     }

  183.     @Override public org.drip.state.identifier.EntityCDSLabel creditLabel()
  184.     {
  185.         return _stream.creditLabel();
  186.     }

  187.     @Override public org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.identifier.FXLabel>
  188.         fxLabel()
  189.     {
  190.         org.drip.state.identifier.FXLabel fxLabel = _stream.fxLabel();

  191.         if (null == fxLabel) return null;

  192.         org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.identifier.FXLabel> mapFXLabel = new
  193.             org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.identifier.FXLabel>();

  194.         mapFXLabel.put (name(), fxLabel);

  195.         return mapFXLabel;
  196.     }

  197.     @Override public
  198.         org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.state.identifier.VolatilityLabel>
  199.             volatilityLabel()
  200.     {
  201.         return null;
  202.     }

  203.     @Override public double initialNotional()
  204.         throws java.lang.Exception
  205.     {
  206.         return _stream.initialNotional();
  207.     }

  208.     @Override public double notional (
  209.         final int iDate)
  210.         throws java.lang.Exception
  211.     {
  212.         return _stream.notional (iDate);
  213.     }

  214.     @Override public double notional (
  215.         final int iDate1,
  216.         final int iDate2)
  217.         throws java.lang.Exception
  218.     {
  219.         return _stream.notional (iDate1, iDate2);
  220.     }

  221.     @Override public org.drip.analytics.output.CompositePeriodCouponMetrics couponMetrics (
  222.         final int iAccrualEndDate,
  223.         final org.drip.param.valuation.ValuationParams valParams,
  224.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs)
  225.     {
  226.         return _stream.coupon (iAccrualEndDate, valParams, csqs);
  227.     }

  228.     @Override public int freq()
  229.     {
  230.         return _stream.freq();
  231.     }

  232.     @Override public org.drip.analytics.date.JulianDate effectiveDate()
  233.     {
  234.         return _stream.effective();
  235.     }

  236.     @Override public org.drip.analytics.date.JulianDate maturityDate()
  237.     {
  238.         return _stream.maturity();
  239.     }

  240.     @Override public org.drip.analytics.date.JulianDate firstCouponDate()
  241.     {
  242.         return _stream.firstCouponDate();
  243.     }

  244.     @Override public java.util.List<org.drip.analytics.cashflow.CompositePeriod> couponPeriods()
  245.     {
  246.         return _stream.cashFlowPeriod();
  247.     }

  248.     @Override public org.drip.param.valuation.CashSettleParams cashSettleParams()
  249.     {
  250.         return _csp;
  251.     }

  252.     @Override public org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double> value (
  253.         final org.drip.param.valuation.ValuationParams valParams,
  254.         final org.drip.param.pricer.CreditPricerParams pricerParams,
  255.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  256.         final org.drip.param.valuation.ValuationCustomizationParams quotingParams)
  257.     {
  258.         org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double> mapResult = _stream.value
  259.             (valParams, pricerParams, csqs, quotingParams);

  260.         if (null == mapResult) return null;

  261.         mapResult.put ("ForwardRate", mapResult.get ("Rate"));

  262.         return mapResult;
  263.     }

  264.     @Override public java.util.Set<java.lang.String> measureNames()
  265.     {
  266.         java.util.Set<java.lang.String> setMeasureNames = _stream.availableMeasures();

  267.         setMeasureNames.add ("ForwardRate");

  268.         return setMeasureNames;
  269.     }

  270.     @Override public double pv (
  271.         final org.drip.param.valuation.ValuationParams valParams,
  272.         final org.drip.param.pricer.CreditPricerParams pricerParams,
  273.         final org.drip.param.market.CurveSurfaceQuoteContainer csqc,
  274.         final org.drip.param.valuation.ValuationCustomizationParams vcp)
  275.         throws java.lang.Exception
  276.     {
  277.         return _stream.pv (valParams, pricerParams, csqc, vcp);
  278.     }

  279.     @Override public void setPrimaryCode (
  280.         final java.lang.String strCode)
  281.     {
  282.         _strCode = strCode;
  283.     }

  284.     @Override public java.lang.String primaryCode()
  285.     {
  286.         return _strCode;
  287.     }

  288.     @Override public org.drip.numerical.differentiation.WengertJacobian jackDDirtyPVDManifestMeasure (
  289.         final org.drip.param.valuation.ValuationParams valParams,
  290.         final org.drip.param.pricer.CreditPricerParams pricerParams,
  291.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  292.         final org.drip.param.valuation.ValuationCustomizationParams quotingParams)
  293.     {
  294.         if (null == valParams || valParams.valueDate() >= maturityDate().julian() || null == csqs)
  295.             return null;

  296.         org.drip.state.discount.MergedDiscountForwardCurve dcFunding = csqs.fundingState (fundingLabel());

  297.         if (null == dcFunding) return null;

  298.         try {
  299.             org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double> mapMeasures = value
  300.                 (valParams, pricerParams, csqs, quotingParams);

  301.             if (null == mapMeasures) return null;

  302.             int iEffectiveDate = effectiveDate().julian();

  303.             double dblDFEffective = dcFunding.df (iEffectiveDate);

  304.             double dblDFMaturity = dcFunding.df (maturityDate().julian());

  305.             org.drip.numerical.differentiation.WengertJacobian wjDFEffective = dcFunding.jackDDFDManifestMeasure
  306.                 (iEffectiveDate, "Rate");

  307.             org.drip.numerical.differentiation.WengertJacobian wjDFMaturity = dcFunding.jackDDFDManifestMeasure
  308.                 (maturityDate().julian(), "Rate");

  309.             if (null == wjDFEffective || null == wjDFMaturity) return null;

  310.             org.drip.numerical.differentiation.WengertJacobian wjPVDFMicroJack = new
  311.                 org.drip.numerical.differentiation.WengertJacobian (1, wjDFMaturity.numParameters());

  312.             for (int i = 0; i < wjDFMaturity.numParameters(); ++i) {
  313.                 if (!wjPVDFMicroJack.accumulatePartialFirstDerivative (0, i, wjDFMaturity.firstDerivative (0,
  314.                     i) / dblDFEffective))
  315.                     return null;

  316.                 if (!wjPVDFMicroJack.accumulatePartialFirstDerivative (0, i, -wjDFEffective.firstDerivative
  317.                     (0, i) * dblDFMaturity / dblDFEffective / dblDFEffective))
  318.                     return null;
  319.             }

  320.             return wjPVDFMicroJack;
  321.         } catch (java.lang.Exception e) {
  322.             e.printStackTrace();
  323.         }

  324.         return null;
  325.     }

  326.     @Override public org.drip.numerical.differentiation.WengertJacobian manifestMeasureDFMicroJack (
  327.         final java.lang.String strManifestMeasure,
  328.         final org.drip.param.valuation.ValuationParams valParams,
  329.         final org.drip.param.pricer.CreditPricerParams pricerParams,
  330.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  331.         final org.drip.param.valuation.ValuationCustomizationParams quotingParams)
  332.     {
  333.         if (null == valParams || valParams.valueDate() >= maturityDate().julian() || null ==
  334.             strManifestMeasure || strManifestMeasure.isEmpty() || null == csqs)
  335.             return null;

  336.         org.drip.state.discount.MergedDiscountForwardCurve dcFunding = csqs.fundingState (fundingLabel());

  337.         if (null == dcFunding) return null;

  338.         if ("Rate".equalsIgnoreCase (strManifestMeasure)) {
  339.             int iEffectiveDate = effectiveDate().julian();

  340.             try {
  341.                 double dblDFEffective = dcFunding.df (iEffectiveDate);

  342.                 double dblDFMaturity = dcFunding.df (maturityDate().julian());

  343.                 org.drip.numerical.differentiation.WengertJacobian wjDFEffective = dcFunding.jackDDFDManifestMeasure
  344.                     (iEffectiveDate, "Rate");

  345.                 org.drip.numerical.differentiation.WengertJacobian wjDFMaturity = dcFunding.jackDDFDManifestMeasure
  346.                     (maturityDate().julian(), "Rate");

  347.                 if (null == wjDFEffective || null == wjDFMaturity) return null;

  348.                 org.drip.numerical.differentiation.WengertJacobian wjDFMicroJack = new
  349.                     org.drip.numerical.differentiation.WengertJacobian (1, wjDFMaturity.numParameters());

  350.                 for (int i = 0; i < wjDFMaturity.numParameters(); ++i) {
  351.                     if (!wjDFMicroJack.accumulatePartialFirstDerivative (0, i,
  352.                         wjDFMaturity.firstDerivative (0, i) / dblDFEffective))
  353.                         return null;

  354.                     if (!wjDFMicroJack.accumulatePartialFirstDerivative (0, i, -1. *
  355.                         wjDFEffective.firstDerivative (0, i) * dblDFMaturity / dblDFEffective /
  356.                             dblDFEffective))
  357.                         return null;
  358.                 }

  359.                 return wjDFMicroJack;
  360.             } catch (java.lang.Exception e) {
  361.                 e.printStackTrace();
  362.             }
  363.         }

  364.         return null;
  365.     }

  366.     @Override public org.drip.product.calib.ProductQuoteSet calibQuoteSet (
  367.         final org.drip.state.representation.LatentStateSpecification[] aLSS)
  368.     {
  369.         try {
  370.             return new org.drip.product.calib.FloatingStreamQuoteSet (aLSS);
  371.         } catch (java.lang.Exception e) {
  372.             e.printStackTrace();
  373.         }

  374.         return null;
  375.     }

  376.     @Override public org.drip.state.estimator.PredictorResponseWeightConstraint forwardPRWC (
  377.         final org.drip.param.valuation.ValuationParams valParams,
  378.         final org.drip.param.pricer.CreditPricerParams pricerParams,
  379.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  380.         final org.drip.param.valuation.ValuationCustomizationParams vcp,
  381.         final org.drip.product.calib.ProductQuoteSet pqs)
  382.     {
  383.         return _stream.forwardPRWC (valParams, pricerParams, csqs, vcp, pqs);
  384.     }

  385.     @Override public org.drip.state.estimator.PredictorResponseWeightConstraint fundingPRWC (
  386.         final org.drip.param.valuation.ValuationParams valParams,
  387.         final org.drip.param.pricer.CreditPricerParams pricerParams,
  388.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  389.         final org.drip.param.valuation.ValuationCustomizationParams vcp,
  390.         final org.drip.product.calib.ProductQuoteSet pqs)
  391.     {
  392.         return _stream.fundingPRWC (valParams, pricerParams, csqs, vcp, pqs);
  393.     }

  394.     @Override public org.drip.state.estimator.PredictorResponseWeightConstraint fundingForwardPRWC (
  395.         final org.drip.param.valuation.ValuationParams valParams,
  396.         final org.drip.param.pricer.CreditPricerParams pricerParams,
  397.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  398.         final org.drip.param.valuation.ValuationCustomizationParams vcp,
  399.         final org.drip.product.calib.ProductQuoteSet pqs)
  400.     {
  401.         return _stream.fundingForwardPRWC (valParams, pricerParams, csqs, vcp, pqs);
  402.     }

  403.     @Override public org.drip.state.estimator.PredictorResponseWeightConstraint fxPRWC (
  404.         final org.drip.param.valuation.ValuationParams valParams,
  405.         final org.drip.param.pricer.CreditPricerParams pricerParams,
  406.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  407.         final org.drip.param.valuation.ValuationCustomizationParams vcp,
  408.         final org.drip.product.calib.ProductQuoteSet pqs)
  409.     {
  410.         return _stream.fxPRWC (valParams, pricerParams, csqs, vcp, pqs);
  411.     }

  412.     @Override public org.drip.state.estimator.PredictorResponseWeightConstraint govviePRWC (
  413.         final org.drip.param.valuation.ValuationParams valParams,
  414.         final org.drip.param.pricer.CreditPricerParams pricerParams,
  415.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  416.         final org.drip.param.valuation.ValuationCustomizationParams vcp,
  417.         final org.drip.product.calib.ProductQuoteSet pqs)
  418.     {
  419.         return null;
  420.     }

  421.     @Override public org.drip.state.estimator.PredictorResponseWeightConstraint volatilityPRWC (
  422.         final org.drip.param.valuation.ValuationParams valParams,
  423.         final org.drip.param.pricer.CreditPricerParams pricerParams,
  424.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  425.         final org.drip.param.valuation.ValuationCustomizationParams vcp,
  426.         final org.drip.product.calib.ProductQuoteSet pqs)
  427.     {
  428.         return null;
  429.     }

  430.     @Override public org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double> calibMeasures (
  431.         final org.drip.param.valuation.ValuationParams valParams,
  432.         final org.drip.param.pricer.CreditPricerParams pricerParams,
  433.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  434.         final org.drip.param.valuation.ValuationCustomizationParams quotingParams)
  435.     {
  436.         return null;
  437.     }
  438. }