DiscreteGeneralizedGamma.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>DiscreteGeneralizedGamma</i> illustrates the Generation of Discrete Generalized Gamma Random Numbers
  85.  *  using the 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 DiscreteGeneralizedGamma
  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 q,
  180.         final double k,
  181.         final double theta,
  182.         final int simulationCount)
  183.         throws Exception
  184.     {
  185.         double[] marsagliaRandomArray = new double[simulationCount];
  186.         double[] ahrensDieterRandomArray = new double[simulationCount];

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

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

  203.         for (int simulationIndex = 0;
  204.             simulationIndex < simulationCount;
  205.             ++simulationIndex)
  206.         {
  207.             marsagliaRandomArray[simulationIndex] = ahrensDieterGammaDiscrete.randomGeneralizedGamma (
  208.                 q
  209.             );

  210.             ahrensDieterRandomArray[simulationIndex] = marsagilaGammaDiscrete.randomGeneralizedGamma (
  211.                 q
  212.             );
  213.         }

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

  217.         System.out.println (
  218.             "\t|    GENERALIZED GAMMA RANDOM NUMBER GENERATION   ||"
  219.         );

  220.         System.out.println (
  221.             "\t|-------------------------------------------------||"
  222.         );

  223.         System.out.println (
  224.             "\t|          q     => " + q
  225.         );

  226.         System.out.println (
  227.             "\t|          k     => " + k
  228.         );

  229.         System.out.println (
  230.             "\t|          theta => " + theta
  231.         );

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

  235.         System.out.println (
  236.             "\t|          - Using Ahrens-Dieter (1982)           ||"
  237.         );

  238.         System.out.println (
  239.             "\t|          - Using Marsaglia (1977)               ||"
  240.         );

  241.         System.out.println (
  242.             "\t|-------------------------------------------------||"
  243.         );

  244.         StatisticsArray (
  245.             new UnivariateDiscreteThin (
  246.                 ahrensDieterRandomArray
  247.             ),
  248.             new UnivariateDiscreteThin (
  249.                 marsagliaRandomArray
  250.             )
  251.         );

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

  255.         System.out.println();
  256.     }

  257.     public static final void main (
  258.         final String[] argumentArray)
  259.         throws Exception
  260.     {
  261.         EnvManager.InitEnv (
  262.             ""
  263.         );

  264.         double[] qArray = {
  265.             1.5,
  266.             0.5,
  267.         };
  268.         double[] kArray = {
  269.             1.5,
  270.             1.0,
  271.             0.5,
  272.         };
  273.         double[] thetaArray = {
  274.             0.5,
  275.             1.0,
  276.             2.0,
  277.         };
  278.         int simulationCount = 100000;
  279.         int digammaTermCount = 1000;

  280.         R1ToR1 gammaEstimator = new EulerIntegralSecondKind (
  281.             null
  282.         );

  283.         R2ToR1 lowerIncompleteGammaEstimator = LowerIncompleteGamma();

  284.         R1ToR1 digammaEstimator = CumulativeSeriesEstimator.AbramowitzStegun2007 (
  285.             digammaTermCount
  286.         );

  287.         for (double q : qArray)
  288.         {
  289.             for (double k : kArray)
  290.             {
  291.                 for (double theta : thetaArray)
  292.                 {
  293.                     GenerateAndComputeStatistics (
  294.                         gammaEstimator,
  295.                         digammaEstimator,
  296.                         lowerIncompleteGammaEstimator,
  297.                         q,
  298.                         k,
  299.                         theta,
  300.                         simulationCount
  301.                     );
  302.                 }
  303.             }
  304.         }

  305.         EnvManager.TerminateEnv();
  306.     }
  307. }