BasisMonicBSpline.java

  1. package org.drip.sample.spline;

  2. import org.drip.numerical.common.FormatUtil;
  3. import org.drip.spline.bspline.*;

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

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

  52. /**
  53.  * BasisMonicBSpline implements Samples for the Construction and the usage of various monic basis B Splines.
  54.  *  It demonstrates the following:
  55.  *  - Construction of segment B Spline Hat Basis Functions.
  56.  *  - Estimation of the derivatives and the basis envelope cumulative integrands.
  57.  *  - Estimation of the normalizer and the basis envelope cumulative normalized integrands.
  58.  *
  59.  * @author Lakshmi Krishnamurthy
  60.  */

  61. public class BasisMonicBSpline {

  62.     /*
  63.      * This sample illustrates the construction and the usage of the monic basis B Splines. It shows the
  64.      *  following:
  65.      *  - Construct the segment basis monic function from the specified hat type, the shape controller, the
  66.      *      derivative order, and the tension.
  67.      *  - Compare the responses emitted by the basis hat functions and the monic basis functions.
  68.      *  - Compute the normalized cumulative emitted by the monic basis functions.
  69.      *  - Compute the ordered derivative emitted by the monic basis functions.
  70.      *
  71.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  72.      */

  73.     private static final void TestMonicHatBasis (
  74.         final String strHatType,
  75.         final String strShapeController,
  76.         final TensionBasisHat[] aTBH,
  77.         final double[] adblPredictorOrdinate,
  78.         final String strTest)
  79.         throws Exception
  80.     {
  81.         /*
  82.          * Construct the segment basis monic function from the specified hat type, the shape controller, the
  83.          *  derivative order, and the tension.
  84.          */

  85.         SegmentBasisFunction me = SegmentBasisFunctionGenerator.Monic (
  86.             strHatType,
  87.             strShapeController,
  88.             adblPredictorOrdinate,
  89.             2,
  90.             aTBH[0].tension()
  91.         );

  92.         /*
  93.          * Compare the responses emitted by the basis hat functions and the monic basis functions.
  94.          */

  95.         double dblX = 1.0;
  96.         double dblXIncrement = 0.25;

  97.         System.out.println ("\n\t-------------------------------------------------");

  98.         System.out.println ("\t--------------" + strTest + "-------------");

  99.         System.out.println ("\t-------------------------------------------------\n");

  100.         System.out.println ("\t-------------X---|---LEFT---|---RIGHT--|--MONIC--\n");

  101.         while (dblX <= 3.0) {
  102.             System.out.println (
  103.                 "\tResponse[" + FormatUtil.FormatDouble (dblX, 1, 3, 1.) + "] : " +
  104.                 FormatUtil.FormatDouble (aTBH[0].evaluate (dblX), 1, 5, 1.) + " | " +
  105.                 FormatUtil.FormatDouble (aTBH[1].evaluate (dblX), 1, 5, 1.) + " | " +
  106.                 FormatUtil.FormatDouble (me.evaluate (dblX), 1, 5, 1.));

  107.             dblX += dblXIncrement;
  108.         }

  109.         System.out.println ("\n\t------------------------------------------------\n");

  110.         /*
  111.          * Compute the normalized cumulative emitted by the monic basis functions.
  112.          */

  113.         dblX = 1.0;

  114.         while (dblX <= 3.0) {
  115.             System.out.println (
  116.                 "\t\tNormCumulative[" + FormatUtil.FormatDouble (dblX, 1, 3, 1.) + "] : " +
  117.                 FormatUtil.FormatDouble (me.normalizedCumulative (dblX), 1, 5, 1.)
  118.             );

  119.             dblX += dblXIncrement;
  120.         }

  121.         System.out.println ("\n\t------------------------------------------------\n");

  122.         /*
  123.          * Compute the ordered derivative emitted by the monic basis functions.
  124.          */

  125.         dblX = 1.0;
  126.         int iOrder = 1;

  127.         while (dblX <= 3.0) {
  128.             System.out.println (
  129.                 "\t\t\tDeriv[" + FormatUtil.FormatDouble (dblX, 1, 3, 1.) + "] : " +
  130.                 FormatUtil.FormatDouble (me.derivative (dblX, iOrder), 1, 5, 1.)
  131.             );

  132.             dblX += dblXIncrement;
  133.         }

  134.         System.out.println ("\n\t-----------------------------------------------\n");
  135.     }

  136.     /*
  137.      * This sample illustrates the construction and usage of raw/processed basis tension splines, and their
  138.      *  comparisons with the correspondingly constructed monic hat basis functions. It shows the following:
  139.      *  - Construct the Processed Hyperbolic Tension Hat Pair from the co-ordinate arrays, the Ck, and the
  140.      *      tension.
  141.      *  - Implement and test the basis monic spline function using the constructed Processed Hyperbolic
  142.      *      Tension Hat Pair and the Rational Linear Shape Controller.
  143.      *  - Construct the Raw Hyperbolic Tension Hat Pair from the co-ordinate arrays and the tension.
  144.      *  - Implement and test the basis monic spline function using the constructed Raw Hyperbolic Tension Hat
  145.      *      Pair and the Rational Linear Shape Controller.
  146.      *  - Construct the Processed Cubic Rational Tension Hat Pair from the co-ordinate arrays, Linear
  147.      *      Rational Shape Controller, and no tension.
  148.      *  - Implement and test the basis monic spline function using the constructed Flat Processed Cubic
  149.      *      Tension Hat Pair and the Rational Linear Shape Controller.
  150.      *  - Construct the Processed Cubic Rational Tension Hat Pair from the co-ordinate arrays, Linear
  151.      *      Rational Shape Controller, and non-zero tension.
  152.      *  - Implement and test the basis monic spline function using the constructed Processed Cubic Rational
  153.      *      Tension Hat Pair and the Rational Linear Shape Controller.
  154.      *  - Construct the Processed Cubic Rational Tension Hat Pair from the co-ordinate arrays, Quadratic
  155.      *      Rational Shape Controller, and the tension.
  156.      *  - Implement and test the basis monic spline function using the constructed Processed Cubic Rational
  157.      *      Tension Hat Pair and the Quadratic Linear Shape Controller.
  158.      *  - Construct the Processed Cubic Rational Tension Hat Pair from the co-ordinate arrays, Exponential
  159.      *      Rational Shape Controller, and the tension.
  160.      *  - Implement and test the basis monic spline function using the constructed Processed Cubic Rational
  161.      *      Tension Hat Pair and the Rational Exponential Shape Controller.
  162.      *
  163.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  164.      */

  165.     private static final void BasisMonicBSplineSample()
  166.         throws Exception
  167.     {
  168.         double[] adblPredictorOrdinate = new double[] {1., 2., 3.};

  169.         /*
  170.          * Construct the Processed Hyperbolic Tension Hat Pair from the co-ordinate arrays, the Ck, and the
  171.          *  tension.
  172.          */

  173.         TensionBasisHat[] aTBHProcessed = BasisHatPairGenerator.ProcessedHyperbolicTensionHatPair (
  174.             adblPredictorOrdinate[0],
  175.             adblPredictorOrdinate[1],
  176.             adblPredictorOrdinate[2],
  177.             2,
  178.             1.
  179.         );

  180.         /*
  181.          * Implement and test the basis monic spline function using the constructed Processed Hyperbolic
  182.          *  Tension Hat Pair and the Rational Linear Shape Controller.
  183.          */

  184.         TestMonicHatBasis (
  185.             BasisHatPairGenerator.PROCESSED_TENSION_HYPERBOLIC,
  186.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_LINEAR,
  187.             aTBHProcessed,
  188.             adblPredictorOrdinate,
  189.             " PROCESSED HYPERBOLIC "
  190.         );

  191.         /*
  192.          * Construct the Raw Hyperbolic Tension Hat Pair from the co-ordinate arrays and the tension.
  193.          */

  194.         TensionBasisHat[] aTBHStraight = BasisHatPairGenerator.HyperbolicTensionHatPair (
  195.             adblPredictorOrdinate[0],
  196.             adblPredictorOrdinate[1],
  197.             adblPredictorOrdinate[2],
  198.             1.
  199.         );

  200.         /*
  201.          * Implement and test the basis monic spline function using the constructed Raw Hyperbolic Tension
  202.          *  Hat Pair and the Rational Linear Shape Controller.
  203.          */

  204.         TestMonicHatBasis (
  205.             BasisHatPairGenerator.RAW_TENSION_HYPERBOLIC,
  206.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_LINEAR,
  207.             aTBHStraight,
  208.             adblPredictorOrdinate,
  209.             " STRAIGHT  HYPERBOLIC "
  210.         );

  211.         /*
  212.          * Construct the Processed Cubic Rational Tension Hat Pair from the co-ordinate arrays, Linear
  213.          *  Rational Shape Controller, and no tension.
  214.          */

  215.         TensionBasisHat[] aTBHCubicRationalPlain = BasisHatPairGenerator.ProcessedCubicRationalHatPair (
  216.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_LINEAR,
  217.             adblPredictorOrdinate[0],
  218.             adblPredictorOrdinate[1],
  219.             adblPredictorOrdinate[2],
  220.             2,
  221.             0.
  222.         );

  223.         /*
  224.          * Implement and test the basis monic spline function using the constructed Flat Processed Cubic
  225.          *  Tension Hat Pair and the Rational Linear Shape Controller.
  226.          */

  227.         TestMonicHatBasis (
  228.             BasisHatPairGenerator.PROCESSED_CUBIC_RATIONAL,
  229.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_LINEAR,
  230.             aTBHCubicRationalPlain,
  231.             adblPredictorOrdinate,
  232.             "     CUBIC     FLAT   "
  233.         );

  234.         /*
  235.          * Construct the Processed Cubic Rational Tension Hat Pair from the co-ordinate arrays, Linear
  236.          *  Rational Shape Controller, and non-zero tension.
  237.          */

  238.         TensionBasisHat[] aTBHCubicRationalLinear = BasisHatPairGenerator.ProcessedCubicRationalHatPair (
  239.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_LINEAR,
  240.             adblPredictorOrdinate[0],
  241.             adblPredictorOrdinate[1],
  242.             adblPredictorOrdinate[2],
  243.             2,
  244.             1.
  245.         );

  246.         /*
  247.          * Implement and test the basis monic spline function using the constructed Processed Cubic Rational
  248.          *  Tension Hat Pair and the Rational Linear Shape Controller.
  249.          */

  250.         TestMonicHatBasis (
  251.             BasisHatPairGenerator.PROCESSED_CUBIC_RATIONAL,
  252.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_LINEAR,
  253.             aTBHCubicRationalLinear,
  254.             adblPredictorOrdinate,
  255.             " CUBIC LINEAR RATIONAL "
  256.         );

  257.         /*
  258.          * Construct the Processed Cubic Rational Tension Hat Pair from the co-ordinate arrays, Quadratic
  259.          *  Rational Shape Controller, and the tension.
  260.          */

  261.         TensionBasisHat[] aTBHCubicRationalQuadratic = BasisHatPairGenerator.ProcessedCubicRationalHatPair (
  262.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_QUADRATIC,
  263.             adblPredictorOrdinate[0],
  264.             adblPredictorOrdinate[1],
  265.             adblPredictorOrdinate[2],
  266.             2,
  267.             1.
  268.         );

  269.         /*
  270.          * Implement and test the basis monic spline function using the constructed Processed Cubic Rational
  271.          *  Tension Hat Pair and the Quadratic Linear Shape Controller.
  272.          */

  273.         TestMonicHatBasis (
  274.             BasisHatPairGenerator.PROCESSED_CUBIC_RATIONAL,
  275.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_QUADRATIC,
  276.             aTBHCubicRationalQuadratic,
  277.             adblPredictorOrdinate,
  278.             " CUBIC  QUAD  RATIONAL "
  279.         );

  280.         /*
  281.          * Construct the Processed Cubic Rational Tension Hat Pair from the co-ordinate arrays, Exponential
  282.          *  Rational Shape Controller, and the tension.
  283.          */

  284.         TensionBasisHat[] aTBHCubicRationalExponential = BasisHatPairGenerator.ProcessedCubicRationalHatPair (
  285.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_EXPONENTIAL,
  286.             adblPredictorOrdinate[0],
  287.             adblPredictorOrdinate[1],
  288.             adblPredictorOrdinate[2],
  289.             2,
  290.             1.
  291.         );

  292.         /*
  293.          * Implement and test the basis monic spline function using the constructed Processed Cubic Rational
  294.          *  Tension Hat Pair and the Rational Exponential Shape Controller.
  295.          */

  296.         TestMonicHatBasis (
  297.             BasisHatPairGenerator.PROCESSED_CUBIC_RATIONAL,
  298.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_EXPONENTIAL,
  299.             aTBHCubicRationalExponential,
  300.             adblPredictorOrdinate,
  301.             " CUBIC  EXP  RATIONAL "
  302.         );
  303.     }

  304.     public static final void main (
  305.         final String[] astrArgs)
  306.         throws Exception
  307.     {
  308.         BasisMonicBSplineSample();
  309.     }
  310. }