StandardExponentialSignificanceTest.java

  1. package org.drip.sample.hypothesistest;

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

  110. public class StandardExponentialSignificanceTest
  111. {

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

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

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

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

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

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

  138.         return sampleArray;
  139.     }

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

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

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

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

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

  174.         System.out.println (
  175.             "\t| " + FormatUtil.FormatDouble (significanceTest.testStatistic(), 1, 8, 1.) +
  176.             " | " + FormatUtil.FormatDouble (significanceTest.rightTailPValue(), 1, 8, 1.) +
  177.             " | " + FormatUtil.FormatDouble (significanceTest.leftTailPValue(), 1, 8, 1.) +
  178.             " | " + significanceTest.pass()
  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 significanceTestCount = 20;

  189.         TestStatisticEvaluator[] testStatisticEvaluatorArray = MakeTestStatisticEvaluatorArray();

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

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

  198.         SignificanceTestSetting significanceTestSettingRightTail = SignificanceTestSetting.FisherRightTail();

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

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

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

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

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

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

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

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

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

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

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

  218.         SignificanceTestSetting significanceTestSettingLeftTail = SignificanceTestSetting.FisherLeftTail();

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

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

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

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

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

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

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

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

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

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

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

  238.         SignificanceTestSetting significanceTestSettingDoubleTail = SignificanceTestSetting.FisherDoubleTail();

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

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

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

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

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

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

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

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

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

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

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

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