InferenceTestUtils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.stat.inference;

  22. import java.util.Collection;

  23. import org.hipparchus.distribution.RealDistribution;
  24. import org.hipparchus.exception.MathIllegalArgumentException;
  25. import org.hipparchus.exception.MathIllegalStateException;
  26. import org.hipparchus.exception.NullArgumentException;
  27. import org.hipparchus.stat.descriptive.StatisticalSummary;

  28. /**
  29.  * A collection of static methods to create inference test instances or to
  30.  * perform inference tests.
  31.  */
  32. public class InferenceTestUtils  {

  33.     /** Singleton TTest instance. */
  34.     private static final TTest T_TEST = new TTest();

  35.     /** Singleton ChiSquareTest instance. */
  36.     private static final ChiSquareTest CHI_SQUARE_TEST = new ChiSquareTest();

  37.     /** Singleton OneWayAnova instance. */
  38.     private static final OneWayAnova ONE_WAY_ANANOVA = new OneWayAnova();

  39.     /** Singleton G-Test instance. */
  40.     private static final GTest G_TEST = new GTest();

  41.     /** Singleton K-S test instance */
  42.     private static final KolmogorovSmirnovTest KS_TEST = new KolmogorovSmirnovTest();

  43.     /**
  44.      * Prevent instantiation.
  45.      */
  46.     private InferenceTestUtils() {
  47.         super();
  48.     }

  49.     /**
  50.      * Computes a 2-sample t statistic,  under the hypothesis of equal
  51.      * subpopulation variances.  To compute a t-statistic without the
  52.      * equal variances hypothesis, use {@link #t(double[], double[])}.
  53.      * <p>
  54.      * This statistic can be used to perform a (homoscedastic) two-sample
  55.      * t-test to compare sample means.</p>
  56.      * <p>
  57.      * The t-statistic is</p>
  58.      * <p>
  59.      * &nbsp;&nbsp;<code>  t = (m1 - m2) / (sqrt(1/n1 +1/n2) sqrt(var))</code>
  60.      * </p><p>
  61.      * where <strong><code>n1</code></strong> is the size of first sample;
  62.      * <strong><code> n2</code></strong> is the size of second sample;
  63.      * <strong><code> m1</code></strong> is the mean of first sample;
  64.      * <strong><code> m2</code></strong> is the mean of second sample
  65.      * and <strong><code>var</code></strong> is the pooled variance estimate:
  66.      * </p><p>
  67.      * <code>var = sqrt(((n1 - 1)var1 + (n2 - 1)var2) / ((n1-1) + (n2-1)))</code>
  68.      * </p><p>
  69.      * with <strong><code>var1</code></strong> the variance of the first sample and
  70.      * <strong><code>var2</code></strong> the variance of the second sample.
  71.      * </p><p>
  72.      * <strong>Preconditions</strong>:</p>
  73.      * <ul>
  74.      * <li>The observed array lengths must both be at least 2.
  75.      * </li></ul>
  76.      *
  77.      * @param sample1 array of sample data values
  78.      * @param sample2 array of sample data values
  79.      * @return t statistic
  80.      * @throws NullArgumentException if the arrays are <code>null</code>
  81.      * @throws MathIllegalArgumentException if the length of the arrays is &lt; 2
  82.      */
  83.     public static double homoscedasticT(final double[] sample1, final double[] sample2)
  84.         throws MathIllegalArgumentException, NullArgumentException {
  85.         return T_TEST.homoscedasticT(sample1, sample2);
  86.     }

  87.     /**
  88.      * Computes a 2-sample t statistic, comparing the means of the datasets
  89.      * described by two {@link StatisticalSummary} instances, under the
  90.      * assumption of equal subpopulation variances.  To compute a t-statistic
  91.      * without the equal variances assumption, use
  92.      * {@link #t(StatisticalSummary, StatisticalSummary)}.
  93.      * <p>
  94.      * This statistic can be used to perform a (homoscedastic) two-sample
  95.      * t-test to compare sample means.</p>
  96.      * <p>
  97.      * The t-statistic returned is</p>
  98.      * <p>
  99.      * &nbsp;&nbsp;<code>  t = (m1 - m2) / (sqrt(1/n1 +1/n2) sqrt(var))</code>
  100.      * </p><p>
  101.      * where <strong><code>n1</code></strong> is the size of first sample;
  102.      * <strong><code> n2</code></strong> is the size of second sample;
  103.      * <strong><code> m1</code></strong> is the mean of first sample;
  104.      * <strong><code> m2</code></strong> is the mean of second sample
  105.      * and <strong><code>var</code></strong> is the pooled variance estimate:
  106.      * </p><p>
  107.      * <code>var = sqrt(((n1 - 1)var1 + (n2 - 1)var2) / ((n1-1) + (n2-1)))</code>
  108.      * </p><p>
  109.      * with <strong><code>var1</code></strong> the variance of the first sample and
  110.      * <strong><code>var2</code></strong> the variance of the second sample.
  111.      * </p><p>
  112.      * <strong>Preconditions</strong>:</p><ul>
  113.      * <li>The datasets described by the two Univariates must each contain
  114.      * at least 2 observations.
  115.      * </li></ul>
  116.      *
  117.      * @param sampleStats1 StatisticalSummary describing data from the first sample
  118.      * @param sampleStats2 StatisticalSummary describing data from the second sample
  119.      * @return t statistic
  120.      * @throws NullArgumentException if the sample statistics are <code>null</code>
  121.      * @throws MathIllegalArgumentException if the number of samples is &lt; 2
  122.      */
  123.     public static double homoscedasticT(final StatisticalSummary sampleStats1,
  124.                                         final StatisticalSummary sampleStats2)
  125.         throws MathIllegalArgumentException, NullArgumentException {
  126.         return T_TEST.homoscedasticT(sampleStats1, sampleStats2);
  127.     }

  128.     /**
  129.      * Performs a
  130.      * <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm">
  131.      * two-sided t-test</a> evaluating the null hypothesis that <code>sample1</code>
  132.      * and <code>sample2</code> are drawn from populations with the same mean,
  133.      * with significance level <code>alpha</code>,  assuming that the
  134.      * subpopulation variances are equal.  Use
  135.      * {@link #tTest(double[], double[], double)} to perform the test without
  136.      * the assumption of equal variances.
  137.      * <p>
  138.      * Returns <code>true</code> iff the null hypothesis that the means are
  139.      * equal can be rejected with confidence <code>1 - alpha</code>.  To
  140.      * perform a 1-sided test, use <code>alpha * 2.</code>  To perform the test
  141.      * without the assumption of equal subpopulation variances, use
  142.      * {@link #tTest(double[], double[], double)}.</p>
  143.      * <p>
  144.      * A pooled variance estimate is used to compute the t-statistic. See
  145.      * {@link #t(double[], double[])} for the formula. The sum of the sample
  146.      * sizes minus 2 is used as the degrees of freedom.</p>
  147.      * <p>
  148.      * <strong>Examples:</strong></p><ol>
  149.      * <li>To test the (2-sided) hypothesis <code>mean 1 = mean 2 </code> at
  150.      * the 95% level, use <br><code>tTest(sample1, sample2, 0.05). </code>
  151.      * </li>
  152.      * <li>To test the (one-sided) hypothesis <code> mean 1 &lt; mean 2, </code>
  153.      * at the 99% level, first verify that the measured mean of
  154.      * <code>sample 1</code> is less than the mean of <code>sample 2</code>
  155.      * and then use
  156.      * <br><code>tTest(sample1, sample2, 0.02) </code>
  157.      * </li></ol>
  158.      * <p>
  159.      * <strong>Usage Note:</strong><br>
  160.      * The validity of the test depends on the assumptions of the parametric
  161.      * t-test procedure, as discussed
  162.      * <a href="http://www.basic.nwu.edu/statguidefiles/ttest_unpaired_ass_viol.html">
  163.      * here</a></p>
  164.      * <p>
  165.      * <strong>Preconditions</strong>:</p>
  166.      * <ul>
  167.      * <li>The observed array lengths must both be at least 2.
  168.      * </li>
  169.      * <li> <code> 0 &lt; alpha &lt; 0.5 </code>
  170.      * </li></ul>
  171.      *
  172.      * @param sample1 array of sample data values
  173.      * @param sample2 array of sample data values
  174.      * @param alpha significance level of the test
  175.      * @return true if the null hypothesis can be rejected with
  176.      * confidence 1 - alpha
  177.      * @throws NullArgumentException if the arrays are <code>null</code>
  178.      * @throws MathIllegalArgumentException if the length of the arrays is &lt; 2
  179.      * @throws MathIllegalArgumentException if <code>alpha</code> is not in the range (0, 0.5]
  180.      * @throws MathIllegalStateException if an error occurs computing the p-value
  181.      */
  182.     public static boolean homoscedasticTTest(final double[] sample1, final double[] sample2,
  183.                                              final double alpha)
  184.         throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException {
  185.         return T_TEST.homoscedasticTTest(sample1, sample2, alpha);
  186.     }

  187.     /**
  188.      * Returns the <i>observed significance level</i>, or
  189.      * <i>p-value</i>, associated with a two-sample, two-tailed t-test
  190.      * comparing the means of the input arrays, under the assumption that
  191.      * the two samples are drawn from subpopulations with equal variances.
  192.      * To perform the test without the equal variances assumption, use
  193.      * {@link #tTest(double[], double[])}.
  194.      * <p>
  195.      * The number returned is the smallest significance level
  196.      * at which one can reject the null hypothesis that the two means are
  197.      * equal in favor of the two-sided alternative that they are different.
  198.      * For a one-sided test, divide the returned value by 2.</p>
  199.      * <p>
  200.      * A pooled variance estimate is used to compute the t-statistic.  See
  201.      * {@link #homoscedasticT(double[], double[])}. The sum of the sample sizes
  202.      * minus 2 is used as the degrees of freedom.</p>
  203.      * <p>
  204.      * <strong>Usage Note:</strong><br>
  205.      * The validity of the p-value depends on the assumptions of the parametric
  206.      * t-test procedure, as discussed
  207.      * <a href="http://www.basic.nwu.edu/statguidefiles/ttest_unpaired_ass_viol.html">
  208.      * here</a></p>
  209.      * <p>
  210.      * <strong>Preconditions</strong>:</p>
  211.      * <ul>
  212.      * <li>The observed array lengths must both be at least 2.
  213.      * </li></ul>
  214.      *
  215.      * @param sample1 array of sample data values
  216.      * @param sample2 array of sample data values
  217.      * @return p-value for t-test
  218.      * @throws NullArgumentException if the arrays are <code>null</code>
  219.      * @throws MathIllegalArgumentException if the length of the arrays is &lt; 2
  220.      * @throws MathIllegalStateException if an error occurs computing the p-value
  221.      */
  222.     public static double homoscedasticTTest(final double[] sample1, final double[] sample2)
  223.         throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException {
  224.         return T_TEST.homoscedasticTTest(sample1, sample2);
  225.     }

  226.     /**
  227.      * Returns the <i>observed significance level</i>, or
  228.      * <i>p-value</i>, associated with a two-sample, two-tailed t-test
  229.      * comparing the means of the datasets described by two StatisticalSummary
  230.      * instances, under the hypothesis of equal subpopulation variances. To
  231.      * perform a test without the equal variances assumption, use
  232.      * {@link #tTest(StatisticalSummary, StatisticalSummary)}.
  233.      * <p>
  234.      * The number returned is the smallest significance level
  235.      * at which one can reject the null hypothesis that the two means are
  236.      * equal in favor of the two-sided alternative that they are different.
  237.      * For a one-sided test, divide the returned value by 2.</p>
  238.      * <p>
  239.      * See {@link #homoscedasticT(double[], double[])} for the formula used to
  240.      * compute the t-statistic. The sum of the  sample sizes minus 2 is used as
  241.      * the degrees of freedom.</p>
  242.      * <p>
  243.      * <strong>Usage Note:</strong><br>
  244.      * The validity of the p-value depends on the assumptions of the parametric
  245.      * t-test procedure, as discussed
  246.      * <a href="http://www.basic.nwu.edu/statguidefiles/ttest_unpaired_ass_viol.html">here</a>
  247.      * </p><p>
  248.      * <strong>Preconditions</strong>:</p>
  249.      * <ul>
  250.      * <li>The datasets described by the two Univariates must each contain
  251.      * at least 2 observations.
  252.      * </li></ul>
  253.      *
  254.      * @param sampleStats1  StatisticalSummary describing data from the first sample
  255.      * @param sampleStats2  StatisticalSummary describing data from the second sample
  256.      * @return p-value for t-test
  257.      * @throws NullArgumentException if the sample statistics are <code>null</code>
  258.      * @throws MathIllegalArgumentException if the number of samples is &lt; 2
  259.      * @throws MathIllegalStateException if an error occurs computing the p-value
  260.      */
  261.     public static double homoscedasticTTest(final StatisticalSummary sampleStats1,
  262.                                             final StatisticalSummary sampleStats2)
  263.         throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException {
  264.         return T_TEST.homoscedasticTTest(sampleStats1, sampleStats2);
  265.     }

  266.     /**
  267.      * Computes a paired, 2-sample t-statistic based on the data in the input
  268.      * arrays.  The t-statistic returned is equivalent to what would be returned by
  269.      * computing the one-sample t-statistic {@link #t(double, double[])}, with
  270.      * <code>mu = 0</code> and the sample array consisting of the (signed)
  271.      * differences between corresponding entries in <code>sample1</code> and
  272.      * <code>sample2.</code>
  273.      * <p>
  274.      * <strong>Preconditions</strong>:</p>
  275.      * <ul>
  276.      * <li>The input arrays must have the same length and their common length
  277.      * must be at least 2.
  278.      * </li></ul>
  279.      *
  280.      * @param sample1 array of sample data values
  281.      * @param sample2 array of sample data values
  282.      * @return t statistic
  283.      * @throws NullArgumentException if the arrays are <code>null</code>
  284.      * @throws MathIllegalArgumentException if the arrays are empty
  285.      * @throws MathIllegalArgumentException if the length of the arrays is not equal
  286.      * @throws MathIllegalArgumentException if the length of the arrays is &lt; 2
  287.      */
  288.     public static double pairedT(final double[] sample1, final double[] sample2)
  289.         throws MathIllegalArgumentException, NullArgumentException {
  290.         return T_TEST.pairedT(sample1, sample2);
  291.     }

  292.     /**
  293.      * Performs a paired t-test evaluating the null hypothesis that the
  294.      * mean of the paired differences between <code>sample1</code> and
  295.      * <code>sample2</code> is 0 in favor of the two-sided alternative that the
  296.      * mean paired difference is not equal to 0, with significance level
  297.      * <code>alpha</code>.
  298.      * <p>
  299.      * Returns <code>true</code> iff the null hypothesis can be rejected with
  300.      * confidence <code>1 - alpha</code>.  To perform a 1-sided test, use
  301.      * <code>alpha * 2</code></p>
  302.      * <p>
  303.      * <strong>Usage Note:</strong><br>
  304.      * The validity of the test depends on the assumptions of the parametric
  305.      * t-test procedure, as discussed
  306.      * <a href="http://www.basic.nwu.edu/statguidefiles/ttest_unpaired_ass_viol.html">
  307.      * here</a></p>
  308.      * <p>
  309.      * <strong>Preconditions</strong>:</p>
  310.      * <ul>
  311.      * <li>The input array lengths must be the same and their common length
  312.      * must be at least 2.
  313.      * </li>
  314.      * <li> <code> 0 &lt; alpha &lt; 0.5 </code>
  315.      * </li></ul>
  316.      *
  317.      * @param sample1 array of sample data values
  318.      * @param sample2 array of sample data values
  319.      * @param alpha significance level of the test
  320.      * @return true if the null hypothesis can be rejected with
  321.      * confidence 1 - alpha
  322.      * @throws NullArgumentException if the arrays are <code>null</code>
  323.      * @throws MathIllegalArgumentException if the arrays are empty
  324.      * @throws MathIllegalArgumentException if the length of the arrays is not equal
  325.      * @throws MathIllegalArgumentException if the length of the arrays is &lt; 2
  326.      * @throws MathIllegalArgumentException if <code>alpha</code> is not in the range (0, 0.5]
  327.      * @throws MathIllegalStateException if an error occurs computing the p-value
  328.      */
  329.     public static boolean pairedTTest(final double[] sample1, final double[] sample2,
  330.                                       final double alpha)
  331.         throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException {
  332.         return T_TEST.pairedTTest(sample1, sample2, alpha);
  333.     }

  334.     /**
  335.      * Returns the <i>observed significance level</i>, or
  336.      * <i> p-value</i>, associated with a paired, two-sample, two-tailed t-test
  337.      * based on the data in the input arrays.
  338.      * <p>
  339.      * The number returned is the smallest significance level
  340.      * at which one can reject the null hypothesis that the mean of the paired
  341.      * differences is 0 in favor of the two-sided alternative that the mean paired
  342.      * difference is not equal to 0. For a one-sided test, divide the returned
  343.      * value by 2.</p>
  344.      * <p>
  345.      * This test is equivalent to a one-sample t-test computed using
  346.      * {@link #tTest(double, double[])} with <code>mu = 0</code> and the sample
  347.      * array consisting of the signed differences between corresponding elements of
  348.      * <code>sample1</code> and <code>sample2.</code></p>
  349.      * <p>
  350.      * <strong>Usage Note:</strong><br>
  351.      * The validity of the p-value depends on the assumptions of the parametric
  352.      * t-test procedure, as discussed
  353.      * <a href="http://www.basic.nwu.edu/statguidefiles/ttest_unpaired_ass_viol.html">
  354.      * here</a></p>
  355.      * <p>
  356.      * <strong>Preconditions</strong>:</p>
  357.      * <ul>
  358.      * <li>The input array lengths must be the same and their common length must
  359.      * be at least 2.
  360.      * </li></ul>
  361.      *
  362.      * @param sample1 array of sample data values
  363.      * @param sample2 array of sample data values
  364.      * @return p-value for t-test
  365.      * @throws NullArgumentException if the arrays are <code>null</code>
  366.      * @throws MathIllegalArgumentException if the arrays are empty
  367.      * @throws MathIllegalArgumentException if the length of the arrays is not equal
  368.      * @throws MathIllegalArgumentException if the length of the arrays is &lt; 2
  369.      * @throws MathIllegalStateException if an error occurs computing the p-value
  370.      */
  371.     public static double pairedTTest(final double[] sample1, final double[] sample2)
  372.         throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException {
  373.         return T_TEST.pairedTTest(sample1, sample2);
  374.     }

  375.     /**
  376.      * Computes a <a href="http://www.itl.nist.gov/div898/handbook/prc/section2/prc22.htm#formula">
  377.      * t statistic </a> given observed values and a comparison constant.
  378.      * <p>
  379.      * This statistic can be used to perform a one sample t-test for the mean.
  380.      * </p><p>
  381.      * <strong>Preconditions</strong>:</p>
  382.      * <ul>
  383.      * <li>The observed array length must be at least 2.
  384.      * </li></ul>
  385.      *
  386.      * @param mu comparison constant
  387.      * @param observed array of values
  388.      * @return t statistic
  389.      * @throws NullArgumentException if <code>observed</code> is <code>null</code>
  390.      * @throws MathIllegalArgumentException if the length of <code>observed</code> is &lt; 2
  391.      */
  392.     public static double t(final double mu, final double[] observed)
  393.         throws MathIllegalArgumentException, NullArgumentException {
  394.         return T_TEST.t(mu, observed);
  395.     }

  396.     /**
  397.      * Computes a <a href="http://www.itl.nist.gov/div898/handbook/prc/section2/prc22.htm#formula">
  398.      * t statistic </a> to use in comparing the mean of the dataset described by
  399.      * <code>sampleStats</code> to <code>mu</code>.
  400.      * <p>
  401.      * This statistic can be used to perform a one sample t-test for the mean.
  402.      * </p><p>
  403.      * <strong>Preconditions</strong>:</p>
  404.      * <ul>
  405.      * <li><code>observed.getN() &ge; 2</code>.
  406.      * </li></ul>
  407.      *
  408.      * @param mu comparison constant
  409.      * @param sampleStats DescriptiveStatistics holding sample summary statitstics
  410.      * @return t statistic
  411.      * @throws NullArgumentException if <code>sampleStats</code> is <code>null</code>
  412.      * @throws MathIllegalArgumentException if the number of samples is &lt; 2
  413.      */
  414.     public static double t(final double mu, final StatisticalSummary sampleStats)
  415.         throws MathIllegalArgumentException, NullArgumentException {
  416.         return T_TEST.t(mu, sampleStats);
  417.     }

  418.     /**
  419.      * Computes a 2-sample t statistic, without the hypothesis of equal
  420.      * subpopulation variances.  To compute a t-statistic assuming equal
  421.      * variances, use {@link #homoscedasticT(double[], double[])}.
  422.      * <p>
  423.      * This statistic can be used to perform a two-sample t-test to compare
  424.      * sample means.</p>
  425.      * <p>
  426.      * The t-statistic is</p>
  427.      * <p>
  428.      * &nbsp;&nbsp; <code>  t = (m1 - m2) / sqrt(var1/n1 + var2/n2)</code>
  429.      * </p><p>
  430.      *  where <strong><code>n1</code></strong> is the size of the first sample
  431.      * <strong><code> n2</code></strong> is the size of the second sample;
  432.      * <strong><code> m1</code></strong> is the mean of the first sample;
  433.      * <strong><code> m2</code></strong> is the mean of the second sample;
  434.      * <strong><code> var1</code></strong> is the variance of the first sample;
  435.      * <strong><code> var2</code></strong> is the variance of the second sample;
  436.      * </p><p>
  437.      * <strong>Preconditions</strong>:</p>
  438.      * <ul>
  439.      * <li>The observed array lengths must both be at least 2.
  440.      * </li></ul>
  441.      *
  442.      * @param sample1 array of sample data values
  443.      * @param sample2 array of sample data values
  444.      * @return t statistic
  445.      * @throws NullArgumentException if the arrays are <code>null</code>
  446.      * @throws MathIllegalArgumentException if the length of the arrays is &lt; 2
  447.      */
  448.     public static double t(final double[] sample1, final double[] sample2)
  449.         throws MathIllegalArgumentException, NullArgumentException {
  450.         return T_TEST.t(sample1, sample2);
  451.     }

  452.     /**
  453.      * Computes a 2-sample t statistic, comparing the means of the datasets
  454.      * described by two {@link StatisticalSummary} instances, without the
  455.      * assumption of equal subpopulation variances.  Use
  456.      * {@link #homoscedasticT(StatisticalSummary, StatisticalSummary)} to
  457.      * compute a t-statistic under the equal variances assumption.
  458.      * <p>
  459.      * This statistic can be used to perform a two-sample t-test to compare
  460.      * sample means.</p>
  461.      * <p>
  462.       * The returned  t-statistic is</p>
  463.      * <p>
  464.      * &nbsp;&nbsp; <code>  t = (m1 - m2) / sqrt(var1/n1 + var2/n2)</code>
  465.      * </p><p>
  466.      * where <strong><code>n1</code></strong> is the size of the first sample;
  467.      * <strong><code> n2</code></strong> is the size of the second sample;
  468.      * <strong><code> m1</code></strong> is the mean of the first sample;
  469.      * <strong><code> m2</code></strong> is the mean of the second sample
  470.      * <strong><code> var1</code></strong> is the variance of the first sample;
  471.      * <strong><code> var2</code></strong> is the variance of the second sample
  472.      * </p><p>
  473.      * <strong>Preconditions</strong>:</p>
  474.      * <ul>
  475.      * <li>The datasets described by the two Univariates must each contain
  476.      * at least 2 observations.
  477.      * </li></ul>
  478.      *
  479.      * @param sampleStats1 StatisticalSummary describing data from the first sample
  480.      * @param sampleStats2 StatisticalSummary describing data from the second sample
  481.      * @return t statistic
  482.      * @throws NullArgumentException if the sample statistics are <code>null</code>
  483.      * @throws MathIllegalArgumentException if the number of samples is &lt; 2
  484.      */
  485.     public static double t(final StatisticalSummary sampleStats1,
  486.                            final StatisticalSummary sampleStats2)
  487.         throws MathIllegalArgumentException, NullArgumentException {
  488.         return T_TEST.t(sampleStats1, sampleStats2);
  489.     }

  490.     /**
  491.      * Performs a <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm">
  492.      * two-sided t-test</a> evaluating the null hypothesis that the mean of the population from
  493.      * which <code>sample</code> is drawn equals <code>mu</code>.
  494.      * <p>
  495.      * Returns <code>true</code> iff the null hypothesis can be
  496.      * rejected with confidence <code>1 - alpha</code>.  To
  497.      * perform a 1-sided test, use <code>alpha * 2</code></p>
  498.      * <p>
  499.      * <strong>Examples:</strong></p><ol>
  500.      * <li>To test the (2-sided) hypothesis <code>sample mean = mu </code> at
  501.      * the 95% level, use <br><code>tTest(mu, sample, 0.05) </code>
  502.      * </li>
  503.      * <li>To test the (one-sided) hypothesis <code> sample mean &lt; mu </code>
  504.      * at the 99% level, first verify that the measured sample mean is less
  505.      * than <code>mu</code> and then use
  506.      * <br><code>tTest(mu, sample, 0.02) </code>
  507.      * </li></ol>
  508.      * <p>
  509.      * <strong>Usage Note:</strong><br>
  510.      * The validity of the test depends on the assumptions of the one-sample
  511.      * parametric t-test procedure, as discussed
  512.      * <a href="http://www.basic.nwu.edu/statguidefiles/sg_glos.html#one-sample">here</a>
  513.      * </p><p>
  514.      * <strong>Preconditions</strong>:</p>
  515.      * <ul>
  516.      * <li>The observed array length must be at least 2.
  517.      * </li></ul>
  518.      *
  519.      * @param mu constant value to compare sample mean against
  520.      * @param sample array of sample data values
  521.      * @param alpha significance level of the test
  522.      * @return p-value
  523.      * @throws NullArgumentException if the sample array is <code>null</code>
  524.      * @throws MathIllegalArgumentException if the length of the array is &lt; 2
  525.      * @throws MathIllegalArgumentException if <code>alpha</code> is not in the range (0, 0.5]
  526.      * @throws MathIllegalStateException if an error computing the p-value
  527.      */
  528.     public static boolean tTest(final double mu, final double[] sample, final double alpha)
  529.         throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException {
  530.         return T_TEST.tTest(mu, sample, alpha);
  531.     }

  532.     /**
  533.      * Returns the <i>observed significance level</i>, or
  534.      * <i>p-value</i>, associated with a one-sample, two-tailed t-test
  535.      * comparing the mean of the input array with the constant <code>mu</code>.
  536.      * <p>
  537.      * The number returned is the smallest significance level
  538.      * at which one can reject the null hypothesis that the mean equals
  539.      * <code>mu</code> in favor of the two-sided alternative that the mean
  540.      * is different from <code>mu</code>. For a one-sided test, divide the
  541.      * returned value by 2.</p>
  542.      * <p>
  543.      * <strong>Usage Note:</strong><br>
  544.      * The validity of the test depends on the assumptions of the parametric
  545.      * t-test procedure, as discussed
  546.      * <a href="http://www.basic.nwu.edu/statguidefiles/ttest_unpaired_ass_viol.html">here</a>
  547.      * </p><p>
  548.      * <strong>Preconditions</strong>:</p>
  549.      * <ul>
  550.      * <li>The observed array length must be at least 2.
  551.      * </li></ul>
  552.      *
  553.      * @param mu constant value to compare sample mean against
  554.      * @param sample array of sample data values
  555.      * @return p-value
  556.      * @throws NullArgumentException if the sample array is <code>null</code>
  557.      * @throws MathIllegalArgumentException if the length of the array is &lt; 2
  558.      * @throws MathIllegalStateException if an error occurs computing the p-value
  559.      */
  560.     public static double tTest(final double mu, final double[] sample)
  561.         throws MathIllegalArgumentException, NullArgumentException,
  562.         MathIllegalStateException {
  563.         return T_TEST.tTest(mu, sample);
  564.     }

  565.     /**
  566.      * Performs a <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm">
  567.      * two-sided t-test</a> evaluating the null hypothesis that the mean of the
  568.      * population from which the dataset described by <code>stats</code> is
  569.      * drawn equals <code>mu</code>.
  570.      * <p>
  571.      * Returns <code>true</code> iff the null hypothesis can be rejected with
  572.      * confidence <code>1 - alpha</code>.  To  perform a 1-sided test, use
  573.      * <code>alpha * 2.</code></p>
  574.      * <p>
  575.      * <strong>Examples:</strong></p><ol>
  576.      * <li>To test the (2-sided) hypothesis <code>sample mean = mu </code> at
  577.      * the 95% level, use <br><code>tTest(mu, sampleStats, 0.05) </code>
  578.      * </li>
  579.      * <li>To test the (one-sided) hypothesis <code> sample mean &lt; mu </code>
  580.      * at the 99% level, first verify that the measured sample mean is less
  581.      * than <code>mu</code> and then use
  582.      * <br><code>tTest(mu, sampleStats, 0.02) </code>
  583.      * </li></ol>
  584.      * <p>
  585.      * <strong>Usage Note:</strong><br>
  586.      * The validity of the test depends on the assumptions of the one-sample
  587.      * parametric t-test procedure, as discussed
  588.      * <a href="http://www.basic.nwu.edu/statguidefiles/sg_glos.html#one-sample">here</a>
  589.      * </p><p>
  590.      * <strong>Preconditions</strong>:</p>
  591.      * <ul>
  592.      * <li>The sample must include at least 2 observations.
  593.      * </li></ul>
  594.      *
  595.      * @param mu constant value to compare sample mean against
  596.      * @param sampleStats StatisticalSummary describing sample data values
  597.      * @param alpha significance level of the test
  598.      * @return p-value
  599.      * @throws NullArgumentException if <code>sampleStats</code> is <code>null</code>
  600.      * @throws MathIllegalArgumentException if the number of samples is &lt; 2
  601.      * @throws MathIllegalArgumentException if <code>alpha</code> is not in the range (0, 0.5]
  602.      * @throws MathIllegalStateException if an error occurs computing the p-value
  603.      */
  604.     public static boolean tTest(final double mu, final StatisticalSummary sampleStats,
  605.                                 final double alpha)
  606.         throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException {
  607.         return T_TEST.tTest(mu, sampleStats, alpha);
  608.     }

  609.     /**
  610.      * Returns the <i>observed significance level</i>, or
  611.      * <i>p-value</i>, associated with a one-sample, two-tailed t-test
  612.      * comparing the mean of the dataset described by <code>sampleStats</code>
  613.      * with the constant <code>mu</code>.
  614.      * <p>
  615.      * The number returned is the smallest significance level
  616.      * at which one can reject the null hypothesis that the mean equals
  617.      * <code>mu</code> in favor of the two-sided alternative that the mean
  618.      * is different from <code>mu</code>. For a one-sided test, divide the
  619.      * returned value by 2.</p>
  620.      * <p>
  621.      * <strong>Usage Note:</strong><br>
  622.      * The validity of the test depends on the assumptions of the parametric
  623.      * t-test procedure, as discussed
  624.      * <a href="http://www.basic.nwu.edu/statguidefiles/ttest_unpaired_ass_viol.html">
  625.      * here</a></p>
  626.      * <p>
  627.      * <strong>Preconditions</strong>:</p>
  628.      * <ul>
  629.      * <li>The sample must contain at least 2 observations.
  630.      * </li></ul>
  631.      *
  632.      * @param mu constant value to compare sample mean against
  633.      * @param sampleStats StatisticalSummary describing sample data
  634.      * @return p-value
  635.      * @throws NullArgumentException if <code>sampleStats</code> is <code>null</code>
  636.      * @throws MathIllegalArgumentException if the number of samples is &lt; 2
  637.      * @throws MathIllegalStateException if an error occurs computing the p-value
  638.      */
  639.     public static double tTest(final double mu, final StatisticalSummary sampleStats)
  640.         throws MathIllegalArgumentException, NullArgumentException,
  641.         MathIllegalStateException {
  642.         return T_TEST.tTest(mu, sampleStats);
  643.     }

  644.     /**
  645.      * Performs a
  646.      * <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm">
  647.      * two-sided t-test</a> evaluating the null hypothesis that <code>sample1</code>
  648.      * and <code>sample2</code> are drawn from populations with the same mean,
  649.      * with significance level <code>alpha</code>.  This test does not assume
  650.      * that the subpopulation variances are equal.  To perform the test assuming
  651.      * equal variances, use
  652.      * {@link #homoscedasticTTest(double[], double[], double)}.
  653.      * <p>
  654.      * Returns <code>true</code> iff the null hypothesis that the means are
  655.      * equal can be rejected with confidence <code>1 - alpha</code>.  To
  656.      * perform a 1-sided test, use <code>alpha * 2</code></p>
  657.      * <p>
  658.      * See {@link #t(double[], double[])} for the formula used to compute the
  659.      * t-statistic.  Degrees of freedom are approximated using the
  660.      * <a href="http://www.itl.nist.gov/div898/handbook/prc/section3/prc31.htm">
  661.      * Welch-Satterthwaite approximation.</a></p>
  662.      * <p>
  663.      * <strong>Examples:</strong></p><ol>
  664.      * <li>To test the (2-sided) hypothesis <code>mean 1 = mean 2 </code> at
  665.      * the 95% level,  use
  666.      * <br><code>tTest(sample1, sample2, 0.05). </code>
  667.      * </li>
  668.      * <li>To test the (one-sided) hypothesis <code> mean 1 &lt; mean 2 </code>,
  669.      * at the 99% level, first verify that the measured  mean of <code>sample 1</code>
  670.      * is less than the mean of <code>sample 2</code> and then use
  671.      * <br><code>tTest(sample1, sample2, 0.02) </code>
  672.      * </li></ol>
  673.      * <p>
  674.      * <strong>Usage Note:</strong><br>
  675.      * The validity of the test depends on the assumptions of the parametric
  676.      * t-test procedure, as discussed
  677.      * <a href="http://www.basic.nwu.edu/statguidefiles/ttest_unpaired_ass_viol.html">
  678.      * here</a></p>
  679.      * <p>
  680.      * <strong>Preconditions</strong>:</p>
  681.      * <ul>
  682.      * <li>The observed array lengths must both be at least 2.
  683.      * </li>
  684.      * <li> <code> 0 &lt; alpha &lt; 0.5 </code>
  685.      * </li></ul>
  686.      *
  687.      * @param sample1 array of sample data values
  688.      * @param sample2 array of sample data values
  689.      * @param alpha significance level of the test
  690.      * @return true if the null hypothesis can be rejected with
  691.      * confidence 1 - alpha
  692.      * @throws NullArgumentException if the arrays are <code>null</code>
  693.      * @throws MathIllegalArgumentException if the length of the arrays is &lt; 2
  694.      * @throws MathIllegalArgumentException if <code>alpha</code> is not in the range (0, 0.5]
  695.      * @throws MathIllegalStateException if an error occurs computing the p-value
  696.      */
  697.     public static boolean tTest(final double[] sample1, final double[] sample2,
  698.                                 final double alpha)
  699.         throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException {
  700.         return T_TEST.tTest(sample1, sample2, alpha);
  701.     }

  702.     /**
  703.      * Returns the <i>observed significance level</i>, or
  704.      * <i>p-value</i>, associated with a two-sample, two-tailed t-test
  705.      * comparing the means of the input arrays.
  706.      * <p>
  707.      * The number returned is the smallest significance level
  708.      * at which one can reject the null hypothesis that the two means are
  709.      * equal in favor of the two-sided alternative that they are different.
  710.      * For a one-sided test, divide the returned value by 2.</p>
  711.      * <p>
  712.      * The test does not assume that the underlying popuation variances are
  713.      * equal  and it uses approximated degrees of freedom computed from the
  714.      * sample data to compute the p-value.  The t-statistic used is as defined in
  715.      * {@link #t(double[], double[])} and the Welch-Satterthwaite approximation
  716.      * to the degrees of freedom is used,
  717.      * as described
  718.      * <a href="http://www.itl.nist.gov/div898/handbook/prc/section3/prc31.htm">
  719.      * here.</a>  To perform the test under the assumption of equal subpopulation
  720.      * variances, use {@link #homoscedasticTTest(double[], double[])}.</p>
  721.      * <p>
  722.      * <strong>Usage Note:</strong><br>
  723.      * The validity of the p-value depends on the assumptions of the parametric
  724.      * t-test procedure, as discussed
  725.      * <a href="http://www.basic.nwu.edu/statguidefiles/ttest_unpaired_ass_viol.html">
  726.      * here</a></p>
  727.      * <p>
  728.      * <strong>Preconditions</strong>:</p>
  729.      * <ul>
  730.      * <li>The observed array lengths must both be at least 2.
  731.      * </li></ul>
  732.      *
  733.      * @param sample1 array of sample data values
  734.      * @param sample2 array of sample data values
  735.      * @return p-value for t-test
  736.      * @throws NullArgumentException if the arrays are <code>null</code>
  737.      * @throws MathIllegalArgumentException if the length of the arrays is &lt; 2
  738.      * @throws MathIllegalStateException if an error occurs computing the p-value
  739.      */
  740.     public static double tTest(final double[] sample1, final double[] sample2)
  741.         throws MathIllegalArgumentException, NullArgumentException,
  742.         MathIllegalStateException {
  743.         return T_TEST.tTest(sample1, sample2);
  744.     }

  745.     /**
  746.      * Performs a
  747.      * <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm">
  748.      * two-sided t-test</a> evaluating the null hypothesis that
  749.      * <code>sampleStats1</code> and <code>sampleStats2</code> describe
  750.      * datasets drawn from populations with the same mean, with significance
  751.      * level <code>alpha</code>.   This test does not assume that the
  752.      * subpopulation variances are equal.  To perform the test under the equal
  753.      * variances assumption, use
  754.      * {@link #homoscedasticTTest(StatisticalSummary, StatisticalSummary)}.
  755.      * <p>
  756.      * Returns <code>true</code> iff the null hypothesis that the means are
  757.      * equal can be rejected with confidence <code>1 - alpha</code>.  To
  758.      * perform a 1-sided test, use <code>alpha * 2</code></p>
  759.      * <p>
  760.      * See {@link #t(double[], double[])} for the formula used to compute the
  761.      * t-statistic.  Degrees of freedom are approximated using the
  762.      * <a href="http://www.itl.nist.gov/div898/handbook/prc/section3/prc31.htm">
  763.      * Welch-Satterthwaite approximation.</a></p>
  764.      * <p>
  765.      * <strong>Examples:</strong></p><ol>
  766.      * <li>To test the (2-sided) hypothesis <code>mean 1 = mean 2 </code> at
  767.      * the 95%, use
  768.      * <br><code>tTest(sampleStats1, sampleStats2, 0.05) </code>
  769.      * </li>
  770.      * <li>To test the (one-sided) hypothesis <code> mean 1 &lt; mean 2 </code>
  771.      * at the 99% level,  first verify that the measured mean of
  772.      * <code>sample 1</code> is less than  the mean of <code>sample 2</code>
  773.      * and then use
  774.      * <br><code>tTest(sampleStats1, sampleStats2, 0.02) </code>
  775.      * </li></ol>
  776.      * <p>
  777.      * <strong>Usage Note:</strong><br>
  778.      * The validity of the test depends on the assumptions of the parametric
  779.      * t-test procedure, as discussed
  780.      * <a href="http://www.basic.nwu.edu/statguidefiles/ttest_unpaired_ass_viol.html">
  781.      * here</a></p>
  782.      * <p>
  783.      * <strong>Preconditions</strong>:</p>
  784.      * <ul>
  785.      * <li>The datasets described by the two Univariates must each contain
  786.      * at least 2 observations.
  787.      * </li>
  788.      * <li> <code> 0 &lt; alpha &lt; 0.5 </code>
  789.      * </li></ul>
  790.      *
  791.      * @param sampleStats1 StatisticalSummary describing sample data values
  792.      * @param sampleStats2 StatisticalSummary describing sample data values
  793.      * @param alpha significance level of the test
  794.      * @return true if the null hypothesis can be rejected with
  795.      * confidence 1 - alpha
  796.      * @throws NullArgumentException if the sample statistics are <code>null</code>
  797.      * @throws MathIllegalArgumentException if the number of samples is &lt; 2
  798.      * @throws MathIllegalArgumentException if <code>alpha</code> is not in the range (0, 0.5]
  799.      * @throws MathIllegalStateException if an error occurs computing the p-value
  800.      */
  801.     public static boolean tTest(final StatisticalSummary sampleStats1,
  802.                                 final StatisticalSummary sampleStats2,
  803.                                 final double alpha)
  804.         throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException {
  805.         return T_TEST.tTest(sampleStats1, sampleStats2, alpha);
  806.     }

  807.     /**
  808.      * Returns the <i>observed significance level</i>, or
  809.      * <i>p-value</i>, associated with a two-sample, two-tailed t-test
  810.      * comparing the means of the datasets described by two StatisticalSummary
  811.      * instances.
  812.      * <p>
  813.      * The number returned is the smallest significance level
  814.      * at which one can reject the null hypothesis that the two means are
  815.      * equal in favor of the two-sided alternative that they are different.
  816.      * For a one-sided test, divide the returned value by 2.</p>
  817.      * <p>
  818.      * The test does not assume that the underlying population variances are
  819.      * equal  and it uses approximated degrees of freedom computed from the
  820.      * sample data to compute the p-value.   To perform the test assuming
  821.      * equal variances, use
  822.      * {@link #homoscedasticTTest(StatisticalSummary, StatisticalSummary)}.</p>
  823.      * <p>
  824.      * <strong>Usage Note:</strong><br>
  825.      * The validity of the p-value depends on the assumptions of the parametric
  826.      * t-test procedure, as discussed
  827.      * <a href="http://www.basic.nwu.edu/statguidefiles/ttest_unpaired_ass_viol.html">
  828.      * here</a></p>
  829.      * <p>
  830.      * <strong>Preconditions</strong>:</p>
  831.      * <ul>
  832.      * <li>The datasets described by the two Univariates must each contain
  833.      * at least 2 observations.
  834.      * </li></ul>
  835.      *
  836.      * @param sampleStats1  StatisticalSummary describing data from the first sample
  837.      * @param sampleStats2  StatisticalSummary describing data from the second sample
  838.      * @return p-value for t-test
  839.      * @throws NullArgumentException if the sample statistics are <code>null</code>
  840.      * @throws MathIllegalArgumentException if the number of samples is &lt; 2
  841.      * @throws MathIllegalStateException if an error occurs computing the p-value
  842.      */
  843.     public static double tTest(final StatisticalSummary sampleStats1,
  844.                                final StatisticalSummary sampleStats2)
  845.         throws MathIllegalArgumentException, NullArgumentException,
  846.         MathIllegalStateException {
  847.         return T_TEST.tTest(sampleStats1, sampleStats2);
  848.     }

  849.     /**
  850.      * Computes the <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm">
  851.      * Chi-Square statistic</a> comparing <code>observed</code> and <code>expected</code>
  852.      * frequency counts.
  853.      * <p>
  854.      * This statistic can be used to perform a Chi-Square test evaluating the null
  855.      * hypothesis that the observed counts follow the expected distribution.
  856.      * <p>
  857.      * <strong>Preconditions</strong>:
  858.      * <ul>
  859.      * <li>Expected counts must all be positive.</li>
  860.      * <li>Observed counts must all be &ge; 0.</li>
  861.      * <li>The observed and expected arrays must have the same length and
  862.      * their common length must be at least 2.</li>
  863.      * </ul>
  864.      * <p>
  865.      * If any of the preconditions are not met, an
  866.      * <code>IllegalArgumentException</code> is thrown.
  867.      * <p>
  868.      * <strong>Note: </strong>This implementation rescales the
  869.      * <code>expected</code> array if necessary to ensure that the sum of the
  870.      * expected and observed counts are equal.
  871.      *
  872.      * @param observed array of observed frequency counts
  873.      * @param expected array of expected frequency counts
  874.      * @return chiSquare test statistic
  875.      * @throws MathIllegalArgumentException if <code>observed</code> has negative entries
  876.      * @throws MathIllegalArgumentException if <code>expected</code> has entries that are
  877.      * not strictly positive
  878.      * @throws MathIllegalArgumentException if the arrays length is less than 2
  879.      */
  880.     public static double chiSquare(final double[] expected, final long[] observed)
  881.         throws MathIllegalArgumentException {
  882.         return CHI_SQUARE_TEST.chiSquare(expected, observed);
  883.     }

  884.     /**
  885.      * Computes the Chi-Square statistic associated with a
  886.      * <a href="http://www.itl.nist.gov/div898/handbook/prc/section4/prc45.htm">
  887.      * chi-square test of independence</a> based on the input <code>counts</code>
  888.      * array, viewed as a two-way table.
  889.      * <p>
  890.      * The rows of the 2-way table are
  891.      * <code>count[0], ... , count[count.length - 1] </code>
  892.      * <p>
  893.      * <strong>Preconditions</strong>:
  894.      * <ul>
  895.      * <li>All counts must be &ge; 0.</li>
  896.      * <li>The count array must be rectangular (i.e. all count[i] subarrays
  897.      * must have the same length).</li>
  898.      * <li>The 2-way table represented by <code>counts</code> must have at
  899.      * least 2 columns and at least 2 rows.</li>
  900.      * </ul>
  901.      * <p>
  902.      * If any of the preconditions are not met, an
  903.      * <code>IllegalArgumentException</code> is thrown.
  904.      *
  905.      * @param counts array representation of 2-way table
  906.      * @return chiSquare test statistic
  907.      * @throws NullArgumentException if the array is null
  908.      * @throws MathIllegalArgumentException if the array is not rectangular
  909.      * @throws MathIllegalArgumentException if {@code counts} has negative entries
  910.      */
  911.     public static double chiSquare(final long[][] counts)
  912.         throws MathIllegalArgumentException, NullArgumentException {
  913.         return CHI_SQUARE_TEST.chiSquare(counts);
  914.     }

  915.     /**
  916.      * Performs a <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm">
  917.      * Chi-square goodness of fit test</a> evaluating the null hypothesis that the
  918.      * observed counts conform to the frequency distribution described by the expected
  919.      * counts, with significance level <code>alpha</code>.  Returns true iff the null
  920.      * hypothesis can be rejected with 100 * (1 - alpha) percent confidence.
  921.      * <p>
  922.      * <strong>Example:</strong><br>
  923.      * To test the hypothesis that <code>observed</code> follows
  924.      * <code>expected</code> at the 99% level, use
  925.      * <code>chiSquareTest(expected, observed, 0.01)</code>
  926.      * <p>
  927.      * <strong>Preconditions</strong>:
  928.      * <ul>
  929.      * <li>Expected counts must all be positive.</li>
  930.      * <li>Observed counts must all be &ge; 0.</li>
  931.      * <li>The observed and expected arrays must have the same length and
  932.      * their common length must be at least 2.</li>
  933.      * <li><code> 0 &lt; alpha &lt; 0.5</code></li>
  934.      * </ul>
  935.      * <p>
  936.      * If any of the preconditions are not met, an
  937.      * <code>IllegalArgumentException</code> is thrown.
  938.      * <p>
  939.      * <strong>Note: </strong>This implementation rescales the
  940.      * <code>expected</code> array if necessary to ensure that the sum of the
  941.      * expected and observed counts are equal.
  942.      *
  943.      * @param observed array of observed frequency counts
  944.      * @param expected array of expected frequency counts
  945.      * @param alpha significance level of the test
  946.      * @return true iff null hypothesis can be rejected with confidence
  947.      * 1 - alpha
  948.      * @throws MathIllegalArgumentException if <code>observed</code> has negative entries
  949.      * @throws MathIllegalArgumentException if <code>expected</code> has entries that are
  950.      * not strictly positive
  951.      * @throws MathIllegalArgumentException if the arrays length is less than 2
  952.      * @throws MathIllegalArgumentException if <code>alpha</code> is not in the range (0, 0.5]
  953.      * @throws MathIllegalStateException if an error occurs computing the p-value
  954.      */
  955.     public static boolean chiSquareTest(final double[] expected, final long[] observed,
  956.                                         final double alpha)
  957.         throws MathIllegalArgumentException, MathIllegalStateException {
  958.         return CHI_SQUARE_TEST.chiSquareTest(expected, observed, alpha);
  959.     }

  960.     /**
  961.      * Returns the <i>observed significance level</i>, or <a href=
  962.      * "http://www.cas.lancs.ac.uk/glossary_v1.1/hyptest.html#pvalue">
  963.      * p-value</a>, associated with a
  964.      * <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm">
  965.      * Chi-square goodness of fit test</a> comparing the <code>observed</code>
  966.      * frequency counts to those in the <code>expected</code> array.
  967.      * <p>
  968.      * The number returned is the smallest significance level at which one can reject
  969.      * the null hypothesis that the observed counts conform to the frequency distribution
  970.      * described by the expected counts.
  971.      * <p>
  972.      * <strong>Preconditions</strong>:
  973.      * <ul>
  974.      * <li>Expected counts must all be positive.</li>
  975.      * <li>Observed counts must all be &ge; 0.</li>
  976.      * <li>The observed and expected arrays must have the same length and
  977.      * their common length must be at least 2.</li>
  978.      * </ul>
  979.      * <p>
  980.      * If any of the preconditions are not met, an
  981.      * <code>IllegalArgumentException</code> is thrown.
  982.      * <p>
  983.      * <strong>Note: </strong>This implementation rescales the
  984.      * <code>expected</code> array if necessary to ensure that the sum of the
  985.      * expected and observed counts are equal.
  986.      *
  987.      * @param observed array of observed frequency counts
  988.      * @param expected array of expected frequency counts
  989.      * @return p-value
  990.      * @throws MathIllegalArgumentException if <code>observed</code> has negative entries
  991.      * @throws MathIllegalArgumentException if <code>expected</code> has entries that are
  992.      * not strictly positive
  993.      * @throws MathIllegalArgumentException if the arrays length is less than 2
  994.      * @throws MathIllegalStateException if an error occurs computing the p-value
  995.      */
  996.     public static double chiSquareTest(final double[] expected, final long[] observed)
  997.         throws MathIllegalArgumentException, MathIllegalStateException {
  998.         return CHI_SQUARE_TEST.chiSquareTest(expected, observed);
  999.     }

  1000.     /**
  1001.      * Performs a <a href="http://www.itl.nist.gov/div898/handbook/prc/section4/prc45.htm">
  1002.      * chi-square test of independence</a> evaluating the null hypothesis that the
  1003.      * classifications represented by the counts in the columns of the input 2-way table
  1004.      * are independent of the rows, with significance level <code>alpha</code>.
  1005.      * Returns true iff the null hypothesis can be rejected with 100 * (1 - alpha) percent
  1006.      * confidence.
  1007.      * <p>
  1008.      * The rows of the 2-way table are
  1009.      * <code>count[0], ... , count[count.length - 1] </code>
  1010.      * <p>
  1011.      * <strong>Example:</strong><br>
  1012.      * To test the null hypothesis that the counts in
  1013.      * <code>count[0], ... , count[count.length - 1] </code>
  1014.      * all correspond to the same underlying probability distribution at the 99% level,
  1015.      * use <code>chiSquareTest(counts, 0.01)</code>.
  1016.      * <p>
  1017.      * <strong>Preconditions</strong>:
  1018.      * <ul>
  1019.      * <li>All counts must be &ge; 0.</li>
  1020.      * <li>The count array must be rectangular (i.e. all count[i] subarrays must have the
  1021.      * same length).</li>
  1022.      * <li>The 2-way table represented by <code>counts</code> must have at least 2 columns and
  1023.      * at least 2 rows.</li>
  1024.      * </ul>
  1025.      * <p>
  1026.      * If any of the preconditions are not met, an
  1027.      * <code>IllegalArgumentException</code> is thrown.
  1028.      *
  1029.      * @param counts array representation of 2-way table
  1030.      * @param alpha significance level of the test
  1031.      * @return true iff null hypothesis can be rejected with confidence
  1032.      * 1 - alpha
  1033.      * @throws NullArgumentException if the array is null
  1034.      * @throws MathIllegalArgumentException if the array is not rectangular
  1035.      * @throws MathIllegalArgumentException if {@code counts} has any negative entries
  1036.      * @throws MathIllegalArgumentException if <code>alpha</code> is not in the range (0, 0.5]
  1037.      * @throws MathIllegalStateException if an error occurs computing the p-value
  1038.      */
  1039.     public static boolean chiSquareTest(final long[][] counts, final double alpha)
  1040.         throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException {
  1041.         return CHI_SQUARE_TEST.chiSquareTest(counts, alpha);
  1042.     }

  1043.     /**
  1044.      * Returns the <i>observed significance level</i>, or <a href=
  1045.      * "http://www.cas.lancs.ac.uk/glossary_v1.1/hyptest.html#pvalue">
  1046.      * p-value</a>, associated with a
  1047.      * <a href="http://www.itl.nist.gov/div898/handbook/prc/section4/prc45.htm">
  1048.      * chi-square test of independence</a> based on the input <code>counts</code>
  1049.      * array, viewed as a two-way table.
  1050.      * <p>
  1051.      * The rows of the 2-way table are
  1052.      * <code>count[0], ... , count[count.length - 1] </code>
  1053.      * <p>
  1054.      * <strong>Preconditions</strong>:
  1055.      * <ul>
  1056.      * <li>All counts must be &ge; 0.</li>
  1057.      * <li>The count array must be rectangular (i.e. all count[i] subarrays must have
  1058.      * the same length).</li>
  1059.      * <li>The 2-way table represented by <code>counts</code> must have at least 2
  1060.      * columns and at least 2 rows.</li>
  1061.      * </ul>
  1062.      * <p>
  1063.      * If any of the preconditions are not met, an
  1064.      * <code>IllegalArgumentException</code> is thrown.
  1065.      *
  1066.      * @param counts array representation of 2-way table
  1067.      * @return p-value
  1068.      * @throws NullArgumentException if the array is null
  1069.      * @throws MathIllegalArgumentException if the array is not rectangular
  1070.      * @throws MathIllegalArgumentException if {@code counts} has negative entries
  1071.      * @throws MathIllegalStateException if an error occurs computing the p-value
  1072.      */
  1073.     public static double chiSquareTest(final long[][] counts)
  1074.         throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException {
  1075.         return CHI_SQUARE_TEST.chiSquareTest(counts);
  1076.     }

  1077.     /**
  1078.      * Computes a
  1079.      * <a href="http://www.itl.nist.gov/div898/software/dataplot/refman1/auxillar/chi2samp.htm">
  1080.      * Chi-Square two sample test statistic</a> comparing bin frequency counts
  1081.      * in <code>observed1</code> and <code>observed2</code>.
  1082.      * <p>
  1083.      * The sums of frequency counts in the two samples are not required to be the
  1084.      * same. The formula used to compute the test statistic is
  1085.      * </p>
  1086.      * <code>
  1087.      * &sum;[(K * observed1[i] - observed2[i]/K)<sup>2</sup> / (observed1[i] + observed2[i])]
  1088.      * </code>
  1089.      * <p>
  1090.      * where
  1091.      * </p>
  1092.      * <code>K = √[&sum;(observed2 / &sum;(observed1)]</code>
  1093.      * <p>
  1094.      * This statistic can be used to perform a Chi-Square test evaluating the
  1095.      * null hypothesis that both observed counts follow the same distribution.
  1096.      * </p>
  1097.      * <p><strong>Preconditions</strong>:</p>
  1098.      * <ul>
  1099.      * <li>Observed counts must be non-negative.</li>
  1100.      * <li>Observed counts for a specific bin must not both be zero.</li>
  1101.      * <li>Observed counts for a specific sample must not all be 0.</li>
  1102.      * <li>The arrays <code>observed1</code> and <code>observed2</code> must have
  1103.      * the same length and their common length must be at least 2.</li>
  1104.      * </ul>
  1105.      * <p>
  1106.      * If any of the preconditions are not met, an
  1107.      * <code>IllegalArgumentException</code> is thrown.
  1108.      * </p>
  1109.      *
  1110.      * @param observed1 array of observed frequency counts of the first data set
  1111.      * @param observed2 array of observed frequency counts of the second data set
  1112.      * @return chiSquare test statistic
  1113.      * @throws MathIllegalArgumentException the the length of the arrays does not match
  1114.      * @throws MathIllegalArgumentException if any entries in <code>observed1</code> or
  1115.      * <code>observed2</code> are negative
  1116.      * @throws MathIllegalArgumentException if either all counts of <code>observed1</code> or
  1117.      * <code>observed2</code> are zero, or if the count at some index is zero
  1118.      * for both arrays
  1119.      */
  1120.     public static double chiSquareDataSetsComparison(final long[] observed1,
  1121.                                                      final long[] observed2)
  1122.         throws MathIllegalArgumentException {
  1123.         return CHI_SQUARE_TEST.chiSquareDataSetsComparison(observed1, observed2);
  1124.     }

  1125.     /**
  1126.      * Returns the <i>observed significance level</i>, or <a href=
  1127.      * "http://www.cas.lancs.ac.uk/glossary_v1.1/hyptest.html#pvalue">
  1128.      * p-value</a>, associated with a Chi-Square two sample test comparing
  1129.      * bin frequency counts in <code>observed1</code> and
  1130.      * <code>observed2</code>.
  1131.      * <p>
  1132.      * The number returned is the smallest significance level at which one
  1133.      * can reject the null hypothesis that the observed counts conform to the
  1134.      * same distribution.
  1135.      * <p>
  1136.      * See {@link #chiSquareDataSetsComparison(long[], long[])} for details
  1137.      * on the formula used to compute the test statistic. The degrees of
  1138.      * of freedom used to perform the test is one less than the common length
  1139.      * of the input observed count arrays.
  1140.      * <p>
  1141.      * <strong>Preconditions</strong>:
  1142.      * <ul>
  1143.      * <li>Observed counts must be non-negative.</li>
  1144.      * <li>Observed counts for a specific bin must not both be zero.</li>
  1145.      * <li>Observed counts for a specific sample must not all be 0.</li>
  1146.      * <li>The arrays <code>observed1</code> and <code>observed2</code> must
  1147.      * have the same length and their common length must be at least 2.</li>
  1148.      * </ul>
  1149.      * <p>
  1150.      * If any of the preconditions are not met, an
  1151.      * <code>IllegalArgumentException</code> is thrown.
  1152.      *
  1153.      * @param observed1 array of observed frequency counts of the first data set
  1154.      * @param observed2 array of observed frequency counts of the second data set
  1155.      * @return p-value
  1156.      * @throws MathIllegalArgumentException the the length of the arrays does not match
  1157.      * @throws MathIllegalArgumentException if any entries in <code>observed1</code> or
  1158.      * <code>observed2</code> are negative
  1159.      * @throws MathIllegalArgumentException if either all counts of <code>observed1</code> or
  1160.      * <code>observed2</code> are zero, or if the count at the same index is zero
  1161.      * for both arrays
  1162.      * @throws MathIllegalStateException if an error occurs computing the p-value
  1163.      */
  1164.     public static double chiSquareTestDataSetsComparison(final long[] observed1,
  1165.                                                          final long[] observed2)
  1166.         throws MathIllegalArgumentException,
  1167.         MathIllegalStateException {
  1168.         return CHI_SQUARE_TEST.chiSquareTestDataSetsComparison(observed1, observed2);
  1169.     }

  1170.     /**
  1171.      * Performs a Chi-Square two sample test comparing two binned data
  1172.      * sets. The test evaluates the null hypothesis that the two lists of
  1173.      * observed counts conform to the same frequency distribution, with
  1174.      * significance level <code>alpha</code>.  Returns true iff the null
  1175.      * hypothesis can be rejected with 100 * (1 - alpha) percent confidence.
  1176.      * <p>
  1177.      * See {@link #chiSquareDataSetsComparison(long[], long[])} for
  1178.      * details on the formula used to compute the Chisquare statistic used
  1179.      * in the test. The degrees of of freedom used to perform the test is
  1180.      * one less than the common length of the input observed count arrays.
  1181.      * <p>
  1182.      * <strong>Preconditions</strong>:
  1183.      * <ul>
  1184.      * <li>Observed counts must be non-negative.</li>
  1185.      * <li>Observed counts for a specific bin must not both be zero.</li>
  1186.      * <li>Observed counts for a specific sample must not all be 0.</li>
  1187.      * <li>The arrays <code>observed1</code> and <code>observed2</code> must
  1188.      * have the same length and their common length must be at least 2.</li>
  1189.      * <li><code> 0 &lt; alpha &lt; 0.5</code></li>
  1190.      * </ul>
  1191.      * <p>
  1192.      * If any of the preconditions are not met, an
  1193.      * <code>IllegalArgumentException</code> is thrown.
  1194.      *
  1195.      * @param observed1 array of observed frequency counts of the first data set
  1196.      * @param observed2 array of observed frequency counts of the second data set
  1197.      * @param alpha significance level of the test
  1198.      * @return true iff null hypothesis can be rejected with confidence
  1199.      * 1 - alpha
  1200.      * @throws MathIllegalArgumentException the the length of the arrays does not match
  1201.      * @throws MathIllegalArgumentException if any entries in <code>observed1</code> or
  1202.      * <code>observed2</code> are negative
  1203.      * @throws MathIllegalArgumentException if either all counts of <code>observed1</code> or
  1204.      * <code>observed2</code> are zero, or if the count at the same index is zero
  1205.      * for both arrays
  1206.      * @throws MathIllegalArgumentException if <code>alpha</code> is not in the range (0, 0.5]
  1207.      * @throws MathIllegalStateException if an error occurs performing the test
  1208.      */
  1209.     public static boolean chiSquareTestDataSetsComparison(final long[] observed1,
  1210.                                                           final long[] observed2,
  1211.                                                           final double alpha)
  1212.         throws MathIllegalArgumentException, MathIllegalStateException {
  1213.         return CHI_SQUARE_TEST.chiSquareTestDataSetsComparison(observed1, observed2, alpha);
  1214.     }

  1215.     /**
  1216.      * Computes the ANOVA F-value for a collection of <code>double[]</code>
  1217.      * arrays.
  1218.      *
  1219.      * <p><strong>Preconditions</strong>:</p>
  1220.      * <ul>
  1221.      * <li>The categoryData <code>Collection</code> must contain
  1222.      * <code>double[]</code> arrays.</li>
  1223.      * <li> There must be at least two <code>double[]</code> arrays in the
  1224.      * <code>categoryData</code> collection and each of these arrays must
  1225.      * contain at least two values.</li></ul>
  1226.      * <p>
  1227.      * This implementation computes the F statistic using the definitional
  1228.      * formula</p>
  1229.      * <pre>
  1230.      *   F = msbg/mswg</pre>
  1231.      * <p>where</p>
  1232.      * <pre>
  1233.      *  msbg = between group mean square
  1234.      *  mswg = within group mean square</pre>
  1235.      * <p>
  1236.      * are as defined <a href="http://faculty.vassar.edu/lowry/ch13pt1.html">
  1237.      * here</a></p>
  1238.      *
  1239.      * @param categoryData <code>Collection</code> of <code>double[]</code>
  1240.      * arrays each containing data for one category
  1241.      * @return Fvalue
  1242.      * @throws NullArgumentException if <code>categoryData</code> is <code>null</code>
  1243.      * @throws MathIllegalArgumentException if the length of the <code>categoryData</code>
  1244.      * array is less than 2 or a contained <code>double[]</code> array does not have
  1245.      * at least two values
  1246.      */
  1247.     public static double oneWayAnovaFValue(final Collection<double[]> categoryData)
  1248.         throws MathIllegalArgumentException, NullArgumentException {
  1249.         return ONE_WAY_ANANOVA.anovaFValue(categoryData);
  1250.     }

  1251.     /**
  1252.      * Computes the ANOVA P-value for a collection of <code>double[]</code>
  1253.      * arrays.
  1254.      *
  1255.      * <p><strong>Preconditions</strong>:</p>
  1256.      * <ul>
  1257.      * <li>The categoryData <code>Collection</code> must contain
  1258.      * <code>double[]</code> arrays.</li>
  1259.      * <li> There must be at least two <code>double[]</code> arrays in the
  1260.      * <code>categoryData</code> collection and each of these arrays must
  1261.      * contain at least two values.</li></ul>
  1262.      * <p>
  1263.      * This implementation uses the
  1264.      * {@link org.hipparchus.distribution.continuous.FDistribution
  1265.      * Hipparchus F Distribution implementation} to estimate the exact
  1266.      * p-value, using the formula</p>
  1267.      * <pre>
  1268.      *   p = 1 - cumulativeProbability(F)</pre>
  1269.      * <p>
  1270.      * where <code>F</code> is the F value and <code>cumulativeProbability</code>
  1271.      * is the Hipparchus implementation of the F distribution.</p>
  1272.      *
  1273.      * @param categoryData <code>Collection</code> of <code>double[]</code>
  1274.      * arrays each containing data for one category
  1275.      * @return Pvalue
  1276.      * @throws NullArgumentException if <code>categoryData</code> is <code>null</code>
  1277.      * @throws MathIllegalArgumentException if the length of the <code>categoryData</code>
  1278.      * array is less than 2 or a contained <code>double[]</code> array does not have
  1279.      * at least two values
  1280.      * @throws MathIllegalStateException if the p-value can not be computed due to a convergence error
  1281.      * @throws MathIllegalStateException if the maximum number of iterations is exceeded
  1282.      */
  1283.     public static double oneWayAnovaPValue(final Collection<double[]> categoryData)
  1284.         throws MathIllegalArgumentException, NullArgumentException,
  1285.         MathIllegalStateException {
  1286.         return ONE_WAY_ANANOVA.anovaPValue(categoryData);
  1287.     }

  1288.     /**
  1289.      * Performs an ANOVA test, evaluating the null hypothesis that there
  1290.      * is no difference among the means of the data categories.
  1291.      *
  1292.      * <p><strong>Preconditions</strong>:</p>
  1293.      * <ul>
  1294.      * <li>The categoryData <code>Collection</code> must contain
  1295.      * <code>double[]</code> arrays.</li>
  1296.      * <li> There must be at least two <code>double[]</code> arrays in the
  1297.      * <code>categoryData</code> collection and each of these arrays must
  1298.      * contain at least two values.</li>
  1299.      * <li>alpha must be strictly greater than 0 and less than or equal to 0.5.
  1300.      * </li></ul>
  1301.      * <p>
  1302.      * This implementation uses the
  1303.      * {@link org.hipparchus.distribution.continuous.FDistribution
  1304.      * Hipparchus F Distribution implementation} to estimate the exact
  1305.      * p-value, using the formula</p><pre>
  1306.      *   p = 1 - cumulativeProbability(F)</pre>
  1307.      * <p>where <code>F</code> is the F value and <code>cumulativeProbability</code>
  1308.      * is the Hipparchus implementation of the F distribution.</p>
  1309.      * <p>True is returned iff the estimated p-value is less than alpha.</p>
  1310.      *
  1311.      * @param categoryData <code>Collection</code> of <code>double[]</code>
  1312.      * arrays each containing data for one category
  1313.      * @param alpha significance level of the test
  1314.      * @return true if the null hypothesis can be rejected with
  1315.      * confidence 1 - alpha
  1316.      * @throws NullArgumentException if <code>categoryData</code> is <code>null</code>
  1317.      * @throws MathIllegalArgumentException if the length of the <code>categoryData</code>
  1318.      * array is less than 2 or a contained <code>double[]</code> array does not have
  1319.      * at least two values
  1320.      * @throws MathIllegalArgumentException if <code>alpha</code> is not in the range (0, 0.5]
  1321.      * @throws MathIllegalStateException if the p-value can not be computed due to a convergence error
  1322.      * @throws MathIllegalStateException if the maximum number of iterations is exceeded
  1323.      */
  1324.     public static boolean oneWayAnovaTest(final Collection<double[]> categoryData,
  1325.                                           final double alpha)
  1326.         throws MathIllegalArgumentException, NullArgumentException, MathIllegalStateException {
  1327.         return ONE_WAY_ANANOVA.anovaTest(categoryData, alpha);
  1328.     }

  1329.     /**
  1330.      * Computes the <a href="http://en.wikipedia.org/wiki/G-test">G statistic
  1331.      * for Goodness of Fit</a> comparing {@code observed} and {@code expected}
  1332.      * frequency counts.
  1333.      * <p>
  1334.      * This statistic can be used to perform a G test (Log-Likelihood Ratio
  1335.      * Test) evaluating the null hypothesis that the observed counts follow the
  1336.      * expected distribution.
  1337.      * <p>
  1338.      * <strong>Preconditions</strong>:
  1339.      * <ul>
  1340.      * <li>Expected counts must all be positive.</li>
  1341.      * <li>Observed counts must all be &ge; 0.</li>
  1342.      * <li>The observed and expected arrays must have the same length and their
  1343.      * common length must be at least 2. </li>
  1344.      * </ul>
  1345.      * <p>
  1346.      * If any of the preconditions are not met, a
  1347.      * {@code MathIllegalArgumentException} is thrown.
  1348.      * <p>
  1349.      * <strong>Note:</strong>This implementation rescales the
  1350.      * {@code expected} array if necessary to ensure that the sum of the
  1351.      * expected and observed counts are equal.
  1352.      *
  1353.      * @param observed array of observed frequency counts
  1354.      * @param expected array of expected frequency counts
  1355.      * @return G-Test statistic
  1356.      * @throws MathIllegalArgumentException if {@code observed} has negative entries
  1357.      * @throws MathIllegalArgumentException if {@code expected} has entries that
  1358.      * are not strictly positive
  1359.      * @throws MathIllegalArgumentException if the array lengths do not match or
  1360.      * are less than 2.
  1361.      */
  1362.     public static double g(final double[] expected, final long[] observed)
  1363.         throws MathIllegalArgumentException {
  1364.         return G_TEST.g(expected, observed);
  1365.     }

  1366.     /**
  1367.      * Returns the <i>observed significance level</i>, or <a href=
  1368.      * "http://www.cas.lancs.ac.uk/glossary_v1.1/hyptest.html#pvalue"> p-value</a>,
  1369.      * associated with a G-Test for goodness of fit comparing the
  1370.      * {@code observed} frequency counts to those in the {@code expected} array.
  1371.      *
  1372.      * <p>The number returned is the smallest significance level at which one
  1373.      * can reject the null hypothesis that the observed counts conform to the
  1374.      * frequency distribution described by the expected counts.</p>
  1375.      *
  1376.      * <p>The probability returned is the tail probability beyond
  1377.      * {@link #g(double[], long[]) g(expected, observed)}
  1378.      * in the ChiSquare distribution with degrees of freedom one less than the
  1379.      * common length of {@code expected} and {@code observed}.</p>
  1380.      *
  1381.      * <p> <strong>Preconditions</strong>:</p>
  1382.      * <ul>
  1383.      * <li>Expected counts must all be positive. </li>
  1384.      * <li>Observed counts must all be &ge; 0. </li>
  1385.      * <li>The observed and expected arrays must have the
  1386.      * same length and their common length must be at least 2.</li>
  1387.      * </ul>
  1388.      *
  1389.      * <p>If any of the preconditions are not met, a
  1390.      * {@code MathIllegalArgumentException} is thrown.</p>
  1391.      *
  1392.      * <p><strong>Note:</strong>This implementation rescales the
  1393.      * {@code expected} array if necessary to ensure that the sum of the
  1394.      *  expected and observed counts are equal.</p>
  1395.      *
  1396.      * @param observed array of observed frequency counts
  1397.      * @param expected array of expected frequency counts
  1398.      * @return p-value
  1399.      * @throws MathIllegalArgumentException if {@code observed} has negative entries
  1400.      * @throws MathIllegalArgumentException if {@code expected} has entries that
  1401.      * are not strictly positive
  1402.      * @throws MathIllegalArgumentException if the array lengths do not match or
  1403.      * are less than 2.
  1404.      * @throws MathIllegalStateException if an error occurs computing the
  1405.      * p-value.
  1406.      */
  1407.     public static double gTest(final double[] expected, final long[] observed)
  1408.         throws MathIllegalArgumentException, MathIllegalStateException {
  1409.         return G_TEST.gTest(expected, observed);
  1410.     }

  1411.     /**
  1412.      * Returns the intrinsic (Hardy-Weinberg proportions) p-Value, as described
  1413.      * in p64-69 of McDonald, J.H. 2009. Handbook of Biological Statistics
  1414.      * (2nd ed.). Sparky House Publishing, Baltimore, Maryland.
  1415.      *
  1416.      * <p> The probability returned is the tail probability beyond
  1417.      * {@link #g(double[], long[]) g(expected, observed)}
  1418.      * in the ChiSquare distribution with degrees of freedom two less than the
  1419.      * common length of {@code expected} and {@code observed}.</p>
  1420.      *
  1421.      * @param observed array of observed frequency counts
  1422.      * @param expected array of expected frequency counts
  1423.      * @return p-value
  1424.      * @throws MathIllegalArgumentException if {@code observed} has negative entries
  1425.      * @throws MathIllegalArgumentException {@code expected} has entries that are
  1426.      * not strictly positive
  1427.      * @throws MathIllegalArgumentException if the array lengths do not match or
  1428.      * are less than 2.
  1429.      * @throws MathIllegalStateException if an error occurs computing the
  1430.      * p-value.
  1431.      */
  1432.     public static double gTestIntrinsic(final double[] expected, final long[] observed)
  1433.         throws MathIllegalArgumentException, MathIllegalStateException {
  1434.         return G_TEST.gTestIntrinsic(expected, observed);
  1435.     }

  1436.     /**
  1437.      * Performs a G-Test (Log-Likelihood Ratio Test) for goodness of fit
  1438.      * evaluating the null hypothesis that the observed counts conform to the
  1439.      * frequency distribution described by the expected counts, with
  1440.      * significance level {@code alpha}. Returns true iff the null
  1441.      * hypothesis can be rejected with {@code 100 * (1 - alpha)} percent confidence.
  1442.      *
  1443.      * <p><strong>Example:</strong><br> To test the hypothesis that
  1444.      * {@code observed} follows {@code expected} at the 99% level,
  1445.      * use </p><p>
  1446.      * {@code gTest(expected, observed, 0.01)}</p>
  1447.      *
  1448.      * <p>Returns true iff {@link #gTest(double[], long[])
  1449.      *  gTestGoodnessOfFitPValue(expected, observed)} &gt; alpha</p>
  1450.      *
  1451.      * <p><strong>Preconditions</strong>:</p>
  1452.      * <ul>
  1453.      * <li>Expected counts must all be positive. </li>
  1454.      * <li>Observed counts must all be &ge; 0. </li>
  1455.      * <li>The observed and expected arrays must have the same length and their
  1456.      * common length must be at least 2.
  1457.      * <li> {@code 0 < alpha < 0.5} </li></ul>
  1458.      *
  1459.      * <p>If any of the preconditions are not met, a
  1460.      * {@code MathIllegalArgumentException} is thrown.</p>
  1461.      *
  1462.      * <p><strong>Note:</strong>This implementation rescales the
  1463.      * {@code expected} array if necessary to ensure that the sum of the
  1464.      * expected and observed counts are equal.</p>
  1465.      *
  1466.      * @param observed array of observed frequency counts
  1467.      * @param expected array of expected frequency counts
  1468.      * @param alpha significance level of the test
  1469.      * @return true iff null hypothesis can be rejected with confidence 1 -
  1470.      * alpha
  1471.      * @throws MathIllegalArgumentException if {@code observed} has negative entries
  1472.      * @throws MathIllegalArgumentException if {@code expected} has entries that
  1473.      * are not strictly positive
  1474.      * @throws MathIllegalArgumentException if the array lengths do not match or
  1475.      * are less than 2.
  1476.      * @throws MathIllegalStateException if an error occurs computing the
  1477.      * p-value.
  1478.      * @throws MathIllegalArgumentException if alpha is not strictly greater than zero
  1479.      * and less than or equal to 0.5
  1480.      */
  1481.     public static boolean gTest(final double[] expected, final long[] observed,
  1482.                                 final double alpha)
  1483.         throws MathIllegalArgumentException, MathIllegalStateException {
  1484.         return G_TEST.gTest(expected, observed, alpha);
  1485.     }

  1486.     /**
  1487.      * <p>Computes a G (Log-Likelihood Ratio) two sample test statistic for
  1488.      * independence comparing frequency counts in
  1489.      * {@code observed1} and {@code observed2}. The sums of frequency
  1490.      * counts in the two samples are not required to be the same. The formula
  1491.      * used to compute the test statistic is </p>
  1492.      *
  1493.      * <p>{@code 2 * totalSum * [H(rowSums) + H(colSums) - H(k)]}</p>
  1494.      *
  1495.      * <p> where {@code H} is the
  1496.      * <a href="http://en.wikipedia.org/wiki/Entropy_%28information_theory%29">
  1497.      * Shannon Entropy</a> of the random variable formed by viewing the elements
  1498.      * of the argument array as incidence counts; <br>
  1499.      * {@code k} is a matrix with rows {@code [observed1, observed2]}; <br>
  1500.      * {@code rowSums, colSums} are the row/col sums of {@code k}; <br>
  1501.      * and {@code totalSum} is the overall sum of all entries in {@code k}.</p>
  1502.      *
  1503.      * <p>This statistic can be used to perform a G test evaluating the null
  1504.      * hypothesis that both observed counts are independent </p>
  1505.      *
  1506.      * <p> <strong>Preconditions</strong>:</p>
  1507.      * <ul>
  1508.      * <li>Observed counts must be non-negative. </li>
  1509.      * <li>Observed counts for a specific bin must not both be zero. </li>
  1510.      * <li>Observed counts for a specific sample must not all be  0. </li>
  1511.      * <li>The arrays {@code observed1} and {@code observed2} must have
  1512.      * the same length and their common length must be at least 2. </li></ul>
  1513.      *
  1514.      * <p>If any of the preconditions are not met, a
  1515.      * {@code MathIllegalArgumentException} is thrown.</p>
  1516.      *
  1517.      * @param observed1 array of observed frequency counts of the first data set
  1518.      * @param observed2 array of observed frequency counts of the second data
  1519.      * set
  1520.      * @return G-Test statistic
  1521.      * @throws MathIllegalArgumentException the the lengths of the arrays do not
  1522.      * match or their common length is less than 2
  1523.      * @throws MathIllegalArgumentException if any entry in {@code observed1} or
  1524.      * {@code observed2} is negative
  1525.      * @throws MathIllegalArgumentException if either all counts of
  1526.      * {@code observed1} or {@code observed2} are zero, or if the count
  1527.      * at the same index is zero for both arrays.
  1528.      */
  1529.     public static double gDataSetsComparison(final long[] observed1,
  1530.                                                   final long[] observed2)
  1531.         throws MathIllegalArgumentException {
  1532.         return G_TEST.gDataSetsComparison(observed1, observed2);
  1533.     }

  1534.     /**
  1535.      * Calculates the root log-likelihood ratio for 2 state Datasets. See
  1536.      * {@link #gDataSetsComparison(long[], long[] )}.
  1537.      *
  1538.      * <p>Given two events A and B, let k11 be the number of times both events
  1539.      * occur, k12 the incidence of B without A, k21 the count of A without B,
  1540.      * and k22 the number of times neither A nor B occurs.  What is returned
  1541.      * by this method is </p>
  1542.      *
  1543.      * <p>{@code (sgn) sqrt(gValueDataSetsComparison({k11, k12}, {k21, k22})}</p>
  1544.      *
  1545.      * <p>where {@code sgn} is -1 if {@code k11 / (k11 + k12) < k21 / (k21 + k22))};<br>
  1546.      * 1 otherwise.</p>
  1547.      *
  1548.      * <p>Signed root LLR has two advantages over the basic LLR: a) it is positive
  1549.      * where k11 is bigger than expected, negative where it is lower b) if there is
  1550.      * no difference it is asymptotically normally distributed. This allows one
  1551.      * to talk about "number of standard deviations" which is a more common frame
  1552.      * of reference than the chi^2 distribution.</p>
  1553.      *
  1554.      * @param k11 number of times the two events occurred together (AB)
  1555.      * @param k12 number of times the second event occurred WITHOUT the
  1556.      * first event (notA,B)
  1557.      * @param k21 number of times the first event occurred WITHOUT the
  1558.      * second event (A, notB)
  1559.      * @param k22 number of times something else occurred (i.e. was neither
  1560.      * of these events (notA, notB)
  1561.      * @return root log-likelihood ratio
  1562.      *
  1563.      */
  1564.     public static double rootLogLikelihoodRatio(final long k11, final long k12, final long k21, final long k22)
  1565.         throws MathIllegalArgumentException {
  1566.         return G_TEST.rootLogLikelihoodRatio(k11, k12, k21, k22);
  1567.     }


  1568.     /**
  1569.      * <p>Returns the <i>observed significance level</i>, or <a href=
  1570.      * "http://www.cas.lancs.ac.uk/glossary_v1.1/hyptest.html#pvalue">
  1571.      * p-value</a>, associated with a G-Value (Log-Likelihood Ratio) for two
  1572.      * sample test comparing bin frequency counts in {@code observed1} and
  1573.      * {@code observed2}.</p>
  1574.      *
  1575.      * <p>The number returned is the smallest significance level at which one
  1576.      * can reject the null hypothesis that the observed counts conform to the
  1577.      * same distribution. </p>
  1578.      *
  1579.      * <p>See {@link #gTest(double[], long[])} for details
  1580.      * on how the p-value is computed.  The degrees of of freedom used to
  1581.      * perform the test is one less than the common length of the input observed
  1582.      * count arrays.</p>
  1583.      *
  1584.      * <p><strong>Preconditions</strong>:</p>
  1585.      * <ul> <li>Observed counts must be non-negative. </li>
  1586.      * <li>Observed counts for a specific bin must not both be zero. </li>
  1587.      * <li>Observed counts for a specific sample must not all be 0. </li>
  1588.      * <li>The arrays {@code observed1} and {@code observed2} must
  1589.      * have the same length and their common length must be at least 2. </li>
  1590.      * </ul>
  1591.      * <p> If any of the preconditions are not met, a
  1592.      * {@code MathIllegalArgumentException} is thrown.</p>
  1593.      *
  1594.      * @param observed1 array of observed frequency counts of the first data set
  1595.      * @param observed2 array of observed frequency counts of the second data
  1596.      * set
  1597.      * @return p-value
  1598.      * @throws MathIllegalArgumentException the the length of the arrays does not
  1599.      * match or their common length is less than 2
  1600.      * @throws MathIllegalArgumentException if any of the entries in {@code observed1} or
  1601.      * {@code observed2} are negative
  1602.      * @throws MathIllegalArgumentException if either all counts of {@code observed1} or
  1603.      * {@code observed2} are zero, or if the count at some index is
  1604.      * zero for both arrays
  1605.      * @throws MathIllegalStateException if an error occurs computing the
  1606.      * p-value.
  1607.      */
  1608.     public static double gTestDataSetsComparison(final long[] observed1,
  1609.                                                         final long[] observed2)
  1610.         throws MathIllegalArgumentException,
  1611.         MathIllegalStateException {
  1612.         return G_TEST.gTestDataSetsComparison(observed1, observed2);
  1613.     }

  1614.     /**
  1615.      * <p>Performs a G-Test (Log-Likelihood Ratio Test) comparing two binned
  1616.      * data sets. The test evaluates the null hypothesis that the two lists
  1617.      * of observed counts conform to the same frequency distribution, with
  1618.      * significance level {@code alpha}. Returns true iff the null
  1619.      * hypothesis can be rejected  with 100 * (1 - alpha) percent confidence.
  1620.      * </p>
  1621.      * <p>See {@link #gDataSetsComparison(long[], long[])} for details
  1622.      * on the formula used to compute the G (LLR) statistic used in the test and
  1623.      * {@link #gTest(double[], long[])} for information on how
  1624.      * the observed significance level is computed. The degrees of of freedom used
  1625.      * to perform the test is one less than the common length of the input observed
  1626.      * count arrays. </p>
  1627.      *
  1628.      * <p><strong>Preconditions</strong>:</p>
  1629.      * <ul>
  1630.      * <li>Observed counts must be non-negative. </li>
  1631.      * <li>Observed counts for a specific bin must not both be zero. </li>
  1632.      * <li>Observed counts for a specific sample must not all be 0. </li>
  1633.      * <li>The arrays {@code observed1} and {@code observed2} must
  1634.      * have the same length and their common length must be at least 2. </li>
  1635.      * <li>{@code 0 < alpha < 0.5} </li></ul>
  1636.      *
  1637.      * <p>If any of the preconditions are not met, a
  1638.      * {@code MathIllegalArgumentException} is thrown.</p>
  1639.      *
  1640.      * @param observed1 array of observed frequency counts of the first data set
  1641.      * @param observed2 array of observed frequency counts of the second data
  1642.      * set
  1643.      * @param alpha significance level of the test
  1644.      * @return true iff null hypothesis can be rejected with confidence 1 -
  1645.      * alpha
  1646.      * @throws MathIllegalArgumentException the the length of the arrays does not
  1647.      * match
  1648.      * @throws MathIllegalArgumentException if any of the entries in {@code observed1} or
  1649.      * {@code observed2} are negative
  1650.      * @throws MathIllegalArgumentException if either all counts of {@code observed1} or
  1651.      * {@code observed2} are zero, or if the count at some index is
  1652.      * zero for both arrays
  1653.      * @throws MathIllegalArgumentException if {@code alpha} is not in the range
  1654.      * (0, 0.5]
  1655.      * @throws MathIllegalStateException if an error occurs performing the test
  1656.      */
  1657.     public static boolean gTestDataSetsComparison(final long[] observed1,
  1658.                                                   final long[] observed2,
  1659.                                                   final double alpha)
  1660.         throws MathIllegalArgumentException, MathIllegalStateException {
  1661.         return G_TEST.gTestDataSetsComparison(observed1, observed2, alpha);
  1662.     }

  1663.     /**
  1664.      * Computes the one-sample Kolmogorov-Smirnov test statistic, \(D_n=\sup_x |F_n(x)-F(x)|\) where
  1665.      * \(F\) is the distribution (cdf) function associated with {@code distribution}, \(n\) is the
  1666.      * length of {@code data} and \(F_n\) is the empirical distribution that puts mass \(1/n\) at
  1667.      * each of the values in {@code data}.
  1668.      *
  1669.      * @param dist reference distribution
  1670.      * @param data sample being evaluated
  1671.      * @return Kolmogorov-Smirnov statistic \(D_n\)
  1672.      * @throws MathIllegalArgumentException if {@code data} does not have length at least 2
  1673.      * @throws org.hipparchus.exception.NullArgumentException if {@code data} is null
  1674.      */
  1675.     public static double kolmogorovSmirnovStatistic(RealDistribution dist, double[] data)
  1676.             throws MathIllegalArgumentException, NullArgumentException {
  1677.         return KS_TEST.kolmogorovSmirnovStatistic(dist, data);
  1678.     }

  1679.     /**
  1680.      * Computes the <i>p-value</i>, or <i>observed significance level</i>, of a one-sample <a
  1681.      * href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"> Kolmogorov-Smirnov test</a>
  1682.      * evaluating the null hypothesis that {@code data} conforms to {@code distribution}.
  1683.      *
  1684.      * @param dist reference distribution
  1685.      * @param data sample being being evaluated
  1686.      * @return the p-value associated with the null hypothesis that {@code data} is a sample from
  1687.      *         {@code distribution}
  1688.      * @throws MathIllegalArgumentException if {@code data} does not have length at least 2
  1689.      * @throws org.hipparchus.exception.NullArgumentException if {@code data} is null
  1690.      */
  1691.     public static double kolmogorovSmirnovTest(RealDistribution dist, double[] data)
  1692.             throws MathIllegalArgumentException, NullArgumentException {
  1693.         return KS_TEST.kolmogorovSmirnovTest(dist, data);
  1694.     }

  1695.     /**
  1696.      * Computes the <i>p-value</i>, or <i>observed significance level</i>, of a one-sample <a
  1697.      * href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"> Kolmogorov-Smirnov test</a>
  1698.      * evaluating the null hypothesis that {@code data} conforms to {@code distribution}. If
  1699.      * {@code exact} is true, the distribution used to compute the p-value is computed using
  1700.      * extended precision. See {@link KolmogorovSmirnovTest#cdfExact(double, int)}.
  1701.      *
  1702.      * @param dist reference distribution
  1703.      * @param data sample being being evaluated
  1704.      * @param strict whether or not to force exact computation of the p-value
  1705.      * @return the p-value associated with the null hypothesis that {@code data} is a sample from
  1706.      *         {@code distribution}
  1707.      * @throws MathIllegalArgumentException if {@code data} does not have length at least 2
  1708.      * @throws org.hipparchus.exception.NullArgumentException if {@code data} is null
  1709.      */
  1710.     public static double kolmogorovSmirnovTest(RealDistribution dist, double[] data, boolean strict)
  1711.             throws MathIllegalArgumentException, NullArgumentException {
  1712.         return KS_TEST.kolmogorovSmirnovTest(dist, data, strict);
  1713.     }

  1714.     /**
  1715.      * Performs a <a href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"> Kolmogorov-Smirnov
  1716.      * test</a> evaluating the null hypothesis that {@code data} conforms to {@code distribution}.
  1717.      *
  1718.      * @param dist reference distribution
  1719.      * @param data sample being being evaluated
  1720.      * @param alpha significance level of the test
  1721.      * @return true iff the null hypothesis that {@code data} is a sample from {@code distribution}
  1722.      *         can be rejected with confidence 1 - {@code alpha}
  1723.      * @throws MathIllegalArgumentException if {@code data} does not have length at least 2
  1724.      * @throws org.hipparchus.exception.NullArgumentException if {@code data} is null
  1725.      */
  1726.     public static boolean kolmogorovSmirnovTest(RealDistribution dist, double[] data, double alpha)
  1727.             throws MathIllegalArgumentException, NullArgumentException {
  1728.         return KS_TEST.kolmogorovSmirnovTest(dist, data, alpha);
  1729.     }

  1730.     /**
  1731.      * Computes the two-sample Kolmogorov-Smirnov test statistic, \(D_{n,m}=\sup_x |F_n(x)-F_m(x)|\)
  1732.      * where \(n\) is the length of {@code x}, \(m\) is the length of {@code y}, \(F_n\) is the
  1733.      * empirical distribution that puts mass \(1/n\) at each of the values in {@code x} and \(F_m\)
  1734.      * is the empirical distribution of the {@code y} values.
  1735.      *
  1736.      * @param x first sample
  1737.      * @param y second sample
  1738.      * @return test statistic \(D_{n,m}\) used to evaluate the null hypothesis that {@code x} and
  1739.      *         {@code y} represent samples from the same underlying distribution
  1740.      * @throws MathIllegalArgumentException if either {@code x} or {@code y} does not have length at
  1741.      *         least 2
  1742.      * @throws org.hipparchus.exception.NullArgumentException if either {@code x} or {@code y} is null
  1743.      */
  1744.     public static double kolmogorovSmirnovStatistic(double[] x, double[] y)
  1745.             throws MathIllegalArgumentException, NullArgumentException {
  1746.         return KS_TEST.kolmogorovSmirnovStatistic(x, y);
  1747.     }

  1748.     /**
  1749.      * Computes the <i>p-value</i>, or <i>observed significance level</i>, of a two-sample <a
  1750.      * href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"> Kolmogorov-Smirnov test</a>
  1751.      * evaluating the null hypothesis that {@code x} and {@code y} are samples drawn from the same
  1752.      * probability distribution. Assumes the strict form of the inequality used to compute the
  1753.      * p-value. See {@link KolmogorovSmirnovTest#kolmogorovSmirnovTest(RealDistribution, double[], boolean)}.
  1754.      *
  1755.      * @param x first sample dataset
  1756.      * @param y second sample dataset
  1757.      * @return p-value associated with the null hypothesis that {@code x} and {@code y} represent
  1758.      *         samples from the same distribution
  1759.      * @throws MathIllegalArgumentException if either {@code x} or {@code y} does not have length at
  1760.      *         least 2
  1761.      * @throws org.hipparchus.exception.NullArgumentException if either {@code x} or {@code y} is null
  1762.      */
  1763.     public static double kolmogorovSmirnovTest(double[] x, double[] y)
  1764.             throws MathIllegalArgumentException, NullArgumentException {
  1765.         return KS_TEST.kolmogorovSmirnovTest(x, y);
  1766.     }

  1767.     /**
  1768.      * Computes the <i>p-value</i>, or <i>observed significance level</i>, of a two-sample <a
  1769.      * href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"> Kolmogorov-Smirnov test</a>
  1770.      * evaluating the null hypothesis that {@code x} and {@code y} are samples drawn from the same
  1771.      * probability distribution. Specifically, what is returned is an estimate of the probability
  1772.      * that the {@link KolmogorovSmirnovTest#kolmogorovSmirnovStatistic(double[], double[])} associated with a randomly
  1773.      * selected partition of the combined sample into subsamples of sizes {@code x.length} and
  1774.      * {@code y.length} will strictly exceed (if {@code strict} is {@code true}) or be at least as
  1775.      * large as {@code strict = false}) as {@code kolmogorovSmirnovStatistic(x, y)}.
  1776.      * <ul>
  1777.      * <li>For small samples (where the product of the sample sizes is less than
  1778.      * {@link KolmogorovSmirnovTest#LARGE_SAMPLE_PRODUCT}), the exact p-value is computed using the method presented
  1779.      * in [4], implemented in {@link #exactP(double, int, int, boolean)}. </li>
  1780.      * <li>When the product of the sample sizes exceeds {@link KolmogorovSmirnovTest#LARGE_SAMPLE_PRODUCT}, the
  1781.      * asymptotic distribution of \(D_{n,m}\) is used. See {@link #approximateP(double, int, int)}
  1782.      * for details on the approximation.</li>
  1783.      * </ul><p>
  1784.      * If {@code x.length * y.length} &lt; {@link KolmogorovSmirnovTest#LARGE_SAMPLE_PRODUCT} and the combined set of values in
  1785.      * {@code x} and {@code y} contains ties, random jitter is added to {@code x} and {@code y} to
  1786.      * break ties before computing \(D_{n,m}\) and the p-value. The jitter is uniformly distributed
  1787.      * on (-minDelta / 2, minDelta / 2) where minDelta is the smallest pairwise difference between
  1788.      * values in the combined sample.</p>
  1789.      * <p>
  1790.      * If ties are known to be present in the data, {@link KolmogorovSmirnovTest#bootstrap(double[], double[], int, boolean)}
  1791.      * may be used as an alternative method for estimating the p-value.</p>
  1792.      *
  1793.      * @param x first sample dataset
  1794.      * @param y second sample dataset
  1795.      * @param strict whether or not the probability to compute is expressed as a strict inequality
  1796.      *        (ignored for large samples)
  1797.      * @return p-value associated with the null hypothesis that {@code x} and {@code y} represent
  1798.      *         samples from the same distribution
  1799.      * @throws MathIllegalArgumentException if either {@code x} or {@code y} does not have length at
  1800.      *         least 2
  1801.      * @throws org.hipparchus.exception.NullArgumentException if either {@code x} or {@code y} is null
  1802.      * @see KolmogorovSmirnovTest#bootstrap(double[], double[], int, boolean)
  1803.      */
  1804.     public static double kolmogorovSmirnovTest(double[] x, double[] y, boolean strict)
  1805.             throws MathIllegalArgumentException, NullArgumentException  {
  1806.         return KS_TEST.kolmogorovSmirnovTest(x, y, strict);
  1807.     }

  1808.     /**
  1809.      * Computes \(P(D_{n,m} &gt; d)\) if {@code strict} is {@code true}; otherwise \(P(D_{n,m} \ge
  1810.      * d)\), where \(D_{n,m}\) is the 2-sample Kolmogorov-Smirnov statistic. See
  1811.      * {@link KolmogorovSmirnovTest#kolmogorovSmirnovStatistic(double[], double[])} for the definition of \(D_{n,m}\).
  1812.      * <p>
  1813.      * The returned probability is exact, implemented by unwinding the recursive function
  1814.      * definitions presented in [4] from the class javadoc.
  1815.      * </p>
  1816.      *
  1817.      * @param d D-statistic value
  1818.      * @param n first sample size
  1819.      * @param m second sample size
  1820.      * @param strict whether or not the probability to compute is expressed as a strict inequality
  1821.      * @return probability that a randomly selected m-n partition of m + n generates \(D_{n,m}\)
  1822.      *         greater than (resp. greater than or equal to) {@code d}
  1823.      */
  1824.     public static double exactP(double d, int m, int n, boolean strict) {
  1825.         return KS_TEST.exactP(d, n, m, strict);
  1826.     }

  1827.     /**
  1828.      * Uses the Kolmogorov-Smirnov distribution to approximate \(P(D_{n,m} &gt; d)\) where \(D_{n,m}\)
  1829.      * is the 2-sample Kolmogorov-Smirnov statistic. See
  1830.      * {@link KolmogorovSmirnovTest#kolmogorovSmirnovStatistic(double[], double[])} for the definition of \(D_{n,m}\).
  1831.      * <p>
  1832.      * Specifically, what is returned is \(1 - k(d \sqrt{mn / (m + n)})\) where \(k(t) = 1 + 2
  1833.      * \sum_{i=1}^\infty (-1)^i e^{-2 i^2 t^2}\). See {@link KolmogorovSmirnovTest#ksSum(double, double, int)} for
  1834.      * details on how convergence of the sum is determined. This implementation passes {@code ksSum}
  1835.      * {@link KolmogorovSmirnovTest#KS_SUM_CAUCHY_CRITERION} as {@code tolerance} and
  1836.      * {@link KolmogorovSmirnovTest#MAXIMUM_PARTIAL_SUM_COUNT} as {@code maxIterations}.
  1837.      * </p>
  1838.      *
  1839.      * @param d D-statistic value
  1840.      * @param n first sample size
  1841.      * @param m second sample size
  1842.      * @return approximate probability that a randomly selected m-n partition of m + n generates
  1843.      *         \(D_{n,m}\) greater than {@code d}
  1844.      */
  1845.     public static double approximateP(double d, int n, int m) {
  1846.         return KS_TEST.approximateP(d, n, m);
  1847.     }

  1848. }