BestFitFlexurePenalizer.java

  1. package org.drip.spline.segment;

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

  80. /**
  81.  * <i>BestFitFlexurePenalizer</i> implements the Segment's Best Fit, Curvature, and Length Penalizers. It
  82.  * provides the following functionality:
  83.  *
  84.  * <br><br>
  85.  *  <ul>
  86.  *      <li>
  87.  *          Compute the Cross-Curvature Penalty for the given Basis Pair
  88.  *      </li>
  89.  *      <li>
  90.  *          Compute the Cross-Length Penalty for the given Basis Pair
  91.  *      </li>
  92.  *      <li>
  93.  *          Compute the Best Fit Cross-Product Penalty for the given Basis Pair
  94.  *      </li>
  95.  *      <li>
  96.  *          Compute the Basis Pair Penalty Coefficient for the Best Fit and the Curvature Penalties
  97.  *      </li>
  98.  *      <li>
  99.  *          Compute the Penalty Constraint for the Basis Pair
  100.  *      </li>
  101.  *  </ul>
  102.  *
  103.  * <br><br>
  104.  *  <ul>
  105.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  106.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/SplineBuilderLibrary.md">Spline Builder Library</a></li>
  107.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spline/README.md">Basis Splines and Linear Compounders across a Broad Family of Spline Basis Functions</a></li>
  108.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spline/segment/README.md">Flexure Penalizing Best Fit Segment</a></li>
  109.  *  </ul>
  110.  * <br><br>
  111.  *
  112.  * @author Lakshmi Krishnamurthy
  113.  */

  114. public class BestFitFlexurePenalizer {
  115.     private org.drip.spline.segment.BasisEvaluator _lbe = null;
  116.     private org.drip.spline.segment.LatentStateInelastic _ics = null;
  117.     private org.drip.spline.params.SegmentBestFitResponse _sbfr = null;
  118.     private org.drip.spline.params.SegmentFlexurePenaltyControl _sfpcLength = null;
  119.     private org.drip.spline.params.SegmentFlexurePenaltyControl _sfpcCurvature = null;

  120.     /**
  121.      * BestFitFlexurePenalizer constructor
  122.      *
  123.      * @param ics Segment Inelastics
  124.      * @param sfpcCurvature Curvature Penalty Parameters
  125.      * @param sfpcLength Length Penalty Parameters
  126.      * @param sbfr Best Fit Weighted Response
  127.      * @param lbe The Local Basis Evaluator
  128.      *
  129.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  130.      */

  131.     public BestFitFlexurePenalizer (
  132.         final org.drip.spline.segment.LatentStateInelastic ics,
  133.         final org.drip.spline.params.SegmentFlexurePenaltyControl sfpcCurvature,
  134.         final org.drip.spline.params.SegmentFlexurePenaltyControl sfpcLength,
  135.         final org.drip.spline.params.SegmentBestFitResponse sbfr,
  136.         final org.drip.spline.segment.BasisEvaluator lbe)
  137.         throws java.lang.Exception
  138.     {
  139.         if (null == (_lbe = lbe) || null == (_ics = ics))
  140.             throw new java.lang.Exception ("BestFitFlexurePenalizer ctr: Invalid Inputs");

  141.         _sbfr = sbfr;
  142.         _sfpcLength = sfpcLength;
  143.         _sfpcCurvature = sfpcCurvature;
  144.     }

  145.     /**
  146.      * Compute the Cross-Curvature Penalty for the given Basis Pair
  147.      *
  148.      * @param iBasisIndexI I Basis Index (I is the Summation Index)
  149.      * @param iBasisIndexR R Basis Index (R is the Separator Index)
  150.      *
  151.      * @return The Cross-Curvature Penalty for the given Basis Pair
  152.      *
  153.      * @throws java.lang.Exception Thrown if the Cross-Curvature Penalty cannot be computed
  154.      */

  155.     public double basisPairCurvaturePenalty (
  156.         final int iBasisIndexI,
  157.         final int iBasisIndexR)
  158.         throws java.lang.Exception
  159.     {
  160.         if (null == _sfpcCurvature) return 0.;

  161.         org.drip.function.definition.R1ToR1 au = new org.drip.function.definition.R1ToR1
  162.             (null) {
  163.             @Override public double evaluate (
  164.                 final double dblVariate)
  165.                 throws Exception
  166.             {
  167.                 int iOrder = _sfpcCurvature.derivativeOrder();

  168.                 return _lbe.shapedBasisFunctionDerivative (dblVariate, iOrder, iBasisIndexI) *
  169.                     _lbe.shapedBasisFunctionDerivative (dblVariate, iOrder, iBasisIndexR);
  170.             }

  171.             @Override public double integrate (
  172.                 final double dblBegin,
  173.                 final double dblEnd)
  174.                 throws java.lang.Exception
  175.             {
  176.                 return org.drip.numerical.integration.R1ToR1Integrator.Boole (this, dblBegin, dblEnd);
  177.             }
  178.         };

  179.         return _sfpcCurvature.amplitude() * au.integrate (_ics.left(), _ics.right());
  180.     }

  181.     /**
  182.      * Compute the Cross-Length Penalty for the given Basis Pair
  183.      *
  184.      * @param iBasisIndexI I Basis Index (I is the Summation Index)
  185.      * @param iBasisIndexR R Basis Index (R is the Separator Index)
  186.      *
  187.      * @return The Cross-Length Penalty for the given Basis Pair
  188.      *
  189.      * @throws java.lang.Exception Thrown if the Cross-Length Penalty cannot be computed
  190.      */

  191.     public double basisPairLengthPenalty (
  192.         final int iBasisIndexI,
  193.         final int iBasisIndexR)
  194.         throws java.lang.Exception
  195.     {
  196.         if (null == _sfpcLength) return 0.;

  197.         org.drip.function.definition.R1ToR1 au = new org.drip.function.definition.R1ToR1
  198.             (null) {
  199.             @Override public double evaluate (
  200.                 final double dblVariate)
  201.                 throws Exception
  202.             {
  203.                 int iOrder = _sfpcLength.derivativeOrder();

  204.                 return _lbe.shapedBasisFunctionDerivative (dblVariate, iOrder, iBasisIndexI) *
  205.                     _lbe.shapedBasisFunctionDerivative (dblVariate, iOrder, iBasisIndexR);
  206.             }

  207.             @Override public double integrate (
  208.                 final double dblBegin,
  209.                 final double dblEnd)
  210.                 throws java.lang.Exception
  211.             {
  212.                 return org.drip.numerical.integration.R1ToR1Integrator.Boole (this, dblBegin, dblEnd);
  213.             }
  214.         };

  215.         return _sfpcLength.amplitude() * au.integrate (_ics.left(), _ics.right());
  216.     }

  217.     /**
  218.      * Compute the Best Fit Cross-Product Penalty for the given Basis Pair
  219.      *
  220.      * @param iBasisIndexI I Basis Index (I is the Summation Index)
  221.      * @param iBasisIndexR R Basis Index (R is the Separator Index)
  222.      *
  223.      * @return The Best Fit Cross-Product Penalty for the given Basis Pair
  224.      *
  225.      * @throws java.lang.Exception Thrown if the Best Fit Cross-Product Penalty cannot be computed
  226.      */

  227.     public double basisBestFitPenalty (
  228.         final int iBasisIndexI,
  229.         final int iBasisIndexR)
  230.         throws java.lang.Exception
  231.     {
  232.         if (null == _sbfr) return 0.;

  233.         int iNumPoint = _sbfr.numPoint();

  234.         if (0 == iNumPoint) return 0.;

  235.         double dblBasisPairFitnessPenalty = 0.;

  236.         for (int i = 0; i < iNumPoint; ++i) {
  237.             double dblPredictorOrdinate = _sbfr.predictorOrdinate (i);

  238.             dblBasisPairFitnessPenalty += _sbfr.weight (i) * _lbe.shapedBasisFunctionResponse
  239.                 (dblPredictorOrdinate, iBasisIndexI) * _lbe.shapedBasisFunctionResponse
  240.                     (dblPredictorOrdinate, iBasisIndexR);
  241.         }

  242.         return dblBasisPairFitnessPenalty / iNumPoint;
  243.     }

  244.     /**
  245.      * Compute the Basis Pair Penalty Coefficient for the Best Fit and the Curvature Penalties
  246.      *
  247.      * @param iBasisIndexI I Basis Index (I is the Summation Index)
  248.      * @param iBasisIndexR R Basis Index (R is the Separator Index)
  249.      *
  250.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  251.      *
  252.      * @return The Basis Pair Penalty Coefficient for the Fitness and the Curvature Penalties
  253.      */

  254.     public double basisPairConstraintCoefficient (
  255.         final int iBasisIndexI,
  256.         final int iBasisIndexR)
  257.         throws java.lang.Exception
  258.     {
  259.         return basisPairCurvaturePenalty (iBasisIndexI, iBasisIndexR) + basisPairLengthPenalty (iBasisIndexI,
  260.             iBasisIndexR) + basisBestFitPenalty (iBasisIndexI, iBasisIndexR);
  261.     }

  262.     /**
  263.      * Compute the Penalty Constraint for the Basis Pair
  264.      *
  265.      * @param iBasisIndexR R Basis Index (R is the Separator Index)
  266.      *
  267.      * @return Penalty Constraint for the Basis Pair
  268.      *
  269.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  270.      */

  271.     public double basisPairPenaltyConstraint (
  272.         final int iBasisIndexR)
  273.         throws java.lang.Exception
  274.     {
  275.         if (null == _sbfr) return 0.;

  276.         int iNumPoint = _sbfr.numPoint();

  277.         if (0 == iNumPoint) return 0.;

  278.         double dblBasisPairPenaltyConstraint = 0.;

  279.         for (int i = 0; i < iNumPoint; ++i) {
  280.             double dblPredictorOrdinate = _sbfr.predictorOrdinate (i);

  281.             dblBasisPairPenaltyConstraint += _sbfr.weight (i) * _lbe.shapedBasisFunctionResponse
  282.                 (dblPredictorOrdinate, iBasisIndexR) * _sbfr.response (i);
  283.         }

  284.         return dblBasisPairPenaltyConstraint / iNumPoint;
  285.     }
  286. }