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

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

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

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

  121. public class ConsistentInference
  122. {

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

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

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

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

  165.         return marsagliaRandomArray;
  166.     }

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

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

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

  197.         R2ToR1 lowerIncompleteGammaEstimator = LowerIncompleteGamma();

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

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

  202.         System.out.println ("\t|        SHAPE SCALE CONSISTENT INFERENCE        ||");

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

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

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

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

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

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

  209.         System.out.println ("\t|        OUT => Shape Bias Correction Factor     ||");

  210.         System.out.println ("\t|        OUT => Scale Bias Adjustment            ||");

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

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

  227.                 ShapeScaleParameters shapeScaleParameters = consistentEstimator.inferShapeScaleParameter();

  228.                 System.out.println (
  229.                     "\t| [" + FormatUtil.FormatDouble (k, 1, 2, 1., false) + " | " +
  230.                     FormatUtil.FormatDouble (theta, 1, 2, 1., false) + "] => {" +
  231.                     FormatUtil.FormatDouble (shapeScaleParameters.shape(), 1, 2, 1., false) + " | " +
  232.                     FormatUtil.FormatDouble (shapeScaleParameters.scale(), 1, 2, 1., false) + "} | {" +
  233.                     FormatUtil.FormatDouble (
  234.                         consistentEstimator.shapeBiasCorrectionAdjustment (
  235.                             shapeScaleParameters.shape()
  236.                         ), 1, 2, 1., false
  237.                     ) + " | " +
  238.                     FormatUtil.FormatDouble (
  239.                         consistentEstimator.scaleBiasCorrectionFactor(), 1, 2, 1., false
  240.                     ) + "} ||"
  241.                 );
  242.             }
  243.         }

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

  245.         EnvManager.TerminateEnv();
  246.     }
  247. }