BondStream.java

  1. package org.drip.product.params;

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

  82. /**
  83.  * <i>BondStream</i> is the place-holder for the bond period generation parameters. Contains the bond date
  84.  * adjustment parameters for period start/end, period accrual start/end, effective, maturity, pay and reset,
  85.  * first coupon date, and interest accrual start date. It exports serialization into and de-serialization out
  86.  * of byte arrays.
  87.  *
  88.  * <br><br>
  89.  *  <ul>
  90.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  91.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  92.  *      <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>
  93.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/product/params/README.md">Fixed Income Product Customization Parameters</a></li>
  94.  *  </ul>
  95.  * <br><br>
  96.  *
  97.  * @author Lakshmi Krishnamurthy
  98.  */

  99. public class BondStream extends org.drip.product.rates.Stream {
  100.     private java.lang.String _strMaturityType = "";
  101.     private int _iFinalMaturityDate = java.lang.Integer.MIN_VALUE;

  102.     /**
  103.      * Construct an Instance of BondStream from the First/Penultimate Dates using the specified Parameters
  104.      *
  105.      * @param iMaturityDate Maturity Date
  106.      * @param iEffectiveDate Effective Date
  107.      * @param iFinalMaturityDate Final Maturity Date
  108.      * @param iFirstCouponDate First Coupon Date
  109.      * @param iPenultimateCouponDate Penultimate Coupon Date
  110.      * @param iFreq Coupon Frequency
  111.      * @param dblCoupon Coupon Rate
  112.      * @param strCouponDC Coupon day count convention
  113.      * @param strAccrualDC Accrual day count convention
  114.      * @param dapPay Pay Date Adjustment Parameters
  115.      * @param dapReset Reset Date Adjustment Parameters
  116.      * @param dapMaturity Maturity Date Adjustment Parameters
  117.      * @param dapEffective Effective Date Adjustment Parameters
  118.      * @param dapPeriodEnd Period End Date Adjustment Parameters
  119.      * @param dapAccrualEnd Accrual Date Adjustment Parameters
  120.      * @param dapPeriodStart Period Start Date Adjustment Parameters
  121.      * @param dapAccrualStart Accrual Start  Date Adjustment Parameters
  122.      * @param strMaturityType Maturity Type
  123.      * @param bPeriodsFromForward Generate Periods forward (True) or Backward (False)
  124.      * @param strCalendar Optional Holiday Calendar for Accrual Calculations
  125.      * @param strCurrency Coupon Currency
  126.      * @param floaterLabel The Floater Label
  127.      * @param creditLabel The Credit Label
  128.      *
  129.      * @return The BondStream Instance
  130.      */

  131.     public static final BondStream FromFirstPenultimateCouponDate (
  132.         final int iMaturityDate,
  133.         final int iEffectiveDate,
  134.         final int iFinalMaturityDate,
  135.         final int iFirstCouponDate,
  136.         final int iPenultimateCouponDate,
  137.         final int iFreq,
  138.         final double dblCoupon,
  139.         final java.lang.String strCouponDC,
  140.         final java.lang.String strAccrualDC,
  141.         final org.drip.analytics.daycount.DateAdjustParams dapPay,
  142.         final org.drip.analytics.daycount.DateAdjustParams dapReset,
  143.         final org.drip.analytics.daycount.DateAdjustParams dapMaturity,
  144.         final org.drip.analytics.daycount.DateAdjustParams dapEffective,
  145.         final org.drip.analytics.daycount.DateAdjustParams dapPeriodEnd,
  146.         final org.drip.analytics.daycount.DateAdjustParams dapAccrualEnd,
  147.         final org.drip.analytics.daycount.DateAdjustParams dapPeriodStart,
  148.         final org.drip.analytics.daycount.DateAdjustParams dapAccrualStart,
  149.         final java.lang.String strMaturityType,
  150.         final boolean bPeriodsFromForward,
  151.         final java.lang.String strCalendar,
  152.         final java.lang.String strCurrency,
  153.         final org.drip.state.identifier.FloaterLabel floaterLabel,
  154.         final org.drip.state.identifier.EntityCDSLabel creditLabel)
  155.     {
  156.         java.util.List<org.drip.analytics.cashflow.CompositePeriod> lsCouponPeriod = null == floaterLabel ?
  157.             org.drip.product.creator.StreamBuilder.FirstPenultimateDateFixedStream (
  158.                 iEffectiveDate,
  159.                 iMaturityDate,
  160.                 iFirstCouponDate,
  161.                 iPenultimateCouponDate,
  162.                 iFreq,
  163.                 dblCoupon,
  164.                 strCouponDC,
  165.                 strAccrualDC,
  166.                 dapPay,
  167.                 dapMaturity,
  168.                 dapAccrualEnd,
  169.                 strCurrency,
  170.                 creditLabel
  171.             ) : org.drip.product.creator.StreamBuilder.FirstPenultimateDateFloatStream (
  172.                 iEffectiveDate,
  173.                 iMaturityDate,
  174.                 iFirstCouponDate,
  175.                 iPenultimateCouponDate,
  176.                 iFreq,
  177.                 dblCoupon,
  178.                 dapPay,
  179.                 dapMaturity,
  180.                 dapAccrualEnd,
  181.                 floaterLabel,
  182.                 creditLabel
  183.             );

  184.         try {
  185.             return new BondStream (
  186.                 lsCouponPeriod,
  187.                 iFinalMaturityDate,
  188.                 strMaturityType
  189.             );
  190.         } catch (java.lang.Exception e) {
  191.             e.printStackTrace();
  192.         }

  193.         return null;
  194.     }

  195.     /**
  196.      * Construct and Instance of BondStream from the specified Parameters
  197.      *
  198.      * @param iMaturityDate Maturity Date
  199.      * @param iEffectiveDate Effective Date
  200.      * @param iFinalMaturityDate Final Maturity Date
  201.      * @param iFirstCouponDate First Coupon Date
  202.      * @param iInterestAccrualStartDate Interest Accrual Start Date
  203.      * @param iFreq Coupon Frequency
  204.      * @param dblCoupon Coupon Rate
  205.      * @param strCouponDC Coupon day count convention
  206.      * @param strAccrualDC Accrual day count convention
  207.      * @param dapPay Pay Date Adjustment Parameters
  208.      * @param dapReset Reset Date Adjustment Parameters
  209.      * @param dapMaturity Maturity Date Adjustment Parameters
  210.      * @param dapEffective Effective Date Adjustment Parameters
  211.      * @param dapPeriodEnd Period End Date Adjustment Parameters
  212.      * @param dapAccrualEnd Accrual Date Adjustment Parameters
  213.      * @param dapPeriodStart Period Start Date Adjustment Parameters
  214.      * @param dapAccrualStart Accrual Start  Date Adjustment Parameters
  215.      * @param strMaturityType Maturity Type
  216.      * @param bPeriodsFromForward Generate Periods forward (True) or Backward (False)
  217.      * @param strCalendar Optional Holiday Calendar for accrual calculations
  218.      * @param strCurrency Coupon Currency
  219.      * @param floaterLabel The Floater Label
  220.      * @param creditLabel The Credit Label
  221.      *
  222.      * @return The BondStream Instance
  223.      */

  224.     public static final BondStream Create (
  225.         final int iMaturityDate,
  226.         final int iEffectiveDate,
  227.         final int iFinalMaturityDate,
  228.         final int iFirstCouponDate,
  229.         final int iInterestAccrualStartDate,
  230.         final int iFreq,
  231.         final double dblCoupon,
  232.         final java.lang.String strCouponDC,
  233.         final java.lang.String strAccrualDC,
  234.         final org.drip.analytics.daycount.DateAdjustParams dapPay,
  235.         final org.drip.analytics.daycount.DateAdjustParams dapReset,
  236.         final org.drip.analytics.daycount.DateAdjustParams dapMaturity,
  237.         final org.drip.analytics.daycount.DateAdjustParams dapEffective,
  238.         final org.drip.analytics.daycount.DateAdjustParams dapPeriodEnd,
  239.         final org.drip.analytics.daycount.DateAdjustParams dapAccrualEnd,
  240.         final org.drip.analytics.daycount.DateAdjustParams dapPeriodStart,
  241.         final org.drip.analytics.daycount.DateAdjustParams dapAccrualStart,
  242.         final java.lang.String strMaturityType,
  243.         final boolean bPeriodsFromForward,
  244.         final java.lang.String strCalendar,
  245.         final java.lang.String strCurrency,
  246.         final org.drip.state.identifier.FloaterLabel floaterLabel,
  247.         final org.drip.state.identifier.EntityCDSLabel creditLabel)
  248.     {
  249.         boolean bCouponEOMAdj = null == strCouponDC ? false : strCouponDC.toUpperCase().contains ("EOM");

  250.         int iCouponDCIndex = null == strCouponDC ? -1 : strCouponDC.indexOf (" NON");

  251.         java.lang.String strCouponDCAdj = -1 != iCouponDCIndex ? strCouponDC.substring (0, iCouponDCIndex) :
  252.             strCouponDC;

  253.         boolean bAccrualEOMAdj = null == strAccrualDC ? false : strAccrualDC.toUpperCase().contains ("EOM");

  254.         int iAccrualDCIndex = null == strAccrualDC ? -1 : strAccrualDC.indexOf (" NON");

  255.         java.lang.String strAccrualDCAdj = -1 != iAccrualDCIndex ? strAccrualDC.substring (0,
  256.             iAccrualDCIndex) : strAccrualDC;

  257.         org.drip.analytics.date.JulianDate dtEffective = new org.drip.analytics.date.JulianDate
  258.             (iEffectiveDate);

  259.         org.drip.analytics.date.JulianDate dtMaturity = new org.drip.analytics.date.JulianDate
  260.             (iMaturityDate);

  261.         java.lang.String strTenor = (12 / iFreq) + "M";
  262.         java.util.List<org.drip.analytics.cashflow.CompositePeriod> lsCouponPeriod = null;

  263.         try {
  264.             org.drip.param.period.UnitCouponAccrualSetting ucas = new
  265.                 org.drip.param.period.UnitCouponAccrualSetting (iFreq, strCouponDCAdj, bCouponEOMAdj,
  266.                     strAccrualDCAdj, bAccrualEOMAdj, strCurrency, false,
  267.                         org.drip.analytics.support.CompositePeriodBuilder.ACCRUAL_COMPOUNDING_RULE_GEOMETRIC);

  268.             org.drip.param.period.CompositePeriodSetting cps = new
  269.                 org.drip.param.period.CompositePeriodSetting (iFreq, strTenor, strCurrency, dapPay, 1., null,
  270.                     null, null, creditLabel);

  271.             java.util.List<java.lang.Integer> lsStreamEdgeDate = bPeriodsFromForward ?
  272.                 org.drip.analytics.support.CompositePeriodBuilder.ForwardEdgeDates (dtEffective, dtMaturity,
  273.                     strTenor, dapAccrualEnd, org.drip.analytics.support.CompositePeriodBuilder.LONG_STUB) :
  274.                         org.drip.analytics.support.CompositePeriodBuilder.BackwardEdgeDates (dtEffective,
  275.                             dtMaturity, strTenor, dapAccrualEnd,
  276.                                 org.drip.analytics.support.CompositePeriodBuilder.LONG_STUB);

  277.             if (null == floaterLabel) {
  278.                 org.drip.param.period.ComposableFixedUnitSetting cfus = new
  279.                     org.drip.param.period.ComposableFixedUnitSetting (strTenor,
  280.                         org.drip.analytics.support.CompositePeriodBuilder.EDGE_DATE_SEQUENCE_SINGLE, null,
  281.                             dblCoupon, 0., strCurrency);

  282.                 lsCouponPeriod = org.drip.analytics.support.CompositePeriodBuilder.FixedCompositeUnit
  283.                     (lsStreamEdgeDate, cps, ucas, cfus);
  284.             } else {
  285.                 org.drip.param.period.ComposableFloatingUnitSetting cfus = new
  286.                     org.drip.param.period.ComposableFloatingUnitSetting (strTenor,
  287.                         org.drip.analytics.support.CompositePeriodBuilder.EDGE_DATE_SEQUENCE_SINGLE, null,
  288.                             floaterLabel,
  289.                                 org.drip.analytics.support.CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  290.                     dblCoupon);

  291.                 lsCouponPeriod = org.drip.analytics.support.CompositePeriodBuilder.FloatingCompositeUnit
  292.                     (lsStreamEdgeDate, cps, cfus);
  293.             }

  294.             return new BondStream (lsCouponPeriod, iFinalMaturityDate, strMaturityType);
  295.         } catch (java.lang.Exception e) {
  296.             e.printStackTrace();
  297.         }

  298.         return null;
  299.     }

  300.     /**
  301.      * Construct the BondStream instance from the list of coupon periods
  302.      *
  303.      * @param lsCouponPeriod List of Coupon Period
  304.      * @param iFinalMaturityDate Final Maturity Date
  305.      * @param strMaturityType Maturity Type
  306.      *
  307.      * @throws java.lang.Exception Thrown if Inputs are invalid
  308.      */

  309.     public BondStream (
  310.         final java.util.List<org.drip.analytics.cashflow.CompositePeriod> lsCouponPeriod,
  311.         final int iFinalMaturityDate,
  312.         final java.lang.String strMaturityType)
  313.         throws java.lang.Exception
  314.     {
  315.         super (lsCouponPeriod);

  316.         _strMaturityType = strMaturityType;
  317.         _iFinalMaturityDate = iFinalMaturityDate;
  318.     }

  319.     /**
  320.      * Return the first Coupon period
  321.      *
  322.      * @return The first Coupon period
  323.      */

  324.     public org.drip.analytics.cashflow.CompositePeriod firstPeriod()
  325.     {
  326.         return periods().get (0);
  327.     }

  328.     /**
  329.      * Returns the final Coupon period
  330.      *
  331.      * @return The final Coupon period
  332.      */

  333.     public org.drip.analytics.cashflow.CompositePeriod lastPeriod()
  334.     {
  335.         java.util.List<org.drip.analytics.cashflow.CompositePeriod> lsCouponPeriod = periods();

  336.         return lsCouponPeriod.get (lsCouponPeriod.size() - 1);
  337.     }

  338.     /**
  339.      * Return the period index containing the specified date
  340.      *
  341.      * @param iDate Date input
  342.      *
  343.      * @return Period index containing the date
  344.      *
  345.      * @throws java.lang.Exception Thrown if the input date not in the period set range
  346.      */

  347.     public int periodIndex (
  348.         final int iDate)
  349.         throws java.lang.Exception
  350.     {
  351.         int i = 0;

  352.         for (org.drip.analytics.cashflow.CompositePeriod period : periods()) {
  353.             if (period.contains (iDate)) return i;

  354.             ++i;
  355.         }

  356.         throw new java.lang.Exception ("BondStream::periodIndex => Input date not in the period set range!");
  357.     }
  358.    
  359.     /**
  360.      * Retrieve the period corresponding to the given index
  361.      *
  362.      * @param iIndex Period index
  363.      *
  364.      * @return Period object corresponding to the input index
  365.      */

  366.     public org.drip.analytics.cashflow.CompositePeriod period (
  367.         final int iIndex)
  368.     {
  369.         try {
  370.             return periods().get (iIndex);
  371.         } catch (java.lang.Exception e) {
  372.         }

  373.         return null;
  374.     }

  375.     /**
  376.      * Retrieve the Maturity Type
  377.      *
  378.      * @return The Maturity Type
  379.      */

  380.     public java.lang.String maturityType()
  381.     {
  382.         return _strMaturityType;
  383.     }

  384.     /**
  385.      * Retrieve the Final Maturity Date
  386.      *
  387.      * @return The Final Maturity Date
  388.      */

  389.     public int finalMaturityDate()
  390.     {
  391.         return _iFinalMaturityDate;
  392.     }
  393. }