BasisMulticBSpline.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.  * BasisMulticBSpline implements Samples for the Construction and the usage of various multic basis B Splines.
  54.  *  It demonstrates the following:
  55.  *  - Construction of segment higher order B Spline from the corresponding 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 BasisMulticBSpline {

  62.     /*
  63.      * This sample illustrates the construction and the usage of multic basis functions, and their eventual
  64.      *  response/derivative computation and comparison with the corresponding raw/processed and monic basis.
  65.      *  It shows the following:
  66.      *  - Construct the hyperbolic tension basis hat pair using the left predictor ordinates and the tension.
  67.      *  - Construct the hyperbolic tension basis hat pair using the right predictor ordinates and the
  68.      *      tension.
  69.      *  - Generate the left monic basis function from the hat type, left predictor ordinates, shape control,
  70.      *      and the tension parameters.
  71.      *  - Generate the right monic basis function from the hat type, right predictor ordinates, shape
  72.      *      control, and the tension parameters.
  73.      *  - Run a response value calculation comparison across the predictor ordinates for each of the left
  74.      *      basis hat and the monic basis functions.
  75.      *  - Run a response value calculation comparison across the predictor ordinates for each of the right
  76.      *      basis hat and the monic basis functions.
  77.      *  - Construct a multic basis function using the left/right monic basis functions, and the multic order.
  78.      *  - Display the multic Basis Function response as well as normalized Cumulative across the specified
  79.      *      variate range.
  80.      *
  81.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  82.      */

  83.     private static final void RunMulticBSplineTest (
  84.         final String strHatType,
  85.         final String strShapeControlType,
  86.         final double dblTension,
  87.         final int iMulticBSplineOrder)
  88.         throws Exception
  89.     {
  90.         double[] adblPredictorOrdinateLeft = new double[] {
  91.             1., 2., 3.
  92.         };
  93.         double[] adblPredictorOrdinateRight = new double[] {
  94.             2., 3., 4.
  95.         };

  96.         /*
  97.          * Construct the hyperbolic tension basis hat pair using the left predictor ordinates and the
  98.          *  tension.
  99.          */

  100.         TensionBasisHat[] aTBHLeft = BasisHatPairGenerator.HyperbolicTensionHatPair (
  101.             adblPredictorOrdinateLeft[0],
  102.             adblPredictorOrdinateLeft[1],
  103.             adblPredictorOrdinateLeft[2],
  104.             dblTension
  105.         );

  106.         /*
  107.          * Construct the hyperbolic tension basis hat pair using the right predictor ordinates and the
  108.          *  tension.
  109.          */

  110.         TensionBasisHat[] aTBHRight = BasisHatPairGenerator.HyperbolicTensionHatPair (
  111.             adblPredictorOrdinateRight[0],
  112.             adblPredictorOrdinateRight[1],
  113.             adblPredictorOrdinateRight[2],
  114.             dblTension
  115.         );

  116.         /*
  117.          * Generate the left monic basis function from the hat type, left predictor ordinates, shape control,
  118.          *  and the tension parameters.
  119.          */

  120.         SegmentBasisFunction sbfMonicLeft = SegmentBasisFunctionGenerator.Monic (
  121.             strHatType,
  122.             strShapeControlType,
  123.             adblPredictorOrdinateLeft,
  124.             2,
  125.             dblTension
  126.         );

  127.         /*
  128.          * Generate the right monic basis function from the hat type, right predictor ordinates, shape
  129.          *  control, and the tension parameters.
  130.          */

  131.         SegmentBasisFunction sbfMonicRight = SegmentBasisFunctionGenerator.Monic (
  132.             strHatType,
  133.             strShapeControlType,
  134.             adblPredictorOrdinateRight,
  135.             2,
  136.             dblTension
  137.         );

  138.         /*
  139.          * Run a response value calculation comparison across the predictor ordinates for each of the left
  140.          *  basis hat and the monic basis functions.
  141.          */

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

  143.         System.out.println ("\t            X    |   LEFT   |   RIGHT  |   MONIC  ");

  144.         System.out.println ("\t-------------------------------------------------");

  145.         double dblX = 0.50;
  146.         double dblXIncrement = 0.25;

  147.         while (dblX <= 4.50) {
  148.             System.out.println (
  149.                 "\tResponse[" + FormatUtil.FormatDouble (dblX, 1, 3, 1.) + "] : " +
  150.                 FormatUtil.FormatDouble (aTBHLeft[0].evaluate (dblX), 1, 5, 1.) + " | " +
  151.                 FormatUtil.FormatDouble (aTBHLeft[1].evaluate (dblX), 1, 5, 1.) + " | " +
  152.                 FormatUtil.FormatDouble (sbfMonicLeft.evaluate (dblX), 1, 5, 1.)
  153.             );

  154.             dblX += dblXIncrement;
  155.         }

  156.         /*
  157.          * Run a response value calculation comparison across the predictor ordinates for each of the right
  158.          *  basis hat and the monic basis functions.
  159.          */

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

  161.         System.out.println ("\t            X    |   LEFT   |   RIGHT  |   MONIC  ");

  162.         System.out.println ("\t-------------------------------------------------");

  163.         dblX = 0.50;

  164.         while (dblX <= 4.50) {
  165.             System.out.println (
  166.                 "\tResponse[" + FormatUtil.FormatDouble (dblX, 1, 3, 1.) + "] : " +
  167.                 FormatUtil.FormatDouble (aTBHRight[0].evaluate (dblX), 1, 5, 1.) + " | " +
  168.                 FormatUtil.FormatDouble (aTBHRight[1].evaluate (dblX), 1, 5, 1.) + " | " +
  169.                 FormatUtil.FormatDouble (sbfMonicRight.evaluate (dblX), 1, 5, 1.)
  170.             );

  171.             dblX += dblXIncrement;
  172.         }

  173.         /*
  174.          * Construct a multic basis function using the left/right monic basis functions, and the multic
  175.          *  order.
  176.          */

  177.         SegmentBasisFunction[] sbfMultic = SegmentBasisFunctionGenerator.MulticSequence (
  178.             iMulticBSplineOrder,
  179.             new SegmentBasisFunction[] {
  180.                 sbfMonicLeft,
  181.                 sbfMonicRight
  182.             }
  183.         );

  184.         /*
  185.          * Display the multic Basis Function response as well as normalized Cumulative across the specified
  186.          *  variate range.
  187.          */

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

  189.         System.out.println ("\t          PREDICTOR    | RESPONSE | CUMULATIVE  ");

  190.         System.out.println ("\t-------------------------------------------------");

  191.         dblX = 0.50;
  192.         dblXIncrement = 0.125;

  193.         while (dblX <= 4.50) {
  194.             System.out.println (
  195.                 "\t\tMultic[" + FormatUtil.FormatDouble (dblX, 1, 3, 1.) + "] : " +
  196.                 FormatUtil.FormatDouble (sbfMultic[0].evaluate (dblX), 1, 5, 1.) + " | " +
  197.                 FormatUtil.FormatDouble (sbfMultic[0].normalizedCumulative (dblX), 1, 5, 1.)
  198.             );

  199.             dblX += dblXIncrement;
  200.         }

  201.         System.out.println ("\n\t-------------------------------------------------\n");
  202.     }

  203.     /*
  204.      * This sample illustrates a sequence of tests using basis multic B Splines. In particular it shows the
  205.      *  following:
  206.      *  - Creation and usage of Multic B Spline built off of raw hyperbolic tension basis function, rational
  207.      *      linear shape controller, tension = 1.0, and 3rd order multic.
  208.      *  - Creation and usage of Multic B Spline built off of processed hyperbolic tension basis function,
  209.      *      rational linear shape controller, tension = 1.0, and 3rd order multic.
  210.      *  - Creation and usage of Multic B Spline built off of raw cubic tension basis function, rational
  211.      *      linear shape controller, tension = 0.0, and 3rd order multic.
  212.      *  - Creation and usage of Multic B Spline built off of raw cubic tension basis function, rational
  213.      *      linear shape controller, tension = 1.0, and 3rd order multic.
  214.      *  - Creation and usage of Multic B Spline built off of raw cubic tension basis function, rational
  215.      *      quadratic shape controller, tension = 1.0, and 3rd order multic.
  216.      *  - Creation and usage of Multic B Spline built off of raw cubic tension basis function, rational
  217.      *      exponential shape controller, tension = 1.0, and 3rd order multic.
  218.      *
  219.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  220.      */

  221.     private static final void BasisMulticBSplineSample()
  222.         throws Exception
  223.     {
  224.         /*
  225.          * Creation and usage of Multic B Spline built off of raw hyperbolic tension basis function,
  226.          *  rational linear shape controller, tension = 1.0, and 3rd order multic.
  227.          */

  228.         System.out.println ("\n    RAW TENSION HYPERBOLIC | LINEAR SHAPE CONTROL | TENSION = 1.0 | CUBIC B SPLINE");

  229.         RunMulticBSplineTest (
  230.             BasisHatPairGenerator.RAW_TENSION_HYPERBOLIC,
  231.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_LINEAR,
  232.             1.,
  233.             3
  234.         );

  235.         /*
  236.          * Creation and usage of Multic B Spline built off of processed hyperbolic tension basis function,
  237.          *  rational linear shape controller, tension = 1.0, and 3rd order multic.
  238.          */

  239.         System.out.println ("\n   PROC TENSION HYPERBOLIC | LINEAR SHAPE CONTROL | TENSION = 1.0 | CUBIC B SPLINE");

  240.         RunMulticBSplineTest (
  241.             BasisHatPairGenerator.PROCESSED_TENSION_HYPERBOLIC,
  242.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_LINEAR,
  243.             1.,
  244.             3
  245.         );

  246.         /*
  247.          * Creation and usage of Multic B Spline built off of raw cubic tension basis function, rational
  248.          *  linear shape controller, tension = 0.0, and 3rd order multic.
  249.          */

  250.         System.out.println ("\n   RAW CUBIC RATIONAL | LINEAR SHAPE CONTROL | TENSION = 0.0 | CUBIC B SPLINE");

  251.         RunMulticBSplineTest (
  252.             BasisHatPairGenerator.PROCESSED_CUBIC_RATIONAL,
  253.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_LINEAR,
  254.             0.,
  255.             3
  256.         );

  257.         /*
  258.          * Creation and usage of Multic B Spline built off of raw cubic tension basis function, rational
  259.          *  linear shape controller, tension = 1.0, and 3rd order multic.
  260.          */

  261.         System.out.println ("\n   RAW CUBIC RATIONAL | LINEAR SHAPE CONTROL | TENSION = 1.0 | CUBIC B SPLINE");

  262.         RunMulticBSplineTest (
  263.             BasisHatPairGenerator.PROCESSED_CUBIC_RATIONAL,
  264.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_LINEAR,
  265.             1.,
  266.             3
  267.         );

  268.         /*
  269.          * Creation and usage of Multic B Spline built off of raw cubic tension basis function, rational
  270.          *  quadratic shape controller, tension = 1.0, and 3rd order multic.
  271.          */

  272.         System.out.println ("\n   RAW CUBIC RATIONAL | QUADRATIC SHAPE CONTROL | TENSION = 1.0 | CUBIC B SPLINE");

  273.         RunMulticBSplineTest (
  274.             BasisHatPairGenerator.PROCESSED_CUBIC_RATIONAL,
  275.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_QUADRATIC,
  276.             1.,
  277.             3
  278.         );

  279.         /*
  280.          * Creation and usage of Multic B Spline built off of raw cubic tension basis function, rational
  281.          *  exponential shape controller, tension = 1.0, and 3rd order multic.
  282.          */

  283.         System.out.println ("\n   RAW CUBIC RATIONAL | EXPONENTIAL SHAPE CONTROL | TENSION = 1.0 | CUBIC B SPLINE");

  284.         RunMulticBSplineTest (
  285.             BasisHatPairGenerator.PROCESSED_CUBIC_RATIONAL,
  286.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_EXPONENTIAL,
  287.             1.,
  288.             3
  289.         );
  290.     }

  291.     public static final void main (
  292.         final String[] astrArgs)
  293.         throws Exception
  294.     {
  295.         BasisMulticBSplineSample();
  296.     }
  297. }