StandardUniformTTest.java

  1. package org.drip.sample.hypothesistest;

  2. import org.drip.measure.continuous.R1UnivariateUniform;
  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.service.env.EnvManager;
  5. import org.drip.validation.evidence.Ensemble;
  6. import org.drip.validation.evidence.Sample;
  7. import org.drip.validation.evidence.TestStatisticEvaluator;

  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>StandardUniformTTest</i> illustrates t-Test for a Standard Uniform Ensemble.
  70.  *
  71.  *  <br><br>
  72.  *  <ul>
  73.  *      <li>
  74.  *          Bhattacharya, B., and D. Habtzghi (2002): Median of the p-value under the Alternate Hypothesis
  75.  *              American Statistician 56 (3) 202-206
  76.  *      </li>
  77.  *      <li>
  78.  *          Head, M. L., L. Holman, R, Lanfear, A. T. Kahn, and M. D. Jennions (2015): The Extent and
  79.  *              Consequences of p-Hacking in Science PLoS Biology 13 (3) e1002106
  80.  *      </li>
  81.  *      <li>
  82.  *          Wasserstein, R. L., and N. A. Lazar (2016): The ASA’s Statement on p-values: Context, Process,
  83.  *              and Purpose American Statistician 70 (2) 129-133
  84.  *      </li>
  85.  *      <li>
  86.  *          Wetzels, R., D. Matzke, M. D. Lee, J. N. Rouder, G, J, Iverson, and E. J. Wagenmakers (2011):
  87.  *          Statistical Evidence in Experimental Psychology: An Empirical Comparison using 855 t-Tests
  88.  *          Perspectives in Psychological Science 6 (3) 291-298
  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/validation">Model Validation Suite</a></li>
  100.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/validation/core">Core Model Validation Support Utilities</a></li>
  101.  *  </ul>
  102.  * <br><br>
  103.  *
  104.  * @author Lakshmi Krishnamurthy
  105.  */

  106. public class StandardUniformTTest
  107. {

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

  113.     private static final Sample GenerateSample (
  114.         final int drawCount)
  115.         throws Exception
  116.     {
  117.         double[] univariateRandomArray = new double[drawCount];

  118.         for (int drawIndex = 0; drawIndex < drawCount; ++drawIndex)
  119.         {
  120.             univariateRandomArray[drawIndex] = UnivariateRandom();
  121.         }

  122.         return new Sample (univariateRandomArray);
  123.     }

  124.     private static final Sample[] GenerateSampleArray (
  125.         final int drawCount,
  126.         final int sampleCount)
  127.         throws Exception
  128.     {
  129.         Sample[] sampleArray = new Sample[sampleCount];

  130.         for (int sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex)
  131.         {
  132.             sampleArray[sampleIndex] = GenerateSample (drawCount);
  133.         }

  134.         return sampleArray;
  135.     }

  136.     private static final TestStatisticEvaluator[] MakeTestStatisticEvaluatorArray()
  137.         throws Exception
  138.     {
  139.         return new TestStatisticEvaluator[]
  140.         {
  141.             new TestStatisticEvaluator()
  142.             {
  143.                 public double evaluate (
  144.                     final double[] drawArray)
  145.                     throws Exception
  146.                 {
  147.                     double mean = 0.;
  148.                     int drawCount = drawArray.length;

  149.                     for (int drawIndex = 0; drawIndex < drawCount; ++drawIndex)
  150.                     {
  151.                         mean += drawArray[drawIndex];
  152.                     }

  153.                     return mean / drawCount;
  154.                 }
  155.             }
  156.         };
  157.     }

  158.     private static final void TTest (
  159.         final Ensemble ensemble,
  160.         final int drawCount,
  161.         final TestStatisticEvaluator testStatisticEvaluator)
  162.         throws Exception
  163.     {
  164.         Sample testSample = GenerateSample (drawCount);

  165.         org.drip.validation.hypothesis.TTestOutcome[] tTestArray = ensemble.tTest (
  166.             testSample.applyTestStatistic (testStatisticEvaluator)
  167.         );

  168.         System.out.println (
  169.             "\t| " + FormatUtil.FormatDouble (tTestArray[0].testStatistic(), 1, 4, 1.) +
  170.             " | " + FormatUtil.FormatDouble (tTestArray[0].sampleCount(), 3, 0, 1.) +
  171.             " | " + FormatUtil.FormatDouble (tTestArray[0].ensembleMean(), 1, 4, 1.) +
  172.             " | " + FormatUtil.FormatDouble (tTestArray[0].ensembleVariance(), 1, 8, 1.) +
  173.             " | " + FormatUtil.FormatDouble (tTestArray[0].ensembleStandardDeviation(), 1, 6, 1.) +
  174.             " | " + FormatUtil.FormatDouble (tTestArray[0].ensembleStandardError(), 1, 6, 1.) +
  175.             " | " + FormatUtil.FormatDouble (tTestArray[0].ensembleDegreesOfFreedom(), 2, 0, 1.) +
  176.             " | " + FormatUtil.FormatDouble (tTestArray[0].ensemblePredictiveConfidenceInterval(), 1, 6, 1.) +
  177.             " | " + FormatUtil.FormatDouble (tTestArray[0].ensembleTStatistics(), 1, 4, 1.) +
  178.             " | " + FormatUtil.FormatDouble (tTestArray[0].ensembleStandardErrorOffset(), 1, 4, 1.) + " ||"
  179.         );
  180.     }

  181.     public static final void main (
  182.         final String[] argumentArray)
  183.         throws Exception
  184.     {
  185.         EnvManager.InitEnv ("");

  186.         int drawCount = 10000;
  187.         int sampleCount = 100;
  188.         int tTestCount = 25;

  189.         TestStatisticEvaluator[] testStatisticEvaluatorArray = MakeTestStatisticEvaluatorArray();

  190.         Ensemble ensemble = new Ensemble (
  191.             GenerateSampleArray (
  192.                 drawCount,
  193.                 sampleCount
  194.             ),
  195.             testStatisticEvaluatorArray
  196.         );

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

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

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

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

  201.         System.out.println ("\t|            - Test Statistic                                                                          ||");

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

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

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

  205.         System.out.println ("\t|            - Ensemble Standard Deviation                                                             ||");

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

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

  208.         System.out.println ("\t|            - Ensemble Predictive Confidence Interval                                                 ||");

  209.         System.out.println ("\t|            - Ensemble Test Statistic                                                                 ||");

  210.         System.out.println ("\t|            - Ensemble Standard Error Offset                                                          ||");

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

  212.         for (int tTestIndex = 0; tTestIndex < tTestCount; ++tTestIndex)
  213.         {
  214.             TTest (
  215.                 ensemble,
  216.                 drawCount,
  217.                 testStatisticEvaluatorArray[0]
  218.             );
  219.         }

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

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