LocalControlBasisSplineRegressor.java

  1. package org.drip.regression.spline;

  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.  *
  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>LocalControlBasisSplineRegressor</i> implements the local control basis spline regressor for the given
  83.  * basis spline. As part of the regression run, it executes the following:
  84.  *
  85.  * <br><br>
  86.  *  <ul>
  87.  *      <li>
  88.  *          Calibrate and compute the left and the right Jacobian.
  89.  *      </li>
  90.  *      <li>
  91.  *          Insert the Local Control Hermite, Cardinal, and Catmull-Rom knots.
  92.  *      </li>
  93.  *      <li>
  94.  *          Run Regressor for the C1 Local Control C1 Slope Insertion Bessel/Hermite Spline.
  95.  *      </li>
  96.  *      <li>
  97.  *          Compute an intermediate value Jacobian.
  98.  *      </li>
  99.  *  </ul>
  100.  *
  101.  * <br><br>
  102.  *  <ul>
  103.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  104.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  105.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/regression/README.md">Regression Engine Core and the Unit Regressors</a></li>
  106.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/regression/spline/README.md">Custom Basis Spline Regression Engine</a></li>
  107.  *  </ul>
  108.  * <br><br>
  109.  *
  110.  * @author Lakshmi Krishnamurthy
  111.  */

  112. public class LocalControlBasisSplineRegressor extends org.drip.regression.core.UnitRegressionExecutor {
  113.     private org.drip.spline.stretch.MultiSegmentSequence _mss = null;
  114.     private org.drip.spline.stretch.MultiSegmentSequence _mssBesselHermite = null;
  115.     private org.drip.spline.stretch.MultiSegmentSequence _mssHermiteInsert = null;
  116.     private org.drip.spline.stretch.MultiSegmentSequence _mssCardinalInsert = null;
  117.     private org.drip.spline.stretch.MultiSegmentSequence _mssCatmullRomInsert = null;

  118.     private final boolean DumpRNVD (
  119.         final java.lang.String strStretchName,
  120.         final org.drip.spline.stretch.MultiSegmentSequence mss,
  121.         final org.drip.regression.core.RegressionRunDetail rrd)
  122.     {
  123.         double dblX = 0.;
  124.         double dblXMax = 4.;

  125.         while (dblX <= dblXMax) {
  126.             try {
  127.                 if (!rrd.set (getName() + "_" + strStretchName + "_" + dblX,
  128.                     org.drip.numerical.common.FormatUtil.FormatDouble (mss.responseValue (dblX), 1, 2, 1.) +
  129.                         " | " + mss.monotoneType (dblX)))
  130.                     return false;
  131.             } catch (java.lang.Exception e) {
  132.                 e.printStackTrace();

  133.                 return false;
  134.             }

  135.             if (!rrd.set (getName() + "_" + strStretchName + "_" + dblX + "_Jack",
  136.                 mss.jackDResponseDCalibrationInput (dblX, 1).displayString()))
  137.                 return false;

  138.             dblX += 0.5;
  139.         }

  140.         return true;
  141.     }

  142.     /**
  143.      * LocalControlBasisSplineRegressor constructor
  144.      *
  145.      * @param strName Regressor Name
  146.      * @param strScenarioName Regression Scenario Name
  147.      * @param strBasisSpline Basis Spline
  148.      * @param fsbp Basis Set Builder Parameters
  149.      * @param iCk Continuity Ck
  150.      *
  151.      * @throws java.lang.Exception Thrown if inputs are invalid
  152.      */

  153.     public LocalControlBasisSplineRegressor (
  154.         final java.lang.String strName,
  155.         final java.lang.String strScenarioName,
  156.         final java.lang.String strBasisSpline,
  157.         final org.drip.spline.basis.FunctionSetBuilderParams fsbp,
  158.         final int iCk)
  159.         throws java.lang.Exception
  160.     {
  161.         super (strName, strScenarioName);

  162.         double[] adblX = new double[] {0.00, 1.00,  2.00,  3.00,  4.00};
  163.         int iNumSegment = adblX.length - 1;
  164.         org.drip.spline.params.SegmentCustomBuilderControl[] aSCBC = new
  165.             org.drip.spline.params.SegmentCustomBuilderControl[iNumSegment];

  166.         for (int i = 0; i < iNumSegment; ++i)
  167.             aSCBC[i] = new org.drip.spline.params.SegmentCustomBuilderControl (strBasisSpline, fsbp,
  168.                 org.drip.spline.params.SegmentInelasticDesignControl.Create (iCk, 1), new
  169.                     org.drip.spline.params.ResponseScalingShapeControl (true, new
  170.                         org.drip.function.r1tor1.QuadraticRationalShapeControl (1.)), null);

  171.         if (null == (_mss =
  172.             org.drip.spline.stretch.MultiSegmentSequenceBuilder.CreateUncalibratedStretchEstimator
  173.                 ("SPLINE_STRETCH", adblX, aSCBC)))
  174.             throw new java.lang.Exception
  175.                 ("LocalControlBasisSplineRegressor ctr: Cannot Construct Stretch!");
  176.     }

  177.     @Override public boolean preRegression()
  178.     {
  179.         double[] adblY = new double[] {1.00, 4.00, 15.00, 40.00, 85.00};
  180.         double[] adblDYDX = new double[] {1.00, 6.00, 17.00, 34.00, 57.00};

  181.         org.drip.spline.params.SegmentCustomBuilderControl scbc = null;
  182.         org.drip.spline.params.SegmentPredictorResponseDerivative[] aSPRDLeft = new
  183.             org.drip.spline.params.SegmentPredictorResponseDerivative[adblY.length - 1];
  184.         org.drip.spline.params.SegmentPredictorResponseDerivative[] aSPRDRight = new
  185.             org.drip.spline.params.SegmentPredictorResponseDerivative[adblY.length - 1];

  186.         for (int i = 0; i < adblY.length - 1; ++i) {
  187.             try {
  188.                 aSPRDLeft[i] = new org.drip.spline.params.SegmentPredictorResponseDerivative (adblY[i], new
  189.                     double[] {adblDYDX[i]});

  190.                 aSPRDRight[i] = new org.drip.spline.params.SegmentPredictorResponseDerivative (adblY[i + 1],
  191.                     new double[] {adblDYDX[i + 1]});
  192.             } catch (java.lang.Exception e) {
  193.                 e.printStackTrace();

  194.                 return false;
  195.             }
  196.         }

  197.         try {
  198.             scbc = new org.drip.spline.params.SegmentCustomBuilderControl
  199.                 (org.drip.spline.stretch.MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL, new
  200.                     org.drip.spline.basis.PolynomialFunctionSetParams (4),
  201.                         org.drip.spline.params.SegmentInelasticDesignControl.Create (2, 2), new
  202.                             org.drip.spline.params.ResponseScalingShapeControl (true, new
  203.                                 org.drip.function.r1tor1.QuadraticRationalShapeControl (1.)), null);
  204.         } catch (java.lang.Exception e) {
  205.             e.printStackTrace();

  206.             return false;
  207.         }

  208.         org.drip.spline.params.SegmentCustomBuilderControl[] aSCBC = new
  209.             org.drip.spline.params.SegmentCustomBuilderControl[adblY.length - 1];

  210.         for (int i = 0; i < adblY.length - 1; ++i)
  211.             aSCBC[i] = scbc;

  212.         if (null == (_mssBesselHermite =
  213.             org.drip.spline.pchip.LocalControlStretchBuilder.CreateBesselCubicSplineStretch
  214.                 ("BESSEL_STRETCH", new double[] {0.00, 1.00,  2.00,  3.00,  4.00}, adblY, aSCBC, null,
  215.                     org.drip.spline.stretch.MultiSegmentSequence.CALIBRATE, true, true)))
  216.             return false;

  217.         return _mss.setupHermite (aSPRDLeft, aSPRDRight, null, null,
  218.             org.drip.spline.stretch.MultiSegmentSequence.CALIBRATE_JACOBIAN);
  219.     }

  220.     @Override public boolean execRegression()
  221.     {
  222.         try {
  223.             if (null == (_mssHermiteInsert = org.drip.spline.stretch.MultiSegmentSequenceModifier.InsertKnot
  224.                 (_mss, 2.5, new org.drip.spline.params.SegmentPredictorResponseDerivative (27.5, new double[]
  225.                     {25.5}), new org.drip.spline.params.SegmentPredictorResponseDerivative (27.5, new
  226.                         double[] {25.5}))))
  227.                 return false;
  228.         } catch (java.lang.Exception e) {
  229.             e.printStackTrace();

  230.             return false;
  231.         }

  232.         if (null == (_mssCardinalInsert =
  233.             org.drip.spline.stretch.MultiSegmentSequenceModifier.InsertCardinalKnot (_mss, 2.5, 0.)))
  234.             return false;

  235.         return null != (_mssCatmullRomInsert =
  236.             org.drip.spline.stretch.MultiSegmentSequenceModifier.InsertCatmullRomKnot (_mss, 2.5));
  237.     }

  238.     @Override public boolean postRegression (
  239.         final org.drip.regression.core.RegressionRunDetail rrd)
  240.     {
  241.         return DumpRNVD ("LOCAL_NO_KNOT", _mss, rrd) && DumpRNVD ("LOCAL_HERMITE_KNOT", _mssHermiteInsert,
  242.             rrd) && DumpRNVD ("LOCAL_CARDINAL_KNOT", _mssCardinalInsert, rrd) && DumpRNVD
  243.                 ("LOCAL_CATMULL_ROM_KNOT", _mssCatmullRomInsert, rrd) && DumpRNVD ("LOCAL_C1_BESSEL_HERMITE",
  244.                     _mssBesselHermite, rrd);
  245.     }
  246. }