CentralMeasureEstimate.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>CentralMeasureEstimate</i> illustrates the Estimation of the Central Chi-squared Distribution Measures.
  84.  * 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 CentralMeasureEstimate
  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.         int[] dofArray =
  147.         {
  148.              // 1,
  149.              2,
  150.              3,
  151.              4,
  152.              5,
  153.              6,
  154.              7,
  155.              8,
  156.              9,
  157.             10,
  158.             11,
  159.             12,
  160.             13,
  161.             14,
  162.             15,
  163.             16,
  164.             17,
  165.             18,
  166.             19,
  167.             20,
  168.             21,
  169.             22,
  170.             23,
  171.             24,
  172.             25,
  173.         };

  174.         R1ToR1 gammaEstimator = new EulerIntegralSecondKind (null);

  175.         R2ToR1 lowerIncompleteGammaEstimator = LowerIncompleteGamma();

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

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

  178.         System.out.println ("\t|             CENTRAL CHI-SQUARED DISTRIBUTION MEASURES ESTIMATE             ||");

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

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

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

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

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

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

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

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

  187.         System.out.println ("\t|                - Excess Kurtosis                                           ||");

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

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

  190.         for (int dof : dofArray)
  191.         {
  192.             R1Central r1UnivariateCentral = new R1Central (
  193.                 dof,
  194.                 gammaEstimator,
  195.                 digammaEstimator,
  196.                 lowerIncompleteGammaEstimator
  197.             );

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

  199.             display = display + " " + FormatUtil.FormatDouble (
  200.                 r1UnivariateCentral.mean(), 2, 2, 1., false
  201.             ) + " | " + FormatUtil.FormatDouble (
  202.                 r1UnivariateCentral.median(), 2, 6, 1., false
  203.             ) + " | " + FormatUtil.FormatDouble (
  204.                 r1UnivariateCentral.mode(), 2, 2, 1., false
  205.             ) + " | " + FormatUtil.FormatDouble (
  206.                 r1UnivariateCentral.variance(), 2, 2, 1., false
  207.             ) + " | " + FormatUtil.FormatDouble (
  208.                 r1UnivariateCentral.skewness(), 1, 6, 1., false
  209.             ) + " | " + FormatUtil.FormatDouble (
  210.                 r1UnivariateCentral.excessKurtosis(), 1, 6, 1., false
  211.             ) + " | " + FormatUtil.FormatDouble (
  212.                 r1UnivariateCentral.differentialEntropy(), 1, 6, 1., false
  213.             ) + " |";

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

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

  217.         EnvManager.TerminateEnv();
  218.     }
  219. }