R1PiecewiseLinear.java

  1. package org.drip.measure.lebesgue;

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

  77. /**
  78.  * <i>R1PiecewiseLinear</i> implements the Piecewise Linear R<sup>1</sup> Distributions. It exports the
  79.  * Methods corresponding to the R<sup>1</sup> Lebesgue Base Class.
  80.  *
  81.  *  <br><br>
  82.  *  <ul>
  83.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  84.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  85.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/README.md">R<sup>d</sup> Continuous/Discrete Probability Measures</a></li>
  86.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/lebesgue/README.md">Uniform Piece-wise Lebesgue Measure</a></li>
  87.  *  </ul>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class R1PiecewiseLinear extends org.drip.measure.lebesgue.R1Uniform {
  92.     private double[] _adblPiecewiseDensity = null;
  93.     private double[] _adblPredictorOrdinate = null;

  94.     /**
  95.      * Calibrate an R1PiecewiseLinear Lebesgue Instance
  96.      *
  97.      * @param dblLeftPredictorOrdinateEdge Left Predictor Ordinate Edge
  98.      * @param dblRightPredictorOrdinateEdge Right Predictor Ordinate Edge
  99.      * @param adblPredictorOrdinate Array of Intermediate Predictor Ordinates
  100.      * @param adblCumulativeProbability Array of corresponding Cumulative Probabilities
  101.      *
  102.      * @return The R1PiecewiseLinearLebesgue Instance
  103.      */

  104.     public static final R1PiecewiseLinear Standard (
  105.         final double dblLeftPredictorOrdinateEdge,
  106.         final double dblRightPredictorOrdinateEdge,
  107.         final double[] adblPredictorOrdinate,
  108.         final double[] adblCumulativeProbability)
  109.     {
  110.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblLeftPredictorOrdinateEdge) ||
  111.             !org.drip.numerical.common.NumberUtil.IsValid (dblRightPredictorOrdinateEdge) ||
  112.                 dblLeftPredictorOrdinateEdge >= dblRightPredictorOrdinateEdge || null ==
  113.                     adblPredictorOrdinate || null == adblCumulativeProbability)
  114.             return null;

  115.         int iNumPredictorOrdinate = adblPredictorOrdinate.length;
  116.         double[] adblPiecewiseDensity = new double[iNumPredictorOrdinate + 1];

  117.         if (0 == iNumPredictorOrdinate || iNumPredictorOrdinate != adblCumulativeProbability.length)
  118.             return null;

  119.         for (int i = 0; i <= iNumPredictorOrdinate; ++i) {
  120.             double dblLeftPredictorOrdinate = 0 == i ? dblLeftPredictorOrdinateEdge :
  121.                 adblPredictorOrdinate[i - 1];

  122.             if (!org.drip.numerical.common.NumberUtil.IsValid (dblLeftPredictorOrdinate) ||
  123.                 dblLeftPredictorOrdinate < dblLeftPredictorOrdinateEdge)
  124.                 return null;

  125.             double dblRightPredictorOrdinate = iNumPredictorOrdinate == i ? dblRightPredictorOrdinateEdge :
  126.                 adblPredictorOrdinate[i];

  127.             if (!org.drip.numerical.common.NumberUtil.IsValid (dblRightPredictorOrdinate) ||
  128.                 dblRightPredictorOrdinate <= dblLeftPredictorOrdinate || dblRightPredictorOrdinate >
  129.                     dblRightPredictorOrdinateEdge)
  130.                 return null;

  131.             double dblLeftCumulativeProbability = 0 == i ? 0. : adblCumulativeProbability[i - 1];
  132.             double dblRightCumulativeProbability = iNumPredictorOrdinate == i ? 1. :
  133.                 adblCumulativeProbability[i];
  134.             adblPiecewiseDensity[i] = 2. * (dblRightCumulativeProbability - dblLeftCumulativeProbability) /
  135.                 (dblRightPredictorOrdinate * dblRightPredictorOrdinate - dblLeftPredictorOrdinate *
  136.                     dblLeftPredictorOrdinate);
  137.         }

  138.         try {
  139.             return new R1PiecewiseLinear (dblLeftPredictorOrdinateEdge,
  140.                 dblRightPredictorOrdinateEdge, adblPredictorOrdinate,  adblPiecewiseDensity);
  141.         } catch (java.lang.Exception e) {
  142.             e.printStackTrace();
  143.         }

  144.         return null;
  145.     }

  146.     /**
  147.      * R1PiecewiseLinear Constructor
  148.      *
  149.      * @param dblLeftPredictorOrdinateEdge Left Predictor Ordinate Edge
  150.      * @param dblRightPredictorOrdinateEdge Right Predictor Ordinate Edge
  151.      * @param adblPredictorOrdinate Array of Intermediate Predictor Ordinates
  152.      * @param adblPiecewiseDensity Array of corresponding Piece-wise Densities
  153.      *
  154.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  155.      */

  156.     public R1PiecewiseLinear (
  157.         final double dblLeftPredictorOrdinateEdge,
  158.         final double dblRightPredictorOrdinateEdge,
  159.         final double[] adblPredictorOrdinate,
  160.         final double[] adblPiecewiseDensity)
  161.         throws java.lang.Exception
  162.     {
  163.         super (dblLeftPredictorOrdinateEdge, dblRightPredictorOrdinateEdge);

  164.         if (null == (_adblPredictorOrdinate = adblPredictorOrdinate) || null == (_adblPiecewiseDensity =
  165.             adblPiecewiseDensity))
  166.             throw new java.lang.Exception ("R1PiecewiseLinear Constructor: Invalid Inputs");

  167.         int iNumPredictorOrdinate = _adblPredictorOrdinate.length;

  168.         if (0 == iNumPredictorOrdinate || iNumPredictorOrdinate + 1 != _adblPiecewiseDensity.length)
  169.             throw new java.lang.Exception ("R1PiecewiseLinear Constructor: Invalid Inputs");
  170.     }

  171.     /**
  172.      * Retrieve the Array of Predictor Ordinates
  173.      *
  174.      * @return The Array of Predictor Ordinates
  175.      */

  176.     public double[] predictorOrdinates()
  177.     {
  178.         return _adblPredictorOrdinate;
  179.     }

  180.     /**
  181.      * Retrieve the Array of Piecewise Densities
  182.      *
  183.      * @return The Array of Piecewise Densities
  184.      */

  185.     public double[] piecewiseDensities()
  186.     {
  187.         return _adblPiecewiseDensity;
  188.     }

  189.     @Override public double cumulative (
  190.         final double dblX)
  191.         throws java.lang.Exception
  192.     {
  193.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblX))
  194.             throw new java.lang.Exception ("R1PiecewiseLinear::cumulative => Invalid Inputs");

  195.         double dblLeftEdge = leftEdge();

  196.         double dblRightEdge = rightEdge();

  197.         if (dblX <= dblLeftEdge) return 0.;

  198.         if (dblX >= dblRightEdge) return 1.;

  199.         int iSegmentIndex = 0;
  200.         double dblSegmentLeft = dblLeftEdge;
  201.         double dblCumulativeProbability = 0.;
  202.         int iMaxSegmentIndex = _adblPiecewiseDensity.length - 1;

  203.         while (iSegmentIndex < iMaxSegmentIndex) {
  204.             double dblSegmentRight = _adblPredictorOrdinate[iSegmentIndex];

  205.             if (dblX >= dblSegmentLeft && dblX <= dblSegmentRight)
  206.                 return dblCumulativeProbability + 0.5 * _adblPiecewiseDensity[iSegmentIndex] * (dblX * dblX -
  207.                     dblSegmentLeft * dblSegmentLeft);

  208.             dblCumulativeProbability += 0.5 * _adblPiecewiseDensity[iSegmentIndex] * (dblSegmentRight *
  209.                 dblSegmentRight - dblSegmentLeft * dblSegmentLeft);
  210.             dblSegmentLeft = dblSegmentRight;
  211.             ++iSegmentIndex;
  212.         }

  213.         return dblCumulativeProbability + 0.5 * _adblPiecewiseDensity[iMaxSegmentIndex] * (dblX * dblX -
  214.             dblRightEdge * dblRightEdge);
  215.     }

  216.     @Override public double invCumulative (
  217.         final double dblY)
  218.         throws java.lang.Exception
  219.     {
  220.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblY) || dblY < 0. || dblY > 1.)
  221.             throw new java.lang.Exception ("R1PiecewiseLinear::invCumulative => Invalid inputs");

  222.         org.drip.function.definition.R1ToR1 r1ToR1CumulativeProbability = new
  223.             org.drip.function.definition.R1ToR1 (null) {
  224.             @Override public double evaluate (
  225.                 final double dblX)
  226.                 throws java.lang.Exception
  227.             {
  228.                 return cumulative (dblX);
  229.             }
  230.         };

  231.         org.drip.function.r1tor1solver.FixedPointFinderOutput fpfo = new
  232.             org.drip.function.r1tor1solver.FixedPointFinderBracketing (dblY, r1ToR1CumulativeProbability,
  233.                 null, org.drip.function.r1tor1solver.VariateIteratorPrimitive.BISECTION, true).findRoot();

  234.         if (null == fpfo || !fpfo.containsRoot())
  235.             throw new java.lang.Exception ("R1PiecewiseLinear::invCumulative => No roots");

  236.         return fpfo.getRoot();
  237.     }

  238.     @Override public double density (
  239.         final double dblX)
  240.         throws java.lang.Exception
  241.     {
  242.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblX))
  243.             throw new java.lang.Exception ("R1PiecewiseLinear::density => Invalid Inputs");

  244.         if (dblX <= leftEdge() || dblX >= rightEdge()) return 0.;

  245.         int iSegmentIndex = 0;
  246.         int iMaxSegmentIndex = _adblPiecewiseDensity.length - 1;

  247.         while (iSegmentIndex < iMaxSegmentIndex) {
  248.             if (dblX >= _adblPredictorOrdinate[iSegmentIndex] && dblX <=
  249.                 _adblPredictorOrdinate[iSegmentIndex + 1])
  250.                 break;

  251.             ++iSegmentIndex;
  252.         }

  253.         return _adblPiecewiseDensity[iSegmentIndex] * dblX;
  254.     }

  255.     @Override public org.drip.numerical.common.Array2D histogram()
  256.     {
  257.         double dblLeftEdge = leftEdge();

  258.         double[] adblX = new double[GRID_WIDTH];
  259.         double[] adblY = new double[GRID_WIDTH];

  260.         double dblWidth = (rightEdge() - dblLeftEdge) / GRID_WIDTH;

  261.         for (int i = 0; i < GRID_WIDTH; ++i) {
  262.             adblX[i] = dblLeftEdge + (i + 1) * dblWidth;

  263.             try {
  264.                 adblY[i] = incremental (adblX[i] - dblWidth, adblX[i]);
  265.             } catch (java.lang.Exception e) {
  266.                 e.printStackTrace();

  267.                 return null;
  268.             }
  269.         }

  270.         return org.drip.numerical.common.Array2D.FromArray (adblX, adblY);
  271.     }
  272. }