ProbabilityIntegralTransformTest.java

  1. package org.drip.validation.hypothesis;

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

  113. public class ProbabilityIntegralTransformTest
  114. {
  115.     private org.drip.validation.hypothesis.ProbabilityIntegralTransform _probabilityIntegralTransform = null;

  116.     /**
  117.      * ProbabilityIntegralTransformTest Constructor
  118.      *
  119.      * @param probabilityIntegralTransform The Probability Integral Transform Instance
  120.      *
  121.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  122.      */

  123.     public ProbabilityIntegralTransformTest (
  124.         final org.drip.validation.hypothesis.ProbabilityIntegralTransform probabilityIntegralTransform)
  125.         throws java.lang.Exception
  126.     {
  127.         if (null == (_probabilityIntegralTransform = probabilityIntegralTransform))
  128.         {
  129.             throw new java.lang.Exception ("ProbabilityIntegralTransformTest Constructor => Invalid Inputs");
  130.         }
  131.     }

  132.     /**
  133.      * Retrieve the ProbabilityIntegralTransform Instance
  134.      *
  135.      * @return The ProbabilityIntegralTransform Instance
  136.      */

  137.     public org.drip.validation.hypothesis.ProbabilityIntegralTransform probabilityIntegralTransform()
  138.     {
  139.         return _probabilityIntegralTransform;
  140.     }

  141.     /**
  142.      * Run the Significance Test for the Realized Test Statistic
  143.      *
  144.      * @param testStatistic The Realized Test Statistic
  145.      * @param pTestSetting The P-Test Setting
  146.      *
  147.      * @return The Significance Test Result for the Realized Test Statistic
  148.      */

  149.     public org.drip.validation.hypothesis.SignificanceTestOutcome significanceTest (
  150.         final double testStatistic,
  151.         final org.drip.validation.hypothesis.SignificanceTestSetting pTestSetting)
  152.     {
  153.         if (!org.drip.numerical.common.NumberUtil.IsValid (testStatistic) || null == pTestSetting)
  154.         {
  155.             return null;
  156.         }

  157.         double pValue = java.lang.Double.NaN;

  158.         try
  159.         {
  160.             pValue = _probabilityIntegralTransform.pValue (testStatistic);
  161.         }
  162.         catch (java.lang.Exception e)
  163.         {
  164.             e.printStackTrace();

  165.             return null;
  166.         }

  167.         int tailCheck = pTestSetting.tailCheck();

  168.         double threshold = pTestSetting.threshold();

  169.         if (org.drip.validation.hypothesis.SignificanceTestSetting.LEFT_TAIL_CHECK == tailCheck)
  170.         {
  171.             try
  172.             {
  173.                 return new SignificanceTestOutcome (
  174.                     testStatistic,
  175.                     1. - pValue,
  176.                     pValue,
  177.                     pValue > threshold
  178.                 );
  179.             }
  180.             catch (java.lang.Exception e)
  181.             {
  182.                 e.printStackTrace();

  183.                 return null;
  184.             }
  185.         }

  186.         if (org.drip.validation.hypothesis.SignificanceTestSetting.RIGHT_TAIL_CHECK == tailCheck)
  187.         {
  188.             try
  189.             {
  190.                 return new SignificanceTestOutcome (
  191.                     testStatistic,
  192.                     1. - pValue,
  193.                     pValue,
  194.                     1. - pValue > threshold
  195.                 );
  196.             }
  197.             catch (java.lang.Exception e)
  198.             {
  199.                 e.printStackTrace();

  200.                 return null;
  201.             }
  202.         }

  203.         try
  204.         {
  205.             return new SignificanceTestOutcome (
  206.                 testStatistic,
  207.                 1. - pValue,
  208.                 pValue,
  209.                 2. * java.lang.Math.min (
  210.                     pValue,
  211.                     1. - pValue
  212.                 ) > threshold
  213.             );
  214.         }
  215.         catch (java.lang.Exception e)
  216.         {
  217.             e.printStackTrace();
  218.         }

  219.         return null;
  220.     }

  221.     /**
  222.      * Run a Distance Gap Test between the Hypothesis and the Sample
  223.      *
  224.      * @param samplePIT The Sample Probability Integral Transform
  225.      * @param gapTestSetting The Distance Gap Test Setting
  226.      *
  227.      * @return The Distance Gap Test Outcome
  228.      */

  229.     public org.drip.validation.distance.GapTestOutcome distanceTest (
  230.         final org.drip.validation.hypothesis.ProbabilityIntegralTransform samplePIT,
  231.         final org.drip.validation.distance.GapTestSetting gapTestSetting)
  232.     {
  233.         if (null == samplePIT || null == gapTestSetting)
  234.         {
  235.             return null;
  236.         }

  237.         double distance = 0.;
  238.         double hypothesisPValueLeft = 0.;

  239.         org.drip.validation.distance.GapLossFunction gapLossFunction = gapTestSetting.lossFunction();

  240.         org.drip.validation.distance.GapLossWeightFunction gapLossWeightFunction =
  241.             gapTestSetting.lossWeightFunction();

  242.         org.drip.validation.evidence.TestStatisticAccumulator weightedGapLossAccumulator = new
  243.             org.drip.validation.evidence.TestStatisticAccumulator();

  244.         org.drip.validation.evidence.TestStatisticAccumulator unweightedGapLossAccumulator = new
  245.             org.drip.validation.evidence.TestStatisticAccumulator();

  246.         for (java.util.Map.Entry<java.lang.Double, java.lang.Double> testStatisticPValueEntry :
  247.             samplePIT.testStatisticPValueMap().entrySet())
  248.         {
  249.             try
  250.             {
  251.                 double hypothesisPValueRight = _probabilityIntegralTransform.pValue
  252.                     (testStatisticPValueEntry.getKey());

  253.                 double gapLoss = gapLossFunction.loss (testStatisticPValueEntry.getValue() -
  254.                     hypothesisPValueRight);

  255.                 double weightedGapLoss = gapLoss * gapLossWeightFunction.weight (hypothesisPValueRight);

  256.                 distance = distance + weightedGapLoss * (hypothesisPValueRight - hypothesisPValueLeft);

  257.                 if (!unweightedGapLossAccumulator.addTestStatistic (gapLoss) ||
  258.                     !weightedGapLossAccumulator.addTestStatistic (weightedGapLoss))
  259.                 {
  260.                     return null;
  261.                 }

  262.                 hypothesisPValueLeft = hypothesisPValueRight;
  263.             }
  264.             catch (java.lang.Exception e)
  265.             {
  266.                 e.printStackTrace();

  267.                 return null;
  268.             }
  269.         }

  270.         try
  271.         {
  272.             return new org.drip.validation.distance.GapTestOutcome (
  273.                 unweightedGapLossAccumulator.probabilityIntegralTransform(),
  274.                 weightedGapLossAccumulator.probabilityIntegralTransform(),
  275.                 distance
  276.             );
  277.         }
  278.         catch (java.lang.Exception e)
  279.         {
  280.             e.printStackTrace();
  281.         }

  282.         return null;
  283.     }

  284.     /**
  285.      * Run a Histogram Test Corresponding to the Test Statistic and its p-Value
  286.      *
  287.      * @param histogramTestSetting The Histogram Test Setting
  288.      *
  289.      * @return The Histogram Test Corresponding to the Test Statistic and its p-Value
  290.      */

  291.     public org.drip.validation.hypothesis.HistogramTestOutcome histogramTest (
  292.         final org.drip.validation.hypothesis.HistogramTestSetting histogramTestSetting)
  293.     {
  294.         if (null == histogramTestSetting)
  295.         {
  296.             return null;
  297.         }

  298.         org.drip.validation.quantile.PlottingPositionGenerator plottingPositionGenerator =
  299.             histogramTestSetting.plottingPositionGenerator();

  300.         org.drip.validation.quantile.PlottingPosition[] plottingPositionArray =
  301.             plottingPositionGenerator.generate();

  302.         int qqVertexCount = plottingPositionArray.length + 2;
  303.         double[] testStatisticArray = new double[qqVertexCount];
  304.         double[] pValueCumulativeArray = new double[qqVertexCount];
  305.         double[] pValueIncrementalArray = new double[qqVertexCount];

  306.         try
  307.         {
  308.             pValueIncrementalArray[0] = 0.;

  309.             testStatisticArray[0] = _probabilityIntegralTransform.testStatistic
  310.                 (pValueCumulativeArray[0] = 0.);

  311.             testStatisticArray[qqVertexCount - 1] = _probabilityIntegralTransform.testStatistic
  312.                 (pValueCumulativeArray[qqVertexCount - 1] = 1.);
  313.         }
  314.         catch (java.lang.Exception e)
  315.         {
  316.             e.printStackTrace();

  317.             return null;
  318.         }

  319.         for (int qqVertexIndex = 1; qqVertexIndex < qqVertexCount - 1; ++qqVertexIndex)
  320.         {
  321.             try
  322.             {
  323.                 testStatisticArray[qqVertexIndex] = _probabilityIntegralTransform.testStatistic
  324.                     (pValueCumulativeArray[qqVertexIndex] =
  325.                         plottingPositionArray[qqVertexIndex - 1].quantile());

  326.                 pValueIncrementalArray[qqVertexIndex] = pValueCumulativeArray[qqVertexIndex] -
  327.                     pValueCumulativeArray[qqVertexIndex - 1];
  328.             }
  329.             catch (java.lang.Exception e)
  330.             {
  331.                 e.printStackTrace();

  332.                 return null;
  333.             }
  334.         }

  335.         pValueIncrementalArray[qqVertexCount - 1] = pValueCumulativeArray[qqVertexCount - 1] -
  336.             pValueCumulativeArray[qqVertexCount - 2];

  337.         try
  338.         {
  339.             return new org.drip.validation.hypothesis.HistogramTestOutcome (
  340.                 testStatisticArray,
  341.                 pValueCumulativeArray,
  342.                 pValueIncrementalArray,
  343.                 _probabilityIntegralTransform.testStatistic (histogramTestSetting.pValueThreshold())
  344.             );
  345.         }
  346.         catch (java.lang.Exception e)
  347.         {
  348.             e.printStackTrace();
  349.         }

  350.         return null;
  351.     }

  352.     /**
  353.      * Run the Quantile-Quantile Test
  354.      *
  355.      * @param samplePIT The Sample Probability Integral Transform
  356.      * @param plottingPositionGenerator The Quantile-Quantile Test Plotting Position Generator
  357.      *
  358.      * @return The Quantile-Quantile Test Outcome
  359.      */

  360.     public org.drip.validation.quantile.QQTestOutcome qqTest (
  361.         final org.drip.validation.hypothesis.ProbabilityIntegralTransform samplePIT,
  362.         final org.drip.validation.quantile.PlottingPositionGenerator plottingPositionGenerator)
  363.     {
  364.         if (null == samplePIT || null == plottingPositionGenerator)
  365.         {
  366.             return null;
  367.         }

  368.         org.drip.validation.quantile.PlottingPosition[] plottingPositionArray =
  369.             plottingPositionGenerator.generate();

  370.         if (null == plottingPositionArray)
  371.         {
  372.             return null;
  373.         }

  374.         int orderStatisticCount = plottingPositionArray.length;
  375.         org.drip.validation.quantile.QQVertex[] qqVertexArray = new
  376.             org.drip.validation.quantile.QQVertex[orderStatisticCount];

  377.         for (int orderStatisticIndex = 0; orderStatisticIndex < orderStatisticCount; ++orderStatisticIndex)
  378.         {
  379.             try
  380.             {
  381.                 double pValue = plottingPositionArray[orderStatisticIndex].quantile();
  382.        
  383.                 qqVertexArray[orderStatisticIndex] = new org.drip.validation.quantile.QQVertex (
  384.                     plottingPositionArray[orderStatisticIndex],
  385.                     samplePIT.testStatistic (pValue),
  386.                     _probabilityIntegralTransform.testStatistic (pValue)
  387.                 );
  388.             }
  389.             catch (java.lang.Exception e)
  390.             {
  391.                 e.printStackTrace();

  392.                 return null;
  393.             }
  394.         }

  395.         try
  396.         {
  397.             return new org.drip.validation.quantile.QQTestOutcome (qqVertexArray);
  398.         }
  399.         catch (java.lang.Exception e)
  400.         {
  401.             e.printStackTrace();
  402.         }

  403.         return null;
  404.     }
  405. }