GTest.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 org.hipparchus.distribution.continuous.ChiSquaredDistribution;
  23. import org.hipparchus.exception.LocalizedCoreFormats;
  24. import org.hipparchus.exception.MathIllegalArgumentException;
  25. import org.hipparchus.exception.MathIllegalStateException;
  26. import org.hipparchus.stat.LocalizedStatFormats;
  27. import org.hipparchus.util.FastMath;
  28. import org.hipparchus.util.MathArrays;
  29. import org.hipparchus.util.MathUtils;

  30. /**
  31.  * Implements <a href="http://en.wikipedia.org/wiki/G-test">G Test</a>
  32.  * statistics.
  33.  * <p>
  34.  * This is known in statistical genetics as the McDonald-Kreitman test.
  35.  * The implementation handles both known and unknown distributions.
  36.  * <p>
  37.  * Two samples tests can be used when the distribution is unknown <i>a priori</i>
  38.  * but provided by one sample, or when the hypothesis under test is that the two
  39.  * samples come from the same underlying distribution.
  40.  */
  41. public class GTest { // NOPMD - this is not a Junit test class, PMD false positive here

  42.     /** Empty constructor.
  43.      * <p>
  44.      * This constructor is not strictly necessary, but it prevents spurious
  45.      * javadoc warnings with JDK 18 and later.
  46.      * </p>
  47.      * @since 3.0
  48.      */
  49.     public GTest() { // NOPMD - unnecessary constructor added intentionally to make javadoc happy
  50.         // nothing to do
  51.     }

  52.     /**
  53.      * Computes the <a href="http://en.wikipedia.org/wiki/G-test">G statistic
  54.      * for Goodness of Fit</a> comparing {@code observed} and {@code expected}
  55.      * frequency counts.
  56.      * <p>
  57.      * This statistic can be used to perform a G test (Log-Likelihood Ratio
  58.      * Test) evaluating the null hypothesis that the observed counts follow the
  59.      * expected distribution.
  60.      * <p>
  61.      * <strong>Preconditions</strong>:
  62.      * <ul>
  63.      * <li>Expected counts must all be positive.</li>
  64.      * <li>Observed counts must all be &ge; 0.</li>
  65.      * <li>The observed and expected arrays must have the same length and their
  66.      * common length must be at least 2. </li>
  67.      * </ul>
  68.      * <p>
  69.      * If any of the preconditions are not met, a
  70.      * {@code MathIllegalArgumentException} is thrown.
  71.      * <p>
  72.      * <strong>Note:</strong>This implementation rescales the
  73.      * {@code expected} array if necessary to ensure that the sum of the
  74.      * expected and observed counts are equal.
  75.      *
  76.      * @param observed array of observed frequency counts
  77.      * @param expected array of expected frequency counts
  78.      * @return G-Test statistic
  79.      * @throws MathIllegalArgumentException if {@code observed} has negative entries
  80.      * @throws MathIllegalArgumentException if {@code expected} has entries that
  81.      * are not strictly positive
  82.      * @throws MathIllegalArgumentException if the array lengths do not match or
  83.      * are less than 2.
  84.      */
  85.     public double g(final double[] expected, final long[] observed)
  86.             throws MathIllegalArgumentException {

  87.         if (expected.length < 2) {
  88.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  89.                                                    expected.length, 2);
  90.         }
  91.         MathUtils.checkDimension(expected.length, observed.length);
  92.         MathArrays.checkPositive(expected);
  93.         MathArrays.checkNonNegative(observed);

  94.         double sumExpected = 0d;
  95.         double sumObserved = 0d;
  96.         for (int i = 0; i < observed.length; i++) {
  97.             sumExpected += expected[i];
  98.             sumObserved += observed[i];
  99.         }
  100.         double ratio = 1d;
  101.         boolean rescale = false;
  102.         if (FastMath.abs(sumExpected - sumObserved) > 10E-6) {
  103.             ratio = sumObserved / sumExpected;
  104.             rescale = true;
  105.         }
  106.         double sum = 0d;
  107.         for (int i = 0; i < observed.length; i++) {
  108.             final double dev = rescale ?
  109.                     FastMath.log(observed[i] / (ratio * expected[i])) :
  110.                         FastMath.log(observed[i] / expected[i]);
  111.             sum += (observed[i]) * dev;
  112.         }
  113.         return 2d * sum;
  114.     }

  115.     /**
  116.      * Returns the <i>observed significance level</i>, or <a href=
  117.      * "http://www.cas.lancs.ac.uk/glossary_v1.1/hyptest.html#pvalue"> p-value</a>,
  118.      * associated with a G-Test for goodness of fit comparing the
  119.      * {@code observed} frequency counts to those in the {@code expected} array.
  120.      *
  121.      * <p>The number returned is the smallest significance level at which one
  122.      * can reject the null hypothesis that the observed counts conform to the
  123.      * frequency distribution described by the expected counts.</p>
  124.      *
  125.      * <p>The probability returned is the tail probability beyond
  126.      * {@link #g(double[], long[]) g(expected, observed)}
  127.      * in the ChiSquare distribution with degrees of freedom one less than the
  128.      * common length of {@code expected} and {@code observed}.</p>
  129.      *
  130.      * <p> <strong>Preconditions</strong>:</p>
  131.      * <ul>
  132.      * <li>Expected counts must all be positive. </li>
  133.      * <li>Observed counts must all be &ge; 0. </li>
  134.      * <li>The observed and expected arrays must have the
  135.      * same length and their common length must be at least 2.</li>
  136.      * </ul>
  137.      *
  138.      * <p>If any of the preconditions are not met, a
  139.      * {@code MathIllegalArgumentException} is thrown.</p>
  140.      *
  141.      * <p><strong>Note:</strong>This implementation rescales the
  142.      * {@code expected} array if necessary to ensure that the sum of the
  143.      *  expected and observed counts are equal.</p>
  144.      *
  145.      * @param observed array of observed frequency counts
  146.      * @param expected array of expected frequency counts
  147.      * @return p-value
  148.      * @throws MathIllegalArgumentException if {@code observed} has negative entries
  149.      * @throws MathIllegalArgumentException if {@code expected} has entries that
  150.      * are not strictly positive
  151.      * @throws MathIllegalArgumentException if the array lengths do not match or
  152.      * are less than 2.
  153.      * @throws MathIllegalStateException if an error occurs computing the
  154.      * p-value.
  155.      */
  156.     public double gTest(final double[] expected, final long[] observed)
  157.             throws MathIllegalArgumentException, MathIllegalStateException {

  158.         final ChiSquaredDistribution distribution =
  159.                 new ChiSquaredDistribution(expected.length - 1.0);
  160.         return 1.0 - distribution.cumulativeProbability(g(expected, observed));
  161.     }

  162.     /**
  163.      * Returns the intrinsic (Hardy-Weinberg proportions) p-Value, as described
  164.      * in p64-69 of McDonald, J.H. 2009. Handbook of Biological Statistics
  165.      * (2nd ed.). Sparky House Publishing, Baltimore, Maryland.
  166.      *
  167.      * <p> The probability returned is the tail probability beyond
  168.      * {@link #g(double[], long[]) g(expected, observed)}
  169.      * in the ChiSquare distribution with degrees of freedom two less than the
  170.      * common length of {@code expected} and {@code observed}.</p>
  171.      *
  172.      * @param observed array of observed frequency counts
  173.      * @param expected array of expected frequency counts
  174.      * @return p-value
  175.      * @throws MathIllegalArgumentException if {@code observed} has negative entries
  176.      * @throws MathIllegalArgumentException {@code expected} has entries that are
  177.      * not strictly positive
  178.      * @throws MathIllegalArgumentException if the array lengths do not match or
  179.      * are less than 2.
  180.      * @throws MathIllegalStateException if an error occurs computing the
  181.      * p-value.
  182.      */
  183.     public double gTestIntrinsic(final double[] expected, final long[] observed)
  184.             throws MathIllegalArgumentException, MathIllegalStateException {

  185.         final ChiSquaredDistribution distribution =
  186.                 new ChiSquaredDistribution(expected.length - 2.0);
  187.         return 1.0 - distribution.cumulativeProbability(g(expected, observed));
  188.     }

  189.     /**
  190.      * Performs a G-Test (Log-Likelihood Ratio Test) for goodness of fit
  191.      * evaluating the null hypothesis that the observed counts conform to the
  192.      * frequency distribution described by the expected counts, with
  193.      * significance level {@code alpha}. Returns true iff the null
  194.      * hypothesis can be rejected with {@code 100 * (1 - alpha)} percent confidence.
  195.      *
  196.      * <p><strong>Example:</strong><br> To test the hypothesis that
  197.      * {@code observed} follows {@code expected} at the 99% level,
  198.      * use </p><p>
  199.      * {@code gTest(expected, observed, 0.01)}</p>
  200.      *
  201.      * <p>Returns true iff {@link #gTest(double[], long[])
  202.      *  gTestGoodnessOfFitPValue(expected, observed)} &gt; alpha</p>
  203.      *
  204.      * <p><strong>Preconditions</strong>:</p>
  205.      * <ul>
  206.      * <li>Expected counts must all be positive. </li>
  207.      * <li>Observed counts must all be &ge; 0. </li>
  208.      * <li>The observed and expected arrays must have the same length and their
  209.      * common length must be at least 2.
  210.      * <li> {@code 0 < alpha < 0.5} </li></ul>
  211.      *
  212.      * <p>If any of the preconditions are not met, a
  213.      * {@code MathIllegalArgumentException} is thrown.</p>
  214.      *
  215.      * <p><strong>Note:</strong>This implementation rescales the
  216.      * {@code expected} array if necessary to ensure that the sum of the
  217.      * expected and observed counts are equal.</p>
  218.      *
  219.      * @param observed array of observed frequency counts
  220.      * @param expected array of expected frequency counts
  221.      * @param alpha significance level of the test
  222.      * @return true iff null hypothesis can be rejected with confidence 1 -
  223.      * alpha
  224.      * @throws MathIllegalArgumentException if {@code observed} has negative entries
  225.      * @throws MathIllegalArgumentException if {@code expected} has entries that
  226.      * are not strictly positive
  227.      * @throws MathIllegalArgumentException if the array lengths do not match or
  228.      * are less than 2.
  229.      * @throws MathIllegalStateException if an error occurs computing the
  230.      * p-value.
  231.      * @throws MathIllegalArgumentException if alpha is not strictly greater than zero
  232.      * and less than or equal to 0.5
  233.      */
  234.     public boolean gTest(final double[] expected, final long[] observed,
  235.             final double alpha)
  236.             throws MathIllegalArgumentException, MathIllegalStateException {

  237.         if ((alpha <= 0) || (alpha > 0.5)) {
  238.             throw new MathIllegalArgumentException(LocalizedStatFormats.OUT_OF_BOUND_SIGNIFICANCE_LEVEL,
  239.                     alpha, 0, 0.5);
  240.         }
  241.         return gTest(expected, observed) < alpha;
  242.     }

  243.     /**
  244.      * Calculates the <a href=
  245.      * "http://en.wikipedia.org/wiki/Entropy_%28information_theory%29">Shannon
  246.      * entropy</a> for 2 Dimensional Matrix.  The value returned is the entropy
  247.      * of the vector formed by concatenating the rows (or columns) of {@code k}
  248.      * to form a vector. See {@link #entropy(long[])}.
  249.      *
  250.      * @param k 2 Dimensional Matrix of long values (for ex. the counts of a
  251.      * trials)
  252.      * @return Shannon Entropy of the given Matrix
  253.      *
  254.      */
  255.     private double entropy(final long[][] k) {
  256.         double h = 0d;
  257.         double sum_k = 0d;
  258.         for (int i = 0; i < k.length; i++) {
  259.             for (int j = 0; j < k[i].length; j++) {
  260.                 sum_k += k[i][j];
  261.             }
  262.         }
  263.         for (int i = 0; i < k.length; i++) {
  264.             for (int j = 0; j < k[i].length; j++) {
  265.                 if (k[i][j] != 0) {
  266.                     final double p_ij = k[i][j] / sum_k;
  267.                     h += p_ij * FastMath.log(p_ij);
  268.                 }
  269.             }
  270.         }
  271.         return -h;
  272.     }

  273.     /**
  274.      * Calculates the <a href="http://en.wikipedia.org/wiki/Entropy_%28information_theory%29">
  275.      * Shannon entropy</a> for a vector.  The values of {@code k} are taken to be
  276.      * incidence counts of the values of a random variable. What is returned is <br>
  277.      * &sum;p<sub>i</sub>log(p<sub>i</sub><br>
  278.      * where p<sub>i</sub> = k[i] / (sum of elements in k)
  279.      *
  280.      * @param k Vector (for ex. Row Sums of a trials)
  281.      * @return Shannon Entropy of the given Vector
  282.      *
  283.      */
  284.     private double entropy(final long[] k) {
  285.         double h = 0d;
  286.         double sum_k = 0d;
  287.         for (int i = 0; i < k.length; i++) {
  288.             sum_k += k[i];
  289.         }
  290.         for (int i = 0; i < k.length; i++) {
  291.             if (k[i] != 0) {
  292.                 final double p_i = k[i] / sum_k;
  293.                 h += p_i * FastMath.log(p_i);
  294.             }
  295.         }
  296.         return -h;
  297.     }

  298.     /**
  299.      * <p>Computes a G (Log-Likelihood Ratio) two sample test statistic for
  300.      * independence comparing frequency counts in
  301.      * {@code observed1} and {@code observed2}. The sums of frequency
  302.      * counts in the two samples are not required to be the same. The formula
  303.      * used to compute the test statistic is </p>
  304.      *
  305.      * <p>{@code 2 * totalSum * [H(rowSums) + H(colSums) - H(k)]}</p>
  306.      *
  307.      * <p> where {@code H} is the
  308.      * <a href="http://en.wikipedia.org/wiki/Entropy_%28information_theory%29">
  309.      * Shannon Entropy</a> of the random variable formed by viewing the elements
  310.      * of the argument array as incidence counts; <br>
  311.      * {@code k} is a matrix with rows {@code [observed1, observed2]}; <br>
  312.      * {@code rowSums, colSums} are the row/col sums of {@code k}; <br>
  313.      * and {@code totalSum} is the overall sum of all entries in {@code k}.</p>
  314.      *
  315.      * <p>This statistic can be used to perform a G test evaluating the null
  316.      * hypothesis that both observed counts are independent </p>
  317.      *
  318.      * <p> <strong>Preconditions</strong>:</p>
  319.      * <ul>
  320.      * <li>Observed counts must be non-negative. </li>
  321.      * <li>Observed counts for a specific bin must not both be zero. </li>
  322.      * <li>Observed counts for a specific sample must not all be  0. </li>
  323.      * <li>The arrays {@code observed1} and {@code observed2} must have
  324.      * the same length and their common length must be at least 2. </li></ul>
  325.      *
  326.      * <p>If any of the preconditions are not met, a
  327.      * {@code MathIllegalArgumentException} is thrown.</p>
  328.      *
  329.      * @param observed1 array of observed frequency counts of the first data set
  330.      * @param observed2 array of observed frequency counts of the second data
  331.      * set
  332.      * @return G-Test statistic
  333.      * @throws MathIllegalArgumentException the the lengths of the arrays do not
  334.      * match or their common length is less than 2
  335.      * @throws MathIllegalArgumentException if any entry in {@code observed1} or
  336.      * {@code observed2} is negative
  337.      * @throws MathIllegalArgumentException if either all counts of
  338.      * {@code observed1} or {@code observed2} are zero, or if the count
  339.      * at the same index is zero for both arrays.
  340.      */
  341.     public double gDataSetsComparison(final long[] observed1, final long[] observed2)
  342.             throws MathIllegalArgumentException {

  343.         // Make sure lengths are same
  344.         if (observed1.length < 2) {
  345.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  346.                                                    observed1.length, 2);
  347.         }
  348.         MathUtils.checkDimension(observed1.length, observed2.length);

  349.         // Ensure non-negative counts
  350.         MathArrays.checkNonNegative(observed1);
  351.         MathArrays.checkNonNegative(observed2);

  352.         // Compute and compare count sums
  353.         long countSum1 = 0;
  354.         long countSum2 = 0;

  355.         // Compute and compare count sums
  356.         final long[] collSums = new long[observed1.length];
  357.         final long[][] k = new long[2][observed1.length];

  358.         for (int i = 0; i < observed1.length; i++) {
  359.             if (observed1[i] == 0 && observed2[i] == 0) {
  360.                 throw new MathIllegalArgumentException(LocalizedCoreFormats.OBSERVED_COUNTS_BOTTH_ZERO_FOR_ENTRY, i);
  361.             } else {
  362.                 countSum1 += observed1[i];
  363.                 countSum2 += observed2[i];
  364.                 collSums[i] = observed1[i] + observed2[i];
  365.                 k[0][i] = observed1[i];
  366.                 k[1][i] = observed2[i];
  367.             }
  368.         }
  369.         // Ensure neither sample is uniformly 0
  370.         if (countSum1 == 0 || countSum2 == 0) {
  371.             throw new MathIllegalArgumentException(LocalizedCoreFormats.ZERO_NOT_ALLOWED);
  372.         }
  373.         final long[] rowSums = {countSum1, countSum2};
  374.         final double sum = (double) countSum1 + (double) countSum2;
  375.         return 2 * sum * (entropy(rowSums) + entropy(collSums) - entropy(k));
  376.     }

  377.     /**
  378.      * Calculates the root log-likelihood ratio for 2 state Datasets. See
  379.      * {@link #gDataSetsComparison(long[], long[] )}.
  380.      *
  381.      * <p>Given two events A and B, let k11 be the number of times both events
  382.      * occur, k12 the incidence of B without A, k21 the count of A without B,
  383.      * and k22 the number of times neither A nor B occurs.  What is returned
  384.      * by this method is </p>
  385.      *
  386.      * <p>{@code (sgn) sqrt(gValueDataSetsComparison({k11, k12}, {k21, k22})}</p>
  387.      *
  388.      * <p>where {@code sgn} is -1 if {@code k11 / (k11 + k12) < k21 / (k21 + k22))};<br>
  389.      * 1 otherwise.</p>
  390.      *
  391.      * <p>Signed root LLR has two advantages over the basic LLR: a) it is positive
  392.      * where k11 is bigger than expected, negative where it is lower b) if there is
  393.      * no difference it is asymptotically normally distributed. This allows one
  394.      * to talk about "number of standard deviations" which is a more common frame
  395.      * of reference than the chi^2 distribution.</p>
  396.      *
  397.      * @param k11 number of times the two events occurred together (AB)
  398.      * @param k12 number of times the second event occurred WITHOUT the
  399.      * first event (notA,B)
  400.      * @param k21 number of times the first event occurred WITHOUT the
  401.      * second event (A, notB)
  402.      * @param k22 number of times something else occurred (i.e. was neither
  403.      * of these events (notA, notB)
  404.      * @return root log-likelihood ratio
  405.      *
  406.      */
  407.     public double rootLogLikelihoodRatio(final long k11, long k12,
  408.             final long k21, final long k22) {
  409.         final double llr = gDataSetsComparison(
  410.                 new long[]{k11, k12}, new long[]{k21, k22});
  411.         double sqrt = FastMath.sqrt(llr);
  412.         if ((double) k11 / (k11 + k12) < (double) k21 / (k21 + k22)) {
  413.             sqrt = -sqrt;
  414.         }
  415.         return sqrt;
  416.     }

  417.     /**
  418.      * <p>Returns the <i>observed significance level</i>, or <a href=
  419.      * "http://www.cas.lancs.ac.uk/glossary_v1.1/hyptest.html#pvalue">
  420.      * p-value</a>, associated with a G-Value (Log-Likelihood Ratio) for two
  421.      * sample test comparing bin frequency counts in {@code observed1} and
  422.      * {@code observed2}.</p>
  423.      *
  424.      * <p>The number returned is the smallest significance level at which one
  425.      * can reject the null hypothesis that the observed counts conform to the
  426.      * same distribution. </p>
  427.      *
  428.      * <p>See {@link #gTest(double[], long[])} for details
  429.      * on how the p-value is computed.  The degrees of of freedom used to
  430.      * perform the test is one less than the common length of the input observed
  431.      * count arrays.</p>
  432.      *
  433.      * <p><strong>Preconditions</strong>:</p>
  434.      * <ul> <li>Observed counts must be non-negative. </li>
  435.      * <li>Observed counts for a specific bin must not both be zero. </li>
  436.      * <li>Observed counts for a specific sample must not all be 0. </li>
  437.      * <li>The arrays {@code observed1} and {@code observed2} must
  438.      * have the same length and their common length must be at least 2. </li>
  439.      * </ul>
  440.      * <p> If any of the preconditions are not met, a
  441.      * {@code MathIllegalArgumentException} is thrown.</p>
  442.      *
  443.      * @param observed1 array of observed frequency counts of the first data set
  444.      * @param observed2 array of observed frequency counts of the second data
  445.      * set
  446.      * @return p-value
  447.      * @throws MathIllegalArgumentException the the length of the arrays does not
  448.      * match or their common length is less than 2
  449.      * @throws MathIllegalArgumentException if any of the entries in {@code observed1} or
  450.      * {@code observed2} are negative
  451.      * @throws MathIllegalArgumentException if either all counts of {@code observed1} or
  452.      * {@code observed2} are zero, or if the count at some index is
  453.      * zero for both arrays
  454.      * @throws MathIllegalStateException if an error occurs computing the
  455.      * p-value.
  456.      */
  457.     public double gTestDataSetsComparison(final long[] observed1,
  458.             final long[] observed2)
  459.             throws MathIllegalArgumentException,
  460.             MathIllegalStateException {

  461.         final ChiSquaredDistribution distribution =
  462.                 new ChiSquaredDistribution((double) observed1.length - 1);
  463.         return 1 - distribution.cumulativeProbability(
  464.                 gDataSetsComparison(observed1, observed2));
  465.     }

  466.     /**
  467.      * <p>Performs a G-Test (Log-Likelihood Ratio Test) comparing two binned
  468.      * data sets. The test evaluates the null hypothesis that the two lists
  469.      * of observed counts conform to the same frequency distribution, with
  470.      * significance level {@code alpha}. Returns true iff the null
  471.      * hypothesis can be rejected  with 100 * (1 - alpha) percent confidence.
  472.      * </p>
  473.      * <p>See {@link #gDataSetsComparison(long[], long[])} for details
  474.      * on the formula used to compute the G (LLR) statistic used in the test and
  475.      * {@link #gTest(double[], long[])} for information on how
  476.      * the observed significance level is computed. The degrees of of freedom used
  477.      * to perform the test is one less than the common length of the input observed
  478.      * count arrays. </p>
  479.      *
  480.      * <p><strong>Preconditions</strong>:</p>
  481.      * <ul>
  482.      * <li>Observed counts must be non-negative. </li>
  483.      * <li>Observed counts for a specific bin must not both be zero. </li>
  484.      * <li>Observed counts for a specific sample must not all be 0. </li>
  485.      * <li>The arrays {@code observed1} and {@code observed2} must
  486.      * have the same length and their common length must be at least 2. </li>
  487.      * <li>{@code 0 < alpha < 0.5} </li></ul>
  488.      *
  489.      * <p>If any of the preconditions are not met, a
  490.      * {@code MathIllegalArgumentException} is thrown.</p>
  491.      *
  492.      * @param observed1 array of observed frequency counts of the first data set
  493.      * @param observed2 array of observed frequency counts of the second data
  494.      * set
  495.      * @param alpha significance level of the test
  496.      * @return true iff null hypothesis can be rejected with confidence 1 -
  497.      * alpha
  498.      * @throws MathIllegalArgumentException the the length of the arrays does not
  499.      * match
  500.      * @throws MathIllegalArgumentException if any of the entries in {@code observed1} or
  501.      * {@code observed2} are negative
  502.      * @throws MathIllegalArgumentException if either all counts of {@code observed1} or
  503.      * {@code observed2} are zero, or if the count at some index is
  504.      * zero for both arrays
  505.      * @throws MathIllegalArgumentException if {@code alpha} is not in the range
  506.      * (0, 0.5]
  507.      * @throws MathIllegalStateException if an error occurs performing the test
  508.      */
  509.     public boolean gTestDataSetsComparison(
  510.             final long[] observed1,
  511.             final long[] observed2,
  512.             final double alpha)
  513.             throws MathIllegalArgumentException, MathIllegalStateException {

  514.         if (alpha <= 0 || alpha > 0.5) {
  515.             throw new MathIllegalArgumentException(
  516.                     LocalizedStatFormats.OUT_OF_BOUND_SIGNIFICANCE_LEVEL, alpha, 0, 0.5);
  517.         }
  518.         return gTestDataSetsComparison(observed1, observed2) < alpha;
  519.     }
  520. }