StandardExponentialTStatistic.java

  1. package org.drip.sample.samplestatistics;

  2. import org.drip.measure.continuous.R1UnivariateExponential;
  3. import org.drip.measure.statistics.PopulationCentralMeasures;
  4. import org.drip.measure.statistics.UnivariateMoments;
  5. import org.drip.numerical.common.FormatUtil;
  6. import org.drip.numerical.common.StringUtil;
  7. import org.drip.service.env.EnvManager;

  8. /*
  9.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  10.  */

  11. /*!
  12.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  13.  *
  14.  *  This file is part of DROP, an open-source library targeting risk, transaction costs, exposure, margin
  15.  *      calculations, and portfolio construction within and across fixed income, credit, commodity, equity,
  16.  *      FX, and structured products.
  17.  *  
  18.  *      https://lakshmidrip.github.io/DROP/
  19.  *  
  20.  *  DROP is composed of three main modules:
  21.  *  
  22.  *  - DROP Analytics Core - https://lakshmidrip.github.io/DROP-Analytics-Core/
  23.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  24.  *  - DROP Numerical Core - https://lakshmidrip.github.io/DROP-Numerical-Core/
  25.  *
  26.  *  DROP Analytics Core implements libraries for the following:
  27.  *  - Fixed Income Analytics
  28.  *  - Asset Backed Analytics
  29.  *  - XVA Analytics
  30.  *  - Exposure and Margin Analytics
  31.  *
  32.  *  DROP Portfolio Core implements libraries for the following:
  33.  *  - Asset Allocation Analytics
  34.  *  - Transaction Cost Analytics
  35.  *
  36.  *  DROP Numerical Core implements libraries for the following:
  37.  *  - Statistical Learning Library
  38.  *  - Numerical Optimizer Library
  39.  *  - Machine Learning Library
  40.  *  - Spline Builder Library
  41.  *
  42.  *  Documentation for DROP is Spread Over:
  43.  *
  44.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  45.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  46.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  47.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  48.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  49.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  50.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  51.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  52.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  53.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  54.  *
  55.  *  Licensed under the Apache License, Version 2.0 (the "License");
  56.  *      you may not use this file except in compliance with the License.
  57.  *  
  58.  *  You may obtain a copy of the License at
  59.  *      http://www.apache.org/licenses/LICENSE-2.0
  60.  *  
  61.  *  Unless required by applicable law or agreed to in writing, software
  62.  *      distributed under the License is distributed on an "AS IS" BASIS,
  63.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  64.  *  
  65.  *  See the License for the specific language governing permissions and
  66.  *      limitations under the License.
  67.  */

  68. /**
  69.  * <i>StandardExponentialTStatistic</i> illustrates the Computation of the t-statistic, z-score, and other
  70.  * related Metrics of the Sample/Population Mean for an Empirical Standard Exponential Distribution.
  71.  *
  72.  *  <br><br>
  73.  *  <ul>
  74.  *      <li>
  75.  *          Anfuso, F., D. Karyampas, and A. Nawroth (2017): A Sound Basel III Compliant Framework for
  76.  *              Back-testing Credit Exposure Models
  77.  *              https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2264620 <b>eSSRN</b>
  78.  *      </li>
  79.  *      <li>
  80.  *          Diebold, F. X., T. A. Gunther, and A. S. Tay (1998): Evaluating Density Forecasts with
  81.  *              Applications to Financial Risk Management, International Economic Review 39 (4) 863-883
  82.  *      </li>
  83.  *      <li>
  84.  *          Wikipedia (2018): Probability Integral Transform
  85.  *              https://en.wikipedia.org/wiki/Probability_integral_transform
  86.  *      </li>
  87.  *      <li>
  88.  *          Wikipedia (2018): t-statistic https://en.wikipedia.org/wiki/T-statistic
  89.  *      </li>
  90.  *      <li>
  91.  *          Wikipedia (2019): p-value https://en.wikipedia.org/wiki/P-value
  92.  *      </li>
  93.  *  </ul>
  94.  *
  95.  *  <br><br>
  96.  *  <ul>
  97.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AnalyticsCore.md">Analytics Core Module</a></li>
  98.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ModelValidationAnalyticsLibrary.md">Model Validation Analytics Library</a></li>
  99.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample">Sample</a></li>
  100.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/hypothesistest">Statistical Hypothesis Tests</a></li>
  101.  *  </ul>
  102.  * <br><br>
  103.  *
  104.  * @author Lakshmi Krishnamurthy
  105.  */

  106. public class StandardExponentialTStatistic
  107. {

  108.     private static final double UnivariateRandom()
  109.         throws Exception
  110.     {
  111.         return R1UnivariateExponential.Standard().random();
  112.     }

  113.     private static final PopulationCentralMeasures PopulationMeasures()
  114.     {
  115.         return R1UnivariateExponential.Standard().populationCentralMeasures();
  116.     }

  117.     private static final double SampleMeanEstimate (
  118.         final int count)
  119.         throws Exception
  120.     {
  121.         double[] univariateRandomArray = new double[count];

  122.         for (int index = 0; index < count; ++index)
  123.         {
  124.             univariateRandomArray[index] = UnivariateRandom();
  125.         }

  126.         return UnivariateMoments.Standard (
  127.             StringUtil.GUID(),
  128.             univariateRandomArray,
  129.             null
  130.         ).mean();
  131.     }

  132.     private static final UnivariateMoments SampleStatistics (
  133.         final int drawCount,
  134.         final int sampleCount)
  135.         throws Exception
  136.     {
  137.         double[] sampleMeanEstimateArray = new double[sampleCount];

  138.         for (int sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex)
  139.         {
  140.             sampleMeanEstimateArray[sampleIndex] = SampleMeanEstimate (drawCount);
  141.         }

  142.         return UnivariateMoments.Standard (
  143.             StringUtil.GUID(),
  144.             sampleMeanEstimateArray,
  145.             null
  146.         );
  147.     }

  148.     public static final void main (
  149.         final String[] argumentArray)
  150.         throws Exception
  151.     {
  152.         EnvManager.InitEnv ("");

  153.         int drawCount = 10000;
  154.         int sampleCount = 200;

  155.         UnivariateMoments sampleStatistics = SampleStatistics (
  156.             drawCount,
  157.             sampleCount
  158.         );

  159.         PopulationCentralMeasures populationCentralMeasures = PopulationMeasures();

  160.         double nextDraw = UnivariateRandom();

  161.         double updatedMean = (sampleStatistics.mean() * sampleCount + nextDraw) / (sampleCount + 1);

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

  163.         System.out.println ("\t|          STANDARD UNIFORM DISTRIBUTION           ||");

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

  165.         System.out.println (
  166.             "\t| Population Mean                => " +
  167.             FormatUtil.FormatDouble (populationCentralMeasures.mean(), 1, 8, 1.)
  168.         );

  169.         System.out.println (
  170.             "\t| Population Variance            => " +
  171.             FormatUtil.FormatDouble (populationCentralMeasures.variance(), 1, 8, 1.)
  172.         );

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

  174.         System.out.println (
  175.             "\t| Mean                           => " +
  176.             FormatUtil.FormatDouble (sampleStatistics.mean(), 1, 8, 1.)
  177.         );

  178.         System.out.println (
  179.             "\t| Variance                       => " +
  180.             FormatUtil.FormatDouble (sampleStatistics.variance(), 1, 8, 1.)
  181.         );

  182.         System.out.println (
  183.             "\t| Sample Count                   => " +
  184.             FormatUtil.FormatDouble (sampleStatistics.numSample(), 3, 0, 1.)
  185.         );

  186.         System.out.println (
  187.             "\t| Degrees Of Freedom             => " +
  188.             FormatUtil.FormatDouble (sampleStatistics.degreesOfFreedom(), 3, 0, 1.)
  189.         );

  190.         System.out.println (
  191.             "\t| Standard Deviation             => " +
  192.             FormatUtil.FormatDouble (sampleStatistics.stdDev(), 1, 8, 1.)
  193.         );

  194.         System.out.println (
  195.             "\t| Standard Error                 => " +
  196.             FormatUtil.FormatDouble (sampleStatistics.stdError(), 1, 8, 1.)
  197.         );

  198.         System.out.println (
  199.             "\t| Predictive Confidence Interval => " +
  200.             FormatUtil.FormatDouble (sampleStatistics.predictiveConfidenceLevel(), 1, 8, 1.)
  201.         );

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

  203.         System.out.println (
  204.             "\t| Next Draw                      => " +
  205.             FormatUtil.FormatDouble (nextDraw, 1, 8, 1.)
  206.         );

  207.         System.out.println (
  208.             "\t| Next Draw T-Statistics         => " +
  209.             FormatUtil.FormatDouble (sampleStatistics.tStatistic (updatedMean), 1, 8, 1.)
  210.         );

  211.         System.out.println (
  212.             "\t| Standard Error Offset          => " +
  213.             FormatUtil.FormatDouble (sampleStatistics.standardErrorOffset (nextDraw), 1, 0, 1.)
  214.         );

  215.         System.out.println (
  216.             "\t| Population Z-Score             => " +
  217.             FormatUtil.FormatDouble (populationCentralMeasures.zScore (nextDraw), 1, 8, 1.)
  218.         );

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

  220.         EnvManager.TerminateEnv();
  221.     }
  222. }