HermiteBasisSplineRegressor.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>HermiteBasisSplineRegressor</i> implements the Hermite basis spline regressor for the given basis
  83.  * spline. As 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 HermiteBasisSplineRegressor extends org.drip.regression.spline.BasisSplineRegressor {
  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 Hermite 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 org.drip.regression.spline.BasisSplineRegressor CreateHermiteSplineRegressor (
  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 HermiteBasisSplineRegressor (strName, strScenarioName, fs, iCk);
  137.         } catch (java.lang.Exception e) {
  138.             e.printStackTrace();
  139.         }

  140.         return null;
  141.     }

  142.     private HermiteBasisSplineRegressor (
  143.         final java.lang.String strName,
  144.         final java.lang.String strScenarioName,
  145.         final org.drip.spline.basis.FunctionSet fs,
  146.         final int iCk)
  147.         throws java.lang.Exception
  148.     {
  149.         super (strName, strScenarioName, fs, iCk);

  150.         org.drip.spline.params.SegmentInelasticDesignControl segParams =
  151.             org.drip.spline.params.SegmentInelasticDesignControl.Create (iCk, 1);

  152.         org.drip.spline.params.ResponseScalingShapeControl rssc = new
  153.             org.drip.spline.params.ResponseScalingShapeControl (true, new
  154.                 org.drip.function.r1tor1.QuadraticRationalShapeControl (1.));

  155.         if (null == (_seg1 = org.drip.spline.segment.LatentStateResponseModel.Create (0.0, 1.0, fs, rssc,
  156.             segParams)) || null == (_seg2 = org.drip.spline.segment.LatentStateResponseModel.Create (1.0,
  157.                 2.0, fs, rssc, segParams)))
  158.             throw new java.lang.Exception ("HermiteBasisSplineRegressor ctr: Cant create the segments");
  159.     }

  160.     @Override public boolean execRegression()
  161.     {
  162.         try {
  163.             return null != (_wjLeft = _seg1.jackDCoeffDEdgeParams (new double[] {0., 1.}, new double[] {1.,
  164.                 4.}, new double[] {1.}, new double[] {6.}, null, null)) && null != (_wjRight =
  165.                     _seg2.jackDCoeffDEdgeParams (new double[] {1., 2.}, new double[] {4., 15.}, new double[]
  166.                         {6.}, new double[] {17.}, null, null)) && _seg2.calibrate (_seg1, 14., null) && null
  167.                             != (_wjValue = _seg2.jackDResponseDEdgeInput (1.5, 1));
  168.         } catch (java.lang.Exception e) {
  169.             e.printStackTrace();
  170.         }

  171.         return false;
  172.     }

  173.     @Override public boolean postRegression (
  174.         final org.drip.regression.core.RegressionRunDetail rnvd)
  175.     {
  176.         try {
  177.             if (!rnvd.set (_strName + "_Seg1_0_0", "" + _seg1.responseValue (0.))) return false;

  178.             if (!rnvd.set (_strName + "_Seg1_1_0", "" + _seg1.responseValue (1.))) return false;

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

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

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

  182.             if (!rnvd.set (_strName + "_Seg2_1_0", "" + _seg2.responseValue (1.))) return false;

  183.             if (!rnvd.set (_strName + "_Seg2_2_0", "" + _seg2.responseValue (2.))) return false;

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

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

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

  187.             return rnvd.set (_strName + "_Seg2_Value_Jack", _wjValue.displayString());
  188.         } catch (java.lang.Exception e) {
  189.             e.printStackTrace();
  190.         }

  191.         return false;
  192.     }
  193. }