BasisTensionSplineSet.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. import org.drip.spline.tension.KochLycheKvasovFamily;

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

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

  56. /**
  57.  * BasisTensionSplineSet implements Samples for the Construction and the usage of various basis spline
  58.  *  functions. It demonstrates the following:
  59.  *  - Construction of Kocke-Lyche-Kvasov tension spline segment control parameters - using hyperbolic,
  60.  *      exponential, rational linear, and rational quadratic primitives.
  61.  *  - Control the segment using the rational shape controller, and the appropriate Ck.
  62.  *  - Estimate the node value and the node value Jacobian with the segment, as well as at the boundaries.
  63.  *  - Calculate the segment monotonicity.

  64.  * @author Lakshmi Krishnamurthy
  65.  */

  66. public class BasisTensionSplineSet {

  67.     /*
  68.      * Sample demonstrating the creation of the KLK Hyperbolic tension basis spline set
  69.      *
  70.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  71.      */

  72.     private static final FunctionSet KLKHyperbolicTensionSpline()
  73.         throws Exception
  74.     {
  75.         double dblTension = .01;

  76.         /*
  77.          * Create the basis parameter set from the segment tension parameter, and construct the basis
  78.          */

  79.         ExponentialTensionSetParams etbsbp = new ExponentialTensionSetParams (dblTension);

  80.         return KochLycheKvasovFamily.FromHyperbolicPrimitive (etbsbp);
  81.     }

  82.     /*
  83.      * Sample demonstrating the creation of the KLK Rational Linear tension basis spline set
  84.      *
  85.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  86.      */

  87.     private static final FunctionSet KLKRationalLinearTensionSpline()
  88.         throws Exception
  89.     {
  90.         double dblTension = 1.;

  91.         /*
  92.          * Create the basis parameter set from the segment tension parameter, and construct the basis
  93.          */

  94.         ExponentialTensionSetParams etbsbp = new ExponentialTensionSetParams (dblTension);

  95.         return KochLycheKvasovFamily.FromRationalLinearPrimitive (etbsbp);
  96.     }

  97.     /*
  98.      * Sample demonstrating the creation of the KLK Rational Quadratic tension basis spline set
  99.      *
  100.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  101.      */

  102.     private static final FunctionSet KLKRationalQuadraticTensionSpline()
  103.         throws Exception
  104.     {
  105.         double dblTension = 1.;

  106.         /*
  107.          * Create the basis parameter set from the segment tension parameter, and construct the basis
  108.          */

  109.         ExponentialTensionSetParams etbsbp = new ExponentialTensionSetParams (dblTension);

  110.         return KochLycheKvasovFamily.FromRationalQuadraticPrimitive (etbsbp);
  111.     }

  112.     /*
  113.      * Sample demonstrating the creation of the KLK Exponential tension basis spline set
  114.      *
  115.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  116.      */

  117.     private static final FunctionSet KLKExponentialTensionSpline()
  118.         throws Exception
  119.     {
  120.         double dblTension = 1.;

  121.         /*
  122.          * Create the basis parameter set from the segment tension parameter, and construct the basis
  123.          */

  124.         ExponentialTensionSetParams etbsbp = new ExponentialTensionSetParams (dblTension);

  125.         return KochLycheKvasovFamily.FromExponentialPrimitive (etbsbp);
  126.     }

  127.     /*
  128.      * This sample demonstrates the following:
  129.      *
  130.      *  - Construction of two segments, 1 and 2.
  131.      *  - Calibration of the segments to the left and the right node values
  132.      *  - Extraction of the segment Jacobians and segment monotonicity
  133.      *  - Estimate point value and the Jacobian
  134.      *  - Estimate the curvature penalty
  135.      *
  136.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  137.      */

  138.     private static final void TestSpline (
  139.         final FunctionSet fs,
  140.         final ResponseScalingShapeControl rssc,
  141.         final SegmentInelasticDesignControl segParams)
  142.         throws Exception
  143.     {
  144.         /*
  145.          * Construct the left and the right segments
  146.          */

  147.         LatentStateResponseModel seg1 = LatentStateResponseModel.Create (
  148.             1.0,
  149.             1.5,
  150.             fs,
  151.             rssc,
  152.             segParams
  153.         );

  154.         LatentStateResponseModel seg2 = LatentStateResponseModel.Create (
  155.             1.5,
  156.             2.0,
  157.             fs,
  158.             rssc,
  159.             segParams
  160.         );

  161.         /*
  162.          * Calibrate the left segment using the node values, and compute the segment Jacobian, the monotonicity, and the curvature penalty
  163.          */

  164.         WengertJacobian wj1 = seg1.jackDCoeffDEdgeParams (
  165.             25.,
  166.             0.,
  167.             20.25,
  168.             null
  169.         );

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

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

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

  173.         System.out.println ("Segment 1 Head: " + seg1.jackDCoeffDEdgeInputs().displayString());

  174.         System.out.println ("Segment 1 Monotone Type: " + seg1.monotoneType());

  175.         System.out.println ("\tSegment 1 DPE: " + seg1.curvatureDPE());

  176.         /*
  177.          * Calibrate the right segment using the node values, and compute the segment Jacobian, the monotonicity, and the curvature penalty
  178.          */

  179.         WengertJacobian wj2 = seg2.jackDCoeffDEdgeParams (
  180.             seg1,
  181.             "Default",
  182.             16.,
  183.             null,
  184.             Double.NaN,
  185.             null
  186.         );

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

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

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

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

  191.         System.out.println ("Segment 2 Monotone Type: " + seg2.monotoneType());

  192.         System.out.println ("\tSegment 2 DPE: " + seg2.curvatureDPE());

  193.         /*
  194.          * Re-calibrate Segment #2 with a different response value
  195.          */

  196.         seg2.calibrate (
  197.             seg1,
  198.             14.,
  199.             null
  200.         );

  201.         /*
  202.          * Estimate the segment value at the given variate, and compute the corresponding Jacobian and the curvature penalty
  203.          */

  204.         double dblX = 2.0;

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

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

  207.         System.out.println ("\t\tSegment 2 DPE: " + seg2.curvatureDPE());
  208.     }

  209.     /*
  210.      * This sample illustrates the construction and the usage of basis splines (all types, really). It shows
  211.      *  the following:
  212.      *  - Construct a rational shape controller with the specified shape controller tension.
  213.      *  - Construct the segment inelastic parameter that is C2 (iK = 2 sets it to C2), with second order
  214.      *      curvature penalty, and without constraint.
  215.      *  - Test the KLK Hyperbolic Tension basis tension spline.
  216.      *  - Test the KLK Rational Linear basis tension spline.
  217.      *  - Test the KLK Rational Quadratic basis tension spline.
  218.      *  - Test the KLK Exponential Tension basis tension spline.
  219.      *
  220.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  221.      */

  222.     private static final void BasisTensionSplineSetSample()
  223.         throws Exception
  224.     {
  225.         /*
  226.          * Construct a rational shape controller with the shape controller tension of 1.
  227.          */

  228.         double dblShapeControllerTension = 1.;

  229.         ResponseScalingShapeControl rssc = new ResponseScalingShapeControl (
  230.             true,
  231.             new QuadraticRationalShapeControl (dblShapeControllerTension)
  232.         );

  233.         /*
  234.          * Construct the segment inelastic parameter that is C2 (iK = 2 sets it to C2), with second order
  235.          *  curvature penalty, and without constraint
  236.          */

  237.         int iK = 2;
  238.         int iCurvaturePenaltyDerivativeOrder = 2;

  239.         SegmentInelasticDesignControl segParams = SegmentInelasticDesignControl.Create (
  240.             iK,
  241.             iCurvaturePenaltyDerivativeOrder
  242.         );

  243.         /*
  244.          * Test the KLK Hyperbolic tension spline
  245.          */

  246.         System.out.println ( " ----------- \n KLK HYPERBOLIC \n ----------- \n");

  247.         TestSpline (
  248.             KLKHyperbolicTensionSpline(),
  249.             rssc,
  250.             segParams
  251.         );

  252.         /*
  253.          * Test the KLK Rational Linear tension spline
  254.          */

  255.         System.out.println ( " ----------- \n KLK RATIONAL LINEAR \n ----------- \n");

  256.         TestSpline (
  257.             KLKRationalLinearTensionSpline(),
  258.             rssc,
  259.             segParams
  260.         );

  261.         /*
  262.          * Test the KLK Rational Quadratic tension spline
  263.          */

  264.         System.out.println ( " ----------- \n KLK RATIONAL QUADRATIC \n ----------- \n");

  265.         TestSpline (
  266.             KLKRationalQuadraticTensionSpline(),
  267.             rssc,
  268.             segParams
  269.         );

  270.         /*
  271.          * Test the KLK Exponential tension spline
  272.          */

  273.         System.out.println ( " ----------- \n KLK EXPONENTIAL \n ----------- \n");

  274.         TestSpline (
  275.             KLKExponentialTensionSpline(),
  276.             rssc,
  277.             segParams
  278.         );
  279.     }

  280.     public static final void main (
  281.         final String[] astrArgs)
  282.         throws Exception
  283.     {
  284.         BasisTensionSplineSetSample();
  285.     }
  286. }