CentralPDFEstimate.java

  1. package org.drip.sample.chisquaredistribution;

  2. import org.drip.function.definition.R1ToR1;
  3. import org.drip.function.definition.R2ToR1;
  4. import org.drip.measure.chisquare.R1Central;
  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>CentralPDFEstimate</i> illustrates the Construction and the Usage of a Central Chi-squared
  84.  * Distribution. The References are:
  85.  *
  86.  * <br><br>
  87.  *  <ul>
  88.  *      <li>
  89.  *          Abramowitz, M., and I. A. Stegun (2007): <i>Handbook of Mathematics Functions</i> <b>Dover Book
  90.  *              on Mathematics</b>
  91.  *      </li>
  92.  *      <li>
  93.  *          Backstrom, T., and J. Fischer (2018): Fast Randomization for Distributed Low Bit-rate Coding of
  94.  *              Speech and Audio <i>IEEE/ACM Transactions on Audio, Speech, and Language Processing</i> <b>26
  95.  *              (1)</b> 19-30
  96.  *      </li>
  97.  *      <li>
  98.  *          Chi-Squared Distribution (2019): Chi-Squared Function
  99.  *              https://en.wikipedia.org/wiki/Chi-squared_distribution
  100.  *      </li>
  101.  *      <li>
  102.  *          Johnson, N. L., S. Kotz, and N. Balakrishnan (1994): <i>Continuous Univariate Distributions
  103.  *              2<sup>nd</sup> Edition</i> <b>John Wiley and Sons</b>
  104.  *      </li>
  105.  *      <li>
  106.  *          National Institute of Standards and Technology (2019): Chi-Squared Distribution
  107.  *              https://www.itl.nist.gov/div898/handbook/eda/section3/eda3666.htm
  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/sample/README.md">DROP API Construction and Usage</a></li>
  116.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/chisquaredistribution/README.md">Chi-Square Distribution Usage/Properties</a></li>
  117.  *  </ul>
  118.  *
  119.  * @author Lakshmi Krishnamurthy
  120.  */

  121. public class CentralPDFEstimate
  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 (s);
  137.             }
  138.         };
  139.     }

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

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

  188.         R1ToR1 gammaEstimator = new EulerIntegralSecondKind (null);

  189.         R2ToR1 lowerIncompleteGammaEstimator = LowerIncompleteGamma();

  190.         R1ToR1 digammaEstimator = CumulativeSeriesEstimator.AbramowitzStegun2007 (digammaTermCount);

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

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

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

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

  195.         System.out.println ("\t|                - Degrees of Freedom                                                                                          ||");

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

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

  198.         for (int dof : dofArray)
  199.         {
  200.             R1Central r1UnivariateScaledExponential = new R1Central (
  201.                 dof,
  202.                 gammaEstimator,
  203.                 digammaEstimator,
  204.                 lowerIncompleteGammaEstimator
  205.             );

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

  207.             for (double t : tArray)
  208.             {
  209.                 display = display + " " + FormatUtil.FormatDouble (
  210.                     r1UnivariateScaledExponential.density (t), 1, 5, 1., false
  211.                 ) + " |";
  212.             }

  213.             System.out.println (display + "|");
  214.         }

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

  216.         System.out.println();

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

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

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

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

  221.         System.out.println ("\t|                - Degrees of Freedom                                                                                          ||");

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

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

  224.         for (int dof : dofArray)
  225.         {
  226.             R1Central r1UnivariateScaledExponential = new R1Central (
  227.                 dof,
  228.                 gammaEstimator,
  229.                 digammaEstimator,
  230.                 lowerIncompleteGammaEstimator
  231.             );

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

  233.             for (double t : tArray)
  234.             {
  235.                 display = display + " " + FormatUtil.FormatDouble (
  236.                     r1UnivariateScaledExponential.cumulative (t), 1, 5, 1., false
  237.                 ) + " |";
  238.             }

  239.             System.out.println (display + "|");
  240.         }

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

  242.         System.out.println();

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

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

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

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

  247.         System.out.println ("\t|                - Degrees of Freedom                                                                                          ||");

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

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

  250.         for (int dof : dofArray)
  251.         {
  252.             R1Central r1UnivariateScaledExponential = new R1Central (
  253.                 dof,
  254.                 gammaEstimator,
  255.                 digammaEstimator,
  256.                 lowerIncompleteGammaEstimator
  257.             );

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

  259.             for (double p : pValueArray)
  260.             {
  261.                 display = display + " " + FormatUtil.FormatDouble (
  262.                     r1UnivariateScaledExponential.invCumulative (p), 1, 5, 1., false
  263.                 ) + " |";
  264.             }

  265.             System.out.println (display + "|");
  266.         }

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

  268.         EnvManager.TerminateEnv();
  269.     }
  270. }