CkSegmentSequenceBuilder.java

  1.    
  2. package org.drip.spline.stretch;

  3. /*
  4.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  5.  */

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

  81. /**
  82.  * <i>CkSegmentSequenceBuilder</i> implements the SegmentSequenceBuilder interface to customize segment
  83.  * sequence construction. Customization is applied at several levels:
  84.  *
  85.  * <br><br>
  86.  *  <ul>
  87.  *      <li>
  88.  *          Segment Calibration Boundary Setting/Segment Best Fit Response Settings
  89.  *      </li>
  90.  *      <li>
  91.  *          Segment Response Value Constraints for the starting and the subsequent Segments
  92.  *      </li>
  93.  *  </ul>
  94.  *
  95.  * <br><br>
  96.  *  <ul>
  97.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  98.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/SplineBuilderLibrary.md">Spline Builder Library</a></li>
  99.  *      <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>
  100.  *      <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>
  101.  *  </ul>
  102.  * <br><br>
  103.  *
  104.  * @author Lakshmi Krishnamurthy
  105.  */

  106. public class CkSegmentSequenceBuilder implements org.drip.spline.stretch.SegmentSequenceBuilder {
  107.     private org.drip.spline.stretch.BoundarySettings _bs = null;
  108.     private org.drip.spline.stretch.MultiSegmentSequence _mss = null;
  109.     private org.drip.spline.params.StretchBestFitResponse _rbfr = null;
  110.     private org.drip.spline.params.SegmentResponseValueConstraint[] _aSRVC = null;
  111.     private org.drip.spline.params.SegmentResponseValueConstraint _srvcLeading = null;

  112.     /**
  113.      * CkSegmentSequenceBuilder constructor
  114.      *
  115.      * @param srvcLeading Leading Segment Response Value Constraint
  116.      * @param aSRVC Array of Segment Response Value Constraints
  117.      * @param rbfr Sequence Best Fit Weighted Response
  118.      * @param bs The Calibration Boundary Condition
  119.      *
  120.      * @throws java.lang.Exception Thrown if the inputs are invalid
  121.      */

  122.     public CkSegmentSequenceBuilder (
  123.         final org.drip.spline.params.SegmentResponseValueConstraint srvcLeading,
  124.         final org.drip.spline.params.SegmentResponseValueConstraint[] aSRVC,
  125.         final org.drip.spline.params.StretchBestFitResponse rbfr,
  126.         final org.drip.spline.stretch.BoundarySettings bs)
  127.         throws java.lang.Exception
  128.     {
  129.         _rbfr = rbfr;
  130.         _aSRVC = aSRVC;
  131.         _srvcLeading = srvcLeading;

  132.         if (null == _srvcLeading && (null == _aSRVC || 0 == _aSRVC.length) && null == _rbfr)
  133.             throw new java.lang.Exception ("CkSegmentSequenceBuilder ctr: Invalid inputs!");

  134.         if (null == (_bs = bs))
  135.             throw new java.lang.Exception ("CkSegmentSequenceBuilder ctr: Invalid inputs!");
  136.     }

  137.     @Override public boolean setStretch (
  138.         final org.drip.spline.stretch.MultiSegmentSequence mss)
  139.     {
  140.         if (null == mss) return false;

  141.         _mss = mss;
  142.         return true;
  143.     }

  144.     @Override public org.drip.spline.stretch.BoundarySettings getCalibrationBoundaryCondition()
  145.     {
  146.         return _bs;
  147.     }

  148.     @Override public boolean calibStartingSegment (
  149.         final double dblLeftSlope)
  150.     {
  151.         if (null == _mss) return false;

  152.         org.drip.spline.segment.LatentStateResponseModel[] aCS = _mss.segments();

  153.         return null != aCS && 0 < aCS.length && aCS[0].calibrate (_srvcLeading, dblLeftSlope, null == _aSRVC
  154.             ? null : _aSRVC[0], null == _rbfr ? null : _rbfr.sizeToSegment (aCS[0]));
  155.     }

  156.     @Override public boolean calibSegmentSequence (
  157.         final int iStartingSegment)
  158.     {
  159.         if (null == _mss) return false;

  160.         org.drip.spline.segment.LatentStateResponseModel[] aCS = _mss.segments();

  161.         int iNumSegment = aCS.length;

  162.         for (int iSegment = iStartingSegment; iSegment < iNumSegment; ++iSegment) {
  163.             if (!aCS[iSegment].calibrate (0 == iSegment ? null : aCS[iSegment - 1], null == _aSRVC ? null :
  164.                 _aSRVC[iSegment], null == _rbfr ? null : _rbfr.sizeToSegment (aCS[iSegment])))
  165.                 return false;
  166.         }

  167.         return true;
  168.     }

  169.     @Override public boolean manifestMeasureSensitivity (
  170.         final double dblLeftSlopeSensitivity)
  171.     {
  172.         return true;
  173.     }
  174. }