MaxwellBoltzmannSquaredPDFEstimate.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.MaxwellBoltzmannSquaredDistribution;
  5. import org.drip.numerical.common.FormatUtil;
  6. import org.drip.service.env.EnvManager;
  7. import org.drip.specialfunction.digamma.CumulativeSeriesEstimator;
  8. import org.drip.specialfunction.gamma.EulerIntegralSecondKind;
  9. import org.drip.specialfunction.incompletegamma.LowerEulerIntegral;

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

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

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

  119. public class MaxwellBoltzmannSquaredPDFEstimate
  120. {

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

  140.     public static final void main (
  141.         final String[] argumentArray)
  142.         throws Exception
  143.     {
  144.         EnvManager.InitEnv (
  145.             ""
  146.         );

  147.         int digammaTermCount = 1000;
  148.         double[] tArray =
  149.         {
  150.              0.1,
  151.              1.0,
  152.              2.0,
  153.              3.0,
  154.              4.0,
  155.              5.0,
  156.              6.0,
  157.              7.0,
  158.              8.0,
  159.              9.0,
  160.             10.0,
  161.             12.0,
  162.         };
  163.         double[] aArray =
  164.         {
  165.              // 1,
  166.              2,
  167.              3,
  168.              4,
  169.              5,
  170.              6,
  171.              7,
  172.              8,
  173.         };
  174.         double[] pValueArray =
  175.         {
  176.              0.05,
  177.              0.10,
  178.              0.15,
  179.              0.20,
  180.              0.25,
  181.              0.30,
  182.              0.35,
  183.              0.40,
  184.              0.45,
  185.              0.50,
  186.              0.55,
  187.              0.60,
  188.              0.65,
  189.              0.70,
  190.              0.75,
  191.              0.80,
  192.              0.85,
  193.              0.90,
  194.              0.95,
  195.              0.99,
  196.         };

  197.         R1ToR1 gammaEstimator = new EulerIntegralSecondKind (
  198.             null
  199.         );

  200.         R2ToR1 lowerIncompleteGammaEstimator = LowerIncompleteGamma();

  201.         R1ToR1 digammaEstimator = CumulativeSeriesEstimator.AbramowitzStegun2007 (
  202.             digammaTermCount
  203.         );

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

  205.         System.out.println ("\t|                                               PROBABILITY DENSITY FUNCTION ESTIMATE                                             ||");

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

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

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

  209.         System.out.println ("\t|                - Values for different t                                                                                         ||");

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

  211.         for (double a : aArray)
  212.         {
  213.             MaxwellBoltzmannSquaredDistribution maxwellBoltzmannSquaredDistribution =
  214.                 new MaxwellBoltzmannSquaredDistribution (
  215.                     a,
  216.                     gammaEstimator,
  217.                     digammaEstimator,
  218.                     lowerIncompleteGammaEstimator
  219.                 );

  220.             String display = "\t| [" + FormatUtil.FormatDouble (a, 1, 0, 1., false) + "] =>";

  221.             for (double t : tArray)
  222.             {
  223.                 display = display + " " + FormatUtil.FormatDouble (
  224.                     maxwellBoltzmannSquaredDistribution.density (
  225.                         t
  226.                     ), 1, 5, 1., false
  227.                 ) + " |";
  228.             }

  229.             System.out.println (display + "|");
  230.         }

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

  232.         System.out.println();

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

  234.         System.out.println ("\t|                                             CUMULATIVE DISTRIBUTION FUNCTION ESTIMATE                                           ||");

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

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

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

  238.         System.out.println ("\t|                - Values for different t                                                                                         ||");

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

  240.         for (double a : aArray)
  241.         {
  242.             MaxwellBoltzmannSquaredDistribution maxwellBoltzmannSquaredDistribution =
  243.                 new MaxwellBoltzmannSquaredDistribution (
  244.                     a,
  245.                     gammaEstimator,
  246.                     digammaEstimator,
  247.                     lowerIncompleteGammaEstimator
  248.                 );

  249.             String display = "\t| [" + FormatUtil.FormatDouble (a, 1, 0, 1., false) + "] =>";

  250.             for (double t : tArray)
  251.             {
  252.                 display = display + " " + FormatUtil.FormatDouble (
  253.                     maxwellBoltzmannSquaredDistribution.cumulative (t), 1, 5, 1., false
  254.                 ) + " |";
  255.             }

  256.             System.out.println (display + "|");
  257.         }

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

  259.         System.out.println();

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

  261.         System.out.println ("\t|                                         INVERSE CUMULATIVE DISTRIBUTION FUNCTION ESTIMATE                                       ||");

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

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

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

  265.         System.out.println ("\t|                - Values for different p                                                                                         ||");

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

  267.         for (double a : aArray)
  268.         {
  269.             MaxwellBoltzmannSquaredDistribution maxwellBoltzmannSquaredDistribution =
  270.                 new MaxwellBoltzmannSquaredDistribution (
  271.                     a,
  272.                     gammaEstimator,
  273.                     digammaEstimator,
  274.                     lowerIncompleteGammaEstimator
  275.                 );

  276.             String display = "\t| [" + FormatUtil.FormatDouble (a, 1, 0, 1., false) + "] =>";

  277.             for (double p : pValueArray)
  278.             {
  279.                 display = display + " " + FormatUtil.FormatDouble (
  280.                     maxwellBoltzmannSquaredDistribution.invCumulative (p), 1, 2, 1., false
  281.                 ) + " |";
  282.             }

  283.             System.out.println (display + "|");
  284.         }

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

  286.         EnvManager.TerminateEnv();
  287.     }
  288. }