KnottedRegressionSplineEstimator.java

  1. package org.drip.sample.stretch;

  2. import org.drip.numerical.common.FormatUtil;
  3. import org.drip.spline.basis.*;
  4. import org.drip.spline.params.*;
  5. import org.drip.spline.stretch.*;

  6. /*
  7.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  8.  */

  9. /*!
  10.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  11.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  12.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  13.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2014 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2013 Lakshmi Krishnamurthy
  16.  *
  17.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  18.  *      libraries targeting analysts and developers
  19.  *      https://lakshmidrip.github.io/DRIP/
  20.  *  
  21.  *  DRIP is composed of four main libraries:
  22.  *  
  23.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  24.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  25.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  26.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  27.  *
  28.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  29.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  30.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  31.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  32.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  33.  *
  34.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  35.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  36.  *
  37.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  38.  *
  39.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  40.  *
  41.  *  Licensed under the Apache License, Version 2.0 (the "License");
  42.  *      you may not use this file except in compliance with the License.
  43.  *  
  44.  *  You may obtain a copy of the License at
  45.  *      http://www.apache.org/licenses/LICENSE-2.0
  46.  *  
  47.  *  Unless required by applicable law or agreed to in writing, software
  48.  *      distributed under the License is distributed on an "AS IS" BASIS,
  49.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  50.  *  
  51.  *  See the License for the specific language governing permissions and
  52.  *      limitations under the License.
  53.  */

  54. /**
  55.  * KnottedRegressionSplineEstimator shows the sample construction and usage of Knot-based Regression Splines.
  56.  *  It demonstrates construction of the segment's predictor ordinate/response value combination, and eventual
  57.  *  calibration.
  58.  *
  59.  * @author Lakshmi Krishnamurthy
  60.  */

  61. public class KnottedRegressionSplineEstimator {

  62.     /*
  63.      * Build Polynomial Segment Control Parameters
  64.      *
  65.      *  WARNING: Insufficient Error Checking, so use caution
  66.      */

  67.     private static final SegmentCustomBuilderControl PolynomialSegmentControlParams (
  68.         final int iNumBasis,
  69.         final SegmentInelasticDesignControl sdic)
  70.         throws Exception
  71.     {
  72.         return new SegmentCustomBuilderControl (
  73.             MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  74.             new PolynomialFunctionSetParams (iNumBasis),
  75.             sdic,
  76.             null,
  77.             null
  78.         );
  79.     }

  80.     /*
  81.      * Basis Spline Stretch Test Sample. Performs the following:
  82.      *  - Construct the Array of Segment Builder Parameters - one per segment.
  83.      *  - Construct a Stretch instance using the predictor ordinate array and the Segment Best Fit Response Values.
  84.      *  - Estimate, compute the segment-by-segment monotonicity and the Stretch Jacobian
  85.      *  - Compute the Segment Curvature Penalty Estimate.
  86.      *
  87.      *  WARNING: Insufficient Error Checking, so use caution
  88.      */

  89.     private static final void BasisSplineStretchTest (
  90.         final double[] adblX,
  91.         final SegmentCustomBuilderControl scbc,
  92.         final StretchBestFitResponse sbfr)
  93.         throws Exception
  94.     {
  95.         double dblX = 1.;
  96.         double dblXMax = 10.;

  97.         /*
  98.          * Array of Segment Builder Parameters - one per segment
  99.          */

  100.         SegmentCustomBuilderControl[] aSCBC = new SegmentCustomBuilderControl[adblX.length - 1];

  101.         for (int i = 0; i < adblX.length - 1; ++i)
  102.             aSCBC[i] = scbc;

  103.         /*
  104.          * Construct a Stretch instance using the predictor ordinate array and the Segment Best Fit Response Values
  105.          */

  106.         MultiSegmentSequence mss = MultiSegmentSequenceBuilder.CreateRegressionSplineEstimator (
  107.             "SPLINE_STRETCH",
  108.             adblX, // predictors
  109.             aSCBC, // Basis Segment Builder parameters
  110.             sbfr,
  111.             BoundarySettings.NaturalStandard(), // Boundary Condition - Natural
  112.             MultiSegmentSequence.CALIBRATE // Calibrate the Stretch predictors to the responses
  113.         );

  114.         /*
  115.          * Estimate, compute the segment-by-segment monotonicity and the Stretch Jacobian
  116.          */

  117.         while (dblX <= dblXMax) {
  118.             System.out.println ("Y[" + dblX + "] " + FormatUtil.FormatDouble (mss.responseValue (dblX), 1, 2, 1.) + " | " +
  119.                 mss.monotoneType (dblX));

  120.             System.out.println ("\t\tJacobian Y[" + dblX + "]=" + mss.jackDResponseDCalibrationInput (dblX, 1).displayString());

  121.             dblX += 1.;
  122.         }

  123.         /*
  124.          * Compute the Segment Curvature Penalty Estimate
  125.          */

  126.         System.out.println ("\tSPLINE_STRETCH DPE: " + mss.curvatureDPE());
  127.     }

  128.     /*
  129.      * Bring together to compose the Regression Spline Estimator Test. It is made up of the following steps:
  130.      *  - Set the Predictor Ordinate Knot Points.
  131.      *  - Construct a set of Predictor Ordinates, their Responses, and corresponding Weights to serve as
  132.      *      weighted closeness of fit.
  133.      *  - Construct the segment inelastic parameter that is C2 (iK = 2 sets it to C2), with 2nd order
  134.      *      roughness penalty derivative, and without constraint.
  135.      *  - Basis Spline Stretch Test Using the Segment Best Fit Response.
  136.      *
  137.      *  WARNING: Insufficient Error Checking, so use caution
  138.      */

  139.     private static final void RegressionSplineEstimatorTest()
  140.         throws Exception
  141.     {
  142.         /*
  143.          * Set the Knot Points
  144.          */

  145.         double[] adblX = new double[] { 1.00,  5.00, 10.00};

  146.         /*
  147.          * Construct a set of Predictor Ordinates, their Responses, and corresponding Weights to serve as
  148.          *  weighted closeness of fit.
  149.          */

  150.         StretchBestFitResponse sbfr = StretchBestFitResponse.Create (
  151.             new double[] { 2.28,  2.52,  2.73, 3.00,  5.50, 8.44,  8.76,  9.08,  9.80,  9.92},
  152.             new double[] {14.27, 12.36, 10.61, 9.25, -0.50, 7.92, 10.07, 12.23, 15.51, 16.36},
  153.             new double[] { 1.09,  0.82,  1.34, 1.10,  0.50, 0.79,  0.65,  0.49,  0.24,  0.21}
  154.         );

  155.         /*
  156.          * Construct the segment inelastic parameter that is C2 (iK = 2 sets it to C2), with 2nd order
  157.          *  roughness penalty derivative, and without constraint
  158.          */

  159.         int iK = 2;
  160.         int iRoughnessPenaltyDerivativeOrder = 2;

  161.         SegmentInelasticDesignControl sdic = SegmentInelasticDesignControl.Create (
  162.             iK,
  163.             iRoughnessPenaltyDerivativeOrder
  164.         );

  165.         int iPolyNumBasis = 4;

  166.         /*
  167.          * Basis Spline Stretch Test Using the Segment Best Fit Response
  168.          */

  169.         BasisSplineStretchTest (
  170.             adblX,
  171.             PolynomialSegmentControlParams (
  172.                 iPolyNumBasis,
  173.                 sdic
  174.             ),
  175.             sbfr
  176.         );
  177.     }

  178.     public static final void main (
  179.         final String[] astrArgs)
  180.         throws Exception
  181.     {
  182.         RegressionSplineEstimatorTest();
  183.     }
  184. }