R1Univariate.java

  1. package org.drip.measure.continuous;

  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>R1Univariate</i> exposes the Base Abstract Class behind Univariate R<sup>1</sup> Distributions. It
  82.  *  exports the Methods for incremental, cumulative, and inverse cumulative distribution densities.
  83.  *
  84.  *  <br><br>
  85.  *  <ul>
  86.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  87.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  88.  *      <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>
  89.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/continuous/README.md">R<sup>1</sup> and R<sup>d</sup> Continuous Random Measure</a></li>
  90.  *  </ul>
  91.  *
  92.  * @author Lakshmi Krishnamurthy
  93.  */

  94. public abstract class R1Univariate {

  95.     /**
  96.      * Lay out the Support of the PDF Range
  97.      *
  98.      * @return Support of the PDF Range
  99.      */

  100.     public abstract double[] support();

  101.     /**
  102.      * Indicate if x is inside the Supported Range
  103.      *
  104.      * @param x X
  105.      *
  106.      * @return TRUE - x is inside of the Supported Range
  107.      */

  108.     public boolean supported (
  109.         final double x)
  110.     {
  111.         if (java.lang.Double.isNaN (x))
  112.         {
  113.             return false;
  114.         }

  115.         double[] range = support();

  116.         return range[0] <= x && x <= range[1];
  117.     }

  118.     /**
  119.      * Compute the Density under the Distribution at the given Variate
  120.      *
  121.      * @param dblX Variate at which the Density needs to be computed
  122.      *
  123.      * @return The Density
  124.      *
  125.      * @throws java.lang.Exception Thrown if the input is invalid
  126.      */

  127.     public abstract double density (
  128.         final double dblX)
  129.         throws java.lang.Exception;

  130.     /**
  131.      * Compute the cumulative under the distribution to the given value
  132.      *
  133.      * @param dblX Variate to which the cumulative is to be computed
  134.      *
  135.      * @return The cumulative
  136.      *
  137.      * @throws java.lang.Exception Thrown if the inputs are invalid
  138.      */

  139.     public abstract double cumulative (
  140.         final double dblX)
  141.         throws java.lang.Exception;

  142.     /**
  143.      * Compute the Incremental under the Distribution between the 2 variates
  144.      *
  145.      * @param dblXLeft Left Variate to which the cumulative is to be computed
  146.      * @param dblXRight Right Variate to which the cumulative is to be computed
  147.      *
  148.      * @return The Incremental under the Distribution between the 2 variates
  149.      *
  150.      * @throws java.lang.Exception Thrown if the inputs are invalid
  151.      */

  152.     public double incremental (
  153.         final double dblXLeft,
  154.         final double dblXRight)
  155.         throws java.lang.Exception
  156.     {
  157.         return cumulative (dblXRight) - cumulative (dblXLeft);
  158.     }

  159.     /**
  160.      * Compute the inverse cumulative under the distribution corresponding to the given value
  161.      *
  162.      * @param p Value corresponding to which the inverse cumulative is to be computed
  163.      *
  164.      * @return The inverse cumulative
  165.      *
  166.      * @throws java.lang.Exception Thrown if the Input is invalid
  167.      */

  168.     public double invCumulative (
  169.         final double p)
  170.         throws java.lang.Exception
  171.     {
  172.         if (!org.drip.numerical.common.NumberUtil.IsValid (p) || 0. > p || 1. < p)
  173.         {
  174.             throw new java.lang.Exception ("R1Univariate::invCumulative => Invalid Inputs");
  175.         }

  176.         org.drip.function.r1tor1solver.FixedPointFinderOutput fixedPointFinderOutput =
  177.             new org.drip.function.r1tor1solver.FixedPointFinderBrent (
  178.                 0.,
  179.                 new org.drip.function.definition.R1ToR1 (null)
  180.                 {
  181.                     @Override public double evaluate (
  182.                         final double u)
  183.                         throws java.lang.Exception
  184.                     {
  185.                         return cumulative (u) - p;
  186.                     }
  187.                 },
  188.                 true
  189.             ).findRoot();

  190.         if (null == fixedPointFinderOutput)
  191.         {
  192.             throw new java.lang.Exception ("R1Univariate::invCumulative => Cannot find Root");
  193.         }

  194.         return fixedPointFinderOutput.getRoot();
  195.     }

  196.     /**
  197.      * Retrieve the Mean of the Distribution
  198.      *
  199.      * @return The Mean of the Distribution
  200.      *
  201.      * @throws java.lang.Exception Thrown if the Mean cannot be estimated
  202.      */

  203.     public abstract double mean()
  204.         throws java.lang.Exception;

  205.     /**
  206.      * Retrieve the Median of the Distribution
  207.      *
  208.      * @return The Median of the Distribution
  209.      *
  210.      * @throws java.lang.Exception Thrown if the Median cannot be estimated
  211.      */

  212.     public double median()
  213.         throws java.lang.Exception
  214.     {
  215.         return invCumulative (0.50);
  216.     }

  217.     /**
  218.      * Retrieve the Mode of the Distribution
  219.      *
  220.      * @return The Mode of the Distribution
  221.      *
  222.      * @throws java.lang.Exception Thrown if the Mode cannot be estimated
  223.      */

  224.     public double mode()
  225.         throws java.lang.Exception
  226.     {
  227.         final org.drip.function.definition.R1ToR1 densityFunction =
  228.             new org.drip.function.definition.R1ToR1 (null)
  229.         {
  230.             @Override public double evaluate (
  231.                 final double u)
  232.                 throws java.lang.Exception
  233.             {
  234.                 return density (u);
  235.             }
  236.         };

  237.         org.drip.function.r1tor1solver.FixedPointFinderOutput fixedPointFinderOutput =
  238.             new org.drip.function.r1tor1solver.FixedPointFinderBrent (
  239.                 0.,
  240.                 new org.drip.function.definition.R1ToR1 (null)
  241.                 {
  242.                     @Override public double evaluate (
  243.                         final double u)
  244.                         throws java.lang.Exception
  245.                     {
  246.                         return densityFunction.derivative (
  247.                             u,
  248.                             1
  249.                         );
  250.                     }
  251.                 },
  252.                 true
  253.             ).findRoot();

  254.         if (null == fixedPointFinderOutput)
  255.         {
  256.             throw new java.lang.Exception ("R1Univariate::invCumulative => Cannot find Root");
  257.         }

  258.         return fixedPointFinderOutput.getRoot();
  259.     }

  260.     /**
  261.      * Retrieve the Variance of the Distribution
  262.      *
  263.      * @return The Variance of the Distribution
  264.      *
  265.      * @throws java.lang.Exception Thrown if the Variance cannot be estimated
  266.      */

  267.     public abstract double variance()
  268.         throws java.lang.Exception;

  269.     /**
  270.      * Retrieve the Skewness of the Distribution
  271.      *
  272.      * @return The Skewness of the Distribution
  273.      *
  274.      * @throws java.lang.Exception Thrown if the Skewness cannot be estimated
  275.      */

  276.     public double skewness()
  277.         throws java.lang.Exception
  278.     {
  279.         throw new java.lang.Exception ("R1Univariate::skewness => Not implemented");
  280.     }

  281.     /**
  282.      * Retrieve the Excess Kurtosis of the Distribution
  283.      *
  284.      * @return The Excess Kurtosis of the Distribution
  285.      *
  286.      * @throws java.lang.Exception Thrown if the Skewness cannot be estimated
  287.      */

  288.     public double excessKurtosis()
  289.         throws java.lang.Exception
  290.     {
  291.         throw new java.lang.Exception ("R1Univariate::excessKurtosis => Not implemented");
  292.     }

  293.     /**
  294.      * Retrieve the Differential Entropy of the Distribution
  295.      *
  296.      * @return The Differential Entropy of the Distribution
  297.      *
  298.      * @throws java.lang.Exception Thrown if the Entropy cannot be estimated
  299.      */

  300.     public double differentialEntropy()
  301.         throws java.lang.Exception
  302.     {
  303.         return org.drip.numerical.integration.NewtonCotesQuadratureGenerator.GaussLaguerreLeftDefinite (
  304.             0.,
  305.             10000
  306.         ).integrate (
  307.             new org.drip.function.definition.R1ToR1 (null)
  308.             {
  309.                 @Override public double evaluate (
  310.                     final double t)
  311.                     throws java.lang.Exception
  312.                 {
  313.                     double density = density (t);

  314.                     return density * java.lang.Math.log (density);
  315.                 }
  316.             }
  317.         );
  318.     }

  319.     /**
  320.      * Construct the Moment Generating Function
  321.      *
  322.      * @return The Moment Generating Function
  323.      */

  324.     public org.drip.function.definition.R1ToR1 momentGeneratingFunction()
  325.     {
  326.         return null;
  327.     }

  328.     /**
  329.      * Construct the Probability Generating Function
  330.      *
  331.      * @return The Probability Generating Function
  332.      */

  333.     public org.drip.function.definition.R1ToR1 probabilityGeneratingFunction()
  334.     {
  335.         return null;
  336.     }

  337.     /**
  338.      * Generate a Random Variable corresponding to the Distribution
  339.      *
  340.      * @return Random Variable corresponding to the Distribution
  341.      *
  342.      * @throws java.lang.Exception Thrown if the Random Instance cannot be estimated
  343.      */

  344.     public double random()
  345.         throws java.lang.Exception
  346.     {
  347.         return invCumulative (java.lang.Math.random());
  348.     }

  349.     /**
  350.      * Retrieve the Array of Generated Random Variables
  351.      *
  352.      * @param arrayCount Number of Elements
  353.      *
  354.      * @return Array of Generated Random Variables
  355.      */

  356.     public double[] randomArray (
  357.         final int arrayCount)
  358.     {
  359.         if (0 >= arrayCount)
  360.         {
  361.             return null;
  362.         }

  363.         double[] randomArray = new double[arrayCount];

  364.         for (int index = 0; index < arrayCount; ++index)
  365.         {
  366.             try
  367.             {
  368.                 randomArray[index] = random();
  369.             }
  370.             catch (java.lang.Exception e)
  371.             {
  372.                 e.printStackTrace();

  373.                 return null;
  374.             }
  375.         }

  376.         return randomArray;
  377.     }

  378.     /**
  379.      * Retrieve the Population Central Measures
  380.      *
  381.      * @return The Population Central Measures
  382.      */

  383.     public org.drip.measure.statistics.PopulationCentralMeasures populationCentralMeasures()
  384.     {
  385.         try
  386.         {
  387.             return new org.drip.measure.statistics.PopulationCentralMeasures (
  388.                 mean(),
  389.                 variance()
  390.             );
  391.         }
  392.         catch (java.lang.Exception e)
  393.         {
  394.             e.printStackTrace();
  395.         }

  396.         return null;
  397.     }

  398.     /**
  399.      * Retrieve the Univariate Weighted Histogram
  400.      *
  401.      * @return The Univariate Weighted Histogram
  402.      */

  403.     public org.drip.numerical.common.Array2D histogram()
  404.     {
  405.         return null;
  406.     }
  407. }