PolynomialBasisSpline.java

  1. package org.drip.sample.spline;

  2. import org.drip.function.r1tor1.*;
  3. import org.drip.numerical.differentiation.WengertJacobian;
  4. import org.drip.spline.basis.*;
  5. import org.drip.spline.params.*;
  6. import org.drip.spline.segment.*;

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

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

  55. /**
  56.  * PolynomialBasisSpline implements Samples for the Construction and the usage of polynomial (both regular
  57.  *  and Hermite) basis spline functions. It demonstrates the following:
  58.  *  - Control the polynomial segment using the rational shape controller, the appropriate Ck, and the basis
  59.  *      function.
  60.  *  - Demonstrate the variational shape optimization behavior.
  61.  *  - Estimate the node value and the node value Jacobian with the segment, as well as at the boundaries.
  62.  *  - Calculate the segment monotonicity and the curvature penalty.
  63.  *
  64.  * @author Lakshmi Krishnamurthy
  65.  */

  66. public class PolynomialBasisSpline {

  67.     /*
  68.      * This sample demonstrates the following:
  69.      *
  70.      *  - Construction of two segments, 1 and 2.
  71.      *  - Calibration of the segments to the left and the right node values
  72.      *  - Extraction of the segment Jacobians and segment monotonicity
  73.      *  - Estimate point value and the Jacobian, monotonicity, and curvature penalty
  74.      *
  75.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  76.      */

  77.     private static final void TestPolynomialSpline (
  78.         final int iNumBasis,
  79.         final int iCk,
  80.         final int iRoughnessPenaltyDerivativeOrder,
  81.         final ResponseScalingShapeControl rssc)
  82.         throws Exception
  83.     {
  84.         System.out.println (" ------------------------------ \n     POLYNOMIAL n = " + iNumBasis +
  85.             "; Ck = " + iCk + "\n ------------------------------ \n");

  86.         /*
  87.          * Construct the segment inelastic parameter that is C2 (iCk = 2 sets it to C2), without constraint
  88.          */

  89.         SegmentInelasticDesignControl sdic = SegmentInelasticDesignControl.Create (
  90.             iCk,
  91.             iRoughnessPenaltyDerivativeOrder
  92.         );

  93.         /*
  94.          * Create the basis parameter set from the number of basis functions, and construct the basis
  95.          */

  96.         PolynomialFunctionSetParams pfsp = new PolynomialFunctionSetParams (iNumBasis);

  97.         FunctionSet fs = FunctionSetBuilder.PolynomialBasisSet (pfsp);

  98.         /*
  99.          * Construct the left and the right segments
  100.          */

  101.         LatentStateResponseModel ecs1 = LatentStateResponseModel.Create (
  102.             1.0,
  103.             1.5,
  104.             fs,
  105.             rssc,
  106.             sdic
  107.         );

  108.         LatentStateResponseModel ecs2 = LatentStateResponseModel.Create (
  109.             1.5,
  110.             2.0,
  111.             fs,
  112.             rssc,
  113.             sdic
  114.         );

  115.         /*
  116.          * Calibrate the left segment using the node values, and compute the segment Jacobian, monotonicity, and curvature penalty
  117.          */

  118.         WengertJacobian wj1 = ecs1.jackDCoeffDEdgeParams (
  119.             25.,
  120.             0.,
  121.             20.25,
  122.             null
  123.         );

  124.         System.out.println ("\tY[" + 1.0 + "]: " + ecs1.responseValue (1.));

  125.         System.out.println ("\tY[" + 1.5 + "]: " + ecs1.responseValue (1.5));

  126.         System.out.println ("Segment 1 Jacobian: " + wj1.displayString());

  127.         System.out.println ("Segment 1 Head: " + ecs1.jackDCoeffDEdgeInputs().displayString());

  128.         System.out.println ("Segment 1 Monotone Type: " + ecs1.monotoneType());

  129.         System.out.println ("Segment 1 DPE: " + ecs1.curvatureDPE());

  130.         /*
  131.          * Calibrate the right segment using the node values, and compute the segment Jacobian, monotonicity, and curvature penalty
  132.          */

  133.         WengertJacobian wj2 = ecs2.jackDCoeffDEdgeParams (
  134.             ecs1,
  135.             "Default",
  136.             16.,
  137.             null,
  138.             Double.NaN,
  139.             null
  140.         );

  141.         System.out.println ("\tY[" + 1.5 + "]: " + ecs2.responseValue (1.5));

  142.         System.out.println ("\tY[" + 2. + "]: " + ecs2.responseValue (2.));

  143.         System.out.println ("Segment 2 Jacobian: " + wj2.displayString());

  144.         System.out.println ("Segment 2 Regular Jacobian: " + ecs2.jackDCoeffDEdgeInputs().displayString());

  145.         System.out.println ("Segment 2 Monotone Type: " + ecs2.monotoneType());

  146.         System.out.println ("Segment 2 DPE: " + ecs2.curvatureDPE());

  147.         /*
  148.          * Re-calibrate Segment #2 with a new Response Value
  149.          */

  150.         ecs2.calibrate (
  151.             ecs1,
  152.             14.,
  153.             null
  154.         );

  155.         /*
  156.          * Estimate the segment value at the given variate, and compute the corresponding Jacobian, and curvature penalty
  157.          */

  158.         double dblX = 2.0;

  159.         System.out.println ("\t\tValue[" + dblX + "]: " + ecs2.responseValue (dblX));

  160.         System.out.println ("\t\tValue Jacobian[" + dblX + "]: " + ecs2.jackDResponseDEdgeInput (dblX, 1).displayString());

  161.         System.out.println ("\t\tSegment 2 DPE: " + ecs2.curvatureDPE());
  162.     }

  163.     /*
  164.      * This sample demonstrates the following specifically for the Ck Hermite Splines, which are calibrated
  165.      *  using left and right node values, along with their derivatives:
  166.      *
  167.      *  - Construction of two segments, 1 and 2.
  168.      *  - Calibration of the segments to the left and the right node values
  169.      *  - Extraction of the segment Jacobians and segment monotonicity
  170.      *  - Estimate point value and the Jacobian, monotonicity, and curvature penalty
  171.      *
  172.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  173.      */

  174.     private static final void TestC1HermiteSpline (
  175.         final int iNumBasis,
  176.         final int iCk,
  177.         final int iRoughnessPenaltyDerivativeOrder,
  178.         final ResponseScalingShapeControl rssc)
  179.         throws Exception
  180.     {
  181.         System.out.println (" ------------------------------ \n     HERMITE POLYNOMIAL n = " + iNumBasis +
  182.             "; Ck = " + iCk + "\n ------------------------------ \n");

  183.         /*
  184.          * Construct the segment inelastic parameter that is C2 (iCk = 2 sets it to C2), without constraint
  185.          */

  186.         SegmentInelasticDesignControl sdic = SegmentInelasticDesignControl.Create (
  187.             iCk,
  188.             iRoughnessPenaltyDerivativeOrder
  189.         );

  190.         /*
  191.          * Create the basis parameter set from the number of basis functions, and construct the basis
  192.          */

  193.         PolynomialFunctionSetParams pfsp = new PolynomialFunctionSetParams (iNumBasis);

  194.         FunctionSet fs = FunctionSetBuilder.PolynomialBasisSet (pfsp);

  195.         /*
  196.          * Construct the left and the right segments
  197.          */

  198.         LatentStateResponseModel ecs1 = LatentStateResponseModel.Create (
  199.             0.0,
  200.             1.0,
  201.             fs,
  202.             rssc,
  203.             sdic
  204.         );

  205.         LatentStateResponseModel ecs2 = LatentStateResponseModel.Create (
  206.             1.0,
  207.             2.0,
  208.             fs,
  209.             rssc,
  210.             sdic
  211.         );

  212.         /*
  213.          * Calibrate the left segment using the node values, and compute the segment Jacobian, monotonicity, and curvature penalty
  214.          */

  215.         ecs1.calibrateState (
  216.             new SegmentStateCalibrationInputs (
  217.                 new double[] {0., 1.}, // Segment Calibration Nodes
  218.                 new double[] {1., 4.}, // Segment Calibration Values
  219.                 new double[] {1.}, // Segment Left Derivative
  220.                 new double[] {6.}, // Segment Left Derivative
  221.                 null,
  222.                 null // Segment Constraint AND Fitness Penalty Response
  223.             )
  224.         );

  225.         System.out.println ("\tY[" + 0.0 + "]: " + ecs1.responseValue (0.0));

  226.         System.out.println ("\tY[" + 1.0 + "]: " + ecs1.responseValue (1.0));

  227.         System.out.println ("Segment 1 Head: " + ecs1.jackDCoeffDEdgeInputs().displayString());

  228.         System.out.println ("Segment 1 Monotone Type: " + ecs1.monotoneType());

  229.         System.out.println ("Segment 1 DPE: " + ecs1.curvatureDPE());

  230.         /*
  231.          * Calibrate the right segment using the node values, and compute the segment Jacobian, monotonicity, and curvature penalty
  232.          */

  233.         ecs2.calibrateState (
  234.             new SegmentStateCalibrationInputs (
  235.                 new double[] {1., 2.}, // Segment Calibration Nodes
  236.                 new double[] {4., 15.}, // Segment Calibration Values
  237.                 new double[] {6.}, // Segment Left Derivative
  238.                 new double[] {17.}, // Segment Left Derivative
  239.                 null, // Segment Constraint
  240.                 null // Fitness Penalty Response
  241.             )
  242.         );

  243.         System.out.println ("\tY[" + 1.0 + "]: " + ecs2.responseValue (1.0));

  244.         System.out.println ("\tY[" + 2.0 + "]: " + ecs2.responseValue (2.0));

  245.         System.out.println ("Segment 2 Regular Jacobian: " + ecs2.jackDCoeffDEdgeInputs().displayString());

  246.         System.out.println ("Segment 2 Monotone Type: " + ecs2.monotoneType());

  247.         System.out.println ("Segment 2 DPE: " + ecs2.curvatureDPE());

  248.         /*
  249.          * Re-calibrate Segment #2 with a new Response Value
  250.          */

  251.         ecs2.calibrate (
  252.             ecs1,
  253.             14.,
  254.             null
  255.         );

  256.         /*
  257.          * Estimate the segment value at the given variate, and compute the corresponding Jacobian, monotonicity, and curvature penalty
  258.          */

  259.         double dblX = 2.0;

  260.         System.out.println ("\t\tValue[" + dblX + "]: " + ecs2.responseValue (dblX));

  261.         System.out.println ("\t\tValue Jacobian[" + dblX + "]: " + ecs2.jackDResponseDEdgeInput (dblX, 1).displayString());

  262.         System.out.println ("\t\tSegment 2 DPE: " + ecs2.curvatureDPE());
  263.     }

  264.     /*
  265.      * This sample illustrates the construction and usage for polynomial basis splines. It shows the
  266.      *  following:
  267.      *  - Construct a rational shape controller with the specified shape controller tension.
  268.      *  - Set the Roughness Penalty to 2nd order Roughness Penalty Derivative Order.
  269.      *  - Test the polynomial spline across different polynomial degrees and Ck's.
  270.      *  - Test the C1 Hermite spline.
  271.      *
  272.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  273.      */

  274.     private static final void PolynomialBasisSplineSample()
  275.         throws Exception
  276.     {
  277.         /*
  278.          * Construct a rational shape controller with the shape controller tension of 1.
  279.          */

  280.         double dblShapeControllerTension = 1.;

  281.         ResponseScalingShapeControl rssc = new ResponseScalingShapeControl (
  282.             true,
  283.             new QuadraticRationalShapeControl (dblShapeControllerTension)
  284.         );

  285.         /*
  286.          * Set to 2nd order Roughness Penalty Derivative Order.
  287.          */

  288.         int iRoughnessPenaltyDerivativeOrder = 2;

  289.         /*
  290.          * Test the polynomial spline across different polynomial degrees and Ck's
  291.          */

  292.         TestPolynomialSpline (2, 0, iRoughnessPenaltyDerivativeOrder, rssc);

  293.         TestPolynomialSpline (3, 0, iRoughnessPenaltyDerivativeOrder, rssc);

  294.         TestPolynomialSpline (3, 1, iRoughnessPenaltyDerivativeOrder, rssc);

  295.         TestPolynomialSpline (4, 0, iRoughnessPenaltyDerivativeOrder, rssc);

  296.         TestPolynomialSpline (4, 1, iRoughnessPenaltyDerivativeOrder, rssc);

  297.         TestPolynomialSpline (4, 2, iRoughnessPenaltyDerivativeOrder, rssc);

  298.         TestPolynomialSpline (5, 0, iRoughnessPenaltyDerivativeOrder, rssc);

  299.         TestPolynomialSpline (5, 1, iRoughnessPenaltyDerivativeOrder, rssc);

  300.         TestPolynomialSpline (5, 2, iRoughnessPenaltyDerivativeOrder, rssc);

  301.         TestPolynomialSpline (5, 3, iRoughnessPenaltyDerivativeOrder, rssc);

  302.         TestPolynomialSpline (6, 0, iRoughnessPenaltyDerivativeOrder, rssc);

  303.         TestPolynomialSpline (6, 1, iRoughnessPenaltyDerivativeOrder, rssc);

  304.         TestPolynomialSpline (6, 2, iRoughnessPenaltyDerivativeOrder, rssc);

  305.         TestPolynomialSpline (6, 3, iRoughnessPenaltyDerivativeOrder, rssc);

  306.         TestPolynomialSpline (6, 4, iRoughnessPenaltyDerivativeOrder, rssc);

  307.         TestPolynomialSpline (7, 0, iRoughnessPenaltyDerivativeOrder, rssc);

  308.         TestPolynomialSpline (7, 1, iRoughnessPenaltyDerivativeOrder, rssc);

  309.         TestPolynomialSpline (7, 2, iRoughnessPenaltyDerivativeOrder, rssc);

  310.         TestPolynomialSpline (7, 3, iRoughnessPenaltyDerivativeOrder, rssc);

  311.         TestPolynomialSpline (7, 4, iRoughnessPenaltyDerivativeOrder, rssc);

  312.         TestPolynomialSpline (7, 5, iRoughnessPenaltyDerivativeOrder, rssc);

  313.         /*
  314.          * Test the C1 Hermite spline
  315.          */

  316.         System.out.println (" -------------------- \n Ck HERMITE \n -------------------- \n");

  317.         TestC1HermiteSpline (4, 1, iRoughnessPenaltyDerivativeOrder, rssc);
  318.     }

  319.     public static final void main (
  320.         final String[] astrArgs)
  321.         throws Exception
  322.     {
  323.         PolynomialBasisSplineSample();
  324.     }
  325. }