BasisSplineRegressor.java

  1. package org.drip.regression.spline;

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

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  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.  * Copyright (C) 2012 Lakshmi Krishnamurthy
  15.  *
  16.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  17.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  18.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  19.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  20.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  21.  *      and computational support.
  22.  *  
  23.  *      https://lakshmidrip.github.io/DROP/
  24.  *  
  25.  *  DROP is composed of three modules:
  26.  *  
  27.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  28.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  29.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  30.  *
  31.  *  DROP Product Core implements libraries for the following:
  32.  *  - Fixed Income Analytics
  33.  *  - Loan Analytics
  34.  *  - Transaction Cost Analytics
  35.  *
  36.  *  DROP Portfolio Core implements libraries for the following:
  37.  *  - Asset Allocation Analytics
  38.  *  - Asset Liability Management Analytics
  39.  *  - Capital Estimation Analytics
  40.  *  - Exposure Analytics
  41.  *  - Margin Analytics
  42.  *  - XVA Analytics
  43.  *
  44.  *  DROP Computational Core implements libraries for the following:
  45.  *  - Algorithm Support
  46.  *  - Computation Support
  47.  *  - Function Analysis
  48.  *  - Model Validation
  49.  *  - Numerical Analysis
  50.  *  - Numerical Optimizer
  51.  *  - Spline Builder
  52.  *  - Statistical Learning
  53.  *
  54.  *  Documentation for DROP is Spread Over:
  55.  *
  56.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  57.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  58.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  59.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  60.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  61.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  62.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  63.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  64.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  65.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  66.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  67.  *
  68.  *  Licensed under the Apache License, Version 2.0 (the "License");
  69.  *      you may not use this file except in compliance with the License.
  70.  *  
  71.  *  You may obtain a copy of the License at
  72.  *      http://www.apache.org/licenses/LICENSE-2.0
  73.  *  
  74.  *  Unless required by applicable law or agreed to in writing, software
  75.  *      distributed under the License is distributed on an "AS IS" BASIS,
  76.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  77.  *  
  78.  *  See the License for the specific language governing permissions and
  79.  *      limitations under the License.
  80.  */

  81. /**
  82.  * <i>BasisSplineRegressor</i> implements the custom basis spline regressor for the given basis spline. As
  83.  * part of the regression run, it executes the following:
  84.  *
  85.  * <br><br>
  86.  *  <ul>
  87.  *      <li>
  88.  *          Calibrate and compute the left and the right Jacobian.
  89.  *      </li>
  90.  *      <li>
  91.  *          Reset right node and re-run calibration.
  92.  *      </li>
  93.  *      <li>
  94.  *          Compute an intermediate value Jacobian.
  95.  *      </li>
  96.  *  </ul>
  97.  *
  98.  * <br><br>
  99.  *  <ul>
  100.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  101.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  102.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/regression/README.md">Regression Engine Core and the Unit Regressors</a></li>
  103.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/regression/spline/README.md">Custom Basis Spline Regression Engine</a></li>
  104.  *  </ul>
  105.  * <br><br>
  106.  *
  107.  * @author Lakshmi Krishnamurthy
  108.  */

  109. public class BasisSplineRegressor extends org.drip.regression.core.UnitRegressionExecutor {
  110.     private java.lang.String _strName = "";
  111.     private org.drip.spline.segment.LatentStateResponseModel _seg1 = null;
  112.     private org.drip.spline.segment.LatentStateResponseModel _seg2 = null;
  113.     private org.drip.numerical.differentiation.WengertJacobian _wjLeft = null;
  114.     private org.drip.numerical.differentiation.WengertJacobian _wjRight = null;
  115.     private org.drip.numerical.differentiation.WengertJacobian _wjValue = null;

  116.     /**
  117.      * Create an instance of Polynomial BasisSplineRegressor
  118.      *
  119.      * @param strName Regressor Name
  120.      * @param strScenarioName Regressor Scenario Name
  121.      * @param iNumBasis Number of Basis Functions
  122.      * @param iCk Ck
  123.      *
  124.      * @return The BasisSplineRegressor Instance
  125.      */

  126.     public static final BasisSplineRegressor CreatePolynomialSplineRegressor (
  127.         final java.lang.String strName,
  128.         final java.lang.String strScenarioName,
  129.         final int iNumBasis,
  130.         final int iCk)
  131.     {
  132.         try {
  133.             org.drip.spline.basis.FunctionSet fs =
  134.                 org.drip.spline.basis.FunctionSetBuilder.PolynomialBasisSet (new
  135.                     org.drip.spline.basis.PolynomialFunctionSetParams (iNumBasis));

  136.             return null == fs ? null : new BasisSplineRegressor (strName, strScenarioName, fs, iCk);
  137.         } catch (java.lang.Exception e) {
  138.             e.printStackTrace();
  139.         }

  140.         return null;
  141.     }

  142.     /**
  143.      * Create an instance of Bernstein Polynomial BasisSplineRegressor
  144.      *
  145.      * @param strName Regressor Name
  146.      * @param strScenarioName Regressor Scenario Name
  147.      * @param iNumBasis Number of Basis Functions
  148.      * @param iCk Ck
  149.      *
  150.      * @return The BasisSplineRegressor Instance
  151.      */

  152.     public static final BasisSplineRegressor CreateBernsteinPolynomialSplineRegressor (
  153.         final java.lang.String strName,
  154.         final java.lang.String strScenarioName,
  155.         final int iNumBasis,
  156.         final int iCk)
  157.     {
  158.         try {
  159.             org.drip.spline.basis.FunctionSet fs =
  160.                 org.drip.spline.basis.FunctionSetBuilder.BernsteinPolynomialBasisSet (new
  161.                     org.drip.spline.basis.PolynomialFunctionSetParams (iNumBasis));

  162.             return null == fs ? null : new BasisSplineRegressor (strName, strScenarioName, fs, iCk);
  163.         } catch (java.lang.Exception e) {
  164.             e.printStackTrace();
  165.         }

  166.         return null;
  167.     }

  168.     /**
  169.      * Create an instance of Exponential BasisSplineRegressor
  170.      *
  171.      * @param strName Regressor Name
  172.      * @param strScenarioName Regressor Scenario Name
  173.      * @param dblTension Tension Parameter
  174.      *
  175.      * @return The BasisSplineRegressor Instance
  176.      */

  177.     public static final BasisSplineRegressor CreateExponentialTensionSplineRegressor (
  178.         final java.lang.String strName,
  179.         final java.lang.String strScenarioName,
  180.         final double dblTension)
  181.     {
  182.         try {
  183.             org.drip.spline.basis.FunctionSet fs =
  184.                 org.drip.spline.basis.FunctionSetBuilder.ExponentialTensionBasisSet (new
  185.                     org.drip.spline.basis.ExponentialTensionSetParams (dblTension));

  186.             return null == fs ? null : new BasisSplineRegressor (strName, strScenarioName, fs, 2);
  187.         } catch (java.lang.Exception e) {
  188.             e.printStackTrace();
  189.         }

  190.         return null;
  191.     }

  192.     /**
  193.      * Create an instance of Hyperbolic BasisSplineRegressor
  194.      *
  195.      * @param strName Regressor Name
  196.      * @param strScenarioName Regressor Scenario Name
  197.      * @param dblTension Tension Parameter
  198.      *
  199.      * @return The BasisSplineRegressor Instance
  200.      */

  201.     public static final BasisSplineRegressor CreateHyperbolicTensionSplineRegressor (
  202.         final java.lang.String strName,
  203.         final java.lang.String strScenarioName,
  204.         final double dblTension)
  205.     {
  206.         try {
  207.             org.drip.spline.basis.FunctionSet fs =
  208.                 org.drip.spline.basis.FunctionSetBuilder.HyperbolicTensionBasisSet (new
  209.                     org.drip.spline.basis.ExponentialTensionSetParams (dblTension));

  210.             return null == fs ? null : new BasisSplineRegressor (strName, strScenarioName, fs, 2);
  211.         } catch (java.lang.Exception e) {
  212.             e.printStackTrace();
  213.         }

  214.         return null;
  215.     }

  216.     /**
  217.      * Create an instance of the Kaklis-Pandelis BasisSplineRegressor
  218.      *
  219.      * @param strName Regressor Name
  220.      * @param strScenarioName Regressor Scenario Name
  221.      * @param iKPPolynomialTension KP Polynomial Tension Parameter
  222.      *
  223.      * @return The BasisSplineRegressor Instance
  224.      */

  225.     public static final BasisSplineRegressor CreateKaklisPandelisSplineRegressor (
  226.         final java.lang.String strName,
  227.         final java.lang.String strScenarioName,
  228.         final int iKPPolynomialTension)
  229.     {
  230.         try {
  231.             org.drip.spline.basis.FunctionSet fs =
  232.                 org.drip.spline.basis.FunctionSetBuilder.KaklisPandelisBasisSet (new
  233.                     org.drip.spline.basis.KaklisPandelisSetParams (iKPPolynomialTension));

  234.             return null == fs ? null : new BasisSplineRegressor (strName, strScenarioName, fs, 2);
  235.         } catch (java.lang.Exception e) {
  236.             e.printStackTrace();
  237.         }

  238.         return null;
  239.     }

  240.     protected BasisSplineRegressor (
  241.         final java.lang.String strName,
  242.         final java.lang.String strScenarioName,
  243.         final org.drip.spline.basis.FunctionSet fs,
  244.         final int iCk)
  245.         throws java.lang.Exception
  246.     {
  247.         super (strName, strScenarioName);

  248.         org.drip.spline.params.SegmentInelasticDesignControl segParams =
  249.             org.drip.spline.params.SegmentInelasticDesignControl.Create (iCk, 2);

  250.         org.drip.spline.params.ResponseScalingShapeControl rssc = new
  251.             org.drip.spline.params.ResponseScalingShapeControl (true, new
  252.                 org.drip.function.r1tor1.QuadraticRationalShapeControl (1.));

  253.         if (null == (_seg1 = org.drip.spline.segment.LatentStateResponseModel.Create (1.0, 3.0, fs, rssc,
  254.             segParams)) || null == (_seg2 = org.drip.spline.segment.LatentStateResponseModel.Create (3.0,
  255.                 6.0, fs, rssc, segParams)))
  256.             throw new java.lang.Exception ("BasisSplineRegressor ctr: Cant create the segments");
  257.     }

  258.     @Override public boolean preRegression()
  259.     {
  260.         return true;
  261.     }

  262.     @Override public boolean execRegression()
  263.     {
  264.         try {
  265.             return null != (_wjLeft = _seg1.jackDCoeffDEdgeParams (25., 0., 20.25, null)) && null !=
  266.                 (_wjRight = _seg2.jackDCoeffDEdgeParams (_seg1, "Default", 16., null, java.lang.Double.NaN,
  267.                     null)) && _seg2.calibrate (_seg1, 14., null) && null != (_wjValue =
  268.                         _seg2.jackDResponseDEdgeInput (5., 1));
  269.         } catch (java.lang.Exception e) {
  270.             e.printStackTrace();
  271.         }

  272.         return false;
  273.     }

  274.     @Override public boolean postRegression (
  275.         final org.drip.regression.core.RegressionRunDetail rnvd)
  276.     {
  277.         try {
  278.             if (!rnvd.set (_strName + "_Seg1_1_0", "" + _seg1.responseValue (1.))) return false;

  279.             if (!rnvd.set (_strName + "_Seg1_3_0", "" + _seg1.responseValue (3.))) return false;

  280.             if (!rnvd.set (_strName + "_Seg1_Jack", _wjLeft.displayString()));

  281.             if (!rnvd.set (_strName + "_Seg1_Head_Jack", _seg1.jackDCoeffDEdgeInputs().displayString()));

  282.             if (!rnvd.set (_strName + "_Seg1_Monotone", _seg1.monotoneType().toString()));

  283.             if (!rnvd.set (_strName + "_Seg2_3_0", "" + _seg2.responseValue (3.))) return false;

  284.             if (!rnvd.set (_strName + "_Seg2_6_0", "" + _seg2.responseValue (6.))) return false;

  285.             if (!rnvd.set (_strName + "_Seg2_Jack", _wjRight.displayString()));

  286.             if (!rnvd.set (_strName + "_Seg2_Head_Jack", _seg2.jackDCoeffDEdgeInputs().displayString()));

  287.             if (!rnvd.set (_strName + "_Seg2_Monotone", _seg2.monotoneType().toString()));

  288.             return rnvd.set (_strName + "_Seg2_Value_Jack", _wjValue.displayString());
  289.         } catch (java.lang.Exception e) {
  290.             e.printStackTrace();
  291.         }

  292.         return false;
  293.     }
  294. }