ShapeScalePDFEstimate.java

  1. package org.drip.sample.gammadistribution;

  2. import org.drip.function.definition.R1ToR1;
  3. import org.drip.function.definition.R2ToR1;
  4. import org.drip.measure.gamma.R1ShapeScaleDistribution;
  5. import org.drip.numerical.common.FormatUtil;
  6. import org.drip.service.env.EnvManager;
  7. import org.drip.specialfunction.digamma.CumulativeSeriesEstimator;
  8. import org.drip.specialfunction.gamma.EulerIntegralSecondKind;
  9. import org.drip.specialfunction.incompletegamma.LowerEulerIntegral;

  10. /*
  11.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  12.  */

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

  82. /**
  83.  * <i>ShapeScalePDFEstimate</i> demonstrates the Construction and Analysis of the R<sup>1</sup> Gamma
  84.  *  Distribution using the Shape/Scale Parameterization. The References are:
  85.  *
  86.  * <br><br>
  87.  *  <ul>
  88.  *      <li>
  89.  *          Devroye, L. (1986): <i>Non-Uniform Random Variate Generation</i> <b>Springer-Verlag</b> New York
  90.  *      </li>
  91.  *      <li>
  92.  *          Gamma Distribution (2019): Gamma Distribution
  93.  *              https://en.wikipedia.org/wiki/Chi-squared_distribution
  94.  *      </li>
  95.  *      <li>
  96.  *          Louzada, F., P. L. Ramos, and E. Ramos (2019): A Note on Bias of Closed-Form Estimators for the
  97.  *              Gamma Distribution Derived From Likelihood Equations <i>The American Statistician</i> <b>73
  98.  *              (2)</b> 195-199
  99.  *      </li>
  100.  *      <li>
  101.  *          Minka, T. (2002): Estimating a Gamma distribution https://tminka.github.io/papers/minka-gamma.pdf
  102.  *      </li>
  103.  *      <li>
  104.  *          Ye, Z. S., and N. Chen (2017): Closed-Form Estimators for the Gamma Distribution Derived from
  105.  *              Likelihood Equations <i>The American Statistician</i> <b>71 (2)</b> 177-181
  106.  *      </li>
  107.  *  </ul>
  108.  *
  109.  *  <br><br>
  110.  *  <ul>
  111.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  112.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  113.  *      <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>
  114.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/dynamics/README.md">R<sup>1</sup> Gamma Distribution Implementation/Properties</a></li>
  115.  *  </ul>
  116.  *
  117.  * @author Lakshmi Krishnamurthy
  118.  */

  119. public class ShapeScalePDFEstimate
  120. {

  121.     private static final R2ToR1 LowerIncompleteGamma()
  122.         throws Exception
  123.     {
  124.         return new R2ToR1()
  125.         {
  126.             @Override public double evaluate (
  127.                 final double s,
  128.                 final double t)
  129.                 throws Exception
  130.             {
  131.                 return new LowerEulerIntegral (
  132.                     null,
  133.                     t
  134.                 ).evaluate (
  135.                     s
  136.                 );
  137.             }
  138.         };
  139.     }

  140.     public static final void main (
  141.         final String[] argumentArray)
  142.         throws Exception
  143.     {
  144.         EnvManager.InitEnv (
  145.             ""
  146.         );

  147.         int digammaTermCount = 1000;
  148.         double[] thetaArray =
  149.         {
  150.              1.0,
  151.              2.0,
  152.              3.0,
  153.              4.0,
  154.              5.0,
  155.         };
  156.         double[] tArray =
  157.         {
  158.              0.1,
  159.              1.0,
  160.              2.0,
  161.              3.0,
  162.              4.0,
  163.              5.0,
  164.              6.0,
  165.              7.0,
  166.              8.0,
  167.              9.0,
  168.             10.0,
  169.             12.0,
  170.         };
  171.         int[] kArray =
  172.         {
  173.              // 1,
  174.              2,
  175.              3,
  176.              4,
  177.              5,
  178.              6,
  179.              7,
  180.              8,
  181.         };
  182.         double[] pValueArray =
  183.         {
  184.              0.05,
  185.              0.10,
  186.              0.15,
  187.              0.20,
  188.              0.25,
  189.              0.30,
  190.              0.35,
  191.              0.40,
  192.              0.45,
  193.              0.50,
  194.              0.55,
  195.              0.60,
  196.              0.65,
  197.              0.70,
  198.              0.75,
  199.              0.80,
  200.              0.85,
  201.              0.90,
  202.              0.95,
  203.              0.99,
  204.         };

  205.         R1ToR1 gammaEstimator = new EulerIntegralSecondKind (
  206.             null
  207.         );

  208.         R2ToR1 lowerIncompleteGammaEstimator = LowerIncompleteGamma();

  209.         R1ToR1 digammaEstimator = CumulativeSeriesEstimator.AbramowitzStegun2007 (
  210.             digammaTermCount
  211.         );

  212.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------||");

  213.         System.out.println ("\t|                                               PROBABILITY DENSITY FUNCTION ESTIMATE                                             ||");

  214.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------||");

  215.         System.out.println ("\t|        L -> R:                                                                                                                  ||");

  216.         System.out.println ("\t|                - Shape, Scale                                                                                                   ||");

  217.         System.out.println ("\t|                - Values for different t                                                                                         ||");

  218.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------||");

  219.         for (double theta : thetaArray)
  220.         {
  221.             for (int k : kArray)
  222.             {
  223.                 R1ShapeScaleDistribution gammaDistribution = R1ShapeScaleDistribution.Standard (
  224.                     k,
  225.                     theta,
  226.                     gammaEstimator,
  227.                     digammaEstimator,
  228.                     lowerIncompleteGammaEstimator
  229.                 );

  230.                 String display = "\t| [" +
  231.                     FormatUtil.FormatDouble (k, 1, 0, 1., false) + ", " +
  232.                     FormatUtil.FormatDouble (theta, 1, 0, 1., false) +
  233.                 "] =>";

  234.                 for (double t : tArray)
  235.                 {
  236.                     display = display + " " + FormatUtil.FormatDouble (
  237.                         gammaDistribution.density (
  238.                             t
  239.                         ), 1, 5, 1., false
  240.                     ) + " |";
  241.                 }

  242.                 System.out.println (display + "|");
  243.             }
  244.         }

  245.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------||");

  246.         System.out.println();

  247.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------||");

  248.         System.out.println ("\t|                                             CUMULATIVE DISTRIBUTION FUNCTION ESTIMATE                                           ||");

  249.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------||");

  250.         System.out.println ("\t|        L -> R:                                                                                                                  ||");

  251.         System.out.println ("\t|                - Shape, Scale                                                                                                   ||");

  252.         System.out.println ("\t|                - Values for different t                                                                                         ||");

  253.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------||");

  254.         for (double theta : thetaArray)
  255.         {
  256.             for (int k : kArray)
  257.             {
  258.                 R1ShapeScaleDistribution gammaDistribution = R1ShapeScaleDistribution.Standard (
  259.                     k,
  260.                     theta,
  261.                     gammaEstimator,
  262.                     digammaEstimator,
  263.                     lowerIncompleteGammaEstimator
  264.                 );

  265.                 String display = "\t| [" +
  266.                     FormatUtil.FormatDouble (k, 1, 0, 1., false) + ", " +
  267.                     FormatUtil.FormatDouble (theta, 1, 0, 1., false) +
  268.                 "] =>";

  269.                 for (double t : tArray)
  270.                 {
  271.                     display = display + " " + FormatUtil.FormatDouble (
  272.                         gammaDistribution.cumulative (t), 1, 5, 1., false
  273.                     ) + " |";
  274.                 }

  275.                 System.out.println (display + "|");
  276.             }
  277.         }

  278.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------||");

  279.         System.out.println();

  280.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------||");

  281.         System.out.println ("\t|                                         INVERSE CUMULATIVE DISTRIBUTION FUNCTION ESTIMATE                                       ||");

  282.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------||");

  283.         System.out.println ("\t|        L -> R:                                                                                                                  ||");

  284.         System.out.println ("\t|                - Shape, Scale                                                                                                   ||");

  285.         System.out.println ("\t|                - Values for different p                                                                                         ||");

  286.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------||");

  287.         for (double theta : thetaArray)
  288.         {
  289.             for (int k : kArray)
  290.             {
  291.                 R1ShapeScaleDistribution gammaDistribution = R1ShapeScaleDistribution.Standard (
  292.                     k,
  293.                     theta,
  294.                     gammaEstimator,
  295.                     digammaEstimator,
  296.                     lowerIncompleteGammaEstimator
  297.                 );

  298.                 String display = "\t| [" +
  299.                     FormatUtil.FormatDouble (k, 1, 0, 1., false) + ", " +
  300.                     FormatUtil.FormatDouble (theta, 1, 0, 1., false) +
  301.                 "] =>";

  302.                 for (double p : pValueArray)
  303.                 {
  304.                     display = display + " " + FormatUtil.FormatDouble (
  305.                         gammaDistribution.invCumulative (p), 1, 2, 1., false
  306.                     ) + " |";
  307.                 }

  308.                 System.out.println (display + "|");
  309.             }
  310.         }

  311.         System.out.println ("\t|---------------------------------------------------------------------------------------------------------------------------------||");

  312.         EnvManager.TerminateEnv();
  313.     }
  314. }