DiscreteRandomGenerationScheme.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.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>DiscreteRandomGenerationScheme</i> illustrates the Generation of Discrete Random Gamma Numbers
  85.  *  following the R<sup>1</sup> Gamma Distribution using a variety of 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 DiscreteRandomGenerationScheme
  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 inverseCDFThinStatistics,
  143.         final UnivariateDiscreteThin ahrensDieterThinStatistics,
  144.         final UnivariateDiscreteThin marsagliaThinStatistics)
  145.         throws Exception
  146.     {
  147.         System.out.println (
  148.             "\t| Average => " + FormatUtil.FormatDouble (
  149.                 inverseCDFThinStatistics.average(), 2, 6, 1.
  150.             ) + " | " + FormatUtil.FormatDouble (
  151.                 ahrensDieterThinStatistics.average(), 2, 6, 1.
  152.             ) + " | " + FormatUtil.FormatDouble (
  153.                 marsagliaThinStatistics.average(), 2, 6, 1.
  154.             ) + " ||"
  155.         );

  156.         System.out.println (
  157.             "\t| Error   => " + FormatUtil.FormatDouble (
  158.                 inverseCDFThinStatistics.error(), 2, 6, 1.
  159.             ) + " | " + FormatUtil.FormatDouble (
  160.                 ahrensDieterThinStatistics.error(), 2, 6, 1.
  161.             ) + " | " + FormatUtil.FormatDouble (
  162.                 marsagliaThinStatistics.error(), 2, 6, 1.
  163.             ) + " ||"
  164.         );

  165.         System.out.println (
  166.             "\t| Maximum => " + FormatUtil.FormatDouble (
  167.                 inverseCDFThinStatistics.maximum(), 2, 6, 1.
  168.             ) + " | " + FormatUtil.FormatDouble (
  169.                 ahrensDieterThinStatistics.maximum(), 2, 6, 1.
  170.             ) + " | " + FormatUtil.FormatDouble (
  171.                 marsagliaThinStatistics.maximum(), 2, 6, 1.
  172.             ) + " ||"
  173.         );

  174.         System.out.println (
  175.             "\t| Minimum => " + FormatUtil.FormatDouble (
  176.                 inverseCDFThinStatistics.minimum(), 2, 6, 1.
  177.             ) + " | " + FormatUtil.FormatDouble (
  178.                 ahrensDieterThinStatistics.minimum(), 2, 6, 1.
  179.             ) + " | " + FormatUtil.FormatDouble (
  180.                 marsagliaThinStatistics.minimum(), 2, 6, 1.
  181.             ) + " ||"
  182.         );
  183.     }

  184.     private static final void StatisticsArray2 (
  185.         final UnivariateDiscreteThin ahrensDieterThinStatistics,
  186.         final UnivariateDiscreteThin marsagliaThinStatistics)
  187.         throws Exception
  188.     {
  189.         System.out.println (
  190.             "\t| Average => " + FormatUtil.FormatDouble (
  191.                 ahrensDieterThinStatistics.average(), 2, 6, 1.
  192.             ) + " | " + FormatUtil.FormatDouble (
  193.                 marsagliaThinStatistics.average(), 2, 6, 1.
  194.             ) + " ||"
  195.         );

  196.         System.out.println (
  197.             "\t| Error   => " + FormatUtil.FormatDouble (
  198.                 ahrensDieterThinStatistics.error(), 2, 6, 1.
  199.             ) + " | " + FormatUtil.FormatDouble (
  200.                 marsagliaThinStatistics.error(), 2, 6, 1.
  201.             ) + " ||"
  202.         );

  203.         System.out.println (
  204.             "\t| Maximum => " + FormatUtil.FormatDouble (
  205.                 ahrensDieterThinStatistics.maximum(), 2, 6, 1.
  206.             ) + " | " + FormatUtil.FormatDouble (
  207.                 marsagliaThinStatistics.maximum(), 2, 6, 1.
  208.             ) + " ||"
  209.         );

  210.         System.out.println (
  211.             "\t| Minimum => " + FormatUtil.FormatDouble (
  212.                 ahrensDieterThinStatistics.minimum(), 2, 6, 1.
  213.             ) + " | " + FormatUtil.FormatDouble (
  214.                 marsagliaThinStatistics.minimum(), 2, 6, 1.
  215.             ) + " ||"
  216.         );
  217.     }

  218.     private static final void GenerateAndComputeStatistics (
  219.         final R1ToR1 gammaEstimator,
  220.         final R1ToR1 digammaEstimator,
  221.         final R2ToR1 lowerIncompleteGammaEstimator,
  222.         final double k,
  223.         final double theta,
  224.         final int simulationCount)
  225.         throws Exception
  226.     {
  227.         double[] marsagliaRandomArray = new double[simulationCount];
  228.         double[] inverseCDFRandomArray = new double[simulationCount];
  229.         double[] ahrensDieterRandomArray = new double[simulationCount];

  230.         R1ShapeScaleDistribution gammaDistribution = R1ShapeScaleDistribution.Standard (
  231.             k,
  232.             theta,
  233.             gammaEstimator,
  234.             digammaEstimator,
  235.             lowerIncompleteGammaEstimator
  236.         );

  237.         for (int simulationIndex = 0;
  238.             simulationIndex < simulationCount;
  239.             ++simulationIndex)
  240.         {
  241.             if (k > 1)
  242.             {
  243.                 inverseCDFRandomArray[simulationIndex] = gammaDistribution.random();
  244.             }

  245.             marsagliaRandomArray[simulationIndex] = gammaDistribution.randomMarsaglia1977();

  246.             ahrensDieterRandomArray[simulationIndex] = gammaDistribution.randomAhrensDieter1982();
  247.         }

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

  251.         System.out.println (
  252.             "\t|          GAMMA RANDOM NUMBER GENERATION         ||"
  253.         );

  254.         System.out.println (
  255.             "\t|-------------------------------------------------||"
  256.         );

  257.         System.out.println (
  258.             "\t|          k     => " + k
  259.         );

  260.         System.out.println (
  261.             "\t|          theta => " + theta
  262.         );

  263.         System.out.println (
  264.             "\t|-------------------------------------------------||"
  265.         );

  266.         if (k > 1)
  267.         {
  268.             System.out.println (
  269.                 "\t|          - From Uniform CDF                     ||"
  270.             );

  271.             System.out.println (
  272.                 "\t|          - Using Ahrens-Dieter (1982)           ||"
  273.             );

  274.             System.out.println (
  275.                 "\t|          - Using Marsaglia (1977)               ||"
  276.             );

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

  280.             StatisticsArray (
  281.                 new UnivariateDiscreteThin (
  282.                     inverseCDFRandomArray
  283.                 ),
  284.                 new UnivariateDiscreteThin (
  285.                     ahrensDieterRandomArray
  286.                 ),
  287.                 new UnivariateDiscreteThin (
  288.                     marsagliaRandomArray
  289.                 )
  290.             );
  291.         }
  292.         else
  293.         {
  294.             System.out.println (
  295.                 "\t|          - Using Ahrens-Dieter (1982)           ||"
  296.             );

  297.             System.out.println (
  298.                 "\t|          - Using Marsaglia (1977)               ||"
  299.             );

  300.             System.out.println (
  301.                 "\t|-------------------------------------------------||"
  302.             );

  303.             StatisticsArray2 (
  304.                 new UnivariateDiscreteThin (
  305.                     ahrensDieterRandomArray
  306.                 ),
  307.                 new UnivariateDiscreteThin (
  308.                     marsagliaRandomArray
  309.                 )
  310.             );
  311.         }

  312.         System.out.println (
  313.             "\t|-------------------------------------------------||"
  314.         );
  315.     }

  316.     public static final void main (
  317.         final String[] argumentArray)
  318.         throws Exception
  319.     {
  320.         EnvManager.InitEnv (
  321.             ""
  322.         );

  323.         double[] kArray = {
  324.             1.5,
  325.             0.5,
  326.         };
  327.         double[] thetaArray = {
  328.             2.0,
  329.         };
  330.         int simulationCount = 1000;
  331.         int digammaTermCount = 1000;

  332.         R1ToR1 gammaEstimator = new EulerIntegralSecondKind (
  333.             null
  334.         );

  335.         R2ToR1 lowerIncompleteGammaEstimator = LowerIncompleteGamma();

  336.         R1ToR1 digammaEstimator = CumulativeSeriesEstimator.AbramowitzStegun2007 (
  337.             digammaTermCount
  338.         );

  339.         for (double k : kArray)
  340.         {
  341.             for (double theta : thetaArray)
  342.             {
  343.                 GenerateAndComputeStatistics (
  344.                     gammaEstimator,
  345.                     digammaEstimator,
  346.                     lowerIncompleteGammaEstimator,
  347.                     k,
  348.                     theta,
  349.                     simulationCount
  350.                 );
  351.             }
  352.         }

  353.         EnvManager.TerminateEnv();
  354.     }
  355. }