MaximumLikelihoodInference.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.R1MaximumLikelihoodEstimator;
  5. import org.drip.measure.gamma.R1ParameterEstimator;
  6. import org.drip.measure.gamma.R1ShapeScaleDistribution;
  7. import org.drip.measure.gamma.ShapeScaleParameters;
  8. import org.drip.numerical.common.FormatUtil;
  9. import org.drip.service.env.EnvManager;
  10. import org.drip.specialfunction.digamma.CumulativeSeriesEstimator;
  11. import org.drip.specialfunction.gamma.EulerIntegralSecondKind;
  12. import org.drip.specialfunction.incompletegamma.LowerEulerIntegral;

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

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

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

  122. public class MaximumLikelihoodInference
  123. {

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

  143.     private static final double[] Generate (
  144.         final R1ToR1 gammaEstimator,
  145.         final R1ToR1 digammaEstimator,
  146.         final R2ToR1 lowerIncompleteGammaEstimator,
  147.         final double k,
  148.         final double theta,
  149.         final int simulationCount)
  150.         throws Exception
  151.     {
  152.         double[] marsagliaRandomArray = new double[simulationCount];

  153.         R1ShapeScaleDistribution gammaDistribution = R1ShapeScaleDistribution.Standard (
  154.             k,
  155.             theta,
  156.             gammaEstimator,
  157.             digammaEstimator,
  158.             lowerIncompleteGammaEstimator
  159.         );

  160.         for (int simulationIndex = 0;
  161.             simulationIndex < simulationCount;
  162.             ++simulationIndex)
  163.         {
  164.             marsagliaRandomArray[simulationIndex] = gammaDistribution.randomMarsaglia1977();
  165.         }

  166.         return marsagliaRandomArray;
  167.     }

  168.     public static final void main (
  169.         final String[] argumentArray)
  170.         throws Exception
  171.     {
  172.         EnvManager.InitEnv (
  173.             ""
  174.         );

  175.         double[] kArray = {
  176.             0.50,
  177.             1.00,
  178.             1.50,
  179.             2.00,
  180.             2.50,
  181.             3.00,
  182.             3.50,
  183.         };
  184.         double[] thetaArray = {
  185.             0.50,
  186.             1.00,
  187.             1.50,
  188.             2.00,
  189.             2.50,
  190.             3.00,
  191.             3.50,
  192.         };
  193.         int simulationCount = 100000;
  194.         int digammaTermCount = 1000;

  195.         R1ToR1 gammaEstimator = new EulerIntegralSecondKind (
  196.             null
  197.         );

  198.         R2ToR1 lowerIncompleteGammaEstimator = LowerIncompleteGamma();

  199.         R1ToR1 digammaEstimator = CumulativeSeriesEstimator.AbramowitzStegun2007 (
  200.             digammaTermCount
  201.         );

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

  203.         System.out.println ("\t|   SHAPE SCALE MLE INFERENCE    ||");

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

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

  206.         System.out.println ("\t|        IN => Shape             ||");

  207.         System.out.println ("\t|        IN => Scale             ||");

  208.         System.out.println ("\t|        OUT => Shape            ||");

  209.         System.out.println ("\t|        OUT => Scale            ||");

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

  211.         for (double k : kArray)
  212.         {
  213.             for (double theta : thetaArray)
  214.             {
  215.                 R1ParameterEstimator parameterEstimator =
  216.                     R1MaximumLikelihoodEstimator.FromRealizationArray (
  217.                         Generate (
  218.                             gammaEstimator,
  219.                             digammaEstimator,
  220.                             lowerIncompleteGammaEstimator,
  221.                             k,
  222.                             theta,
  223.                             simulationCount
  224.                         )
  225.                     );

  226.                 ShapeScaleParameters shapeScaleParameters = parameterEstimator.inferShapeScaleParameter();

  227.                 System.out.println (
  228.                     "\t| [" + FormatUtil.FormatDouble (k, 1, 2, 1., false) + " | " +
  229.                     FormatUtil.FormatDouble (theta, 1, 2, 1., false) + "] => {" +
  230.                     FormatUtil.FormatDouble (shapeScaleParameters.shape(), 1, 2, 1., false) + " | " +
  231.                     FormatUtil.FormatDouble (shapeScaleParameters.scale(), 1, 2, 1., false) + "} ||"
  232.                 );
  233.             }
  234.         }

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

  236.         EnvManager.TerminateEnv();
  237.     }
  238. }