StreamBuilder.java

  1. package org.drip.product.creator;

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

  76. /**
  77.  * <i>StreamBuilder</i> contains Utility Functions to construct Fixed, Floating, and Mixed Streams.
  78.  *
  79.  *  <br><br>
  80.  *  <ul>
  81.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  82.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  83.  *      <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>
  84.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/product/creator/README.md">Streams and Products Construction Utilities</a></li>
  85.  *  </ul>
  86.  * <br><br>
  87.  *
  88.  * @author Lakshmi Krishnamurthy
  89.  */

  90. public class StreamBuilder {

  91.     /**
  92.      * Generate the Fixed Stream Off of the specified Parameters
  93.      *
  94.      * @param iStreamStartDate The Stream Start Date
  95.      * @param iStreamEndDate The Stream End Date
  96.      * @param iFirstCouponDate The First Coupon Date
  97.      * @param iPenultimateCouponDate The Penultimate Coupon Date
  98.      * @param iFreq Coupon Frequency
  99.      * @param dblCoupon Coupon Rate
  100.      * @param strCouponDC Coupon Day Count
  101.      * @param strAccrualDC Accrual Day Count
  102.      * @param dapPay Pay Date Adjustment Parameters
  103.      * @param dapStreamEnd Stream End Date Adjustment Parameters
  104.      * @param dapAccrualEnd Accrual End Date Adjustment Parameters
  105.      * @param strCurrency Coupon/Pay Currency
  106.      * @param creditLabel The Stream Credit Label
  107.      *
  108.      * @return The Fixed Stream
  109.      */

  110.     public static final java.util.List<org.drip.analytics.cashflow.CompositePeriod>
  111.         FirstPenultimateDateFixedStream (
  112.             final int iStreamStartDate,
  113.             final int iStreamEndDate,
  114.             final int iFirstCouponDate,
  115.             final int iPenultimateCouponDate,
  116.             final int iFreq,
  117.             final double dblCoupon,
  118.             final java.lang.String strCouponDC,
  119.             final java.lang.String strAccrualDC,
  120.             final org.drip.analytics.daycount.DateAdjustParams dapPay,
  121.             final org.drip.analytics.daycount.DateAdjustParams dapStreamEnd,
  122.             final org.drip.analytics.daycount.DateAdjustParams dapAccrualEnd,
  123.             final java.lang.String strCurrency,
  124.             final org.drip.state.identifier.EntityCDSLabel creditLabel)
  125.     {
  126.         boolean bCouponEOMAdj = null == strCouponDC ? false : strCouponDC.toUpperCase().contains ("EOM");

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

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

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

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

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

  134.         org.drip.analytics.date.JulianDate dtFirstCoupon = new org.drip.analytics.date.JulianDate
  135.             (iFirstCouponDate);

  136.         org.drip.analytics.date.JulianDate dtPenultimateCoupon = new org.drip.analytics.date.JulianDate
  137.             (iPenultimateCouponDate);

  138.         java.lang.String strTenor = (12 / iFreq) + "M";

  139.         try {
  140.             java.util.List<java.lang.Integer> lsStreamEdgeDate =
  141.                 org.drip.analytics.support.CompositePeriodBuilder.BackwardEdgeDates (
  142.                     dtFirstCoupon,
  143.                     dtPenultimateCoupon,
  144.                     strTenor,
  145.                     dapAccrualEnd,
  146.                     org.drip.analytics.support.CompositePeriodBuilder.LONG_STUB
  147.                 );

  148.             if (null == lsStreamEdgeDate) return null;

  149.             lsStreamEdgeDate.add (0, iStreamStartDate);

  150.             lsStreamEdgeDate.add (null == dapStreamEnd ? iStreamEndDate : dapStreamEnd.roll
  151.                 (iStreamEndDate));

  152.             return org.drip.analytics.support.CompositePeriodBuilder.FixedCompositeUnit (
  153.                 lsStreamEdgeDate,
  154.                 new org.drip.param.period.CompositePeriodSetting (
  155.                     iFreq,
  156.                     strTenor,
  157.                     strCurrency,
  158.                     dapPay,
  159.                     1.,
  160.                     null,
  161.                     null,
  162.                     null,
  163.                     creditLabel
  164.                 ),
  165.                 new org.drip.param.period.UnitCouponAccrualSetting (
  166.                     iFreq,
  167.                     strCouponDCAdj,
  168.                     bCouponEOMAdj,
  169.                     strAccrualDCAdj,
  170.                     bAccrualEOMAdj,
  171.                     strCurrency,
  172.                     false,
  173.                     org.drip.analytics.support.CompositePeriodBuilder.ACCRUAL_COMPOUNDING_RULE_GEOMETRIC
  174.                 ),
  175.                 new org.drip.param.period.ComposableFixedUnitSetting (
  176.                     strTenor,
  177.                     org.drip.analytics.support.CompositePeriodBuilder.EDGE_DATE_SEQUENCE_SINGLE,
  178.                     null,
  179.                     dblCoupon,
  180.                     0.,
  181.                     strCurrency
  182.                 )
  183.             );
  184.         } catch (java.lang.Exception e) {
  185.             e.printStackTrace();
  186.         }

  187.         return null;
  188.     }

  189.     /**
  190.      * Generate the Float Stream off of the specified Parameters
  191.      *
  192.      * @param iStreamStartDate The Stream Start Date
  193.      * @param iStreamEndDate The Stream End Date
  194.      * @param iFirstCouponDate The First Coupon Date
  195.      * @param iPenultimateCouponDate The Penultimate Coupon Date
  196.      * @param iFreq Coupon Frequency
  197.      * @param dblSpread The Spread
  198.      * @param dapPay Pay Date Adjustment Parameter
  199.      * @param dapStreamEnd Stream End Date Adjustment Parameter
  200.      * @param dapAccrualEnd Accrual End Date Adjustment Parameter
  201.      * @param floaterLabel Floater Label
  202.      * @param creditLabel Credit Label
  203.      *
  204.      * @return The Float Stream
  205.      */

  206.     public static final java.util.List<org.drip.analytics.cashflow.CompositePeriod>
  207.         FirstPenultimateDateFloatStream (
  208.             final int iStreamStartDate,
  209.             final int iStreamEndDate,
  210.             final int iFirstCouponDate,
  211.             final int iPenultimateCouponDate,
  212.             final int iFreq,
  213.             final double dblSpread,
  214.             final org.drip.analytics.daycount.DateAdjustParams dapPay,
  215.             final org.drip.analytics.daycount.DateAdjustParams dapStreamEnd,
  216.             final org.drip.analytics.daycount.DateAdjustParams dapAccrualEnd,
  217.             final org.drip.state.identifier.FloaterLabel floaterLabel,
  218.             final org.drip.state.identifier.EntityCDSLabel creditLabel)
  219.     {
  220.         if (null == floaterLabel) return null;

  221.         org.drip.analytics.date.JulianDate dtFirstCoupon = new org.drip.analytics.date.JulianDate
  222.             (iFirstCouponDate);

  223.         org.drip.analytics.date.JulianDate dtPenultimateCoupon = new org.drip.analytics.date.JulianDate
  224.             (iPenultimateCouponDate);

  225.         java.lang.String strTenor = (12 / iFreq) + "M";

  226.         try {
  227.             java.util.List<java.lang.Integer> lsStreamEdgeDate =
  228.                 org.drip.analytics.support.CompositePeriodBuilder.BackwardEdgeDates (
  229.                     dtFirstCoupon,
  230.                     dtPenultimateCoupon,
  231.                     strTenor,
  232.                     dapAccrualEnd,
  233.                     org.drip.analytics.support.CompositePeriodBuilder.LONG_STUB
  234.                 );

  235.             if (null == lsStreamEdgeDate) return null;

  236.             lsStreamEdgeDate.add (0, iStreamStartDate);

  237.             lsStreamEdgeDate.add (null == dapStreamEnd ? iStreamEndDate : dapStreamEnd.roll
  238.                 (iStreamEndDate));

  239.             return org.drip.analytics.support.CompositePeriodBuilder.FloatingCompositeUnit (
  240.                 lsStreamEdgeDate,
  241.                 new org.drip.param.period.CompositePeriodSetting (
  242.                     iFreq,
  243.                     strTenor,
  244.                     floaterLabel.currency(),
  245.                     dapPay,
  246.                     1.,
  247.                     null,
  248.                     null,
  249.                     null,
  250.                     creditLabel
  251.                 ),
  252.                 new org.drip.param.period.ComposableFloatingUnitSetting (
  253.                     strTenor,
  254.                     org.drip.analytics.support.CompositePeriodBuilder.EDGE_DATE_SEQUENCE_SINGLE,
  255.                     null,
  256.                     floaterLabel,
  257.                     org.drip.analytics.support.CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  258.                     dblSpread
  259.                 )
  260.             );
  261.         } catch (java.lang.Exception e) {
  262.             e.printStackTrace();
  263.         }

  264.         return null;
  265.     }

  266.     /**
  267.      * Generate Mixed Fixed-Float Stream off of the specified Parameters
  268.      *
  269.      * @param iStreamStartDate The Stream Start Date
  270.      * @param iFixedStreamEndDate The Fixed Stream End Date
  271.      * @param iFixedFirstCouponDate The Fixed Stream First Coupon Date
  272.      * @param iFixedPenultimateCouponDate The Fixed Penultimate Coupon Date
  273.      * @param iFixedFreq Fixed Coupon Frequency
  274.      * @param dblFixedCoupon Fixed Coupon Rate
  275.      * @param strFixedCouponDC Fixed Coupon Day Count
  276.      * @param strFixedAccrualDC Fixed Accrual Day Count
  277.      * @param iFloatStreamEndDate The Float Stream End Date
  278.      * @param iFloatFirstCouponDate The Float First Coupon Date
  279.      * @param iFloatPenultimateCouponDate The Float Penultimate Coupon Date
  280.      * @param iFloatFreq Float Coupon Frequency
  281.      * @param dblFloatSpread The Float Spread
  282.      * @param dapPay Pay Date Adjustment Parameter
  283.      * @param dapStreamEnd Stream End Date Adjustment Parameter
  284.      * @param dapAccrualEnd Accrual End Date Adjustment Parameter
  285.      * @param forwardLabel Forward Label
  286.      * @param creditLabel Credit Label
  287.      *
  288.      * @return The Mixed Stream
  289.      */

  290.     public static final java.util.List<org.drip.analytics.cashflow.CompositePeriod>
  291.         FirstPenultimateDateFixedFloat (
  292.             final int iStreamStartDate,
  293.             final int iFixedStreamEndDate,
  294.             final int iFixedFirstCouponDate,
  295.             final int iFixedPenultimateCouponDate,
  296.             final int iFixedFreq,
  297.             final double dblFixedCoupon,
  298.             final java.lang.String strFixedCouponDC,
  299.             final java.lang.String strFixedAccrualDC,
  300.             final int iFloatStreamEndDate,
  301.             final int iFloatFirstCouponDate,
  302.             final int iFloatPenultimateCouponDate,
  303.             final int iFloatFreq,
  304.             final double dblFloatSpread,
  305.             final org.drip.analytics.daycount.DateAdjustParams dapPay,
  306.             final org.drip.analytics.daycount.DateAdjustParams dapStreamEnd,
  307.             final org.drip.analytics.daycount.DateAdjustParams dapAccrualEnd,
  308.             final org.drip.state.identifier.ForwardLabel forwardLabel,
  309.             final org.drip.state.identifier.EntityCDSLabel creditLabel)
  310.     {
  311.         if (null == forwardLabel) return null;

  312.         java.util.List<org.drip.analytics.cashflow.CompositePeriod> lsMixedPeriod =
  313.             FirstPenultimateDateFixedStream (
  314.                 iStreamStartDate,
  315.                 iFixedStreamEndDate,
  316.                 iFixedFirstCouponDate,
  317.                 iFixedPenultimateCouponDate,
  318.                 iFixedFreq,
  319.                 dblFixedCoupon,
  320.                 strFixedCouponDC,
  321.                 strFixedAccrualDC,
  322.                 dapPay,
  323.                 dapStreamEnd,
  324.                 dapAccrualEnd,
  325.                 forwardLabel.currency(),
  326.                 creditLabel
  327.             );

  328.         if (null == lsMixedPeriod) return null;

  329.         java.util.List<org.drip.analytics.cashflow.CompositePeriod> lsFloatPeriod =
  330.             FirstPenultimateDateFloatStream (
  331.                 iFixedStreamEndDate,
  332.                 iFloatStreamEndDate,
  333.                 iFloatFirstCouponDate,
  334.                 iFloatPenultimateCouponDate,
  335.                 iFloatFreq,
  336.                 dblFloatSpread,
  337.                 dapPay,
  338.                 dapStreamEnd,
  339.                 dapAccrualEnd,
  340.                 forwardLabel,
  341.                 creditLabel
  342.             );

  343.         if (null == lsFloatPeriod) return null;

  344.         lsMixedPeriod.addAll (lsFloatPeriod);

  345.         return lsMixedPeriod;
  346.     }
  347. }