NonCentralCentralMoments.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>NonCentralCentralMoments</i> illustrates the Computation of the Four Leading Central Moments for the
  86.  *  Non-central Chi-Square Distribution. The 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 NonCentralCentralMoments
  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 digammaTermCount = 1000;
  151.         int besselFirstTermCount = 20;
  152.         double[] nonCentralityParameterArray =
  153.         {
  154.             0.5,
  155.             1.0,
  156.             1.5,
  157.             2.0,
  158.             2.5,
  159.             3.0,
  160.             3.5,
  161.             4.0,
  162.         };
  163.         int[] dofArray =
  164.         {
  165.              2,
  166.              3,
  167.              4,
  168.              5,
  169.              6,
  170.              7,
  171.              8,
  172.              9,
  173.         };

  174.         R1ToR1 gammaEstimator = new EulerIntegralSecondKind (
  175.             null
  176.         );

  177.         R2ToR1 lowerIncompleteGammaEstimator = LowerIncompleteGamma();

  178.         R1ToR1 digammaEstimator = CumulativeSeriesEstimator.AbramowitzStegun2007 (
  179.             digammaTermCount
  180.         );

  181.         ModifiedBesselFirstKindEstimator modifiedBesselFirstKindEstimator =
  182.             ModifiedFirstFrobeniusSeriesEstimator.Standard (
  183.                 gammaEstimator,
  184.                 besselFirstTermCount
  185.             );

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

  187.         System.out.println ("\t|                                                  NON CENTRAL CENTRAL MOMENTS                                                 ||");

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

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

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

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

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

  193.         for (double nonCentralityParameter : nonCentralityParameterArray)
  194.         {
  195.             for (int dof : dofArray)
  196.             {
  197.                 R1NonCentral r1UnivariateNonCentral = R1NonCentral.Standard (
  198.                     dof,
  199.                     nonCentralityParameter,
  200.                     gammaEstimator,
  201.                     digammaEstimator,
  202.                     lowerIncompleteGammaEstimator,
  203.                     modifiedBesselFirstKindEstimator
  204.                 );

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

  209.                 double[] leadingCentralMoments = r1UnivariateNonCentral.leadingCentralMoments();

  210.                 for (int i = 0;
  211.                     i <= 3;
  212.                     ++i)
  213.                 {
  214.                     display = display + " " + FormatUtil.FormatDouble (
  215.                         leadingCentralMoments[i], 5, 5, 1., false
  216.                     ) + " |";
  217.                 }

  218.                 System.out.println (display + "|");
  219.             }
  220.         }

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

  222.         System.out.println();

  223.         EnvManager.TerminateEnv();
  224.     }
  225. }