Helper.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.  * 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>Helper</i> contains the collection of the analytics related utility functions used by the modules.
  84.  * Following are some of the functionality that it exposes:
  85.  *
  86.  *  <br><br>
  87.  *  <ul>
  88.  *      <li>
  89.  *          Yield to Discount Factor, and vice versa
  90.  *      </li>
  91.  *      <li>
  92.  *          Map Bloomberg Day Count Codes to Credit Analytics Day Count Codes
  93.  *      </li>
  94.  *      <li>
  95.  *          Generate rule-based curve node manifest measure bumps
  96.  *      </li>
  97.  *      <li>
  98.  *          Generate loss periods using a variety of different schemes
  99.  *      </li>
  100.  *      <li>
  101.  *          Aggregate/disaggregate/merge coupon period lists
  102.  *      </li>
  103.  *      <li>
  104.  *          Create fixings objects, rate index from currency/coupon/frequency
  105.  *      </li>
  106.  *      <li>
  107.  *          String Tenor/Month Code/Work-out
  108.  *      </li>
  109.  *      <li>
  110.  *          Standard Treasury Bench-mark off of Maturity
  111.  *      </li>
  112.  *  </ul>
  113.  *
  114.  *  <br><br>
  115.  *  <ul>
  116.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  117.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  118.  *      <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>
  119.  *      <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>
  120.  *  </ul>
  121.  *
  122.  * @author Lakshmi Krishnamurthy
  123.  */

  124. public class Helper {

  125.     /**
  126.      * Tenor Comparator - Left Tenor Greater than Right
  127.      */

  128.     public static int LEFT_TENOR_GREATER = 1;

  129.     /**
  130.      * Tenor Comparator - Left Tenor Lesser than Right
  131.      */

  132.     public static int LEFT_TENOR_LESSER = 2;

  133.     /**
  134.      * Tenor Comparator - Left Tenor Matches Right
  135.      */

  136.     public static int LEFT_TENOR_EQUALS = 4;

  137.     private static final org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.String> s_mapIRSwitch =
  138.         new org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.String>();

  139.     private static final java.util.Map<java.lang.Integer, java.lang.String> s_mapDCBBGCode = new
  140.         java.util.HashMap<java.lang.Integer, java.lang.String>();

  141.     /**
  142.      * Initialize IR switcher and Bloomberg day count maps
  143.      */

  144.     public static final void Init()
  145.     {
  146.         s_mapDCBBGCode.put (1, "ACT/ACT");

  147.         s_mapDCBBGCode.put (2, "ACT/360");

  148.         s_mapDCBBGCode.put (3, "ACT/365");

  149.         s_mapDCBBGCode.put (4, "30/ACT");

  150.         s_mapDCBBGCode.put (5, "30/360");

  151.         s_mapDCBBGCode.put (6, "30/365");

  152.         s_mapDCBBGCode.put (7, "NL/ACT");

  153.         s_mapDCBBGCode.put (8, "NL/360");

  154.         s_mapDCBBGCode.put (9, "NL/365");

  155.         s_mapDCBBGCode.put (10, "ACT/ACT NON-EOM");

  156.         s_mapDCBBGCode.put (11, "ACT/360 NON-EOM");

  157.         s_mapDCBBGCode.put (12, "ACT/365 NON-EOM");

  158.         s_mapDCBBGCode.put (13, "30/ACT NON-EOM");

  159.         s_mapDCBBGCode.put (14, "30/360 NON-EOM");

  160.         s_mapDCBBGCode.put (15, "30/365 NON-EOM");

  161.         s_mapDCBBGCode.put (16, "NL/ACT NON-EOM");

  162.         s_mapDCBBGCode.put (17, "NL/360 NON-EOM");

  163.         s_mapDCBBGCode.put (18, "NL/365 NON-EOM");

  164.         s_mapDCBBGCode.put (19, "ISMA 30/ACT");

  165.         s_mapDCBBGCode.put (20, "ISMA 30/360");

  166.         s_mapDCBBGCode.put (21, "ISMA 30/365");

  167.         s_mapDCBBGCode.put (22, "ISMA 30/ACT NON-EOM");

  168.         s_mapDCBBGCode.put (23, "ISMA 30/360 NON-EOM");

  169.         s_mapDCBBGCode.put (24, "ISMA 30/365 NON-EOM");

  170.         s_mapDCBBGCode.put (27, "ACT/364");

  171.         s_mapDCBBGCode.put (29, "US MUNI: 30/360");

  172.         s_mapDCBBGCode.put (30, "ACT/364 NON-EOM");

  173.         s_mapDCBBGCode.put (32, "MUNI30/360 NON-EOM");

  174.         s_mapDCBBGCode.put (33, "BUS DAYS/252");

  175.         s_mapDCBBGCode.put (35, "GERMAN:30/360");

  176.         s_mapDCBBGCode.put (36, "BUS DAY/252 NON-EOM");

  177.         s_mapDCBBGCode.put (38, "GER:30/360 NON-EOM");

  178.         s_mapDCBBGCode.put (40, "US:WIT ACT/ACT");

  179.         s_mapDCBBGCode.put (41, "US:WIB ACT/360");

  180.         s_mapDCBBGCode.put (44, "ISDA SWAPS:30/360");

  181.         s_mapDCBBGCode.put (45, "ISDA SWAPS:30/365");

  182.         s_mapDCBBGCode.put (46, "ISDA SWAPS:30/ACT");

  183.         s_mapDCBBGCode.put (47, "ISDA30/360 NON-EOM");

  184.         s_mapDCBBGCode.put (48, "ISDA30/365 NON-EOM");

  185.         s_mapDCBBGCode.put (49, "ISDA30/ACT NON-EOM");

  186.         s_mapDCBBGCode.put (50, "ISDA 30E/360");

  187.         s_mapDCBBGCode.put (51, "ISDA 30E/365");

  188.         s_mapDCBBGCode.put (52, "ISDA 30E/ACT");

  189.         s_mapDCBBGCode.put (53, "ISDA 30E/360 N-EOM");

  190.         s_mapDCBBGCode.put (54, "ISDA 30E/365 N-EOM");

  191.         s_mapDCBBGCode.put (55, "ISDA 30E/ACT N-EOM");

  192.         s_mapDCBBGCode.put (101, "ACT/ACT");

  193.         s_mapDCBBGCode.put (102, "ACT/360");

  194.         s_mapDCBBGCode.put (103, "ACT/365");

  195.         s_mapDCBBGCode.put (104, "30/360");

  196.         s_mapDCBBGCode.put (105, "ACT/ACT NON-EOM");

  197.         s_mapDCBBGCode.put (106, "ACT/360 NON-EOM");

  198.         s_mapDCBBGCode.put (107, "ACT/365 NON-EOM");

  199.         s_mapDCBBGCode.put (108, "ACT/360");

  200.         s_mapDCBBGCode.put (131, "ISMA 30/360");

  201.         s_mapDCBBGCode.put (201, "ISDA ACT/ACT");

  202.         s_mapDCBBGCode.put (202, "AFB ACT/ACT");

  203.         s_mapDCBBGCode.put (203, "ISDA ACT/ACT NOM");

  204.         s_mapDCBBGCode.put (204, "AFB ACT/ACT NOM");

  205.         s_mapDCBBGCode.put (206, "ISMA ACT/ACT");

  206.         s_mapDCBBGCode.put (207, "ISMA ACT/ACT NON-EOM");

  207.         s_mapIRSwitch.put ("ITL", "EUR");

  208.         s_mapIRSwitch.put ("FRF", "EUR");

  209.         s_mapIRSwitch.put ("CZK", "EUR");

  210.         s_mapIRSwitch.put ("BEF", "EUR");

  211.         s_mapIRSwitch.put ("ATS", "EUR");

  212.         s_mapIRSwitch.put ("SKK", "EUR");
  213.     }

  214.     /**
  215.      * Calculate the discount factor from the specified frequency, yield, and accrual year fraction
  216.      *
  217.      * @param iFreqIn Input frequency - if zero, set to semi-annual.
  218.      * @param dblYield Yield
  219.      * @param dblTime Time in DC years
  220.      *
  221.      * @return the discount factor
  222.      *
  223.      * @throws java.lang.Exception if input are invalid.
  224.      */

  225.     public static final double Yield2DF (
  226.         final int iFreqIn,
  227.         final double dblYield,
  228.         final double dblTime)
  229.         throws java.lang.Exception
  230.     {
  231.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblYield) || !org.drip.numerical.common.NumberUtil.IsValid
  232.             (dblTime))
  233.             throw new java.lang.Exception ("Helper::YieldDF => Bad yield/time");

  234.         int iFreq = (0 == iFreqIn) ? 2 : iFreqIn;

  235.         return java.lang.Math.pow (1. + (dblYield / iFreq), -1. * dblTime * iFreq);
  236.     }

  237.     /**
  238.      * Calculate the yield from the specified discount factor to the given time.
  239.      *
  240.      * @param iFreqIn Yield calculation frequency - defaults to semi-annual if zero.
  241.      * @param dblDF Discount Factor
  242.      * @param dblTime Time to which the yield/DF are specified
  243.      *
  244.      * @return Implied yield
  245.      *
  246.      * @throws java.lang.Exception Thrown if yield cannot be computed
  247.      */

  248.     public static final double DF2Yield (
  249.         final int iFreqIn,
  250.         final double dblDF,
  251.         final double dblTime)
  252.         throws java.lang.Exception
  253.     {
  254.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblDF) || !org.drip.numerical.common.NumberUtil.IsValid
  255.             (dblTime))
  256.             throw new java.lang.Exception ("CurveProductHelper.DFYield: Bad yield/time");

  257.         int iFreq = (0 == iFreqIn) ? 2 : iFreqIn;

  258.         return iFreq * (java.lang.Math.pow (dblDF, -1. / (iFreq * dblTime)) - 1.);
  259.     }

  260.     /**
  261.      * Compute the uncompounded OIS Rate from the LIBOR Swap Rate and the LIBOR Swap Rate - Fed Fund Basis.
  262.      *  The calculation is from the following Bloomberg Publication:
  263.      *
  264.      *  - Lipman, H. and F. Mercurio (2012): OIS Discounting and Dual-Curve Stripping Methodology at
  265.      *      Bloomberg
  266.      *
  267.      * @param dblLIBORSwapRate LIBOR Swap Rate
  268.      * @param dblFedFundLIBORSwapBasis Fed Fund - LIBOR Swap Rate Basis
  269.      *
  270.      * @return The Uncompounded OIS Rate
  271.      *
  272.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  273.      */

  274.     public static final double OISFromLIBORSwapFedFundBasis (
  275.         final double dblLIBORSwapRate,
  276.         final double dblFedFundLIBORSwapBasis)
  277.         throws java.lang.Exception
  278.     {
  279.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblLIBORSwapRate) ||
  280.             !org.drip.numerical.common.NumberUtil.IsValid (dblFedFundLIBORSwapBasis))
  281.             throw new java.lang.Exception ("Helper::OISFromLIBORSwapFedFundBasis => Invalid Inputs!");

  282.         double dblOISAnnuity = 1. + 0.25 * (4. * (java.lang.Math.sqrt (1. + (dblLIBORSwapRate * 180. / 365.))
  283.             - 1.) - dblFedFundLIBORSwapBasis);

  284.         return dblOISAnnuity * dblOISAnnuity * dblOISAnnuity * dblOISAnnuity - 1.;
  285.     }

  286.     /**
  287.      * Compute the Daily Compounded OIS Rate from the LIBOR Swap Rate and the LIBOR Swap Rate - Fed Fund
  288.      *  Basis. The calculation is from the following Bloomberg Publication:
  289.      *
  290.      *  - Lipman, H. and F. Mercurio (2012): OIS Discounting and Dual-Curve Stripping Methodology at
  291.      *      Bloomberg
  292.      *
  293.      * @param dblLIBORSwapRate LIBOR Swap Rate
  294.      * @param dblFedFundLIBORSwapBasis Fed Fund - LIBOR Swap Rate Basis
  295.      *
  296.      * @return The Daily Compounded OIS Rate
  297.      *
  298.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  299.      */

  300.     public static final double OISFromLIBORSwapFedFundBasis2 (
  301.         final double dblLIBORSwapRate,
  302.         final double dblFedFundLIBORSwapBasis)
  303.         throws java.lang.Exception
  304.     {
  305.         return 4. * (java.lang.Math.pow (1. + (OISFromLIBORSwapFedFundBasis (dblLIBORSwapRate,
  306.             dblFedFundLIBORSwapBasis) / 360.), 90.) - 1.);
  307.     }

  308.     /**
  309.      * Compute the DI-Style Price given the Rate
  310.      *
  311.      * @param dblDIRate The DI Rate
  312.      * @param iStartDate The Start Date
  313.      * @param iEndDate The End Date
  314.      * @param strCalendar The Calendar
  315.      *
  316.      * @return The DI-Style Price
  317.      *
  318.      * @throws java.lang.Exception Thrown if the DI-Style Price cannot be calculated
  319.      */

  320.     public static final double DIStylePriceFromRate (
  321.         final double dblDIRate,
  322.         final int iStartDate,
  323.         final int iEndDate,
  324.         final java.lang.String strCalendar)
  325.         throws java.lang.Exception
  326.     {
  327.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblDIRate) || iStartDate >= iEndDate)
  328.             throw new java.lang.Exception ("Helper::DIStylePriceFromRate => Invalid Inputs");

  329.         return java.lang.Math.pow (1. + dblDIRate, -1. * org.drip.analytics.daycount.Convention.BusinessDays
  330.             (iStartDate, iEndDate, strCalendar) / 252.);
  331.     }

  332.     /**
  333.      * Compute the DI-Style Rate given the Price
  334.      *
  335.      * @param dblDIPrice The DI Price
  336.      * @param iStartDate The Start Date
  337.      * @param iEndDate The End Date
  338.      * @param strCalendar The Calendar
  339.      *
  340.      * @return The DI-Style Rate
  341.      *
  342.      * @throws java.lang.Exception Thrown if the DI-Style Price cannot be calculated
  343.      */

  344.     public static final double DIStyleRateFromPrice (
  345.         final double dblDIPrice,
  346.         final int iStartDate,
  347.         final int iEndDate,
  348.         final java.lang.String strCalendar)
  349.         throws java.lang.Exception
  350.     {
  351.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblDIPrice) || iStartDate >= iEndDate)
  352.             throw new java.lang.Exception ("Helper::DIStyleRateFromPrice => Invalid Inputs");

  353.         return java.lang.Math.pow (dblDIPrice, -252. / org.drip.analytics.daycount.Convention.BusinessDays
  354.             (iStartDate, iEndDate, strCalendar)) - 1.;
  355.     }

  356.     /**
  357.      * Convert the Nominal Yield to the Post Tax Equivalent Yield
  358.      *
  359.      * @param dblNominalYield The Nominal Yield
  360.      * @param dblTaxRate The Tax Rate
  361.      *
  362.      * @return The Post Tax Equivalent Yield
  363.      *
  364.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  365.      */

  366.     public static final double NominalYieldToPostTaxEquivalent (
  367.         final double dblNominalYield,
  368.         final double dblTaxRate)
  369.         throws java.lang.Exception
  370.     {
  371.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblNominalYield) ||
  372.             !org.drip.numerical.common.NumberUtil.IsValid (dblTaxRate))
  373.             throw new java.lang.Exception ("Helper::NominalYieldToPostTaxEquivalent => Invalid Inputs");

  374.         return dblNominalYield * (1. - dblTaxRate);
  375.     }

  376.     /**
  377.      * Convert the Post Tax Equivalent Yield to the Nominal Yield
  378.      *
  379.      * @param dblPostTaxEquivalentYield The Post Tax Equivalent Yield
  380.      * @param dblTaxRate The Tax Rate
  381.      *
  382.      * @return The Nominal Yield
  383.      *
  384.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  385.      */

  386.     public static final double PostTaxEquivalentYieldToNominal (
  387.         final double dblPostTaxEquivalentYield,
  388.         final double dblTaxRate)
  389.         throws java.lang.Exception
  390.     {
  391.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblPostTaxEquivalentYield) ||
  392.             !org.drip.numerical.common.NumberUtil.IsValid (dblTaxRate))
  393.             throw new java.lang.Exception ("Helper::PostTaxEquivalentYieldToNominal => Invalid Inputs");

  394.         return dblPostTaxEquivalentYield / (1. - dblTaxRate);
  395.     }

  396.     /**
  397.      * Return the standard on-the-run benchmark treasury string from the valuation and the maturity dates
  398.      *
  399.      * @param iValueDate the Valuation date
  400.      * @param iMaturityDate the Maturity date
  401.      *
  402.      * @return the standard on-the-run benchmark treasury string
  403.      */

  404.     public static final java.lang.String BaseTsyBmk (
  405.         final int iValueDate,
  406.         final int iMaturityDate)
  407.     {
  408.         double dblMatYears = 1. * (iMaturityDate - iValueDate) / 365.25;

  409.         if (1.0 < dblMatYears && dblMatYears <= 2.5) return "02YON";

  410.         if (2.5 < dblMatYears && dblMatYears <= 4.0) return "03YON";

  411.         if (4.0 < dblMatYears && dblMatYears <= 6.0) return "05YON";

  412.         if (6.0 < dblMatYears && dblMatYears <= 8.5) return "07YON";

  413.         if (8.5 < dblMatYears && dblMatYears <= 15.) return "10YON";

  414.         if (dblMatYears > 15.) return "30YON";

  415.         return null;
  416.     }

  417.     /**
  418.      * Turn the work out type to string
  419.      *
  420.      * @param iWOType One of the WO_TYPE_* fields in the WorkoutInfo class
  421.      *
  422.      * @return String representation of the work out type field
  423.      */

  424.     public static final java.lang.String WorkoutTypeToString (
  425.         final int iWOType)
  426.     {
  427.         if (org.drip.param.valuation.WorkoutInfo.WO_TYPE_PUT == iWOType) return "Put";

  428.         if (org.drip.param.valuation.WorkoutInfo.WO_TYPE_CALL == iWOType) return "Call";

  429.         if (org.drip.param.valuation.WorkoutInfo.WO_TYPE_MATURITY == iWOType) return "Maturity";

  430.         return "Unknown work out type";
  431.     }

  432.     /**
  433.      * Convert the Bloomberg day count code to DRIP day count code.
  434.      *  
  435.      * @param strBBGDCCode String representing the Bloomberg day count code.
  436.      *
  437.      * @return String representing the DRIP day count code.
  438.      */

  439.     public static final java.lang.String ParseFromBBGDCCode (
  440.         final java.lang.String strBBGDCCode)
  441.     {
  442.         if (null == strBBGDCCode) return "Unknown BBG DC";

  443.         try {
  444.             return s_mapDCBBGCode.get ((int) java.lang.Double.parseDouble (strBBGDCCode.trim()));
  445.         } catch (java.lang.Exception e) {
  446.         }

  447.         return "Unknown BBG DC";
  448.     }

  449.     /**
  450.      * Retrieve the tenor from the frequency
  451.      *
  452.      * @param iFreq Integer frequency
  453.      *
  454.      * @return String representing the tenor
  455.      */

  456.     public static final java.lang.String GetTenorFromFreq (
  457.         final int iFreq)
  458.     {
  459.         if (1 == iFreq) return "1Y";

  460.         if (2 == iFreq) return "6M";

  461.         if (3 == iFreq) return "4M";

  462.         if (4 == iFreq) return "3M";

  463.         if (6 == iFreq) return "2M";

  464.         if (12 == iFreq) return "1M";

  465.         return null;
  466.     }

  467.     /**
  468.      * Retrieve the Number of Years from the Tenor
  469.      *
  470.      * @param strTenor The Specified Tenor
  471.      *
  472.      * @return The Number of Years
  473.      *
  474.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  475.      */

  476.     public static final int TenorToYears (
  477.         final java.lang.String strTenor)
  478.         throws java.lang.Exception
  479.     {
  480.         if (null == strTenor || strTenor.isEmpty())
  481.             throw new java.lang.Exception ("Helper::TenorToYears => Invalid Inputs");

  482.         char chTenor = strTenor.charAt (strTenor.length() - 1);

  483.         int iTimeUnit = (int) java.lang.Double.parseDouble (strTenor.substring (0, strTenor.length() -
  484.             1));

  485.         if ('y' == chTenor || 'Y' == chTenor) return iTimeUnit * 12;

  486.         throw new java.lang.Exception ("AnalyticsHelper::TenorToYears => Invalid tenor format " + strTenor);
  487.     }

  488.     /**
  489.      * Retrieve the Number of Months from the Tenor
  490.      *
  491.      * @param strTenor The Specified Tenor
  492.      *
  493.      * @return The Number of Months
  494.      *
  495.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  496.      */

  497.     public static final int TenorToMonths (
  498.         final java.lang.String strTenor)
  499.         throws java.lang.Exception
  500.     {
  501.         if (null == strTenor || strTenor.isEmpty())
  502.             throw new java.lang.Exception ("Helper::TenorToMonths => Invalid Inputs");

  503.         char chTenor = strTenor.charAt (strTenor.length() - 1);

  504.         int iTimeUnit = (int) java.lang.Double.parseDouble (strTenor.substring (0, strTenor.length() -
  505.             1));

  506.         if ('d' == chTenor || 'D' == chTenor) return iTimeUnit * (iTimeUnit / 30);

  507.         if ('w' == chTenor || 'W' == chTenor) return iTimeUnit * (iTimeUnit / 7);

  508.         if ('l' == chTenor || 'L' == chTenor) return iTimeUnit;

  509.         if ('m' == chTenor || 'M' == chTenor) return iTimeUnit;

  510.         if ('y' == chTenor || 'Y' == chTenor) return iTimeUnit * 12;

  511.         throw new java.lang.Exception ("Helper::TenorToMonths => Invalid Tenor Format " + strTenor);
  512.     }

  513.     /**
  514.      * Retrieve the Number of Days from the Tenor
  515.      *
  516.      * @param strTenor The Specified Tenor
  517.      *
  518.      * @return The Number of Days
  519.      *
  520.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  521.      */

  522.     public static final int TenorToDays (
  523.         final java.lang.String strTenor)
  524.         throws java.lang.Exception
  525.     {
  526.         if (null == strTenor || strTenor.isEmpty())
  527.             throw new java.lang.Exception ("Helper::TenorToDays => Invalid Inputs");

  528.         char chTenor = strTenor.charAt (strTenor.length() - 1);

  529.         int iTimeUnit = (int) java.lang.Double.parseDouble (strTenor.substring (0, strTenor.length() -
  530.             1));

  531.         if ('d' == chTenor || 'D' == chTenor) return iTimeUnit;

  532.         if ('w' == chTenor || 'W' == chTenor) return iTimeUnit * 7;

  533.         if ('l' == chTenor || 'L' == chTenor) return iTimeUnit * 28;

  534.         if ('m' == chTenor || 'M' == chTenor) return iTimeUnit * 30;

  535.         if ('y' == chTenor || 'Y' == chTenor) return (int) (365.25 * iTimeUnit);

  536.         throw new java.lang.Exception ("Helper::TenorToDays => Unknown tenor format " + strTenor);
  537.     }

  538.     /**
  539.      * Retrieve the Year Fraction from the Tenor
  540.      *
  541.      * @param strTenor The Specified Tenor
  542.      *
  543.      * @return The Year Fraction
  544.      *
  545.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  546.      */

  547.     public static final double TenorToYearFraction (
  548.         final java.lang.String strTenor)
  549.         throws java.lang.Exception
  550.     {
  551.         if (null == strTenor || strTenor.isEmpty())
  552.             throw new java.lang.Exception ("Helper::TenorToYearFraction => Invalid Inputs");

  553.         char chTenor = strTenor.charAt (strTenor.length() - 1);

  554.         int iTimeUnit = (int) java.lang.Double.parseDouble (strTenor.substring (0, strTenor.length() -
  555.             1));

  556.         if ('d' == chTenor || 'D' == chTenor) return ((double) iTimeUnit) / 365.25;

  557.         if ('w' == chTenor || 'W' == chTenor) return ((double) (7. * iTimeUnit)) / 365.25;

  558.         if ('w' == chTenor || 'W' == chTenor) return ((double) (iTimeUnit)) / 52.;

  559.         if ('l' == chTenor || 'L' == chTenor) return ((double) (28. * iTimeUnit)) / 365.25;

  560.         if ('l' == chTenor || 'L' == chTenor) return ((double) (iTimeUnit)) / 13.;

  561.         if ('m' == chTenor || 'M' == chTenor) return ((double) (iTimeUnit)) / 12.;

  562.         if ('y' == chTenor || 'Y' == chTenor) return iTimeUnit;

  563.         throw new java.lang.Exception ("Helper::TenorToYearFraction => Unknown tenor format " + strTenor);
  564.     }

  565.     /**
  566.      * Retrieve the Year Fraction from the Tenor Array
  567.      *
  568.      * @param astrTenor The Specified Tenor Array
  569.      * @param bForward TRUE - Generated the Incremental Forward Year Fraction
  570.      *
  571.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  572.      *
  573.      * @return The Year Fraction Array
  574.      */

  575.     public static final double[] TenorToYearFraction (
  576.         final java.lang.String[] astrTenor,
  577.         final boolean bForward)
  578.         throws java.lang.Exception
  579.     {
  580.         if (null == astrTenor)
  581.             throw new java.lang.Exception ("Helper::TenorToYearFraction => Invalid Inputs");

  582.         int iNumTenor = astrTenor.length;
  583.         double[] adblYearFraction = 0 == iNumTenor ? null : new double[iNumTenor];

  584.         for (int i = 0; i < iNumTenor; ++i) {
  585.             try {
  586.                 adblYearFraction[i] = TenorToYearFraction (astrTenor[i]);
  587.             } catch (java.lang.Exception e) {
  588.                 e.printStackTrace();

  589.                 return null;
  590.             }
  591.         }

  592.         if (!bForward) return adblYearFraction;

  593.         for (int i = iNumTenor - 1; i > 1; --i)
  594.             adblYearFraction[i] = adblYearFraction[i] - adblYearFraction[i - 1];

  595.         return adblYearFraction;
  596.     }

  597.     /**
  598.      * Retrieve the Annual Frequency from the Tenor
  599.      *
  600.      * @param strTenor The Specified Tenor
  601.      *
  602.      * @return The Annual Frequency
  603.      *
  604.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  605.      */

  606.     public static final int TenorToFreq (
  607.         final java.lang.String strTenor)
  608.         throws java.lang.Exception
  609.     {
  610.         if (null == strTenor || strTenor.isEmpty())
  611.             throw new java.lang.Exception ("Helper::TenorToFreq => Invalid Inputs");

  612.         if ("ON".equalsIgnoreCase (strTenor)) return 365;

  613.         char chTenor = strTenor.charAt (strTenor.length() - 1);

  614.         int iTimeUnit = (int) java.lang.Double.parseDouble (strTenor.substring (0, strTenor.length() -
  615.             1));

  616.         if ('d' == chTenor || 'D' == chTenor) return (int) (365. / iTimeUnit);

  617.         if ('w' == chTenor || 'W' == chTenor) return (int) (52. / iTimeUnit);

  618.         if ('l' == chTenor || 'L' == chTenor) return (int) (13. / iTimeUnit);

  619.         if ('m' == chTenor || 'M' == chTenor) return (int) (12. / iTimeUnit);

  620.         if ('y' == chTenor || 'Y' == chTenor) return iTimeUnit;

  621.         throw new java.lang.Exception ("Helper::TenorToFreq => Unknown tenor format " + strTenor);
  622.     }

  623.     /**
  624.      * Retrieve the Date Array From the Tenor Array
  625.      *
  626.      * @param dtSpot The Spot Date Array
  627.      * @param astrTenor The Specified Tenor Array
  628.      *
  629.      * @return The Date Array From the Tenor Array
  630.      */

  631.     public static final int[] TenorToDate (
  632.         final org.drip.analytics.date.JulianDate dtSpot,
  633.         final java.lang.String[] astrTenor)
  634.     {
  635.         if (null == dtSpot || null == astrTenor) return null;

  636.         int iNumTenor = astrTenor.length;
  637.         int[] aiTenorDate = new int[iNumTenor];

  638.         for (int i = 0; i < iNumTenor; ++i) {
  639.             org.drip.analytics.date.JulianDate dtTenor = dtSpot.addTenor (astrTenor[i]);

  640.             if (null == dtTenor) return null;

  641.             aiTenorDate[i] = dtTenor.julian();
  642.         }

  643.         return aiTenorDate;
  644.     }

  645.     /**
  646.      * Compare the Left and the Right Tenors
  647.      *
  648.      * @param strTenorLeft Left Tenor
  649.      * @param strTenorRight Right Tenor
  650.      *
  651.      * @return Results of the Comparison
  652.      *
  653.      * @throws java.lang.Exception Thrown if the Comparison cannot be done
  654.      */

  655.     public static final int TenorCompare (
  656.         final java.lang.String strTenorLeft,
  657.         final java.lang.String strTenorRight)
  658.         throws java.lang.Exception
  659.     {
  660.         int iLeftTenorDays = TenorToDays (strTenorLeft);

  661.         int iRightTenorDays = TenorToDays (strTenorRight);

  662.         if (iLeftTenorDays == iRightTenorDays) return LEFT_TENOR_EQUALS;

  663.         return iLeftTenorDays > iRightTenorDays ? LEFT_TENOR_GREATER : LEFT_TENOR_LESSER;
  664.     }

  665.     /**
  666.      * Retrieve the month code from input frequency
  667.      *
  668.      * @param iFreq Integer frequency
  669.      *
  670.      * @return String representing the month code
  671.      */

  672.     public static final java.lang.String GetMonthCodeFromFreq (
  673.         final int iFreq)
  674.     {
  675.         if (1 == iFreq) return "0012M";

  676.         if (2 == iFreq) return "0006M";

  677.         if (3 == iFreq) return "0004M";

  678.         if (4 == iFreq) return "0003M";

  679.         if (6 == iFreq) return "0002M";

  680.         if (12 == iFreq) return "0001M";

  681.         return null;
  682.     }

  683.     /**
  684.      * Calculate the rate index from the coupon currency and the frequency
  685.      *
  686.      * @param strCouponCurrency String representing the coupon currency
  687.      * @param iCouponFreq Integer representing the coupon frequency
  688.      *
  689.      * @return String representing the rate index
  690.      */

  691.     public static final java.lang.String CalcRateIndex (
  692.         final java.lang.String strCouponCurrency,
  693.         final int iCouponFreq)
  694.     {
  695.         if (null == strCouponCurrency || strCouponCurrency.isEmpty()) return null;

  696.         java.lang.String strFreqMonthCode = GetMonthCodeFromFreq (iCouponFreq);

  697.         if (null == strFreqMonthCode) return null;

  698.         return strCouponCurrency.substring (0, 2) + strFreqMonthCode;
  699.     }

  700.     /**
  701.      * Get the DRIP day count from the Bloomberg code
  702.      *
  703.      * @param strBBGDC String representing the Bloomberg day count convention
  704.      *
  705.      * @return String representing DRIP day count
  706.      */

  707.     public static final java.lang.String GetDayCountFromBBGCode (
  708.         final java.lang.String strBBGDC)
  709.     {
  710.         if (null == strBBGDC || strBBGDC.isEmpty()) return "30/360";

  711.         return "30/360";
  712.     }

  713.     /**
  714.      * Calculate the rate index from currency and coupon frequency
  715.      *
  716.      * @param strCcy String representing coupon currency
  717.      * @param iCouponFreq Integer representing coupon frequency
  718.      *
  719.      * @return String representing the rate index
  720.      */

  721.     public static final java.lang.String RateIndexFromCcyAndCouponFreq (
  722.         final java.lang.String strCcy,
  723.         final int iCouponFreq)
  724.     {
  725.         if (null == strCcy || strCcy.isEmpty() || 0 >= iCouponFreq) return "";

  726.         java.lang.String strCcyPrefix = strCcy.substring (0, 2);

  727.         if (1 == iCouponFreq)  return strCcyPrefix + "0012M";

  728.         if (2 == iCouponFreq)  return strCcyPrefix + "0006M";

  729.         if (3 == iCouponFreq)  return strCcyPrefix + "0004M";

  730.         if (4 == iCouponFreq)  return strCcyPrefix + "0003M";

  731.         if (6 == iCouponFreq)  return strCcyPrefix + "0002M";

  732.         if (12 == iCouponFreq)  return strCcyPrefix + "0001M";

  733.         return "";
  734.     }

  735.     /**
  736.      * Switch the given IR curve if necessary
  737.      *
  738.      * @param strCurveIn String representing the input curve
  739.      *
  740.      * @return String representing the switched curve
  741.      */

  742.     public static final java.lang.String SwitchIRCurve (
  743.         final java.lang.String strCurveIn)
  744.     {
  745.         if (null == strCurveIn) return null;

  746.         if (!s_mapIRSwitch.containsKey (strCurveIn)) return strCurveIn;

  747.         return s_mapIRSwitch.get (strCurveIn);
  748.     }

  749.     /**
  750.      * Create the Latent State Fixings object from the bond, the fixings date, and the fixing.
  751.      *
  752.      * @param bond The input bond
  753.      * @param dtFixing The Fixings Date
  754.      * @param dblFixing Double representing the fixing
  755.      *
  756.      * @return The Latent State Fixings Instance
  757.      */
  758.    
  759.     public static final org.drip.param.market.LatentStateFixingsContainer CreateFixingsObject (
  760.         final org.drip.product.definition.Bond bond,
  761.         final org.drip.analytics.date.JulianDate dtFixing,
  762.         final double dblFixing)
  763.     {
  764.         if (!bond.isFloater()) return null;

  765.         org.drip.param.market.LatentStateFixingsContainer lsfc = new
  766.             org.drip.param.market.LatentStateFixingsContainer();

  767.         return lsfc.add (dtFixing, bond.forwardLabel().get ("BASE"), dblFixing) ? lsfc : null;
  768.     }

  769.     /**
  770.      * Bump the input array quotes
  771.      *
  772.      * @param adblQuotesIn Array of the input double quotes
  773.      * @param dblBump Bump amount
  774.      * @param bIsProportional True - Bump is proportional
  775.      *
  776.      * @return Bumped array output
  777.      */

  778.     public static final double[] BumpQuotes (
  779.         final double[] adblQuotesIn,
  780.         final double dblBump,
  781.         final boolean bIsProportional)
  782.     {
  783.         if (null == adblQuotesIn || 0 == adblQuotesIn.length || !org.drip.numerical.common.NumberUtil.IsValid
  784.             (dblBump))
  785.             return null;

  786.         double[] adblQuotesOut = new double[adblQuotesIn.length];

  787.         for (int i = 0; i < adblQuotesIn.length; ++i) {
  788.             if (!org.drip.numerical.common.NumberUtil.IsValid (adblQuotesIn[i])) return null;

  789.             if (!bIsProportional)
  790.                 adblQuotesOut[i] = adblQuotesIn[i] + dblBump;
  791.             else
  792.                 adblQuotesOut[i] = adblQuotesIn[i] * (1. + dblBump);
  793.         }

  794.         return adblQuotesOut;
  795.     }

  796.     /**
  797.      * Tweak the Manifest Measures (gor the given set of nodes) in accordance with the specified tweak
  798.      *  parameters
  799.      *
  800.      * @param adblQuotesIn Array of quotes to be bumped
  801.      * @param ntp NodeTweakParams input
  802.      *
  803.      * @return Bumped array output
  804.      */

  805.     public static final double[] TweakManifestMeasure (
  806.         final double[] adblQuotesIn,
  807.         final org.drip.param.definition.ManifestMeasureTweak ntp)
  808.     {
  809.         if (null == adblQuotesIn || 0 == adblQuotesIn.length || null == ntp) return adblQuotesIn;

  810.         double[] adblQuotesOut = new double[adblQuotesIn.length];

  811.         if (org.drip.param.definition.ManifestMeasureTweak.FLAT == ntp.node()) {
  812.             for (int i = 0; i < adblQuotesIn.length; ++i) {
  813.                 if (!org.drip.numerical.common.NumberUtil.IsValid (adblQuotesIn[i])) return null;

  814.                 if (!ntp.isProportional())
  815.                     adblQuotesOut[i] = adblQuotesIn[i] + ntp.amount();
  816.                 else
  817.                     adblQuotesOut[i] = adblQuotesIn[i] * (1. + ntp.amount());
  818.             }
  819.         } else {
  820.             if (ntp.node() < 0 || ntp.node() >= adblQuotesIn.length) return null;

  821.             for (int i = 0; i < adblQuotesIn.length; ++i) {
  822.                 if (!org.drip.numerical.common.NumberUtil.IsValid (adblQuotesIn[i])) return null;

  823.                 if (i == ntp.node()) {
  824.                     if (!ntp.isProportional())
  825.                         adblQuotesOut[i] = adblQuotesIn[i] + ntp.amount();
  826.                     else
  827.                         adblQuotesOut[i] = adblQuotesIn[i] * (1. + ntp.amount());
  828.                 } else
  829.                     adblQuotesOut[i] = adblQuotesIn[i];
  830.             }
  831.         }

  832.         return adblQuotesOut;
  833.     }

  834.     /**
  835.      * Merge two lists of periods
  836.      *
  837.      * @param lsPeriod1 Period 1
  838.      * @param lsPeriod2 Period 2
  839.      *
  840.      * @return The Merged Period List
  841.      */

  842.     public static final java.util.List<org.drip.analytics.cashflow.CompositePeriod> MergePeriodLists (
  843.         final java.util.List<org.drip.analytics.cashflow.CompositePeriod> lsPeriod1,
  844.         final java.util.List<org.drip.analytics.cashflow.CompositePeriod> lsPeriod2)
  845.     {
  846.         if ((null == lsPeriod1 || 0 == lsPeriod1.size()) && (null == lsPeriod2 || 0 == lsPeriod2.size()))
  847.             return null;

  848.         java.util.List<org.drip.analytics.cashflow.CompositePeriod> lsPeriodMerged = new
  849.             java.util.ArrayList<org.drip.analytics.cashflow.CompositePeriod>();

  850.         if (null == lsPeriod1 || 0 == lsPeriod1.size()) {
  851.             for (org.drip.analytics.cashflow.CompositePeriod p : lsPeriod2) {
  852.                 if (null != p) lsPeriodMerged.add (p);
  853.             }

  854.             return lsPeriodMerged;
  855.         }

  856.         if (null == lsPeriod2 || 0 == lsPeriod2.size()) {
  857.             for (org.drip.analytics.cashflow.CompositePeriod p : lsPeriod1) {
  858.                 if (null != p) lsPeriodMerged.add (p);
  859.             }

  860.             return lsPeriodMerged;
  861.         }

  862.         int iPeriod1Index = 0;
  863.         int iPeriod2Index = 0;

  864.         while (iPeriod1Index < lsPeriod1.size() && iPeriod2Index < lsPeriod2.size()) {
  865.             org.drip.analytics.cashflow.CompositePeriod p1 = lsPeriod1.get (iPeriod1Index);

  866.             org.drip.analytics.cashflow.CompositePeriod p2 = lsPeriod2.get (iPeriod2Index);

  867.             if (p1.payDate() < p2.payDate()) {
  868.                 lsPeriodMerged.add (p1);

  869.                 ++iPeriod1Index;
  870.             } else {
  871.                 lsPeriodMerged.add (p2);

  872.                 ++iPeriod2Index;
  873.             }
  874.         }

  875.         if (iPeriod1Index < lsPeriod1.size() - 1) {
  876.             for (int i = iPeriod1Index; i < lsPeriod1.size(); ++i)
  877.                 lsPeriodMerged.add (lsPeriod1.get (i));
  878.         } else if (iPeriod2Index < lsPeriod2.size() - 1) {
  879.             for (int i = iPeriod2Index; i < lsPeriod2.size(); ++i)
  880.                 lsPeriodMerged.add (lsPeriod2.get (i));
  881.         }

  882.         return lsPeriodMerged;
  883.     }

  884.     /**
  885.      * Aggregate the period lists for an array of components
  886.      *
  887.      * @param aComp Array of Components
  888.      *
  889.      * @return The Aggregated Period Set
  890.      */

  891.     public static final java.util.Set<org.drip.analytics.cashflow.CompositePeriod> AggregateComponentPeriods
  892.         (final org.drip.product.definition.Component[] aComp)
  893.     {
  894.         if (null == aComp) return null;

  895.         int iStartIndex = 0;
  896.         int iNumComp = aComp.length;

  897.         if (0 == iNumComp) return null;

  898.         for (int i = 0; i < iNumComp; ++i) {
  899.             if (null != aComp[i]) {
  900.                 iStartIndex = i;
  901.                 break;
  902.             }
  903.         }

  904.         java.util.Set<org.drip.analytics.cashflow.CompositePeriod> setAggregatedPeriod = new
  905.             java.util.TreeSet<org.drip.analytics.cashflow.CompositePeriod>();

  906.         for (int i = iStartIndex; i < iNumComp; ++i) {
  907.             if (null == aComp[i]) continue;

  908.             java.util.List<org.drip.analytics.cashflow.CompositePeriod> lsCompPeriod =
  909.                 aComp[i].couponPeriods();

  910.             if (null == lsCompPeriod || 0 == lsCompPeriod.size()) continue;

  911.             for (org.drip.analytics.cashflow.CompositePeriod p : lsCompPeriod) {
  912.                 if (null != p) setAggregatedPeriod.add (p);
  913.             }
  914.         }

  915.         return setAggregatedPeriod;
  916.     }

  917.     /**
  918.      * Append the Prefixed Map Entries of the specified Input Map onto the Output Map
  919.      *
  920.      * @param mapOutput The Output Map
  921.      * @param strPrefix The Entry Prefix
  922.      * @param mapInput The Input Map
  923.      *
  924.      * @return TRUE - At least one entry appended
  925.      */

  926.     public static final boolean AccumulateMeasures (
  927.         final org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double> mapOutput,
  928.         final java.lang.String strPrefix,
  929.         final org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double> mapInput)
  930.     {
  931.         if (null == mapOutput || null == strPrefix || strPrefix.isEmpty() || null == mapInput) return false;

  932.         java.util.Set<java.util.Map.Entry<java.lang.String, java.lang.Double>> mapInputESSingle =
  933.             mapInput.entrySet();

  934.         if (null == mapInputESSingle) return false;

  935.         for (java.util.Map.Entry<java.lang.String, java.lang.Double> me : mapInputESSingle) {
  936.             if (null == me) continue;

  937.             java.lang.String strKey = me.getKey();

  938.             if (null == strKey || strKey.isEmpty()) continue;

  939.             mapOutput.put (strPrefix + "@" + strKey, me.getValue());
  940.         }

  941.         return true;
  942.     }

  943.     /**
  944.      * Do the Left and the Right Labels Match?
  945.      *
  946.      * @param lslLeft Left Cash Flow Period Label
  947.      * @param lslRight Right Cash Flow Period Label
  948.      *
  949.      * @return TRUE - The Labels Match
  950.      */

  951.     public static final boolean LabelMatch (
  952.         final org.drip.state.identifier.LatentStateLabel lslLeft,
  953.         final org.drip.state.identifier.LatentStateLabel lslRight)
  954.     {
  955.         if (null == lslLeft && null == lslRight) return true;

  956.         if ((null == lslLeft && null != lslRight) || (null != lslLeft && null == lslRight)) return false;

  957.         return lslLeft.match (lslRight);
  958.     }

  959.     /**
  960.      * Compute the Bond Futures Price AUD Bill Style from the Reference Index Level
  961.      *
  962.      * @param dtValue The Valuation Date
  963.      * @param bond The Bond Instance
  964.      * @param dblReferenceIndex The Reference Index
  965.      *
  966.      * @return The Bond Futures Price AUD Bill Style
  967.      *
  968.      * @throws java.lang.Exception Thrown if the Bond Futures Price AUD Bill Style cannot be computed
  969.      */

  970.     public static final double BondFuturesPriceAUDBillStyle (
  971.         final org.drip.analytics.date.JulianDate dtValue,
  972.         final org.drip.product.definition.Bond bond,
  973.         final double dblReferenceIndex)
  974.         throws java.lang.Exception
  975.     {
  976.         if (null == dtValue || null == bond || !org.drip.numerical.common.NumberUtil.IsValid (dblReferenceIndex))
  977.             throw new java.lang.Exception
  978.                 ("AnalyticsHelper::BondFuturesPriceAUDBillStyle => Invalid Inputs");

  979.         return 1. / (1. + (1. - dblReferenceIndex) * org.drip.analytics.daycount.Convention.YearFraction
  980.             (dtValue.julian(), bond.maturityDate().julian(), bond.accrualDC(), false, null,
  981.                 bond.currency()));
  982.     }

  983.     /**
  984.      * Construct a Normalized, Equally Weighted Array from the Specified Number of Elements
  985.      *
  986.      * @param iNumElement Number of Elements
  987.      *
  988.      * @return The Normalized, Equally Weighted Array
  989.      */

  990.     public static final double[] NormalizedEqualWeightedArray (
  991.         final int iNumElement)
  992.     {
  993.         if (0 >= iNumElement) return null;

  994.         double dblWeight = 1. / iNumElement;
  995.         double[] adblEqualWeighted = new double[iNumElement];

  996.         for (int i = 0; i < iNumElement; ++i)
  997.             adblEqualWeighted[i] = dblWeight;

  998.         return adblEqualWeighted;
  999.     }

  1000.     /**
  1001.      * Aggregate the Base and the Roll Tenors onto a Composite Tenor
  1002.      *
  1003.      * @param strBaseTenor The Base Tenor
  1004.      * @param strRollTenor The Roll Tenor
  1005.      *
  1006.      * @return The Agrregated Composite Tenor
  1007.      */

  1008.     public static final java.lang.String AggregateTenor (
  1009.         final java.lang.String strBaseTenor,
  1010.         final java.lang.String strRollTenor)
  1011.     {
  1012.         if (null == strBaseTenor || strBaseTenor.isEmpty()) return strRollTenor;

  1013.         char chBaseTenor = strBaseTenor.charAt (strBaseTenor.length() - 1);

  1014.         char chRollTenor = strRollTenor.charAt (strRollTenor.length() - 1);

  1015.         if (chRollTenor != chBaseTenor) return null;

  1016.         int iBaseTimeUnit = (int) java.lang.Double.parseDouble (strBaseTenor.substring (0, strBaseTenor.length() -
  1017.             1));

  1018.         int iRollTimeUnit = (int) java.lang.Double.parseDouble (strRollTenor.substring (0, strRollTenor.length() -
  1019.             1));

  1020.         return "" + (iBaseTimeUnit + iRollTimeUnit) + chBaseTenor;
  1021.     }

  1022.     /**
  1023.      * Convert the Array of Tenors into Dates off of a Spot
  1024.      *
  1025.      * @param dtSpot Spot Date
  1026.      * @param astrTenor Array of Tenors
  1027.      *
  1028.      * @return Array of Dates
  1029.      */

  1030.     public static final org.drip.analytics.date.JulianDate[] FromTenor (
  1031.         final org.drip.analytics.date.JulianDate dtSpot,
  1032.         final java.lang.String[] astrTenor)
  1033.     {
  1034.         if (null == dtSpot || null == astrTenor) return null;

  1035.         int iNumTenor = astrTenor.length;
  1036.         org.drip.analytics.date.JulianDate[] adt = 0 == iNumTenor ? null : new
  1037.             org.drip.analytics.date.JulianDate[iNumTenor];

  1038.         if (0 == iNumTenor) return null;

  1039.         for (int i = 0; i < iNumTenor; ++i) {
  1040.             if (null == (adt[i] = dtSpot.addTenor (astrTenor[i]))) return null;
  1041.         }

  1042.         return adt;
  1043.     }

  1044.     /**
  1045.      * Generate an Array of Repeated Spot Dates
  1046.      *
  1047.      * @param dtSpot Spot Date
  1048.      * @param iCount Repeat Count
  1049.      *
  1050.      * @return Array of the Repeated Spot Dates
  1051.      */

  1052.     public static final org.drip.analytics.date.JulianDate[] SpotDateArray (
  1053.         final org.drip.analytics.date.JulianDate dtSpot,
  1054.         final int iCount)
  1055.     {
  1056.         if (null == dtSpot || 0 >= iCount) return null;

  1057.         org.drip.analytics.date.JulianDate[] adtSpot = new org.drip.analytics.date.JulianDate[iCount];

  1058.         for (int i = 0; i < iCount; ++i)
  1059.             adtSpot[i] = dtSpot;

  1060.         return adtSpot;
  1061.     }

  1062.     /**
  1063.      * Generate an Array of Bumped Nodes
  1064.      *
  1065.      * @param adblNode Array of Unbumped Nodes
  1066.      * @param dblBump Bump Amount
  1067.      *
  1068.      * @return Array of Bumped Nodes
  1069.      */

  1070.     public static final double[] ParallelNodeBump (
  1071.         final double[] adblNode,
  1072.         final double dblBump)
  1073.     {
  1074.         if (null == adblNode) return null;

  1075.         int iNumNode = adblNode.length;
  1076.         double[] adblBumpedNode = 0 == iNumNode ? null : new double[iNumNode];

  1077.         for (int i = 0; i < iNumNode; ++i) {
  1078.             if (!org.drip.numerical.common.NumberUtil.IsValid (adblBumpedNode[i] = adblNode[i] + dblBump))
  1079.                 return null;
  1080.         }

  1081.         return adblBumpedNode;
  1082.     }

  1083.     /**
  1084.      * Converts the Nano-Second Interval into aH:bM:cS:dMS Format
  1085.      *
  1086.      * @param lElapsedNanos The Elapsed Nano Time
  1087.      *
  1088.      * @return The Nano-Second Interval in the aH:bM:cS:dMS Format
  1089.      */

  1090.     public static final java.lang.String IntervalHMSMS (
  1091.         final long lElapsedNanos)
  1092.     {
  1093.         if (0 >= lElapsedNanos) return "";

  1094.         java.lang.String strHMS = "";
  1095.         long lElapsedSeconds = (long) ((0.5 + lElapsedNanos) * 1.e-09);
  1096.         long lElapsedMillis = (lElapsedNanos - lElapsedSeconds * 1000000000) / 1000000;

  1097.         if (lElapsedSeconds >= 3600)
  1098.         {
  1099.             strHMS = strHMS + (lElapsedSeconds / 3600) + " h ";
  1100.             lElapsedSeconds = lElapsedSeconds % 3600;
  1101.         }

  1102.         if (lElapsedSeconds >= 60)
  1103.         {
  1104.             strHMS = strHMS + (lElapsedSeconds / 60) + " m ";
  1105.             lElapsedSeconds = lElapsedSeconds % 60;
  1106.         }

  1107.         if (lElapsedSeconds > 0)
  1108.         {
  1109.             strHMS = strHMS + lElapsedSeconds + " s ";
  1110.         }

  1111.         return strHMS + lElapsedMillis + " ms";
  1112.     }
  1113. }