CalibratableMultiSegmentSequence.java

  1. package org.drip.spline.stretch;

  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>CalibratableMultiSegmentSequence</i> implements the MultiSegmentSequence span that spans multiple
  82.  * segments. It holds the ordered segment sequence, segment sequence builder, the segment control parameters,
  83.  * and, if available, the spanning Jacobian. It provides a variety of customization for the segment
  84.  * construction and state representation control.
  85.  *
  86.  * <br><br>
  87.  *  <ul>
  88.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  89.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/SplineBuilderLibrary.md">Spline Builder Library</a></li>
  90.  *      <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>
  91.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spline/stretch/README.md">Multi-Segment Sequence Spline Stretch</a></li>
  92.  *  </ul>
  93.  * <br><br>
  94.  *
  95.  * @author Lakshmi Krishnamurthy
  96.  */

  97. public class CalibratableMultiSegmentSequence extends org.drip.function.definition.R1ToR1 implements
  98.     org.drip.spline.stretch.MultiSegmentSequence {
  99.     private static final int MAXIMA_PREDICTOR_ORDINATE_NODE = 1;
  100.     private static final int MINIMA_PREDICTOR_ORDINATE_NODE = 2;
  101.     private static final int MONOTONE_PREDICTOR_ORDINATE_NODE = 4;

  102.     private java.lang.String _strName = "";
  103.     private org.drip.spline.stretch.SegmentSequenceBuilder _ssb = null;
  104.     private org.drip.spline.segment.LatentStateResponseModel[] _aLSRM = null;
  105.     private org.drip.spline.params.SegmentCustomBuilderControl[] _aSCBC = null;
  106.     private org.drip.numerical.differentiation.WengertJacobian _wjDCoeffDEdgeParams = null;

  107.     private boolean setDCoeffDEdgeParams (
  108.         final int iNodeIndex,
  109.         final org.drip.numerical.differentiation.WengertJacobian wjDCoeffDEdgeParams)
  110.     {
  111.         if (null == wjDCoeffDEdgeParams) return false;

  112.         int iParameterIndex = 0 == iNodeIndex ? 0 : 2;

  113.         if (!_wjDCoeffDEdgeParams.accumulatePartialFirstDerivative (0, iNodeIndex,
  114.             wjDCoeffDEdgeParams.firstDerivative (0, iParameterIndex)))
  115.             return false;

  116.         if (!_wjDCoeffDEdgeParams.accumulatePartialFirstDerivative (1, iNodeIndex,
  117.             wjDCoeffDEdgeParams.firstDerivative (1, iParameterIndex)))
  118.             return false;

  119.         if (!_wjDCoeffDEdgeParams.accumulatePartialFirstDerivative (2, iNodeIndex,
  120.             wjDCoeffDEdgeParams.firstDerivative (2, iParameterIndex)))
  121.             return false;

  122.         return _wjDCoeffDEdgeParams.accumulatePartialFirstDerivative (3, iNodeIndex,
  123.             wjDCoeffDEdgeParams.firstDerivative (3, iParameterIndex));
  124.     }

  125.     private final org.drip.numerical.differentiation.WengertJacobian setDResponseDEdgeResponse (
  126.         final int iNodeIndex,
  127.         final org.drip.numerical.differentiation.WengertJacobian wjDResponseDEdgeParams)
  128.     {
  129.         if (null == wjDResponseDEdgeParams) return null;

  130.         int iNumSegment = _aLSRM.length;
  131.         org.drip.numerical.differentiation.WengertJacobian wjDResponseDEdgeResponse = null;

  132.         try {
  133.             wjDResponseDEdgeResponse = new org.drip.numerical.differentiation.WengertJacobian (1, iNumSegment + 1);
  134.         } catch (java.lang.Exception e) {
  135.             e.printStackTrace();

  136.             return null;
  137.         }

  138.         for (int i = 0; i <= iNumSegment; ++i) {
  139.             if (i == iNodeIndex) {
  140.                 if (!wjDResponseDEdgeResponse.accumulatePartialFirstDerivative (0, i,
  141.                     wjDResponseDEdgeParams.firstDerivative (0,
  142.                         org.drip.spline.segment.LatentStateResponseModel.LEFT_NODE_VALUE_PARAMETER_INDEX)) ||
  143.                             !wjDResponseDEdgeResponse.accumulatePartialFirstDerivative (0, i + 1,
  144.                                 wjDResponseDEdgeParams.firstDerivative (0,
  145.                                     org.drip.spline.segment.LatentStateResponseModel.RIGHT_NODE_VALUE_PARAMETER_INDEX)))
  146.                     return null;
  147.             }
  148.         }

  149.         return wjDResponseDEdgeResponse;
  150.     }

  151.     /**
  152.      * CalibratableMultiSegmentSequence constructor - Construct a sequence of Basis Spline Segments
  153.      *
  154.      * @param strName Name of the Stretch
  155.      * @param aCS Array of Segments
  156.      * @param aSCBC Array of Segment Builder Parameters
  157.      *
  158.      * @throws java.lang.Exception Thrown if the inputs are invalid
  159.      */

  160.     public CalibratableMultiSegmentSequence (
  161.         final java.lang.String strName,
  162.         final org.drip.spline.segment.LatentStateResponseModel[] aCS,
  163.         final org.drip.spline.params.SegmentCustomBuilderControl[] aSCBC)
  164.         throws java.lang.Exception
  165.     {
  166.         super (null);

  167.         if (null == aCS || null == aSCBC || null == (_strName = strName) || _strName.isEmpty())
  168.             throw new java.lang.Exception ("CalibratableMultiSegmentSequence ctr => Invalid inputs!");

  169.         int iNumSegment = aCS.length;
  170.         _aLSRM = new org.drip.spline.segment.LatentStateResponseModel[iNumSegment];
  171.         _aSCBC = new org.drip.spline.params.SegmentCustomBuilderControl[iNumSegment];

  172.         if (0 == iNumSegment || iNumSegment != aSCBC.length)
  173.             throw new java.lang.Exception ("CalibratableMultiSegmentSequence ctr => Invalid inputs!");

  174.         for (int i = 0; i < iNumSegment; ++i) {
  175.             if (null == (_aLSRM[i] = aCS[i]) || null == (_aSCBC[i] = aSCBC[i]))
  176.                 throw new java.lang.Exception ("CalibratableMultiSegmentSequence ctr => Invalid inputs!");
  177.         }
  178.     }

  179.     @Override public java.lang.String name()
  180.     {
  181.         return _strName;
  182.     }

  183.     @Override public org.drip.spline.segment.LatentStateResponseModel[] segments()
  184.     {
  185.         return _aLSRM;
  186.     }

  187.     @Override public org.drip.spline.params.SegmentCustomBuilderControl[] segmentBuilderControl()
  188.     {
  189.         return _aSCBC;
  190.     }

  191.     @Override public boolean setup (
  192.         final org.drip.spline.stretch.SegmentSequenceBuilder ssb,
  193.         final int iCalibrationDetail)
  194.     {
  195.         if (null == (_ssb = ssb) || !_ssb.setStretch (this)) return false;

  196.         if (org.drip.spline.stretch.BoundarySettings.BOUNDARY_CONDITION_FLOATING ==
  197.             _ssb.getCalibrationBoundaryCondition().boundaryCondition()) {
  198.             if (!_ssb.calibStartingSegment (0.) || !_ssb.calibSegmentSequence (1) ||
  199.                 !_ssb.manifestMeasureSensitivity (0.))
  200.                 return false;
  201.         } else if (0 != (org.drip.spline.stretch.MultiSegmentSequence.CALIBRATE & iCalibrationDetail)) {
  202.             org.drip.function.r1tor1solver.FixedPointFinderOutput fpop = null;

  203.             if (null == fpop || !fpop.containsRoot()) {
  204.                 try {
  205.                     fpop = new org.drip.function.r1tor1solver.FixedPointFinderZheng (0., this,
  206.                         true).findRoot();
  207.                 } catch (java.lang.Exception e) {
  208.                     e.printStackTrace();

  209.                     return false;
  210.                 }
  211.             }

  212.             if (null == fpop || !org.drip.numerical.common.NumberUtil.IsValid (fpop.getRoot()) ||
  213.                 !_ssb.manifestMeasureSensitivity (0.)) {
  214.                 System.out.println ("FPOP: " + fpop);

  215.                 return false;
  216.             }
  217.         }

  218.         if (0 != (org.drip.spline.stretch.MultiSegmentSequence.CALIBRATE_JACOBIAN & iCalibrationDetail)) {
  219.             int iNumSegment = _aLSRM.length;

  220.             try {
  221.                 if (null == (_wjDCoeffDEdgeParams = new org.drip.numerical.differentiation.WengertJacobian
  222.                     (_aLSRM[0].basisEvaluator().numBasis(), iNumSegment + 1)))
  223.                     return false;
  224.             } catch (java.lang.Exception e) {
  225.                 e.printStackTrace();

  226.                 return false;
  227.             }

  228.             org.drip.numerical.differentiation.WengertJacobian wjHead = _aLSRM[0].jackDCoeffDEdgeInputs();

  229.             if (!setDCoeffDEdgeParams (0, wjHead) || !setDCoeffDEdgeParams (1, wjHead)) return false;

  230.             for (int i = 1; i < iNumSegment; ++i) {
  231.                 if (!setDCoeffDEdgeParams (i + 1, _aLSRM[i].jackDCoeffDEdgeInputs())) return false;
  232.             }
  233.         }

  234.         return true;
  235.     }

  236.     @Override public boolean setup (
  237.         final org.drip.spline.params.SegmentResponseValueConstraint srvcLeading,
  238.         final org.drip.spline.params.SegmentResponseValueConstraint[] aSRVC,
  239.         final org.drip.spline.params.StretchBestFitResponse sbfr,
  240.         final org.drip.spline.stretch.BoundarySettings bs,
  241.         final int iCalibrationDetail)
  242.     {
  243.         try {
  244.             return setup (new org.drip.spline.stretch.CkSegmentSequenceBuilder (srvcLeading, aSRVC, sbfr,
  245.                 bs), iCalibrationDetail);
  246.         } catch (java.lang.Exception e) {
  247.             e.printStackTrace();
  248.         }

  249.         return false;
  250.     }

  251.     @Override public boolean setup (
  252.         final double dblLeftStretchResponseValue,
  253.         final org.drip.spline.params.SegmentResponseValueConstraint[] aSRVC,
  254.         final org.drip.spline.params.StretchBestFitResponse sbfr,
  255.         final org.drip.spline.stretch.BoundarySettings bs,
  256.         final int iCalibrationDetail)
  257.     {
  258.         return setup (org.drip.spline.params.SegmentResponseValueConstraint.FromPredictorResponsePair
  259.             (getLeftPredictorOrdinateEdge(), dblLeftStretchResponseValue), aSRVC, sbfr, bs,
  260.                 iCalibrationDetail);
  261.     }

  262.     @Override public boolean setup (
  263.         final double dblLeftStretchResponseValue,
  264.         final double[] adblSegmentRightResponseValue,
  265.         final org.drip.spline.params.StretchBestFitResponse sbfr,
  266.         final org.drip.spline.stretch.BoundarySettings bs,
  267.         final int iCalibrationDetail)
  268.     {
  269.         int iNumSegment = _aLSRM.length;
  270.         org.drip.spline.params.SegmentResponseValueConstraint[] aSRVCRight = new
  271.             org.drip.spline.params.SegmentResponseValueConstraint[iNumSegment];

  272.         if (0 == iNumSegment || iNumSegment != adblSegmentRightResponseValue.length) return false;

  273.         try {
  274.             for (int i = 0; i < iNumSegment; ++i)
  275.                 aSRVCRight[i] = new org.drip.spline.params.SegmentResponseValueConstraint (new double[]
  276.                     {_aLSRM[i].right()}, new double[] {1.}, adblSegmentRightResponseValue[i]);
  277.         } catch (java.lang.Exception e) {
  278.             e.printStackTrace();

  279.             return false;
  280.         }

  281.         return setup (dblLeftStretchResponseValue, aSRVCRight, sbfr, bs, iCalibrationDetail);
  282.     }

  283.     @Override public boolean setupHermite (
  284.         final org.drip.spline.params.SegmentPredictorResponseDerivative[] aSPRDLeft,
  285.         final org.drip.spline.params.SegmentPredictorResponseDerivative[] aSPRDRight,
  286.         final org.drip.spline.params.SegmentResponseValueConstraint[][] aaSRVC,
  287.         final org.drip.spline.params.StretchBestFitResponse sbfr,
  288.         final int iSetupMode)
  289.     {
  290.         if (null == aSPRDLeft || null == aSPRDRight) return false;

  291.         int iNumSegment = _aLSRM.length;

  292.         if (iNumSegment != aSPRDLeft.length || iNumSegment != aSPRDRight.length || (null != aaSRVC &&
  293.             iNumSegment != aaSRVC.length))
  294.             return false;

  295.         for (int i = 0; i < iNumSegment; ++i) {
  296.             try {
  297.                 int iNumSegmentConstraint = 0;
  298.                 org.drip.spline.params.SegmentBasisFlexureConstraint[] aSBFC = null;

  299.                 if (null != aaSRVC && null != aaSRVC[i]) {
  300.                     aSBFC = new org.drip.spline.params.SegmentBasisFlexureConstraint[iNumSegmentConstraint =
  301.                         aaSRVC[i].length];

  302.                     for (int j = 0; i < iNumSegmentConstraint; ++j)
  303.                         aSBFC[j] = null == aaSRVC[i][j] ? null : aaSRVC[i][j].responseIndexedBasisConstraint
  304.                             (_aLSRM[i].basisEvaluator(), _aLSRM[i]);
  305.                 }

  306.                 if (0 != (org.drip.spline.stretch.MultiSegmentSequence.CALIBRATE & iSetupMode) &&
  307.                     !_aLSRM[i].calibrateState (new org.drip.spline.params.SegmentStateCalibrationInputs (new
  308.                         double[] {_aLSRM[i].left(), _aLSRM[i].right()}, new double[]
  309.                             {aSPRDLeft[i].responseValue(), aSPRDRight[i].responseValue()},
  310.                                 aSPRDLeft[i].getDResponseDPredictorOrdinate(),
  311.                                     aSPRDRight[i].getDResponseDPredictorOrdinate(), aSBFC, null == sbfr ?
  312.                                         null : sbfr.sizeToSegment (_aLSRM[i]))))
  313.                     return false;
  314.             } catch (java.lang.Exception e) {
  315.                 e.printStackTrace();

  316.                 return false;
  317.             }
  318.         }

  319.         if (0 != (org.drip.spline.stretch.MultiSegmentSequence.CALIBRATE_JACOBIAN & iSetupMode)) {
  320.             try {
  321.                 if (null == (_wjDCoeffDEdgeParams = new org.drip.numerical.differentiation.WengertJacobian
  322.                     (_aLSRM[0].basisEvaluator().numBasis(), iNumSegment + 1)))
  323.                     return false;
  324.             } catch (java.lang.Exception e) {
  325.                 e.printStackTrace();

  326.                 return false;
  327.             }

  328.             org.drip.numerical.differentiation.WengertJacobian wjDCoeffDEdgeParamsHead =
  329.                 _aLSRM[0].jackDCoeffDEdgeInputs();

  330.             if (!setDCoeffDEdgeParams (0, wjDCoeffDEdgeParamsHead) || !setDCoeffDEdgeParams (1,
  331.                 wjDCoeffDEdgeParamsHead))
  332.                 return false;

  333.             for (int i = 1; i < iNumSegment; ++i) {
  334.                 if (!setDCoeffDEdgeParams (i + 1, _aLSRM[i].jackDCoeffDEdgeInputs())) return false;
  335.             }
  336.         }

  337.         return true;
  338.     }

  339.     @Override public double evaluate (
  340.         final double dblLeftSlope)
  341.         throws java.lang.Exception
  342.     {
  343.         if (null == _ssb || !_ssb.calibStartingSegment (dblLeftSlope) || !_ssb.calibSegmentSequence (1))
  344.             throw new java.lang.Exception
  345.                 ("CalibratableMultiSegmentSequence::evaluate => cannot set up segments!");

  346.         org.drip.spline.stretch.BoundarySettings bs = _ssb.getCalibrationBoundaryCondition();

  347.         int iBC = bs.boundaryCondition();

  348.         if (org.drip.spline.stretch.BoundarySettings.BOUNDARY_CONDITION_NATURAL == iBC)
  349.             return calcRightEdgeDerivative (bs.rightDerivOrder());

  350.         if (org.drip.spline.stretch.BoundarySettings.BOUNDARY_CONDITION_FINANCIAL == iBC)
  351.         {
  352.             return calcRightEdgeDerivative (bs.rightDerivOrder());
  353.         }

  354.         if (org.drip.spline.stretch.BoundarySettings.BOUNDARY_CONDITION_NOT_A_KNOT == iBC)
  355.             return calcRightEdgeDerivative (bs.rightDerivOrder()) - calcLeftEdgeDerivative
  356.                 (bs.leftDerivOrder());

  357.         throw new java.lang.Exception ("CalibratableMultiSegmentSequence::evaluate => Boundary Condition " +
  358.             iBC + " unknown");
  359.     }

  360.     @Override public double integrate (
  361.         final double dblBegin,
  362.         final double dblEnd)
  363.         throws java.lang.Exception
  364.     {
  365.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblBegin) || !org.drip.numerical.common.NumberUtil.IsValid
  366.             (dblEnd))
  367.             throw new java.lang.Exception ("CalibratableMultiSegmentSequence::integrate => Invalid Inputs");

  368.         return org.drip.numerical.integration.R1ToR1Integrator.Boole (this, dblBegin, dblEnd);
  369.     }

  370.     @Override public boolean setLeftNode (
  371.         final double dblStretchLeftResponse,
  372.         final double dblStretchLeftResponseSlope,
  373.         final double dblStretchRightResponse,
  374.         final org.drip.spline.params.StretchBestFitResponse sbfr)
  375.     {
  376.         return _aLSRM[0].calibrate
  377.             (org.drip.spline.params.SegmentResponseValueConstraint.FromPredictorResponsePair
  378.                 (getLeftPredictorOrdinateEdge(), dblStretchLeftResponse), dblStretchLeftResponseSlope,
  379.                     org.drip.spline.params.SegmentResponseValueConstraint.FromPredictorResponsePair
  380.                         (getRightPredictorOrdinateEdge(), dblStretchRightResponse), null == sbfr ? null :
  381.                             sbfr.sizeToSegment (_aLSRM[0]));
  382.     }

  383.     @Override public double responseValue (
  384.         final double dblPredictorOrdinate)
  385.         throws java.lang.Exception
  386.     {
  387.         return _aLSRM[containingIndex (dblPredictorOrdinate, true, true)].responseValue
  388.             (dblPredictorOrdinate);
  389.     }

  390.     @Override public double responseValueDerivative (
  391.         final double dblPredictorOrdinate,
  392.         final int iOrder)
  393.         throws java.lang.Exception
  394.     {
  395.         return _aLSRM[containingIndex (dblPredictorOrdinate, true, true)].calcResponseValueDerivative
  396.             (dblPredictorOrdinate, iOrder);
  397.     }

  398.     @Override public org.drip.spline.params.SegmentPredictorResponseDerivative calcSPRD (
  399.         final double dblPredictorOrdinate)
  400.     {
  401.         int iIndex = -1;

  402.         try {
  403.             iIndex = containingIndex (dblPredictorOrdinate, true, true);
  404.         } catch (java.lang.Exception e) {
  405.             e.printStackTrace();

  406.             return null;
  407.         }

  408.         int iCk = _aSCBC[iIndex].inelasticParams().Ck();

  409.         double adblDeriv[] = new double[iCk];

  410.         try {
  411.             for (int i = 0; i < iCk; ++i)
  412.                 adblDeriv[i] = _aLSRM[iIndex].calcResponseValueDerivative (dblPredictorOrdinate, i);

  413.             return new org.drip.spline.params.SegmentPredictorResponseDerivative
  414.                 (_aLSRM[iIndex].responseValue (dblPredictorOrdinate), adblDeriv);
  415.         } catch (java.lang.Exception e) {
  416.             e.printStackTrace();
  417.         }

  418.         return null;
  419.     }

  420.     @Override public org.drip.numerical.differentiation.WengertJacobian jackDResponseDCalibrationInput (
  421.         final double dblPredictorOrdinate,
  422.         final int iOrder)
  423.     {
  424.         int iIndex = -1;

  425.         try {
  426.             iIndex = containingIndex (dblPredictorOrdinate, true, true);
  427.         } catch (java.lang.Exception e) {
  428.             e.printStackTrace();

  429.             return null;
  430.         }

  431.         return setDResponseDEdgeResponse (iIndex, _aLSRM[iIndex].jackDResponseDEdgeInput
  432.             (dblPredictorOrdinate, iOrder));
  433.     }

  434.     @Override public org.drip.numerical.differentiation.WengertJacobian jackDResponseDManifestMeasure (
  435.         final java.lang.String strManifestMeasure,
  436.         final double dblPredictorOrdinate,
  437.         final int iOrder)
  438.     {
  439.         int iPriorImpactFadeIndex = 0;
  440.         int iNumSegment = _aLSRM.length;

  441.         try {
  442.             int iIndex = containingIndex (dblPredictorOrdinate, true, true);

  443.             boolean bContainingSegmentImpactFade = _aLSRM[iIndex].impactFade (strManifestMeasure);

  444.             if (!bContainingSegmentImpactFade && 0 != iIndex) {
  445.                 for (int i = iIndex - 1; i >= 0; --i) {
  446.                     if (_aLSRM[i].impactFade (strManifestMeasure)) {
  447.                         iPriorImpactFadeIndex = i;
  448.                         break;
  449.                     }
  450.                 }
  451.             }

  452.             org.drip.numerical.differentiation.WengertJacobian wjDResponseDManifestMeasure = new
  453.                 org.drip.numerical.differentiation.WengertJacobian (1, iNumSegment);

  454.             for (int i = 0; i < iNumSegment; ++i) {
  455.                 double dblDResponseDManifestMeasurei = 0.;

  456.                 if (i == iIndex)
  457.                     dblDResponseDManifestMeasurei = _aLSRM[i].calcDResponseDManifest (strManifestMeasure,
  458.                         dblPredictorOrdinate, iOrder);
  459.                 else if (i == iIndex - 1)
  460.                     dblDResponseDManifestMeasurei = _aLSRM[i + 1].calcDResponseDPreceedingManifest
  461.                         (strManifestMeasure, dblPredictorOrdinate, iOrder);
  462.                 else if (!bContainingSegmentImpactFade && i >= iPriorImpactFadeIndex && i < iIndex - 1)
  463.                     dblDResponseDManifestMeasurei = _aLSRM[i].calcDResponseDManifest (strManifestMeasure,
  464.                         _aLSRM[i].right(), iOrder);

  465.                 if (!wjDResponseDManifestMeasure.accumulatePartialFirstDerivative (0, i,
  466.                     dblDResponseDManifestMeasurei))
  467.                     return null;
  468.             }

  469.             return wjDResponseDManifestMeasure;
  470.         } catch (java.lang.Exception e) {
  471.             // e.printStackTrace();
  472.         }

  473.         return null;
  474.     }

  475.     @Override public org.drip.spline.segment.Monotonocity monotoneType (
  476.         final double dblPredictorOrdinate)
  477.     {
  478.         int iIndex = -1;

  479.         try {
  480.             iIndex = containingIndex (dblPredictorOrdinate, true, true);
  481.         } catch (java.lang.Exception e) {
  482.             e.printStackTrace();

  483.             return null;
  484.         }

  485.         return _aLSRM[iIndex].monotoneType();
  486.     }

  487.     @Override public boolean isLocallyMonotone()
  488.         throws java.lang.Exception
  489.     {
  490.         int iNumSegment = _aLSRM.length;

  491.         for (int i = 0; i < iNumSegment; ++i) {
  492.             org.drip.spline.segment.Monotonocity mono = null;

  493.             try {
  494.                 mono = _aLSRM[i].monotoneType();
  495.             } catch (java.lang.Exception e) {
  496.                 e.printStackTrace();
  497.             }

  498.             if (null == mono || org.drip.spline.segment.Monotonocity.MONOTONIC != mono.type()) return false;
  499.         }

  500.         return true;
  501.     }

  502.     @Override public boolean isCoMonotone (
  503.         final double[] adblMeasuredResponse)
  504.         throws java.lang.Exception
  505.     {
  506.         int iNumSegment = _aLSRM.length;
  507.         int[] aiMonotoneType = new int[iNumSegment];
  508.         int[] aiNodeMiniMax = new int[iNumSegment + 1];

  509.         if (null == adblMeasuredResponse || adblMeasuredResponse.length != iNumSegment + 1)
  510.             throw new java.lang.Exception
  511.                 ("CalibratableMultiSegmentSequence::isCoMonotone => Data input inconsistent with the segment");

  512.         for (int i = 0; i < iNumSegment + 1; ++i) {
  513.             if (0 == i || iNumSegment == i)
  514.                 aiNodeMiniMax[i] = MONOTONE_PREDICTOR_ORDINATE_NODE;
  515.             else {
  516.                 if (adblMeasuredResponse[i - 1] < adblMeasuredResponse[i] && adblMeasuredResponse[i + 1] <
  517.                     adblMeasuredResponse[i])
  518.                     aiNodeMiniMax[i] = MAXIMA_PREDICTOR_ORDINATE_NODE;
  519.                 else if (adblMeasuredResponse[i - 1] > adblMeasuredResponse[i] && adblMeasuredResponse[i + 1]
  520.                     > adblMeasuredResponse[i])
  521.                     aiNodeMiniMax[i] = MINIMA_PREDICTOR_ORDINATE_NODE;
  522.                 else
  523.                     aiNodeMiniMax[i] = MONOTONE_PREDICTOR_ORDINATE_NODE;
  524.             }

  525.             if (i < iNumSegment) {
  526.                 org.drip.spline.segment.Monotonocity mono = _aLSRM[i].monotoneType();

  527.                 if (null != mono) aiMonotoneType[i] = mono.type();
  528.             }
  529.         }

  530.         for (int i = 1; i < iNumSegment; ++i) {
  531.             if (MAXIMA_PREDICTOR_ORDINATE_NODE == aiNodeMiniMax[i]) {
  532.                 if (org.drip.spline.segment.Monotonocity.MAXIMA != aiMonotoneType[i] &&
  533.                     org.drip.spline.segment.Monotonocity.MAXIMA != aiMonotoneType[i - 1])
  534.                     return false;
  535.             } else if (MINIMA_PREDICTOR_ORDINATE_NODE == aiNodeMiniMax[i]) {
  536.                 if (org.drip.spline.segment.Monotonocity.MINIMA != aiMonotoneType[i] &&
  537.                     org.drip.spline.segment.Monotonocity.MINIMA != aiMonotoneType[i - 1])
  538.                     return false;
  539.             }
  540.         }

  541.         return true;
  542.     }

  543.     @Override public boolean isKnot (
  544.         final double dblPredictorOrdinate)
  545.     {
  546.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblPredictorOrdinate)) return false;

  547.         int iNumSegment = _aLSRM.length;

  548.         for (int i = 0; i < iNumSegment; ++i) {
  549.             if (dblPredictorOrdinate == _aLSRM[i].left()) return false;
  550.         }

  551.         return dblPredictorOrdinate == _aLSRM[iNumSegment - 1].left();
  552.     }

  553.     @Override public double calcLeftEdgeDerivative (
  554.         final int iOrder)
  555.         throws java.lang.Exception
  556.     {
  557.         org.drip.spline.segment.LatentStateResponseModel lsrm = _aLSRM[0];

  558.         return lsrm.calcResponseValueDerivative (lsrm.left(), iOrder);
  559.     }

  560.     @Override public double calcRightEdgeDerivative (
  561.         final int iOrder)
  562.         throws java.lang.Exception
  563.     {
  564.         org.drip.spline.segment.LatentStateResponseModel lsrm = _aLSRM[_aLSRM.length - 1];

  565.         return lsrm.calcResponseValueDerivative (lsrm.right(), iOrder);
  566.     }

  567.     @Override public boolean resetNode (
  568.         final int iPredictorOrdinateIndex,
  569.         final double dblResponseReset)
  570.     {
  571.         if (0 == iPredictorOrdinateIndex || 1 == iPredictorOrdinateIndex || _aLSRM.length <
  572.             iPredictorOrdinateIndex || !org.drip.numerical.common.NumberUtil.IsValid (dblResponseReset))
  573.             return false;

  574.         return _aLSRM[iPredictorOrdinateIndex - 1].calibrate (_aLSRM[iPredictorOrdinateIndex - 2],
  575.             dblResponseReset, null);
  576.     }

  577.     @Override public boolean resetNode (
  578.         final int iPredictorOrdinateIndex,
  579.         final org.drip.spline.params.SegmentResponseValueConstraint srvcReset)
  580.     {
  581.         if (0 == iPredictorOrdinateIndex || 1 == iPredictorOrdinateIndex || _aLSRM.length <
  582.             iPredictorOrdinateIndex || null == srvcReset)
  583.             return false;

  584.         return _aLSRM[iPredictorOrdinateIndex - 1].calibrate (_aLSRM[iPredictorOrdinateIndex - 2], srvcReset,
  585.             null);
  586.     }

  587.     @Override public org.drip.function.definition.R1ToR1 toAU()
  588.     {
  589.         org.drip.function.definition.R1ToR1 au = new
  590.             org.drip.function.definition.R1ToR1 (null)
  591.         {
  592.             @Override public double evaluate (
  593.                 final double dblVariate)
  594.                 throws java.lang.Exception
  595.             {
  596.                 return responseValue (dblVariate);
  597.             }

  598.             @Override public double derivative (
  599.                 final double dblVariate,
  600.                 final int iOrder)
  601.                 throws java.lang.Exception
  602.             {
  603.                 return responseValueDerivative (dblVariate, iOrder);
  604.             }
  605.         };

  606.         return au;
  607.     }

  608.     @Override public boolean in (
  609.         final double dblPredictorOrdinate)
  610.         throws java.lang.Exception
  611.     {
  612.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblPredictorOrdinate))
  613.             throw new java.lang.Exception ("CalibratableMultiSegmentSequence::in => Invalid inputs");

  614.         return dblPredictorOrdinate >= getLeftPredictorOrdinateEdge() && dblPredictorOrdinate <=
  615.             getRightPredictorOrdinateEdge();
  616.     }

  617.     @Override public double getLeftPredictorOrdinateEdge()
  618.     {
  619.         return _aLSRM[0].left();
  620.     }

  621.     @Override public double getRightPredictorOrdinateEdge()
  622.     {
  623.         return _aLSRM[_aLSRM.length - 1].right();
  624.     }

  625.     @Override public int containingIndex (
  626.         final double dblPredictorOrdinate,
  627.         final boolean bIncludeLeft,
  628.         final boolean bIncludeRight)
  629.         throws java.lang.Exception
  630.     {
  631.         if (!in (dblPredictorOrdinate))
  632.             throw new java.lang.Exception
  633.                 ("CalibratableMultiSegmentSequence::containingIndex => Predictor Ordinate not in the Stretch Range");

  634.         int iNumSegment = _aLSRM.length;

  635.         for (int i = 0 ; i < iNumSegment; ++i) {
  636.             boolean bLeftValid = bIncludeLeft ? _aLSRM[i].left() <= dblPredictorOrdinate : _aLSRM[i].left() <
  637.                 dblPredictorOrdinate;

  638.             boolean bRightValid = bIncludeRight ? _aLSRM[i].right() >= dblPredictorOrdinate :
  639.                 _aLSRM[i].right() > dblPredictorOrdinate;

  640.             if (bLeftValid && bRightValid) return i;
  641.         }

  642.         throw new java.lang.Exception
  643.             ("CalibratableMultiSegmentSequence::containingIndex => Cannot locate Containing Index");
  644.     }

  645.     @Override public CalibratableMultiSegmentSequence clipLeft (
  646.         final java.lang.String strName,
  647.         final double dblPredictorOrdinate)
  648.     {
  649.         int iNumSegment = _aLSRM.length;
  650.         int iContainingPredictorOrdinateIndex = 0;

  651.         try {
  652.             iContainingPredictorOrdinateIndex = containingIndex (dblPredictorOrdinate, true, false);
  653.         } catch (java.lang.Exception e) {
  654.             e.printStackTrace();

  655.             return null;
  656.         }

  657.         int iNumClippedSegment = iNumSegment - iContainingPredictorOrdinateIndex;
  658.         org.drip.spline.segment.LatentStateResponseModel[] aCS = new
  659.             org.drip.spline.segment.LatentStateResponseModel[iNumClippedSegment];
  660.         org.drip.spline.params.SegmentCustomBuilderControl[] aSCBC = new
  661.             org.drip.spline.params.SegmentCustomBuilderControl[iNumClippedSegment];

  662.         for (int i = 0; i < iNumClippedSegment; ++i) {
  663.             if (null == (aCS[i] = 0 == i ?
  664.                 _aLSRM[iContainingPredictorOrdinateIndex].clipLeftOfPredictorOrdinate (dblPredictorOrdinate)
  665.                     : _aLSRM[i + iContainingPredictorOrdinateIndex]))
  666.                 return null;

  667.             aSCBC[i] = _aSCBC[i + iContainingPredictorOrdinateIndex];
  668.         }

  669.         try {
  670.             return new CalibratableMultiSegmentSequence (strName, aCS, aSCBC);
  671.         } catch (java.lang.Exception e) {
  672.             e.printStackTrace();
  673.         }

  674.         return null;
  675.     }

  676.     @Override public CalibratableMultiSegmentSequence clipRight (
  677.         final java.lang.String strName,
  678.         final double dblPredictorOrdinate)
  679.     {
  680.         int iContainingPredictorOrdinateIndex = 0;

  681.         try {
  682.             iContainingPredictorOrdinateIndex = containingIndex (dblPredictorOrdinate, false, true);
  683.         } catch (java.lang.Exception e) {
  684.             e.printStackTrace();

  685.             return null;
  686.         }

  687.         org.drip.spline.segment.LatentStateResponseModel[] aCS = new
  688.             org.drip.spline.segment.LatentStateResponseModel[iContainingPredictorOrdinateIndex + 1];
  689.         org.drip.spline.params.SegmentCustomBuilderControl[] aSCBC = new
  690.             org.drip.spline.params.SegmentCustomBuilderControl[iContainingPredictorOrdinateIndex + 1];

  691.         for (int i = 0; i <= iContainingPredictorOrdinateIndex; ++i) {
  692.             if (null == (aCS[i] = iContainingPredictorOrdinateIndex == i ?
  693.                 _aLSRM[iContainingPredictorOrdinateIndex].clipRightOfPredictorOrdinate (dblPredictorOrdinate)
  694.                     : _aLSRM[i]))
  695.                 return null;

  696.             aSCBC[i] = _aSCBC[i];
  697.         }

  698.         try {
  699.             return new CalibratableMultiSegmentSequence (strName, aCS, aSCBC);
  700.         } catch (java.lang.Exception e) {
  701.             e.printStackTrace();
  702.         }

  703.         return null;
  704.     }

  705.     @Override public double curvatureDPE()
  706.         throws java.lang.Exception
  707.     {
  708.         double dblDPE = 0.;

  709.         for (org.drip.spline.segment.LatentStateResponseModel lsrm : _aLSRM)
  710.             dblDPE += lsrm.curvatureDPE();

  711.         return dblDPE;
  712.     }

  713.     @Override public double lengthDPE()
  714.         throws java.lang.Exception
  715.     {
  716.         double dblDPE = 0.;

  717.         for (org.drip.spline.segment.LatentStateResponseModel lsrm : _aLSRM)
  718.             dblDPE += lsrm.lengthDPE();

  719.         return dblDPE;
  720.     }

  721.     @Override public double bestFitDPE (
  722.         final org.drip.spline.params.StretchBestFitResponse rbfr)
  723.         throws java.lang.Exception
  724.     {
  725.         if (null == rbfr) return 0.;

  726.         double dblDPE = 0.;

  727.         for (org.drip.spline.segment.LatentStateResponseModel lsrm : _aLSRM)
  728.             dblDPE += lsrm.bestFitDPE (rbfr.sizeToSegment (lsrm));

  729.         return dblDPE;
  730.     }

  731.     @Override public org.drip.state.representation.MergeSubStretchManager msm()
  732.     {
  733.         return null;
  734.     }

  735.     @Override public java.lang.String displayString()
  736.     {
  737.         java.lang.StringBuffer sb = new java.lang.StringBuffer();

  738.         for (int i = 0; i < _aLSRM.length; ++i)
  739.             sb.append (_aLSRM[i].displayString() + " \n");

  740.         return sb.toString();
  741.     }
  742. }