ShapeScaleCentralMeasureEstimate.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>ShapeScaleCentralMeasureEstimate</i> demonstrates the Central Measures Estimation of the R<sup>1</sup>
  84.  *  Gamma 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 ShapeScaleCentralMeasureEstimate
  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 (s);
  135.             }
  136.         };
  137.     }

  138.     public static final void main (
  139.         final String[] argumentArray)
  140.         throws Exception
  141.     {
  142.         EnvManager.InitEnv ("");

  143.         int digammaTermCount = 1000;
  144.         double[] thetaArray =
  145.         {
  146.              1.0,
  147.              2.0,
  148.              3.0,
  149.              4.0,
  150.              5.0,
  151.         };
  152.         int[] kArray =
  153.         {
  154.              // 1,
  155.              2,
  156.              3,
  157.              4,
  158.              5,
  159.              6,
  160.              7,
  161.              8,
  162.         };

  163.         R1ToR1 gammaEstimator = new EulerIntegralSecondKind (null);

  164.         R2ToR1 lowerIncompleteGammaEstimator = LowerIncompleteGamma();

  165.         R1ToR1 digammaEstimator = CumulativeSeriesEstimator.AbramowitzStegun2007 (digammaTermCount);

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

  167.         System.out.println ("\t|                     SHAPE SCALE GAMMA DISTRIBUTION MEASURES ESTIMATE                     ||");

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

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

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

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

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

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

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

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

  176.         System.out.println ("\t|                - Excess Kurtosis                                                         ||");

  177.         System.out.println ("\t|                - Differential Entropy                                                    ||");

  178.         System.out.println ("\t|                - Logarithmic Expectation                                                 ||");

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

  180.         for (double theta : thetaArray)
  181.         {
  182.             for (int k : kArray)
  183.             {
  184.                 R1ShapeScaleDistribution gammaDistribution = R1ShapeScaleDistribution.Standard (
  185.                     k,
  186.                     theta,
  187.                     gammaEstimator,
  188.                     digammaEstimator,
  189.                     lowerIncompleteGammaEstimator
  190.                 );

  191.                 String display = "\t| [" +
  192.                     FormatUtil.FormatDouble (k, 1, 0, 1., false) + ", " +
  193.                     FormatUtil.FormatDouble (theta, 1, 0, 1., false) +
  194.                 "] =>";

  195.                 display = display + " " + FormatUtil.FormatDouble (
  196.                     gammaDistribution.mean(), 2, 2, 1., false
  197.                 ) + " | " + FormatUtil.FormatDouble (
  198.                     gammaDistribution.median(), 2, 6, 1., false
  199.                 ) + " | " + FormatUtil.FormatDouble (
  200.                     gammaDistribution.mode(), 2, 2, 1., false
  201.                 ) + " | " + FormatUtil.FormatDouble (
  202.                     gammaDistribution.variance(), 3, 2, 1., false
  203.                 ) + " | " + FormatUtil.FormatDouble (
  204.                     gammaDistribution.skewness(), 1, 6, 1., false
  205.                 ) + " | " + FormatUtil.FormatDouble (
  206.                     gammaDistribution.excessKurtosis(), 1, 6, 1., false
  207.                 ) + " | " + FormatUtil.FormatDouble (
  208.                     gammaDistribution.differentialEntropy(), 1, 6, 1., false
  209.                 ) + " | " + FormatUtil.FormatDouble (
  210.                     gammaDistribution.logarithmicExpectation(), 1, 6, 1., false
  211.                 ) + " |";

  212.                 System.out.println (display + "|");
  213.             }
  214.         }

  215.         System.out.println ("\t|------------------------------------------------------------------------------------------||");
  216.     }
  217. }