R1CentralCLTProxy.java

  1. package org.drip.measure.chisquare;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  *
  9.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  10.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  11.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  12.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  13.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  14.  *      and computational support.
  15.  *  
  16.  *      https://lakshmidrip.github.io/DROP/
  17.  *  
  18.  *  DROP is composed of three modules:
  19.  *  
  20.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  21.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  22.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  23.  *
  24.  *  DROP Product Core implements libraries for the following:
  25.  *  - Fixed Income Analytics
  26.  *  - Loan Analytics
  27.  *  - Transaction Cost Analytics
  28.  *
  29.  *  DROP Portfolio Core implements libraries for the following:
  30.  *  - Asset Allocation Analytics
  31.  *  - Asset Liability Management Analytics
  32.  *  - Capital Estimation Analytics
  33.  *  - Exposure Analytics
  34.  *  - Margin Analytics
  35.  *  - XVA Analytics
  36.  *
  37.  *  DROP Computational Core implements libraries for the following:
  38.  *  - Algorithm Support
  39.  *  - Computation Support
  40.  *  - Function Analysis
  41.  *  - Model Validation
  42.  *  - Numerical Analysis
  43.  *  - Numerical Optimizer
  44.  *  - Spline Builder
  45.  *  - Statistical Learning
  46.  *
  47.  *  Documentation for DROP is Spread Over:
  48.  *
  49.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  50.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  51.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  52.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  53.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  54.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  55.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  56.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  57.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  58.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  59.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  60.  *
  61.  *  Licensed under the Apache License, Version 2.0 (the "License");
  62.  *      you may not use this file except in compliance with the License.
  63.  *  
  64.  *  You may obtain a copy of the License at
  65.  *      http://www.apache.org/licenses/LICENSE-2.0
  66.  *  
  67.  *  Unless required by applicable law or agreed to in writing, software
  68.  *      distributed under the License is distributed on an "AS IS" BASIS,
  69.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  70.  *  
  71.  *  See the License for the specific language governing permissions and
  72.  *      limitations under the License.
  73.  */

  74. /**
  75.  * <i>R1CentralCLTProxy</i> implements the N (0, 1) CLT Proxy Version for the R<sup>1</sup> Chi-Square
  76.  *  Distribution. The References are:
  77.  *
  78.  * <br><br>
  79.  *  <ul>
  80.  *      <li>
  81.  *          Abramowitz, M., and I. A. Stegun (2007): <i>Handbook of Mathematics Functions</i> <b>Dover Book
  82.  *              on Mathematics</b>
  83.  *      </li>
  84.  *      <li>
  85.  *          Backstrom, T., and J. Fischer (2018): Fast Randomization for Distributed Low Bit-rate Coding of
  86.  *              Speech and Audio <i>IEEE/ACM Transactions on Audio, Speech, and Language Processing</i> <b>26
  87.  *              (1)</b> 19-30
  88.  *      </li>
  89.  *      <li>
  90.  *          Chi-Squared Distribution (2019): Chi-Squared Function
  91.  *              https://en.wikipedia.org/wiki/Chi-squared_distribution
  92.  *      </li>
  93.  *      <li>
  94.  *          Johnson, N. L., S. Kotz, and N. Balakrishnan (1994): <i>Continuous Univariate Distributions
  95.  *              2<sup>nd</sup> Edition</i> <b>John Wiley and Sons</b>
  96.  *      </li>
  97.  *      <li>
  98.  *          National Institute of Standards and Technology (2019): Chi-Squared Distribution
  99.  *              https://www.itl.nist.gov/div898/handbook/eda/section3/eda3666.htm
  100.  *      </li>
  101.  *  </ul>
  102.  *
  103.  *  <br><br>
  104.  *  <ul>
  105.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  106.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  107.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/README.md">R<sup>d</sup> Continuous/Discrete Probability Measures</a></li>
  108.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/chisquare/README.md">Chi-Square Distribution Implementation/Properties</a></li>
  109.  *  </ul>
  110.  *
  111.  * @author Lakshmi Krishnamurthy
  112.  */

  113. public class R1CentralCLTProxy extends org.drip.measure.continuous.R1Univariate
  114. {
  115.     private int _degreesOfFreedom = -1;

  116.     private org.drip.measure.gaussian.R1UnivariateNormal _r1UnivariateNormal =
  117.         org.drip.measure.gaussian.R1UnivariateNormal.Standard();

  118.     /**
  119.      * R1CentralCLTProxy Constructor
  120.      *
  121.      * @param degreesOfFreedom Degrees of Freedom
  122.      *
  123.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  124.      */

  125.     public R1CentralCLTProxy (
  126.         final int degreesOfFreedom)
  127.         throws java.lang.Exception
  128.     {
  129.         if (0 >= (_degreesOfFreedom = degreesOfFreedom))
  130.         {
  131.             throw new java.lang.Exception ("R1CentralCLTProxy Constructor => Invalid Inputs");
  132.         }
  133.     }

  134.     /**
  135.      * Retrieve the Degrees of Freedom
  136.      *
  137.      * @return The Degrees of Freedom
  138.      */

  139.     public int degreesOfFreedom()
  140.     {
  141.         return _degreesOfFreedom;
  142.     }

  143.     @Override public double[] support()
  144.     {
  145.         return new double[]
  146.         {
  147.             -1. * java.lang.Math.sqrt (0.5 * _degreesOfFreedom),
  148.             java.lang.Double.POSITIVE_INFINITY
  149.         };
  150.     }

  151.     @Override public double density (
  152.         final double t)
  153.         throws java.lang.Exception
  154.     {
  155.         return _r1UnivariateNormal.density (t);
  156.     }

  157.     @Override public double cumulative (
  158.         final double t)
  159.         throws java.lang.Exception
  160.     {
  161.         return _r1UnivariateNormal.cumulative (t);
  162.     }

  163.     @Override public double mean()
  164.         throws java.lang.Exception
  165.     {
  166.         return 0.;
  167.     }

  168.     @Override public double median()
  169.         throws java.lang.Exception
  170.     {
  171.         return 0.;
  172.     }

  173.     @Override public double mode()
  174.         throws java.lang.Exception
  175.     {
  176.         return 0.;
  177.     }

  178.     @Override public double variance()
  179.         throws java.lang.Exception
  180.     {
  181.         return 1.;
  182.     }

  183.     @Override public double skewness()
  184.         throws java.lang.Exception
  185.     {
  186.         return java.lang.Math.sqrt (8. / _degreesOfFreedom);
  187.     }

  188.     @Override public double excessKurtosis()
  189.         throws java.lang.Exception
  190.     {
  191.         return 12. / _degreesOfFreedom;
  192.     }

  193.     @Override public double differentialEntropy()
  194.         throws java.lang.Exception
  195.     {
  196.         return _r1UnivariateNormal.differentialEntropy();
  197.     }

  198.     @Override public org.drip.function.definition.R1ToR1 momentGeneratingFunction()
  199.     {
  200.         return _r1UnivariateNormal.momentGeneratingFunction();
  201.     }

  202.     @Override public org.drip.function.definition.R1ToR1 probabilityGeneratingFunction()
  203.     {
  204.         return _r1UnivariateNormal.probabilityGeneratingFunction();
  205.     }

  206.     @Override public double random()
  207.         throws java.lang.Exception
  208.     {
  209.         double sumOfStandardNormalSquares = 0.;

  210.         for (int drawIndex = 0; drawIndex < _degreesOfFreedom; ++drawIndex)
  211.         {
  212.             double randomStandardNormal = org.drip.measure.gaussian.NormalQuadrature.InverseCDF
  213.                 (java.lang.Math.random());

  214.             sumOfStandardNormalSquares = sumOfStandardNormalSquares +
  215.                 randomStandardNormal * randomStandardNormal;
  216.         }

  217.         return (sumOfStandardNormalSquares - _degreesOfFreedom) /
  218.             java.lang.Math.sqrt (2. * _degreesOfFreedom);
  219.     }
  220. }