DiscreteInverseGamma.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.R1ShapeScaleDiscrete;
  5. import org.drip.measure.statistics.UnivariateDiscreteThin;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.service.env.EnvManager;
  8. import org.drip.specialfunction.digamma.CumulativeSeriesEstimator;
  9. import org.drip.specialfunction.gamma.EulerIntegralSecondKind;
  10. import org.drip.specialfunction.incompletegamma.LowerEulerIntegral;

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

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

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

  120. public class DiscreteInverseGamma
  121. {

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

  141.     private static final void StatisticsArray (
  142.         final UnivariateDiscreteThin ahrensDieterThinStatistics,
  143.         final UnivariateDiscreteThin marsagliaThinStatistics)
  144.         throws Exception
  145.     {
  146.         System.out.println (
  147.             "\t| Average => " + FormatUtil.FormatDouble (
  148.                 ahrensDieterThinStatistics.average(), 2, 6, 1.
  149.             ) + " | " + FormatUtil.FormatDouble (
  150.                 marsagliaThinStatistics.average(), 2, 6, 1.
  151.             ) + " ||"
  152.         );

  153.         System.out.println (
  154.             "\t| Error   => " + FormatUtil.FormatDouble (
  155.                 ahrensDieterThinStatistics.error(), 2, 6, 1.
  156.             ) + " | " + FormatUtil.FormatDouble (
  157.                 marsagliaThinStatistics.error(), 2, 6, 1.
  158.             ) + " ||"
  159.         );

  160.         System.out.println (
  161.             "\t| Maximum => " + FormatUtil.FormatDouble (
  162.                 ahrensDieterThinStatistics.maximum(), 2, 6, 1.
  163.             ) + " | " + FormatUtil.FormatDouble (
  164.                 marsagliaThinStatistics.maximum(), 2, 6, 1.
  165.             ) + " ||"
  166.         );

  167.         System.out.println (
  168.             "\t| Minimum => " + FormatUtil.FormatDouble (
  169.                 ahrensDieterThinStatistics.minimum(), 2, 6, 1.
  170.             ) + " | " + FormatUtil.FormatDouble (
  171.                 marsagliaThinStatistics.minimum(), 2, 6, 1.
  172.             ) + " ||"
  173.         );
  174.     }

  175.     private static final void GenerateAndComputeStatistics (
  176.         final R1ToR1 gammaEstimator,
  177.         final R1ToR1 digammaEstimator,
  178.         final R2ToR1 lowerIncompleteGammaEstimator,
  179.         final double k,
  180.         final double theta,
  181.         final int simulationCount)
  182.         throws Exception
  183.     {
  184.         double[] marsagliaRandomArray = new double[simulationCount];
  185.         double[] ahrensDieterRandomArray = new double[simulationCount];

  186.         R1ShapeScaleDiscrete ahrensDieterGammaDiscrete = new R1ShapeScaleDiscrete (
  187.             k,
  188.             theta,
  189.             gammaEstimator,
  190.             digammaEstimator,
  191.             lowerIncompleteGammaEstimator,
  192.             R1ShapeScaleDiscrete.DISCRETE_RANDOM_FROM_AHRENS_DIETER
  193.         );

  194.         R1ShapeScaleDiscrete marsagilaGammaDiscrete = new R1ShapeScaleDiscrete (
  195.             k,
  196.             theta,
  197.             gammaEstimator,
  198.             digammaEstimator,
  199.             lowerIncompleteGammaEstimator,
  200.             R1ShapeScaleDiscrete.DISCRETE_RANDOM_FROM_MARSAGLIA
  201.         );

  202.         for (int simulationIndex = 0;
  203.             simulationIndex < simulationCount;
  204.             ++simulationIndex)
  205.         {
  206.             marsagliaRandomArray[simulationIndex] = ahrensDieterGammaDiscrete.randomInverseGamma();

  207.             ahrensDieterRandomArray[simulationIndex] = marsagilaGammaDiscrete.randomInverseGamma();
  208.         }

  209.         System.out.println (
  210.             "\t|-------------------------------------------------||"
  211.         );

  212.         System.out.println (
  213.             "\t|      INVERSE GAMMA RANDOM NUMBER GENERATION     ||"
  214.         );

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

  218.         System.out.println (
  219.             "\t|          k     => " + k
  220.         );

  221.         System.out.println (
  222.             "\t|          theta => " + theta
  223.         );

  224.         System.out.println (
  225.             "\t|-------------------------------------------------||"
  226.         );

  227.         System.out.println (
  228.             "\t|          - Using Ahrens-Dieter (1982)           ||"
  229.         );

  230.         System.out.println (
  231.             "\t|          - Using Marsaglia (1977)               ||"
  232.         );

  233.         System.out.println (
  234.             "\t|-------------------------------------------------||"
  235.         );

  236.         StatisticsArray (
  237.             new UnivariateDiscreteThin (
  238.                 ahrensDieterRandomArray
  239.             ),
  240.             new UnivariateDiscreteThin (
  241.                 marsagliaRandomArray
  242.             )
  243.         );

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

  247.         System.out.println();
  248.     }

  249.     public static final void main (
  250.         final String[] argumentArray)
  251.         throws Exception
  252.     {
  253.         EnvManager.InitEnv (
  254.             ""
  255.         );

  256.         double[] kArray = {
  257.             1.5,
  258.             1.0,
  259.             0.5,
  260.         };
  261.         double[] thetaArray = {
  262.             0.5,
  263.             1.0,
  264.             2.0,
  265.         };
  266.         int simulationCount = 100000;
  267.         int digammaTermCount = 1000;

  268.         R1ToR1 gammaEstimator = new EulerIntegralSecondKind (
  269.             null
  270.         );

  271.         R2ToR1 lowerIncompleteGammaEstimator = LowerIncompleteGamma();

  272.         R1ToR1 digammaEstimator = CumulativeSeriesEstimator.AbramowitzStegun2007 (
  273.             digammaTermCount
  274.         );

  275.         for (double k : kArray)
  276.         {
  277.             for (double theta : thetaArray)
  278.             {
  279.                 GenerateAndComputeStatistics (
  280.                     gammaEstimator,
  281.                     digammaEstimator,
  282.                     lowerIncompleteGammaEstimator,
  283.                     k,
  284.                     theta,
  285.                     simulationCount
  286.                 );
  287.             }
  288.         }

  289.         EnvManager.TerminateEnv();
  290.     }
  291. }