CentralChernoffBounds.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>CentralChernoffBounds</i> illustrates the Upper Chernoff Bounds for the Central Chi-squared Function.
  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 CentralChernoffBounds
  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[] zArray =
  147.         {
  148.              0.01,
  149.              0.03,
  150.              0.06,
  151.              0.10,
  152.              0.30,
  153.              0.60,
  154.              0.99,
  155.              3.00,
  156.              6.00,
  157.             10.00,
  158.         };
  159.         int[] dofArray =
  160.         {
  161.              // 1,
  162.              2,
  163.              3,
  164.              4,
  165.              5,
  166.              6,
  167.              7,
  168.              8,
  169.              9,
  170.         };

  171.         R1ToR1 gammaEstimator = new EulerIntegralSecondKind (null);

  172.         R2ToR1 lowerIncompleteGammaEstimator = LowerIncompleteGamma();

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

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

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

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

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

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

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

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

  181.         for (int dof : dofArray)
  182.         {
  183.             R1Central r1Univariate = new R1Central (
  184.                 dof,
  185.                 gammaEstimator,
  186.                 digammaEstimator,
  187.                 lowerIncompleteGammaEstimator
  188.             );

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

  190.             for (double z : zArray)
  191.             {
  192.                 display = display + " " + FormatUtil.FormatDouble (
  193.                     r1Univariate.cumulative (z * dof), 1, 5, 1., false
  194.                 ) + " |";
  195.             }

  196.             System.out.println (display + "|");
  197.         }

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

  199.         System.out.println();

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

  201.         System.out.println ("\t|                                        CUMULATIVE DISTRIBUTION CHERNOFF BOUND ESTIMATE                                       ||");

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

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

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

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

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

  207.         for (int dof : dofArray)
  208.         {
  209.             R1Central r1Univariate = new R1Central (
  210.                 dof,
  211.                 gammaEstimator,
  212.                 digammaEstimator,
  213.                 lowerIncompleteGammaEstimator
  214.             );

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

  216.             for (double z : zArray)
  217.             {
  218.                 display = display + " " + FormatUtil.FormatDouble (
  219.                     r1Univariate.chernoffBound (z * dof), 1, 5, 1., false
  220.                 ) + " |";
  221.             }

  222.             System.out.println (display + "|");
  223.         }

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

  225.         System.out.println();

  226.         EnvManager.TerminateEnv();
  227.     }
  228. }