R1PiecewiseDisplaced.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>R1PiecewiseDisplaced</i> implements the Displaced Piecewise Linear R<sup>1</sup> Distributions. It
  79.  * exports the 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 R1PiecewiseDisplaced extends org.drip.measure.lebesgue.R1Uniform {
  92.     private double[] _adblPredictorOrdinate = null;
  93.     private double[] _adblPiecewiseDensitySlope = null;
  94.     private double _dblDensityDisplacement = java.lang.Double.NaN;

  95.     /**
  96.      * Calibrate an R1PiecewiseDisplaced Lebesgue Instance
  97.      *
  98.      * @param dblLeftPredictorOrdinateEdge Left Predictor Ordinate Edge
  99.      * @param dblRightPredictorOrdinateEdge Right Predictor Ordinate Edge
  100.      * @param adblPredictorOrdinate Array of Intermediate Predictor Ordinates
  101.      * @param adblCumulativeProbability Array of corresponding Cumulative Probabilities
  102.      * @param dblMean The Distribution Mean
  103.      *
  104.      * @return The R1PiecewiseDisplacedLebesgue Instance
  105.      */

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

  119.         int iNumSegment = adblPredictorOrdinate.length + 1;
  120.         double[][] aadblM = new double[iNumSegment + 1][iNumSegment + 1];
  121.         double[] adblSecondDegreeIntegral = new double[iNumSegment];
  122.         double[] adblSlope = new double[iNumSegment];
  123.         double[] adblY = new double[iNumSegment + 1];
  124.         adblY[iNumSegment] = dblMean;
  125.         aadblM[iNumSegment][iNumSegment] = 0.5 * (dblRightPredictorOrdinateEdge *
  126.             dblRightPredictorOrdinateEdge - dblLeftPredictorOrdinateEdge * dblLeftPredictorOrdinateEdge);

  127.         if (1 == iNumSegment || iNumSegment - 1 != adblCumulativeProbability.length) return null;

  128.         for (int i = 0; i < iNumSegment; ++i) {
  129.             adblY[i] = i == iNumSegment - 1 ? 1. : adblCumulativeProbability[i];
  130.             double dblSegmentLeft = 0 == i ? dblLeftPredictorOrdinateEdge : adblPredictorOrdinate[i - 1];
  131.             double dblSegmentRight = iNumSegment - 1 == i ? dblRightPredictorOrdinateEdge :
  132.                 adblPredictorOrdinate[i];
  133.             adblSecondDegreeIntegral[i] = 0.5 * (dblSegmentRight * dblSegmentRight - dblSegmentLeft *
  134.                 dblSegmentLeft);
  135.             aadblM[i][iNumSegment] = dblSegmentRight - dblLeftPredictorOrdinateEdge;
  136.             aadblM[iNumSegment][i] = (dblSegmentRight * dblSegmentRight * dblSegmentRight - dblSegmentLeft *
  137.                 dblSegmentLeft * dblSegmentLeft) / 6.;

  138.             if (!org.drip.numerical.common.NumberUtil.IsValid (dblSegmentLeft) || dblSegmentLeft <
  139.                 dblLeftPredictorOrdinateEdge)
  140.                 return null;

  141.             if (!org.drip.numerical.common.NumberUtil.IsValid (dblSegmentRight) || dblSegmentRight <=
  142.                 dblSegmentLeft || dblSegmentRight > dblRightPredictorOrdinateEdge)
  143.                 return null;
  144.         }

  145.         for (int i = 0; i < iNumSegment; ++i) {
  146.             for (int k = 0; k < iNumSegment; ++k)
  147.                 aadblM[i][k] = k > i ? 0. : adblSecondDegreeIntegral[k];
  148.         }

  149.         org.drip.numerical.linearalgebra.LinearizationOutput lo =
  150.             org.drip.numerical.linearalgebra.LinearSystemSolver.SolveUsingMatrixInversion (aadblM, adblY);

  151.         if (null == lo) return null;

  152.         double[] adblSlopeDisplacement = lo.getTransformedRHS();

  153.         if (null == adblSlopeDisplacement || adblSlopeDisplacement.length != iNumSegment + 1) return null;

  154.         for (int i = 0; i < iNumSegment; ++i)
  155.             adblSlope[i] = adblSlopeDisplacement[i];

  156.         try {
  157.             return new R1PiecewiseDisplaced (dblLeftPredictorOrdinateEdge,
  158.                 dblRightPredictorOrdinateEdge, adblPredictorOrdinate, adblSlope,
  159.                     adblSlopeDisplacement[iNumSegment]);
  160.         } catch (java.lang.Exception e) {
  161.             e.printStackTrace();
  162.         }

  163.         return null;
  164.     }

  165.     /**
  166.      * R1PiecewiseDisplaced Constructor
  167.      *
  168.      * @param dblLeftPredictorOrdinateEdge Left Predictor Ordinate Edge
  169.      * @param dblRightPredictorOrdinateEdge Right Predictor Ordinate Edge
  170.      * @param adblPredictorOrdinate Array of Intermediate Predictor Ordinates
  171.      * @param adblPiecewiseDensitySlope Array of corresponding Piece-wise Density Slopes
  172.      * @param dblDensityDisplacement Uniform Density Displacement
  173.      *
  174.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  175.      */

  176.     public R1PiecewiseDisplaced (
  177.         final double dblLeftPredictorOrdinateEdge,
  178.         final double dblRightPredictorOrdinateEdge,
  179.         final double[] adblPredictorOrdinate,
  180.         final double[] adblPiecewiseDensitySlope,
  181.         final double dblDensityDisplacement)
  182.         throws java.lang.Exception
  183.     {
  184.         super (dblLeftPredictorOrdinateEdge, dblRightPredictorOrdinateEdge);

  185.         if (null == (_adblPredictorOrdinate = adblPredictorOrdinate) || null == (_adblPiecewiseDensitySlope =
  186.             adblPiecewiseDensitySlope) || !org.drip.numerical.common.NumberUtil.IsValid (_dblDensityDisplacement
  187.                 = dblDensityDisplacement))
  188.             throw new java.lang.Exception ("R1PiecewiseDisplaced Constructor: Invalid Inputs");

  189.         int iNumPredictorOrdinate = _adblPredictorOrdinate.length;

  190.         if (0 == iNumPredictorOrdinate || iNumPredictorOrdinate + 1 != _adblPiecewiseDensitySlope.length)
  191.             throw new java.lang.Exception ("R1PiecewiseDisplaced Constructor: Invalid Inputs");
  192.     }

  193.     /**
  194.      * Retrieve the Array of Predictor Ordinates
  195.      *
  196.      * @return The Array of Predictor Ordinates
  197.      */

  198.     public double[] predictorOrdinates()
  199.     {
  200.         return _adblPredictorOrdinate;
  201.     }

  202.     /**
  203.      * Retrieve the Array of Piecewise Density Slopes
  204.      *
  205.      * @return The Array of Piecewise Density Slopes
  206.      */

  207.     public double[] piecewiseDensitySlopes()
  208.     {
  209.         return _adblPiecewiseDensitySlope;
  210.     }

  211.     /**
  212.      * Retrieve the Density Displacement
  213.      *
  214.      * @return The Density Displacement
  215.      */

  216.     public double densityDisplacement()
  217.     {
  218.         return _dblDensityDisplacement;
  219.     }

  220.     @Override public double cumulative (
  221.         final double dblX)
  222.         throws java.lang.Exception
  223.     {
  224.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblX))
  225.             throw new java.lang.Exception ("R1PiecewiseDisplaced::cumulative => Invalid Inputs");

  226.         double dblLeftEdge = leftEdge();

  227.         double dblRightEdge = rightEdge();

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

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

  230.         int iSegmentIndex = 0;
  231.         double dblSegmentLeft = dblLeftEdge;
  232.         double dblCumulativeProbability = 0.;
  233.         int iMaxSegmentIndex = _adblPiecewiseDensitySlope.length - 1;

  234.         while (iSegmentIndex < iMaxSegmentIndex) {
  235.             double dblSegmentRight = _adblPredictorOrdinate[iSegmentIndex];

  236.             if (dblX >= dblSegmentLeft && dblX <= dblSegmentRight)
  237.                 return dblCumulativeProbability + 0.5 * _adblPiecewiseDensitySlope[iSegmentIndex] * (dblX *
  238.                     dblX - dblSegmentLeft * dblSegmentLeft) + _dblDensityDisplacement * (dblX -
  239.                         dblSegmentLeft);

  240.             dblCumulativeProbability += (0.5 * _adblPiecewiseDensitySlope[iSegmentIndex] * (dblSegmentRight *
  241.                 dblSegmentRight - dblSegmentLeft * dblSegmentLeft) + _dblDensityDisplacement *
  242.                     (dblSegmentRight - dblSegmentLeft));
  243.             dblSegmentLeft = dblSegmentRight;
  244.             ++iSegmentIndex;
  245.         }

  246.         return dblCumulativeProbability + 0.5 * _adblPiecewiseDensitySlope[iMaxSegmentIndex] * (dblX * dblX -
  247.             _adblPredictorOrdinate[iMaxSegmentIndex - 1] * _adblPredictorOrdinate[iMaxSegmentIndex - 1]) +
  248.                 _dblDensityDisplacement * (dblX - _adblPredictorOrdinate[iMaxSegmentIndex - 1]);
  249.     }

  250.     @Override public double invCumulative (
  251.         final double dblY)
  252.         throws java.lang.Exception
  253.     {
  254.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblY) || dblY < 0. || dblY > 1.)
  255.             throw new java.lang.Exception ("R1PiecewiseDisplaced::invCumulative => Invalid inputs");

  256.         org.drip.function.definition.R1ToR1 r1ToR1CumulativeProbability = new
  257.             org.drip.function.definition.R1ToR1 (null) {
  258.             @Override public double evaluate (
  259.                 final double dblX)
  260.                 throws java.lang.Exception
  261.             {
  262.                 return cumulative (dblX);
  263.             }
  264.         };

  265.         org.drip.function.r1tor1solver.FixedPointFinderOutput fpfo = new
  266.             org.drip.function.r1tor1solver.FixedPointFinderBracketing (dblY, r1ToR1CumulativeProbability,
  267.                 null, org.drip.function.r1tor1solver.VariateIteratorPrimitive.BISECTION, true).findRoot
  268.                     (org.drip.function.r1tor1solver.InitializationHeuristics.FromHardSearchEdges (leftEdge(),
  269.                         rightEdge()));

  270.         if (null == fpfo || !fpfo.containsRoot())
  271.             throw new java.lang.Exception ("R1PiecewiseDisplaced::invCumulative => No roots");

  272.         return fpfo.getRoot();
  273.     }

  274.     @Override public double density (
  275.         final double dblX)
  276.         throws java.lang.Exception
  277.     {
  278.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblX))
  279.             throw new java.lang.Exception ("R1PiecewiseDisplaced::density => Invalid Inputs");

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

  281.         int iSegmentIndex = 0;
  282.         int iMaxSegmentIndex = _adblPiecewiseDensitySlope.length - 1;

  283.         while (iSegmentIndex < iMaxSegmentIndex) {
  284.             if (dblX >= _adblPredictorOrdinate[iSegmentIndex] && dblX <=
  285.                 _adblPredictorOrdinate[iSegmentIndex + 1])
  286.                 break;

  287.             ++iSegmentIndex;
  288.         }

  289.         return _adblPiecewiseDensitySlope[iSegmentIndex] * dblX + _dblDensityDisplacement;
  290.     }

  291.     @Override public org.drip.numerical.common.Array2D histogram()
  292.     {
  293.         double dblLeftEdge = leftEdge();

  294.         double[] adblX = new double[GRID_WIDTH];
  295.         double[] adblY = new double[GRID_WIDTH];

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

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

  299.             try {
  300.                 adblY[i] = incremental (adblX[i] - dblWidth, adblX[i]);
  301.             } catch (java.lang.Exception e) {
  302.                 e.printStackTrace();

  303.                 return null;
  304.             }
  305.         }

  306.         return org.drip.numerical.common.Array2D.FromArray (adblX, adblY);
  307.     }
  308. }