BasisBSplineSet.java

  1. package org.drip.sample.spline;

  2. import org.drip.spline.basis.*;
  3. import org.drip.spline.bspline.*;
  4. import org.drip.spline.params.*;
  5. import org.drip.spline.segment.LatentStateResponseModel;

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

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

  54. /**
  55.  * BasisSplineSet implements Samples for the Construction and the usage of various basis spline functions. It
  56.  *  demonstrates the following:
  57.  *  - Construction of segment control parameters - polynomial (regular/Bernstein) segment control,
  58.  *      exponential/hyperbolic tension segment control, Kaklis-Pandelis tension segment control, and C1
  59.  *      Hermite.
  60.  *  - Control the segment using the rational shape controller, and the appropriate Ck.
  61.  *  - Estimate the node value and the node value Jacobian with the segment, as well as at the boundaries.
  62.  *  - Calculate the segment monotonicity.

  63.  * @author Lakshmi Krishnamurthy
  64.  */

  65. public class BasisBSplineSet {

  66.     /*
  67.      * This sample demonstrates construction and usage of B Spline hat functions over solitary segments. It
  68.      *  shows the constructions of left/right segments, their calibration, and Jacobian evaluation.
  69.      *
  70.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  71.      */

  72.     private static final void TestSpline (
  73.         final FunctionSet fs,
  74.         final ResponseScalingShapeControl rssc,
  75.         final SegmentInelasticDesignControl segParams)
  76.         throws Exception
  77.     {
  78.         /*
  79.          * Construct the left and the right segments
  80.          */

  81.         LatentStateResponseModel seg1 = LatentStateResponseModel.Create (
  82.             1.0,
  83.             1.5,
  84.             fs,
  85.             rssc,
  86.             segParams
  87.         );

  88.         /*
  89.          * Calibrate the left segment using the node values, and compute the segment Jacobian
  90.          */

  91.         System.out.println (
  92.             seg1.calibrate (
  93.                 25.,
  94.                 0.,
  95.                 20.25,
  96.                 null
  97.             )
  98.         );

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

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

  102.     /*
  103.      * This sample demonstrates the construction and usage of the Basis B Spline Set Functionality. It shows
  104.      *  the following:
  105.      *  - Set up the B Spline Sequence Parameters for the Cubic Rational Hat Type, Linear Shape Controller,
  106.      *      using the specified tension and derivative order parameters.
  107.      *  - Setup the B Spline Basis Set.
  108.      *  - Construct the segment inelastic parameter that is C2 (iK = 2 sets it to C2), with second order
  109.      *      curvature penalty, and without constraint.
  110.      *  - Construct and Evaluate the B Spline.
  111.      *
  112.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  113.      */

  114.     private static final void BasisBSplineSetSample()
  115.         throws Exception
  116.     {
  117.         /*
  118.          * Set up the B Spline Sequence Parameters for the Cubic Rational Hat Type, Linear Shape Controller,
  119.          *  using the specified tension and derivative order parameters.
  120.          */

  121.         BSplineSequenceParams bssp = new BSplineSequenceParams (
  122.             BasisHatPairGenerator.PROCESSED_CUBIC_RATIONAL,
  123.             BasisHatShapeControl.SHAPE_CONTROL_RATIONAL_LINEAR,
  124.             2,
  125.             4,
  126.             1.,
  127.             2);

  128.         /*
  129.          * Setup the B Spline Basis Set
  130.          */

  131.         FunctionSet fsBSS = FunctionSetBuilder.BSplineBasisSet (bssp);

  132.         /*
  133.          * Construct the segment inelastic parameter that is C2 (iK = 2 sets it to C2), with second order
  134.          *  curvature penalty, and without constraint
  135.          */

  136.         int iK = 2;
  137.         int iCurvaturePenaltyDerivativeOrder = 2;

  138.         SegmentInelasticDesignControl segParams = SegmentInelasticDesignControl.Create (
  139.             iK,
  140.             iCurvaturePenaltyDerivativeOrder
  141.         );

  142.         /*
  143.          * Construct and Evaluate the B Spline
  144.          */

  145.         TestSpline (
  146.             fsBSS,
  147.             null,
  148.             segParams
  149.         );
  150.     }

  151.     public static final void main (
  152.         final String[] astrArgs)
  153.         throws Exception
  154.     {
  155.         BasisBSplineSetSample();
  156.     }
  157. }