OverlappingStretchSpan.java

  1. package org.drip.spline.grid;

  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>OverlappingStretchSpan</i> implements the Span interface, and the collection functionality of
  81.  * overlapping Stretches. In addition to providing a custom implementation of all the Span interface stubs,
  82.  * it also converts the Overlapping Stretch Span to a non-overlapping Stretch Span. Overlapping Stretches are
  83.  * clipped from the Left.
  84.  *
  85.  * <br><br>
  86.  *  <ul>
  87.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  88.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/SplineBuilderLibrary.md">Spline Builder Library</a></li>
  89.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spline/README.md">Basis Splines and Linear Compounders across a Broad Family of Spline Basis Functions</a></li>
  90.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spline/grid/README.md">Aggregated/Overlapping Stretch/Span Grids</a></li>
  91.  *  </ul>
  92.  * <br><br>
  93.  *
  94.  * @author Lakshmi Krishnamurthy
  95.  */

  96. public class OverlappingStretchSpan implements org.drip.spline.grid.Span {
  97.     private java.util.List<org.drip.spline.stretch.MultiSegmentSequence> _lsMSS = new
  98.         java.util.ArrayList<org.drip.spline.stretch.MultiSegmentSequence>();

  99.     /**
  100.      * OverlappingStretchSpan constructor
  101.      *
  102.      * @param mss The Initial Stretch in the Span
  103.      *
  104.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  105.      */

  106.     public OverlappingStretchSpan (
  107.         final org.drip.spline.stretch.MultiSegmentSequence mss)
  108.         throws java.lang.Exception
  109.     {
  110.         if (null == mss) throw new java.lang.Exception ("OverlappingStretchSpan ctr: Invalid Inputs");

  111.         _lsMSS.add (mss);
  112.     }

  113.     @Override public boolean addStretch (
  114.         final org.drip.spline.stretch.MultiSegmentSequence mss)
  115.     {
  116.         if (null == mss) return false;

  117.         _lsMSS.add (mss);

  118.         return true;
  119.     }

  120.     @Override public org.drip.spline.stretch.MultiSegmentSequence getContainingStretch (
  121.         final double dblPredictorOrdinate)
  122.     {
  123.         if (null == _lsMSS || 0 == _lsMSS.size()) return null;

  124.         for (org.drip.spline.stretch.MultiSegmentSequence mss : _lsMSS) {
  125.             try {
  126.                 if (mss.in (dblPredictorOrdinate)) return mss;
  127.             } catch (java.lang.Exception e) {
  128.                 e.printStackTrace();

  129.                 return null;
  130.             }
  131.         }

  132.         return null;
  133.     }

  134.     @Override public org.drip.spline.stretch.MultiSegmentSequence getStretch (
  135.         final java.lang.String strName)
  136.     {
  137.         if (null == strName) return null;

  138.         for (org.drip.spline.stretch.MultiSegmentSequence mss : _lsMSS) {
  139.             if (strName.equalsIgnoreCase (mss.name())) return mss;
  140.         }

  141.         return null;
  142.     }

  143.     @Override public double left()
  144.         throws java.lang.Exception
  145.     {
  146.         if (0 == _lsMSS.size())
  147.             throw new java.lang.Exception ("OverlappingStretchSpan::left => No valid Stretches found");

  148.         return _lsMSS.get (0).getLeftPredictorOrdinateEdge();
  149.     }

  150.     @Override public double right()
  151.         throws java.lang.Exception
  152.     {
  153.         if (0 == _lsMSS.size())
  154.             throw new java.lang.Exception ("OverlappingStretchSpan::right => No valid Stretches found");

  155.         return _lsMSS.get (_lsMSS.size() - 1).getRightPredictorOrdinateEdge();
  156.     }

  157.     @Override public double calcResponseValue (
  158.         final double dblPredictorOrdinate)
  159.         throws java.lang.Exception
  160.     {
  161.         for (org.drip.spline.stretch.MultiSegmentSequence mss : _lsMSS) {
  162.             if (mss.in (dblPredictorOrdinate)) return mss.responseValue (dblPredictorOrdinate);
  163.         }

  164.         throw new java.lang.Exception ("OverlappingStretchSpan::calcResponseValue => Cannot Calculate!");
  165.     }

  166.     @Override public double calcResponseValueDerivative (
  167.         final double dblPredictorOrdinate,
  168.         final int iOrder)
  169.         throws java.lang.Exception
  170.     {
  171.         for (org.drip.spline.stretch.MultiSegmentSequence mss : _lsMSS) {
  172.             if (mss.in (dblPredictorOrdinate))
  173.                 return mss.responseValueDerivative (dblPredictorOrdinate, iOrder);
  174.         }

  175.         throw new java.lang.Exception
  176.             ("OverlappingStretchSpan::calcResponseValueDerivative => Cannot Calculate!");
  177.     }

  178.     @Override public boolean isMergeState (
  179.         final double dblPredictorOrdinate,
  180.         final org.drip.state.identifier.LatentStateLabel lsl)
  181.     {
  182.         try {
  183.             for (org.drip.spline.stretch.MultiSegmentSequence mss : _lsMSS) {
  184.                 if (mss.in (dblPredictorOrdinate)) {
  185.                     org.drip.state.representation.MergeSubStretchManager msm = mss.msm();

  186.                     return null == msm ? false : msm.partOfMergeState (dblPredictorOrdinate, lsl);
  187.                 }
  188.             }
  189.         } catch (java.lang.Exception e) {
  190.             e.printStackTrace();
  191.         }

  192.         return false;
  193.     }

  194.     @Override public org.drip.numerical.differentiation.WengertJacobian jackDResponseDManifestMeasure (
  195.         final java.lang.String strManifestMeasure,
  196.         final double dblPredictorOrdinate,
  197.         final int iOrder)
  198.     {
  199.         if (0 == _lsMSS.size()) return null;

  200.         java.util.List<org.drip.numerical.differentiation.WengertJacobian> lsWJ = new
  201.             java.util.ArrayList<org.drip.numerical.differentiation.WengertJacobian>();

  202.         boolean bPredictorOrdinateCovered = false;

  203.         for (org.drip.spline.stretch.MultiSegmentSequence mss : _lsMSS) {
  204.             if (null == mss) continue;

  205.             try {
  206.                 org.drip.numerical.differentiation.WengertJacobian wj = null;

  207.                 if (!bPredictorOrdinateCovered && mss.in (dblPredictorOrdinate)) {
  208.                     wj = mss.jackDResponseDManifestMeasure (strManifestMeasure, dblPredictorOrdinate,
  209.                         iOrder);

  210.                     bPredictorOrdinateCovered = true;
  211.                 } else
  212.                     wj = new org.drip.numerical.differentiation.WengertJacobian (1, mss.segments().length);

  213.                 if (null != wj) lsWJ.add (wj);
  214.             } catch (java.lang.Exception e) {
  215.                 e.printStackTrace();

  216.                 return null;
  217.             }
  218.         }

  219.         return org.drip.numerical.common.CollectionUtil.AppendWengert (lsWJ);
  220.     }

  221.     @Override public boolean in (
  222.         final double dblPredictorOrdinate)
  223.         throws java.lang.Exception
  224.     {
  225.         for (org.drip.spline.stretch.MultiSegmentSequence mss : _lsMSS) {
  226.             if (mss.in (dblPredictorOrdinate)) return true;
  227.         }

  228.         return false;
  229.     }

  230.     /**
  231.      * Convert the Overlapping Stretch Span to a non-overlapping Stretch Span. Overlapping Stretches are
  232.      *  clipped from the Left.
  233.      *  
  234.      * @return The Non-overlapping Stretch Span Instance
  235.      */

  236.     public org.drip.spline.grid.Span toNonOverlapping()
  237.     {
  238.         if (0 == _lsMSS.size()) return null;

  239.         org.drip.spline.grid.OverlappingStretchSpan oss = null;
  240.         org.drip.spline.stretch.MultiSegmentSequence mssPrev = null;

  241.         for (org.drip.spline.stretch.MultiSegmentSequence mss : _lsMSS) {
  242.             if (null == mss) continue;

  243.             if (null == oss) {
  244.                 try {
  245.                     oss = new org.drip.spline.grid.OverlappingStretchSpan (mssPrev = mss);
  246.                 } catch (java.lang.Exception e) {
  247.                     e.printStackTrace();

  248.                     return null;
  249.                 }
  250.             } else {
  251.                 double dblPrevRightPredictorOrdinateEdge = mssPrev.getRightPredictorOrdinateEdge();

  252.                 double dblCurrentLeftPredictorOrdinateEdge = mss.getLeftPredictorOrdinateEdge();

  253.                 if (dblCurrentLeftPredictorOrdinateEdge >= dblPrevRightPredictorOrdinateEdge)
  254.                     oss.addStretch (mss);
  255.                 else
  256.                     oss.addStretch (mss.clipLeft (mss.name(), dblPrevRightPredictorOrdinateEdge));
  257.             }
  258.         }

  259.         return oss;
  260.     }

  261.     @Override public java.lang.String displayString()
  262.     {
  263.         java.lang.StringBuffer sb = new java.lang.StringBuffer();

  264.         for (org.drip.spline.stretch.MultiSegmentSequence mss : _lsMSS)
  265.             sb.append (mss.name() + " | " + new org.drip.analytics.date.JulianDate ((int)
  266.                 mss.getLeftPredictorOrdinateEdge()) + " => " + new org.drip.analytics.date.JulianDate ((int)
  267.                     mss.getRightPredictorOrdinateEdge()) + "\n");

  268.         return sb.toString();
  269.     }
  270. }