NonCentralPDFEstimate.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.R1NonCentral;
  5. import org.drip.numerical.common.FormatUtil;
  6. import org.drip.service.env.EnvManager;
  7. import org.drip.specialfunction.bessel.ModifiedFirstFrobeniusSeriesEstimator;
  8. import org.drip.specialfunction.definition.ModifiedBesselFirstKindEstimator;
  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>NonCentralPDFEstimate</i> implements the PDF for the Non-central Chi-Square Distribution. The
  86.  * References are:
  87.  *
  88.  * <br><br>
  89.  *  <ul>
  90.  *      <li>
  91.  *          Johnson, N. L., S. Kotz, and N. Balakrishnan (1995): <i>Continuous Univariate Distributions
  92.  *              2<sup>nd</sup> Edition</i> <b>John Wiley and Sons</b>
  93.  *      </li>
  94.  *      <li>
  95.  *          Muirhead, R. (2005): <i>Aspects of Multivariate Statistical Theory 2<sup>nd</sup> Edition</i>
  96.  *              <b>Wiley</b>
  97.  *      </li>
  98.  *      <li>
  99.  *          Non-central Chi-Squared Distribution (2019): Chi-Squared Function
  100.  *              https://en.wikipedia.org/wiki/Noncentral_chi-squared_distribution
  101.  *      </li>
  102.  *      <li>
  103.  *          Sankaran, M. (1963): Approximations to the Non-Central Chi-Square Distribution <i>Biometrika</i>
  104.  *              <b>50 (1-2)</b> 199-204
  105.  *      </li>
  106.  *      <li>
  107.  *          Young, D. S. (2010): tolerance: An R Package for Estimating Tolerance Intervals <i>Journal of
  108.  *              Statistical Software</i> <b>36 (5)</b> 1-39
  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/sample/README.md">DROP API Construction and Usage</a></li>
  117.  *      <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>
  118.  *  </ul>
  119.  *
  120.  * @author Lakshmi Krishnamurthy
  121.  */

  122. public class NonCentralPDFEstimate
  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.     public static final void main (
  144.         final String[] argumentArray)
  145.         throws Exception
  146.     {
  147.         EnvManager.InitEnv (
  148.             ""
  149.         );

  150.         int besselFirstTermCount = 20;
  151.         int digammaTermCount = 1000;
  152.         double[] nonCentralityParameterArray =
  153.         {
  154.             0.5,
  155.             1.0,
  156.             1.5,
  157.             2.0,
  158.         };
  159.         double[] tArray =
  160.         {
  161.              0.1,
  162.              1.0,
  163.              2.0,
  164.              3.0,
  165.              4.0,
  166.              5.0,
  167.              6.0,
  168.              7.0,
  169.              8.0,
  170.              9.0,
  171.             10.0,
  172.             12.0,
  173.         };
  174.         int[] dofArray =
  175.         {
  176.              2,
  177.              3,
  178.              4,
  179.              5,
  180.              6,
  181.              7,
  182.              8,
  183.              9,
  184.         };
  185.         double[] pValueArray =
  186.         {
  187.              0.05,
  188.              0.10,
  189.              0.15,
  190.              0.20,
  191.              0.25,
  192.              0.30,
  193.              0.35,
  194.              0.40,
  195.              0.45,
  196.              0.50,
  197.              0.55,
  198.              0.60,
  199.         };

  200.         R1ToR1 gammaEstimator = new EulerIntegralSecondKind (
  201.             null
  202.         );

  203.         R2ToR1 lowerIncompleteGammaEstimator = LowerIncompleteGamma();

  204.         R1ToR1 digammaEstimator = CumulativeSeriesEstimator.AbramowitzStegun2007 (
  205.             digammaTermCount
  206.         );

  207.         ModifiedBesselFirstKindEstimator modifiedBesselFirstKindEstimator =
  208.             ModifiedFirstFrobeniusSeriesEstimator.Standard (
  209.                 gammaEstimator,
  210.                 besselFirstTermCount
  211.             );

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

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

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

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

  216.         System.out.println ("\t|                - Degrees of Freedom. Non Centrality Parameter                                                                ||");

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

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

  219.         for (double nonCentralityParameter : nonCentralityParameterArray)
  220.         {
  221.             for (int dof : dofArray)
  222.             {
  223.                 R1NonCentral r1UnivariateNonCentral = R1NonCentral.Standard (
  224.                     dof,
  225.                     nonCentralityParameter,
  226.                     gammaEstimator,
  227.                     digammaEstimator,
  228.                     lowerIncompleteGammaEstimator,
  229.                     modifiedBesselFirstKindEstimator
  230.                 );

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

  235.                 for (double t : tArray)
  236.                 {
  237.                     display = display + " " + FormatUtil.FormatDouble (
  238.                         r1UnivariateNonCentral.density (
  239.                             t
  240.                         ), 1, 5, 1., false
  241.                     ) + " |";
  242.                 }

  243.                 System.out.println (display + "|");
  244.             }
  245.         }

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

  247.         System.out.println();

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

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

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

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

  252.         System.out.println ("\t|                - Degrees of Freedom. Non Centrality Parameter                                                                ||");

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

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

  255.         for (double nonCentralityParameter : nonCentralityParameterArray)
  256.         {
  257.             for (int dof : dofArray)
  258.             {
  259.                 R1NonCentral r1UnivariateNonCentral = R1NonCentral.Standard (
  260.                     dof,
  261.                     nonCentralityParameter,
  262.                     gammaEstimator,
  263.                     digammaEstimator,
  264.                     lowerIncompleteGammaEstimator,
  265.                     modifiedBesselFirstKindEstimator
  266.                 );

  267.                 String display = "\t| [" +
  268.                     FormatUtil.FormatDouble (dof, 2, 0, 1., false) + " | " +
  269.                     FormatUtil.FormatDouble (nonCentralityParameter, 1, 1, 1., false) +
  270.                 "] =>";

  271.                 for (double t : tArray)
  272.                 {
  273.                     display = display + " " + FormatUtil.FormatDouble (
  274.                         r1UnivariateNonCentral.cumulative (
  275.                             t
  276.                         ), 1, 5, 1., false
  277.                     ) + " |";
  278.                 }

  279.                 System.out.println (display + "|");
  280.             }
  281.         }

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

  283.         System.out.println();

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

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

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

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

  288.         System.out.println ("\t|                - Degrees of Freedom. Non Centrality Parameter                                                                ||");

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

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

  291.         for (double nonCentralityParameter : nonCentralityParameterArray)
  292.         {
  293.             for (int dof : dofArray)
  294.             {
  295.                 R1NonCentral r1UnivariateNonCentral = R1NonCentral.Standard (
  296.                     dof,
  297.                     nonCentralityParameter,
  298.                     gammaEstimator,
  299.                     digammaEstimator,
  300.                     lowerIncompleteGammaEstimator,
  301.                     modifiedBesselFirstKindEstimator
  302.                 );

  303.                 String display = "\t| [" +
  304.                     FormatUtil.FormatDouble (dof, 2, 0, 1., false) + " | " +
  305.                     FormatUtil.FormatDouble (nonCentralityParameter, 1, 1, 1., false) +
  306.                 "] =>";

  307.                 for (double p : pValueArray)
  308.                 {
  309.                     display = display + " " + FormatUtil.FormatDouble (
  310.                         r1UnivariateNonCentral.invCumulative (
  311.                             p
  312.                         ), 1, 5, 1., false
  313.                     ) + " |";
  314.                 }

  315.                 System.out.println (display + "|");
  316.             }
  317.         }

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

  319.         EnvManager.TerminateEnv();
  320.     }
  321. }