ForwardDecompositionUtil.java

  1. package org.drip.analytics.support;

  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>ForwardDecompositionUtil</i> contains the utility functions needed to carry out periodic decomposition
  81.  * at MTM sync points for the given stream.
  82.  *
  83.  *  <br><br>
  84.  *  <ul>
  85.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  86.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  87.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/analytics/README.md">Date, Cash Flow, and Cash Flow Period Measure Generation Utilities</a></li>
  88.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/analytics/support/README.md">Assorted Support and Helper Utilities</a></li>
  89.  *  </ul>
  90.  *
  91.  * @author Lakshmi Krishnamurthy
  92.  */

  93. public class ForwardDecompositionUtil {

  94.     /**
  95.      * Decompose the Stream into an Array of Single Forward Period Floating Streams
  96.      *
  97.      * @param fs The Stream
  98.      * @param iNumPeriodsToAccumulate Number of Forward Periods to roll into one
  99.      *
  100.      * @return The Array of Single Forward Period Streams
  101.      */

  102.     public static final org.drip.product.rates.Stream[] SinglePeriodStreamDecompose (
  103.         final org.drip.product.rates.Stream fs,
  104.         final int iNumPeriodsToAccumulate)
  105.     {
  106.         if (null == fs) return null;

  107.         java.util.List<org.drip.analytics.cashflow.CompositePeriod> lsCouponFlow = fs.cashFlowPeriod();

  108.         int iNumPeriods = lsCouponFlow.size();

  109.         int iCFPIndex = 0;
  110.         int iNumPeriodsAccumulated = 0;
  111.         int iNumForward = iNumPeriods / iNumPeriodsToAccumulate;
  112.         org.drip.product.rates.Stream[] aFS = new org.drip.product.rates.Stream[iNumForward];

  113.         java.util.List<java.util.List<org.drip.analytics.cashflow.CompositePeriod>> lslsCouponPeriod = new
  114.             java.util.ArrayList<java.util.List<org.drip.analytics.cashflow.CompositePeriod>>();

  115.         for (int i = 0; i < iNumForward; ++i)
  116.             lslsCouponPeriod.add (new java.util.ArrayList<org.drip.analytics.cashflow.CompositePeriod>());

  117.         for (org.drip.analytics.cashflow.CompositePeriod cfp : lsCouponFlow) {
  118.             java.util.List<org.drip.analytics.cashflow.CompositePeriod> lsCouponPeriod = lslsCouponPeriod.get
  119.                 (iCFPIndex);

  120.             lsCouponPeriod.add (cfp);

  121.             if (++iNumPeriodsAccumulated != iNumPeriodsToAccumulate) continue;

  122.             iNumPeriodsAccumulated = 0;

  123.             try {
  124.                 aFS[iCFPIndex++] = new org.drip.product.rates.Stream (lsCouponPeriod);
  125.             } catch (java.lang.Exception e) {
  126.                 e.printStackTrace();

  127.                 return null;
  128.             }
  129.         }

  130.         return aFS;
  131.     }

  132.     /**
  133.      * Decompose the Dual Stream Component into an Array of Single Forward Period Dual Streams
  134.      *
  135.      * @param dsc The Dual Stream
  136.      *
  137.      * @return The Array of Single Forward Period Dual Streams
  138.      */

  139.     public static final org.drip.product.definition.CalibratableComponent[] DualStreamForwardArray (
  140.         final org.drip.product.rates.DualStreamComponent dsc)
  141.     {
  142.         if (null == dsc) return null;

  143.         org.drip.product.rates.Stream streamDerived = dsc.derivedStream();

  144.         org.drip.product.rates.Stream streamReference = dsc.referenceStream();

  145.         int iNumForward = 0;
  146.         org.drip.product.rates.Stream[] aStreamDerivedForward = null;
  147.         org.drip.product.rates.Stream[] aStreamReferenceForward = null;

  148.         int iDerivedStreamTenorMonths = 12 / streamDerived.freq();

  149.         int iReferenceStreamTenorMonths = 12 / streamReference.freq();

  150.         if (iReferenceStreamTenorMonths > iDerivedStreamTenorMonths) {
  151.             if (null == (aStreamReferenceForward = SinglePeriodStreamDecompose (streamReference, 1)) || 0 ==
  152.                 (iNumForward = aStreamReferenceForward.length))
  153.                 return null;

  154.             if (null == (aStreamDerivedForward = SinglePeriodStreamDecompose (streamDerived,
  155.                 iReferenceStreamTenorMonths / iDerivedStreamTenorMonths)) || iNumForward !=
  156.                     aStreamDerivedForward.length)
  157.                 return null;
  158.         } else {
  159.             if (null == (aStreamDerivedForward = SinglePeriodStreamDecompose (streamDerived, 1)) || 0 ==
  160.                 (iNumForward = aStreamDerivedForward.length))
  161.                 return null;

  162.             if (null == (aStreamReferenceForward = SinglePeriodStreamDecompose (streamReference,
  163.                 iDerivedStreamTenorMonths / iReferenceStreamTenorMonths)) || iNumForward !=
  164.                     aStreamReferenceForward.length)
  165.                 return null;
  166.         }

  167.         org.drip.product.definition.CalibratableComponent[] aRC = new
  168.             org.drip.product.definition.CalibratableComponent[iNumForward];

  169.         for (int i = 0; i < iNumForward; ++i) {
  170.             try {
  171.                 if (null == (aRC[i] = org.drip.product.creator.DualStreamComponentBuilder.MakeFloatFloat
  172.                     (aStreamReferenceForward[i], aStreamDerivedForward[i], new
  173.                         org.drip.param.valuation.CashSettleParams (0, streamDerived.couponCurrency(), 0))))
  174.                     return null;

  175.                 aRC[i].setPrimaryCode (streamReference.name() + "::" + streamDerived.name() + "_" + i);
  176.             } catch (java.lang.Exception e) {
  177.                 e.printStackTrace();

  178.                 return null;
  179.             }
  180.         }

  181.         return aRC;
  182.     }

  183.     /**
  184.      * Decompose the Rates Component into an Array of Single Forward Rates Components
  185.      *
  186.      * @param rc The Rates Component
  187.      *
  188.      * @return The Array of Single Forward Period Rates Components
  189.      */

  190.     public static final org.drip.product.definition.CalibratableComponent[]
  191.         CalibratableFixedIncomeComponentForwardArray (
  192.             final org.drip.product.definition.CalibratableComponent rc)
  193.     {
  194.         return null != rc && rc instanceof org.drip.product.rates.DualStreamComponent ?
  195.             DualStreamForwardArray ((org.drip.product.rates.DualStreamComponent) rc) : null;
  196.     }
  197. }