BSplineSequence.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.  * BSplineSequence implements Samples for the Construction and the usage of various monic basis B Spline
  54.  *  Sequences. It demonstrates the following:
  55.  *  - Construction and Usage of segment Monic B Spline Sequence.
  56.  *  - Construction and Usage of segment Multic B Spline Sequence.
  57.  *
  58.  * @author Lakshmi Krishnamurthy
  59.  */

  60. public class BSplineSequence {

  61.     /*
  62.      * This sample shows the computation of the response value, the normalized cumulative, and the ordered
  63.      *  derivative of the specified Segment Basis Function.
  64.      *
  65.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  66.      */

  67.     private static final void ComputeResponseMetric (
  68.         final SegmentBasisFunction me,
  69.         final String strComment)
  70.         throws Exception
  71.     {
  72.         int iOrder = 1;
  73.         double dblXIncrement = 0.25;

  74.         double dblX = me.leading() - dblXIncrement;

  75.         double dblXEnd = me.trailing() + dblXIncrement;

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

  77.         System.out.println ("\t-------------------------" + strComment + "---------------------------");

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

  79.         while (dblX <= dblXEnd) {
  80.             System.out.println (
  81.                 "\t\tResponse[" + FormatUtil.FormatDouble (dblX, 1, 3, 1.) + "] : " +
  82.                 FormatUtil.FormatDouble (me.evaluate (dblX), 1, 5, 1.) + " | " +
  83.                 FormatUtil.FormatDouble (me.normalizedCumulative (dblX), 1, 5, 1.) + " | " +
  84.                 FormatUtil.FormatDouble (me.derivative (dblX, iOrder), 1, 5, 1.)
  85.             );

  86.             dblX += dblXIncrement;
  87.         }
  88.     }

  89.     /*
  90.      * This sample demonstrates the construction and usage of the following monic/multic basis spline arrays:
  91.      *  - Hyperbolic Rational Linear Monic.
  92.      *  - Multic basis functions of 3rd degree (i.e., quadratic).
  93.      *  - Multic basis functions of 4th degree (i.e., cubic).
  94.      *  - Multic basis functions of 5th degree (i.e., quartic).
  95.      *
  96.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  97.      */

  98.     private static final void BSplineSequenceSample()
  99.         throws Exception
  100.     {
  101.         double[] adblPredictorOrdinate = new double[] {
  102.             1., 2., 3., 4., 5., 6.
  103.         };

  104.         /*
  105.          * Construct the Array of Hyperbolic Rational Linear Monic Segment Basis Functions.
  106.          */

  107.         SegmentBasisFunction[] aMonic = SegmentBasisFunctionGenerator.MonicSequence (
  108.             BasisHatPairGenerator.RAW_TENSION_HYPERBOLIC,
  109.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_LINEAR,
  110.             adblPredictorOrdinate,
  111.             0,
  112.             1.
  113.         );

  114.         /*
  115.          * Display the response value, the normalized cumulative, and the ordered derivative of the Monic
  116.          *  Segment Basis Function.
  117.          */

  118.         for (int i = 0; i < aMonic.length; ++i)
  119.             ComputeResponseMetric (
  120.                 aMonic[i],
  121.                 "   MONIC   "
  122.             );

  123.         /*
  124.          * Construct the array of multic basis functions of 3rd degree (i.e., quadratic).
  125.          */

  126.         SegmentBasisFunction[] aQuadratic = SegmentBasisFunctionGenerator.MulticSequence (
  127.             3,
  128.             aMonic
  129.         );

  130.         /*
  131.          * Display the response value, the normalized cumulative, and the ordered derivative of the Quadratic
  132.          *  Multic Segment Basis Function.
  133.          */

  134.         for (int i = 0; i < aQuadratic.length; ++i)
  135.             ComputeResponseMetric (
  136.                 aQuadratic[i],
  137.                 " QUADRATIC "
  138.             );

  139.         /*
  140.          * Construct the array of multic basis functions of 4th degree (i.e., cubic).
  141.          */

  142.         SegmentBasisFunction[] aCubic = SegmentBasisFunctionGenerator.MulticSequence (
  143.             4,
  144.             aQuadratic
  145.         );

  146.         /*
  147.          * Display the response value, the normalized cumulative, and the ordered derivative of the Cubic
  148.          *  Multic Segment Basis Function.
  149.          */

  150.         for (int i = 0; i < aCubic.length; ++i)
  151.             ComputeResponseMetric (
  152.                 aCubic[i],
  153.                 "   CUBIC   "
  154.             );

  155.         /*
  156.          * Construct the array of multic basis functions of 5th degree (i.e., quartic).
  157.          */

  158.         SegmentBasisFunction[] aQuartic = SegmentBasisFunctionGenerator.MulticSequence (
  159.             5,
  160.             aCubic
  161.         );

  162.         /*
  163.          * Display the response value, the normalized cumulative, and the ordered derivative of the Quartic
  164.          *  Multic Segment Basis Function.
  165.          */

  166.         for (int i = 0; i < aQuartic.length; ++i)
  167.             ComputeResponseMetric (
  168.                 aQuartic[i],
  169.                 "  QUARTIC  "
  170.             );
  171.     }

  172.     public static final void main (
  173.         final String[] astrArgs)
  174.         throws Exception
  175.     {
  176.         BSplineSequenceSample();
  177.     }
  178. }