BasisMonicHatComparison.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 the comparison of the basis hat functions used in the construction of the
  54.  *  monic basis B Splines. It demonstrates the following:
  55.  *  - Construction of the Linear Cubic Rational Raw Hat Functions
  56.  *  - Construction of the Quadratic Cubic Rational Raw Hat Functions
  57.  *  - Construction of the Corresponding Processed Tension Basis Hat Functions
  58.  *  - Construction of the Wrapping Monic Functions
  59.  *  - Estimation and Comparison of the Ordered Derivatives
  60.  *
  61.  * @author Lakshmi Krishnamurthy
  62.  */

  63. public class BasisMonicHatComparison {

  64.     /*
  65.      * This sample display the test of the different shape controller functions. It demonstrates the
  66.      *  following:
  67.      *  - Construct the Raw Cubic rational left Tension Basis using the specified shape controller and
  68.      *      tension.
  69.      *  - Construct the Raw Cubic rational right Tension Basis using the specified shape controller and
  70.      *      tension.
  71.      *  - Construct the processed Cubic rational left Tension Basis using the Raw Cubic rational left Tension
  72.      *      Basis.
  73.      *  - Construct the processed Cubic rational Right Tension Basis using the Raw Cubic rational Right
  74.      *      Tension Basis.
  75.      *  - Construct the Segment Monic Basis Function using the left and the right processed hat functions.
  76.      *  - Display the response and the derivatives for the left/right cubic rational, and their corresponding
  77.      *      processed tension hat basis functions.
  78.      *
  79.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  80.      */

  81.     private static final void ShapeControllerTest (
  82.         final String strShapeController,
  83.         final double dblTension)
  84.         throws Exception
  85.     {
  86.         /*
  87.          * Construct the Raw Cubic rational left Tension Basis using the specified shape controller and
  88.          *  tension.
  89.          */

  90.         CubicRationalLeftRaw crlr = new CubicRationalLeftRaw (
  91.             1.,
  92.             2.,
  93.             strShapeController,
  94.             dblTension
  95.         );

  96.         /*
  97.          * Construct the Raw Cubic rational right Tension Basis using the specified shape controller and
  98.          *  tension.
  99.          */

  100.         CubicRationalRightRaw crrr = new CubicRationalRightRaw (
  101.             2.,
  102.             3.,
  103.             strShapeController,
  104.             dblTension
  105.         );

  106.         /*
  107.          * Construct the processed Cubic rational left Tension Basis using the Raw Cubic rational left
  108.          *  Tension Basis.
  109.          */

  110.         TensionProcessedBasisHat tpbhLeft = new TensionProcessedBasisHat (
  111.             crlr,
  112.             2
  113.         );

  114.         /*
  115.          * Construct the processed Cubic rational right Tension Basis using the Raw Cubic rational Right
  116.          *  Tension Basis.
  117.          */

  118.         TensionProcessedBasisHat tpbhRight = new TensionProcessedBasisHat (
  119.             crrr,
  120.             2
  121.         );

  122.         /*
  123.          * Construct the Segment Monic Basis Function using the left and the right processed hat functions.
  124.          */

  125.         SegmentMonicBasisFunction smbf = new SegmentMonicBasisFunction (
  126.             tpbhLeft,
  127.             tpbhRight
  128.         );

  129.         /*
  130.          * Display the response and the derivatives for the left/right cubic rational, and their
  131.          *  corresponding processed tension hat basis functions.
  132.          */

  133.         double dblX = crlr.left();

  134.         while (dblX <= crrr.right()) {
  135.             System.out.println ("\tDeriv[" + dblX + "] => " +
  136.                 FormatUtil.FormatDouble (smbf.derivative (dblX, 1), 1, 5, 1.));

  137.             System.out.println ("\t\tCubic Rational Left Deriv[" + dblX + "]  => " +
  138.                 FormatUtil.FormatDouble (crlr.derivative (dblX, 3), 1, 5, 1.));

  139.             System.out.println ("\t\tCubic Rational Right Deriv[" + dblX + "] => " +
  140.                 FormatUtil.FormatDouble (crrr.derivative (dblX, 3), 1, 5, 1.));

  141.             System.out.println ("\t\tTPBH Left Deriv[" + dblX + "]  => " +
  142.                 FormatUtil.FormatDouble (tpbhLeft.derivative (dblX, 1), 1, 5, 1.));

  143.             System.out.println ("\t\tTPBH Right Deriv[" + dblX + "] => " +
  144.                 FormatUtil.FormatDouble (tpbhRight.derivative (dblX, 1), 1, 5, 1.));

  145.             dblX += 0.5;
  146.         }
  147.     }

  148.     /*
  149.      * Sample illustrating the construction and usage of different monic basis hat shape controllers. This
  150.      *  example illustrates the following:
  151.      *  - Test Rational Linear Shape Control with 0.0 Tension Parameter (i.e., no shape control).
  152.      *  - Test Rational Linear Shape Control with 1.0 Tension Parameter.
  153.      *  - Test Rational Quadratic Shape Control with 1.0 Tension Parameter.
  154.      *  - Test Exponential Shape Control with 1.0 Tension Parameter.
  155.      *
  156.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  157.      */

  158.     private static final void BasisMonicHatComparisonSample()
  159.         throws Exception
  160.     {
  161.         /*
  162.          * Test Rational Linear Shape Control with 0.0 Tension Parameter (i.e., no shape control)
  163.          */

  164.         System.out.println ("\n-------------------------------------------------------------------");

  165.         System.out.println ("----------------- NO SHAPE CONTROL --------------------------------");

  166.         System.out.println ("-------------------------------------------------------------------");

  167.         ShapeControllerTest (
  168.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_LINEAR,
  169.             0.
  170.         );

  171.         /*
  172.          * Test Rational Linear Shape Control with 1.0 Tension Parameter
  173.          */

  174.         System.out.println ("\n-------------------------------------------------------------------");

  175.         System.out.println ("----------------- LINEAR SHAPE CONTROL; Tension 1.0 ---------------");

  176.         System.out.println ("-------------------------------------------------------------------");

  177.         ShapeControllerTest (
  178.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_LINEAR,
  179.             1.
  180.         );

  181.         /*
  182.          * Test Rational Quadratic Shape Control with 1.0 Tension Parameter
  183.          */

  184.         System.out.println ("\n-------------------------------------------------------------------");

  185.         System.out.println ("-------------- QUADRATIC SHAPE CONTROL; Tension 1.0 ---------------");

  186.         System.out.println ("-------------------------------------------------------------------");

  187.         ShapeControllerTest (
  188.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_QUADRATIC,
  189.             1.
  190.         );

  191.         /*
  192.          * Test Exponential Shape Control with 1.0 Tension Parameter
  193.          */

  194.         System.out.println ("\n-------------------------------------------------------------------");

  195.         System.out.println ("-------------- EXPONENTIAL SHAPE CONTROL; Tension 1.0 ---------------");

  196.         System.out.println ("-------------------------------------------------------------------");

  197.         ShapeControllerTest (
  198.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_EXPONENTIAL,
  199.             1.
  200.         );
  201.     }

  202.     public static final void main (
  203.         final String[] astrArgs)
  204.         throws Exception
  205.     {
  206.         BasisMonicHatComparisonSample();
  207.     }
  208. }