StandardNormalSignificanceTest.java

  1. package org.drip.sample.hypothesistest;

  2. import org.drip.measure.gaussian.R1UnivariateNormal;
  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.service.env.EnvManager;
  5. import org.drip.validation.hypothesis.SignificanceTestSetting;
  6. import org.drip.validation.evidence.Ensemble;
  7. import org.drip.validation.evidence.Sample;
  8. import org.drip.validation.evidence.TestStatisticEvaluator;
  9. import org.drip.validation.hypothesis.SignificanceTestOutcome;
  10. import org.drip.validation.hypothesis.ProbabilityIntegralTransformTest;

  11. /*
  12.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  13.  */

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

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

  109. public class StandardNormalSignificanceTest
  110. {

  111.     private static final double UnivariateRandom()
  112.         throws Exception
  113.     {
  114.         return R1UnivariateNormal.Standard().random();
  115.     }

  116.     private static final Sample GenerateSample (
  117.         final int drawCount)
  118.         throws Exception
  119.     {
  120.         double[] univariateRandomArray = new double[drawCount];

  121.         for (int drawIndex = 0; drawIndex < drawCount; ++drawIndex)
  122.         {
  123.             univariateRandomArray[drawIndex] = UnivariateRandom();
  124.         }

  125.         return new Sample (univariateRandomArray);
  126.     }

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

  133.         for (int sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex)
  134.         {
  135.             sampleArray[sampleIndex] = GenerateSample (drawCount);
  136.         }

  137.         return sampleArray;
  138.     }

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

  152.                     for (int drawIndex = 0; drawIndex < drawCount; ++drawIndex)
  153.                     {
  154.                         mean += drawArray[drawIndex];
  155.                     }

  156.                     return mean / drawCount;
  157.                 }
  158.             }
  159.         };
  160.     }

  161.     private static final void SignificanceTest (
  162.         final ProbabilityIntegralTransformTest test,
  163.         final int drawCount,
  164.         final TestStatisticEvaluator testStatisticEvaluator,
  165.         final SignificanceTestSetting pTestSetting)
  166.         throws Exception
  167.     {
  168.         Sample testSample = GenerateSample (drawCount);

  169.         SignificanceTestOutcome significanceTest = test.significanceTest (
  170.             testSample.applyTestStatistic (testStatisticEvaluator),
  171.             pTestSetting
  172.         );

  173.         System.out.println (
  174.             "\t| " + FormatUtil.FormatDouble (significanceTest.testStatistic(), 1, 8, 1.) +
  175.             " | " + FormatUtil.FormatDouble (significanceTest.rightTailPValue(), 1, 8, 1.) +
  176.             " | " + FormatUtil.FormatDouble (significanceTest.leftTailPValue(), 1, 8, 1.) +
  177.             " | " + significanceTest.pass()
  178.         );
  179.     }

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

  185.         int drawCount = 10000;
  186.         int sampleCount = 100;
  187.         int significanceTestCount = 20;

  188.         TestStatisticEvaluator[] testStatisticEvaluatorArray = MakeTestStatisticEvaluatorArray();

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

  196.         ProbabilityIntegralTransformTest test = ensemble.significanceTest()[0];

  197.         SignificanceTestSetting significanceTestSettingRightTail = SignificanceTestSetting.FisherRightTail();

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

  199.         System.out.println ("\t|      ENSEMBLE SIGNIFICANCE RIGHT TAIL TEST      ||");

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

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

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

  203.         System.out.println ("\t|        - Right Tail p-Value                     ||");

  204.         System.out.println ("\t|        - Left Tail p-Value                      ||");

  205.         System.out.println ("\t|        - Significance Test Status               ||");

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

  207.         for (int significanceTest = 0; significanceTest < significanceTestCount; ++significanceTest)
  208.         {
  209.             SignificanceTest (
  210.                 test,
  211.                 drawCount,
  212.                 testStatisticEvaluatorArray[0],
  213.                 significanceTestSettingRightTail
  214.             );
  215.         }

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

  217.         SignificanceTestSetting significanceTestSettingLeftTail = SignificanceTestSetting.FisherLeftTail();

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

  219.         System.out.println ("\t|      ENSEMBLE SIGNIFICANCE RIGHT TAIL TEST      ||");

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

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

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

  223.         System.out.println ("\t|        - Right Tail p-Value                     ||");

  224.         System.out.println ("\t|        - Left Tail p-Value                      ||");

  225.         System.out.println ("\t|        - Significance Test Status               ||");

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

  227.         for (int significanceTest = 0; significanceTest < significanceTestCount; ++significanceTest)
  228.         {
  229.             SignificanceTest (
  230.                 test,
  231.                 drawCount,
  232.                 testStatisticEvaluatorArray[0],
  233.                 significanceTestSettingLeftTail
  234.             );
  235.         }

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

  237.         SignificanceTestSetting significanceTestSettingDoubleTail = SignificanceTestSetting.FisherDoubleTail();

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

  239.         System.out.println ("\t|      ENSEMBLE SIGNIFICANCE DOUBLE TAIL TEST     ||");

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

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

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

  243.         System.out.println ("\t|        - Right Tail p-Value                     ||");

  244.         System.out.println ("\t|        - Left Tail p-Value                      ||");

  245.         System.out.println ("\t|        - Significance Test Status               ||");

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

  247.         for (int significanceTest = 0; significanceTest < significanceTestCount; ++significanceTest)
  248.         {
  249.             SignificanceTest (
  250.                 test,
  251.                 drawCount,
  252.                 testStatisticEvaluatorArray[0],
  253.                 significanceTestSettingDoubleTail
  254.             );
  255.         }

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

  257.         EnvManager.TerminateEnv();
  258.     }
  259. }