CurveStretch.java

  1. package org.drip.state.estimator;

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

  80. /**
  81.  * <i>CurveStretch</i> expands the regular Multi-Segment Stretch to aid the calibration of Boot-strapped
  82.  * Instruments. In particular, CurveStretch implements the following functions that are used at different
  83.  * stages of curve construction sequence:
  84.  *
  85.  *  <br><br>
  86.  *  <ul>
  87.  *      <li>
  88.  *          Mark the Range of the "built" Segments
  89.  *      </li>
  90.  *      <li>
  91.  *          Clear the built range mark to signal the start of a fresh calibration run
  92.  *      </li>
  93.  *      <li>
  94.  *          Indicate if the specified Predictor Ordinate is inside the "Built" Range
  95.  *      </li>
  96.  *      <li>
  97.  *          Retrieve the MergeSubStretchManager
  98.  *      </li>
  99.  *  </ul>
  100.  *
  101.  *  <br><br>
  102.  *  <ul>
  103.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  104.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  105.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/state/README.md">Latent State Inference and Creation Utilities</a></li>
  106.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/state/estimator/README.md">Multi-Pass Customized Stretch Curve</a></li>
  107.  *  </ul>
  108.  * <br><br>
  109.  *
  110.  * @author Lakshmi Krishnamurthy
  111.  */

  112. public class CurveStretch extends org.drip.spline.stretch.CalibratableMultiSegmentSequence {
  113.     private double _dblBuiltPredictorOrdinateRight = java.lang.Double.NaN;
  114.     private org.drip.state.representation.MergeSubStretchManager _msm = null;

  115.     /**
  116.      * CurveStretch constructor - Construct a sequence of Basis Spline Segments
  117.      *
  118.      * @param strName Name of the Stretch
  119.      * @param aCS Array of Segments
  120.      * @param aSCBC Array of Segment Builder Parameters
  121.      *
  122.      * @throws java.lang.Exception Thrown if the inputs are invalid
  123.      */

  124.     public CurveStretch (
  125.         final java.lang.String strName,
  126.         final org.drip.spline.segment.LatentStateResponseModel[] aCS,
  127.         final org.drip.spline.params.SegmentCustomBuilderControl[] aSCBC)
  128.         throws java.lang.Exception
  129.     {
  130.         super (strName, aCS, aSCBC);

  131.         _dblBuiltPredictorOrdinateRight = getLeftPredictorOrdinateEdge();
  132.     }

  133.     /**
  134.      * Mark the Range of the "built" Segments, and set the set of Merge Latent States
  135.      *
  136.      * @param iSegment The Current Segment Range Built
  137.      * @param setLSL Set of the merging Latent State Labels
  138.      *
  139.      * @return TRUE - Range successfully marked as "built"
  140.      */

  141.     public boolean markSegmentBuilt (
  142.         final int iSegment,
  143.         final java.util.Set<org.drip.state.identifier.LatentStateLabel> setLSL)
  144.     {
  145.         org.drip.spline.segment.LatentStateResponseModel[] aCS = segments();

  146.         if (iSegment >= aCS.length) return false;

  147.         _dblBuiltPredictorOrdinateRight = aCS[iSegment].right();

  148.         if (null == setLSL || 0 == setLSL.size()) return true;

  149.         if (null == _msm) _msm = new org.drip.state.representation.MergeSubStretchManager();

  150.         for (org.drip.state.identifier.LatentStateLabel lsl : setLSL) {
  151.             try {
  152.                 if (!_msm.addMergeStretch (new org.drip.state.representation.LatentStateMergeSubStretch
  153.                     (aCS[iSegment].left(), aCS[iSegment].right(), lsl)))
  154.                     return false;
  155.             } catch (java.lang.Exception e) {
  156.                 e.printStackTrace();
  157.             }
  158.         }

  159.         return true;
  160.     }

  161.     /**
  162.      * Clear the built range mark to signal the start of a fresh calibration run
  163.      *
  164.      * @return TRUE - Built Range successfully cleared
  165.      */

  166.     public boolean clearBuiltRange()
  167.     {
  168.         _dblBuiltPredictorOrdinateRight = getLeftPredictorOrdinateEdge();

  169.         _msm = null;
  170.         return true;
  171.     }

  172.     /**
  173.      * Indicate if the specified Predictor Ordinate is inside the "Built" Range
  174.      *
  175.      * @param dblPredictorOrdinate The Predictor Ordinate
  176.      *
  177.      * @return TRUE - The specified Predictor Ordinate is inside the "Built" Range
  178.      *
  179.      * @throws java.lang.Exception Thrown if inputs are invalid
  180.      */

  181.     public boolean inBuiltRange (
  182.         final double dblPredictorOrdinate)
  183.         throws java.lang.Exception
  184.     {
  185.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblPredictorOrdinate))
  186.             throw new java.lang.Exception ("CurveStretch.inBuiltRange => Invalid Inputs");

  187.         return dblPredictorOrdinate >= getLeftPredictorOrdinateEdge() && dblPredictorOrdinate <=
  188.             _dblBuiltPredictorOrdinateRight;
  189.     }

  190.     @Override public org.drip.state.representation.MergeSubStretchManager msm()
  191.     {
  192.         return _msm;
  193.     }
  194. }