You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2012/02/12 19:07:54 UTC

svn commit: r1243286 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/stat/inference/ test/java/org/apache/commons/math/ test/java/org/apache/commons/math/random/ test/java/org/apache/commons/math/stat/inference/

Author: tn
Date: Sun Feb 12 18:07:53 2012
New Revision: 1243286

URL: http://svn.apache.org/viewvc?rev=1243286&view=rev
Log:
Merged ChiSquareTest implementation and interface, removed use of MathException.
JIRA: MATH-488, MATH-739

Removed:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/ChiSquareTestImpl.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/UnknownDistributionChiSquareTest.java
Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/ChiSquareTest.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/TestUtils.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/ChiSquareTestTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/TestUtilsTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/ChiSquareTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/ChiSquareTest.java?rev=1243286&r1=1243285&r2=1243286&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/ChiSquareTest.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/ChiSquareTest.java Sun Feb 12 18:07:53 2012
@@ -16,43 +16,104 @@
  */
 package org.apache.commons.math.stat.inference;
 
-import org.apache.commons.math.MathException;
+import org.apache.commons.math.distribution.ChiSquaredDistribution;
+import org.apache.commons.math.exception.DimensionMismatchException;
+import org.apache.commons.math.exception.MaxCountExceededException;
+import org.apache.commons.math.exception.NotPositiveException;
+import org.apache.commons.math.exception.NotStrictlyPositiveException;
+import org.apache.commons.math.exception.NullArgumentException;
+import org.apache.commons.math.exception.OutOfRangeException;
+import org.apache.commons.math.exception.ZeroException;
+import org.apache.commons.math.exception.util.LocalizedFormats;
+import org.apache.commons.math.util.FastMath;
+import org.apache.commons.math.util.MathUtils;
 
 /**
- * An interface for Chi-Square tests.
- * <p>This interface handles only known distributions. If the distribution is
- * unknown and should be provided by a sample, then the {@link UnknownDistributionChiSquareTest
- * UnknownDistributionChiSquareTest} extended interface should be used instead.</p>
+ * Implements Chi-Square test statistics.
+ * <p>This implementation handles both, known and unknown distributions.</p>
+ * <p>Two samples tests are used when the distribution is unknown <i>a priori</i>
+ * but provided by one sample. We compare the second sample against the first.</p>
+ *
  * @version $Id$
  */
-public interface ChiSquareTest {
+public class ChiSquareTest {
 
-     /**
+    /**
+     * Construct a ChiSquareTest
+     */
+    public ChiSquareTest() {
+        super();
+    }
+
+    /**
      * Computes the <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm">
      * Chi-Square statistic</a> comparing <code>observed</code> and <code>expected</code>
      * frequency counts.
      * <p>
-     * This statistic can be used to perform a Chi-Square test evaluating the null hypothesis that
-     *  the observed counts follow the expected distribution.</p>
+     * This statistic can be used to perform a Chi-Square test evaluating the null
+     * hypothesis that the observed counts follow the expected distribution.</p>
      * <p>
      * <strong>Preconditions</strong>: <ul>
      * <li>Expected counts must all be positive.
      * </li>
-     * <li>Observed counts must all be >= 0.
+     * <li>Observed counts must all be &ge; 0.
      * </li>
      * <li>The observed and expected arrays must have the same length and
      * their common length must be at least 2.
      * </li></ul></p><p>
      * If any of the preconditions are not met, an
      * <code>IllegalArgumentException</code> is thrown.</p>
+     * <p><strong>Note: </strong>This implementation rescales the
+     * <code>expected</code> array if necessary to ensure that the sum of the
+     * expected and observed counts are equal.</p>
      *
      * @param observed array of observed frequency counts
      * @param expected array of expected frequency counts
-     * @return chiSquare statistic
-     * @throws IllegalArgumentException if preconditions are not met
+     * @return chiSquare test statistic
+     * @throws NotPositiveException if one element of <code>expected</code> is not
+     * positive
+     * @throws NotStrictlyPositiveException if one element of <code>observed</code> is
+     * not strictly positive
+     * @throws DimensionMismatchException if the arrays length is less than 2
      */
-    double chiSquare(double[] expected, long[] observed)
-        throws IllegalArgumentException;
+    public double chiSquare(final double[] expected, final long[] observed)
+        throws NotPositiveException, NotStrictlyPositiveException,
+        DimensionMismatchException {
+
+        if (expected.length < 2) {
+            throw new DimensionMismatchException(expected.length, 2);
+        }
+        if (expected.length != observed.length) {
+            throw new DimensionMismatchException(expected.length, observed.length);
+        }
+        checkPositive(expected);
+        checkNonNegative(observed);
+
+        double sumExpected = 0d;
+        double sumObserved = 0d;
+        for (int i = 0; i < observed.length; i++) {
+            sumExpected += expected[i];
+            sumObserved += observed[i];
+        }
+        double ratio = 1.0d;
+        boolean rescale = false;
+        if (FastMath.abs(sumExpected - sumObserved) > 10E-6) {
+            ratio = sumObserved / sumExpected;
+            rescale = true;
+        }
+        double sumSq = 0.0d;
+        for (int i = 0; i < observed.length; i++) {
+            if (rescale) {
+                final double dev = observed[i] - ratio * expected[i];
+                sumSq += dev * dev / (ratio * expected[i]);
+            } else {
+                final double dev = observed[i] - expected[i];
+                sumSq += dev * dev / expected[i];
+            }
+        }
+        return sumSq;
+
+    }
 
     /**
      * Returns the <i>observed significance level</i>, or <a href=
@@ -69,29 +130,43 @@ public interface ChiSquareTest {
      * <strong>Preconditions</strong>: <ul>
      * <li>Expected counts must all be positive.
      * </li>
-     * <li>Observed counts must all be >= 0.
+     * <li>Observed counts must all be &ge; 0.
      * </li>
      * <li>The observed and expected arrays must have the same length and
      * their common length must be at least 2.
      * </li></ul></p><p>
      * If any of the preconditions are not met, an
      * <code>IllegalArgumentException</code> is thrown.</p>
+     * <p><strong>Note: </strong>This implementation rescales the
+     * <code>expected</code> array if necessary to ensure that the sum of the
+     * expected and observed counts are equal.</p>
      *
      * @param observed array of observed frequency counts
      * @param expected array of expected frequency counts
      * @return p-value
-     * @throws IllegalArgumentException if preconditions are not met
-     * @throws MathException if an error occurs computing the p-value
+     * @throws NotPositiveException if one element of <code>expected</code> is not
+     * positive
+     * @throws NotStrictlyPositiveException if one element of <code>observed</code> is
+     * not strictly positive
+     * @throws DimensionMismatchException if the arrays length is less than 2
+     * @throws MaxCountExceededException if an error occurs computing the p-value
      */
-    double chiSquareTest(double[] expected, long[] observed)
-        throws IllegalArgumentException, MathException;
+    public double chiSquareTest(final double[] expected, final long[] observed)
+        throws NotPositiveException, NotStrictlyPositiveException,
+        DimensionMismatchException, MaxCountExceededException {
+
+        ChiSquaredDistribution distribution =
+            new ChiSquaredDistribution(expected.length - 1.0);
+        return 1.0 - distribution.cumulativeProbability(chiSquare(expected, observed));
+
+    }
 
     /**
      * Performs a <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm">
-     * Chi-square goodness of fit test</a> evaluating the null hypothesis that the observed counts
-     * conform to the frequency distribution described by the expected counts, with
-     * significance level <code>alpha</code>.  Returns true iff the null hypothesis can be rejected
-     * with 100 * (1 - alpha) percent confidence.
+     * Chi-square goodness of fit test</a> evaluating the null hypothesis that the
+     * observed counts conform to the frequency distribution described by the expected
+     * counts, with significance level <code>alpha</code>.  Returns true iff the null
+     * hypothesis can be rejected with 100 * (1 - alpha) percent confidence.
      * <p>
      * <strong>Example:</strong><br>
      * To test the hypothesis that <code>observed</code> follows
@@ -101,25 +176,43 @@ public interface ChiSquareTest {
      * <strong>Preconditions</strong>: <ul>
      * <li>Expected counts must all be positive.
      * </li>
-     * <li>Observed counts must all be >= 0.
+     * <li>Observed counts must all be &ge; 0.
      * </li>
      * <li>The observed and expected arrays must have the same length and
      * their common length must be at least 2.
-     * <li> <code> 0 < alpha < 0.5 </code>
+     * <li> <code> 0 &lt; alpha &lt; 0.5 </code>
      * </li></ul></p><p>
      * If any of the preconditions are not met, an
      * <code>IllegalArgumentException</code> is thrown.</p>
+     * <p><strong>Note: </strong>This implementation rescales the
+     * <code>expected</code> array if necessary to ensure that the sum of the
+     * expected and observed counts are equal.</p>
      *
      * @param observed array of observed frequency counts
      * @param expected array of expected frequency counts
      * @param alpha significance level of the test
      * @return true iff null hypothesis can be rejected with confidence
      * 1 - alpha
-     * @throws IllegalArgumentException if preconditions are not met
-     * @throws MathException if an error occurs performing the test
+     * @throws NotPositiveException if one element of <code>expected</code> is not
+     * positive
+     * @throws NotStrictlyPositiveException if one element of <code>observed</code> is
+     * not strictly positive
+     * @throws DimensionMismatchException if the arrays length is less than 2
+     * @throws OutOfRangeException if <code>alpha</code> is not in the range (0, 0.5]
+     * @throws MaxCountExceededException if an error occurs computing the p-value
      */
-    boolean chiSquareTest(double[] expected, long[] observed, double alpha)
-        throws IllegalArgumentException, MathException;
+    public boolean chiSquareTest(final double[] expected, final long[] observed,
+                                 final double alpha)
+        throws NotPositiveException, NotStrictlyPositiveException,
+        DimensionMismatchException, OutOfRangeException, MaxCountExceededException {
+
+        if ((alpha <= 0) || (alpha > 0.5)) {
+            throw new OutOfRangeException(LocalizedFormats.OUT_OF_BOUND_SIGNIFICANCE_LEVEL,
+                                          alpha, 0, 0.5);
+        }
+        return chiSquareTest(expected, observed) < alpha;
+
+    }
 
     /**
      *  Computes the Chi-Square statistic associated with a
@@ -131,7 +224,7 @@ public interface ChiSquareTest {
      * <code>count[0], ... , count[count.length - 1] </code></p>
      * <p>
      * <strong>Preconditions</strong>: <ul>
-     * <li>All counts must be >= 0.
+     * <li>All counts must be &ge; 0.
      * </li>
      * <li>The count array must be rectangular (i.e. all count[i] subarrays
      *  must have the same length).
@@ -144,11 +237,44 @@ public interface ChiSquareTest {
      * <code>IllegalArgumentException</code> is thrown.</p>
      *
      * @param counts array representation of 2-way table
-     * @return chiSquare statistic
-     * @throws IllegalArgumentException if preconditions are not met
+     * @return chiSquare test statistic
+     * @throws NullArgumentException if the array is null
+     * @throws DimensionMismatchException if the array is not rectangular
+     * @throws NotPositiveException if one entry is not positive
      */
-    double chiSquare(long[][] counts)
-    throws IllegalArgumentException;
+    public double chiSquare(final long[][] counts)
+        throws NullArgumentException, NotPositiveException,
+        DimensionMismatchException {
+
+        checkArray(counts);
+        int nRows = counts.length;
+        int nCols = counts[0].length;
+
+        // compute row, column and total sums
+        double[] rowSum = new double[nRows];
+        double[] colSum = new double[nCols];
+        double total = 0.0d;
+        for (int row = 0; row < nRows; row++) {
+            for (int col = 0; col < nCols; col++) {
+                rowSum[row] += counts[row][col];
+                colSum[col] += counts[row][col];
+                total += counts[row][col];
+            }
+        }
+
+        // compute expected counts and chi-square
+        double sumSq = 0.0d;
+        double expected = 0.0d;
+        for (int row = 0; row < nRows; row++) {
+            for (int col = 0; col < nCols; col++) {
+                expected = (rowSum[row] * colSum[col]) / total;
+                sumSq += ((counts[row][col] - expected) *
+                        (counts[row][col] - expected)) / expected;
+            }
+        }
+        return sumSq;
+
+    }
 
     /**
      * Returns the <i>observed significance level</i>, or <a href=
@@ -162,12 +288,13 @@ public interface ChiSquareTest {
      * <code>count[0], ... , count[count.length - 1] </code></p>
      * <p>
      * <strong>Preconditions</strong>: <ul>
-     * <li>All counts must be >= 0.
+     * <li>All counts must be &ge; 0.
      * </li>
-     * <li>The count array must be rectangular (i.e. all count[i] subarrays must have the same length).
+     * <li>The count array must be rectangular (i.e. all count[i] subarrays must have
+     *     the same length).
      * </li>
-     * <li>The 2-way table represented by <code>counts</code> must have at least 2 columns and
-     *        at least 2 rows.
+     * <li>The 2-way table represented by <code>counts</code> must have at least 2
+     *     columns and at least 2 rows.
      * </li>
      * </li></ul></p><p>
      * If any of the preconditions are not met, an
@@ -175,18 +302,30 @@ public interface ChiSquareTest {
      *
      * @param counts array representation of 2-way table
      * @return p-value
-     * @throws IllegalArgumentException if preconditions are not met
-     * @throws MathException if an error occurs computing the p-value
+     * @throws NullArgumentException if the array is null
+     * @throws DimensionMismatchException if the array is not rectangular
+     * @throws NotPositiveException if one entry is not positive
+     * @throws MaxCountExceededException if an error occurs computing the p-value
      */
-    double chiSquareTest(long[][] counts)
-    throws IllegalArgumentException, MathException;
+    public double chiSquareTest(final long[][] counts)
+        throws NullArgumentException, DimensionMismatchException,
+        NotPositiveException, MaxCountExceededException {
+
+        checkArray(counts);
+        double df = ((double) counts.length -1) * ((double) counts[0].length - 1);
+        ChiSquaredDistribution distribution;
+        distribution = new ChiSquaredDistribution(df);
+        return 1 - distribution.cumulativeProbability(chiSquare(counts));
+
+    }
 
     /**
      * Performs a <a href="http://www.itl.nist.gov/div898/handbook/prc/section4/prc45.htm">
-     * chi-square test of independence</a> evaluating the null hypothesis that the classifications
-     * represented by the counts in the columns of the input 2-way table are independent of the rows,
-     * with significance level <code>alpha</code>.  Returns true iff the null hypothesis can be rejected
-     * with 100 * (1 - alpha) percent confidence.
+     * chi-square test of independence</a> evaluating the null hypothesis that the
+     * classifications represented by the counts in the columns of the input 2-way table
+     * are independent of the rows, with significance level <code>alpha</code>.
+     * Returns true iff the null hypothesis can be rejected with 100 * (1 - alpha) percent
+     * confidence.
      * <p>
      * The rows of the 2-way table are
      * <code>count[0], ... , count[count.length - 1] </code></p>
@@ -194,17 +333,16 @@ public interface ChiSquareTest {
      * <strong>Example:</strong><br>
      * To test the null hypothesis that the counts in
      * <code>count[0], ... , count[count.length - 1] </code>
-     *  all correspond to the same underlying probability distribution at the 99% level, use </p><p>
-     * <code>chiSquareTest(counts, 0.01) </code></p>
+     *  all correspond to the same underlying probability distribution at the 99% level, use</p>
+     * <p><code>chiSquareTest(counts, 0.01)</code></p>
      * <p>
      * <strong>Preconditions</strong>: <ul>
-     * <li>All counts must be >= 0.
-     * </li>
-     * <li>The count array must be rectangular (i.e. all count[i] subarrays must have the same length).
+     * <li>All counts must be &ge; 0.
      * </li>
+     * <li>The count array must be rectangular (i.e. all count[i] subarrays must have the
+     *     same length).</li>
      * <li>The 2-way table represented by <code>counts</code> must have at least 2 columns and
-     *        at least 2 rows.
-     * </li>
+     *     at least 2 rows.</li>
      * </li></ul></p><p>
      * If any of the preconditions are not met, an
      * <code>IllegalArgumentException</code> is thrown.</p>
@@ -213,10 +351,328 @@ public interface ChiSquareTest {
      * @param alpha significance level of the test
      * @return true iff null hypothesis can be rejected with confidence
      * 1 - alpha
-     * @throws IllegalArgumentException if preconditions are not met
-     * @throws MathException if an error occurs performing the test
+     * @throws NullArgumentException if the array is null
+     * @throws DimensionMismatchException if the array is not rectangular
+     * @throws NotPositiveException if one entry is not positive
+     * @throws OutOfRangeException if <code>alpha</code> is not in the range (0, 0.5]
+     * @throws MaxCountExceededException if an error occurs computing the p-value
+     */
+    public boolean chiSquareTest(final long[][] counts, final double alpha)
+        throws NullArgumentException, DimensionMismatchException,
+        NotPositiveException, OutOfRangeException, MaxCountExceededException {
+
+        if ((alpha <= 0) || (alpha > 0.5)) {
+            throw new OutOfRangeException(LocalizedFormats.OUT_OF_BOUND_SIGNIFICANCE_LEVEL,
+                                          alpha, 0, 0.5);
+        }
+        return chiSquareTest(counts) < alpha;
+
+    }
+
+    /**
+     * <p>Computes a
+     * <a href="http://www.itl.nist.gov/div898/software/dataplot/refman1/auxillar/chi2samp.htm">
+     * Chi-Square two sample test statistic</a> comparing bin frequency counts
+     * in <code>observed1</code> and <code>observed2</code>.  The
+     * sums of frequency counts in the two samples are not required to be the
+     * same.  The formula used to compute the test statistic is</p>
+     * <code>
+     * &sum;[(K * observed1[i] - observed2[i]/K)<sup>2</sup> / (observed1[i] + observed2[i])]
+     * </code> where
+     * <br/><code>K = &sqrt;[&sum(observed2 / &sum;(observed1)]</code>
+     * </p>
+     * <p>This statistic can be used to perform a Chi-Square test evaluating the
+     * null hypothesis that both observed counts follow the same distribution.</p>
+     * <p>
+     * <strong>Preconditions</strong>: <ul>
+     * <li>Observed counts must be non-negative.
+     * </li>
+     * <li>Observed counts for a specific bin must not both be zero.
+     * </li>
+     * <li>Observed counts for a specific sample must not all be 0.
+     * </li>
+     * <li>The arrays <code>observed1</code> and <code>observed2</code> must have
+     * the same length and their common length must be at least 2.
+     * </li></ul></p><p>
+     * If any of the preconditions are not met, an
+     * <code>IllegalArgumentException</code> is thrown.</p>
+     *
+     * @param observed1 array of observed frequency counts of the first data set
+     * @param observed2 array of observed frequency counts of the second data set
+     * @return chiSquare test statistic
+     * @throws DimensionMismatchException the the length of the arrays does not match
+     * @throws NotPositiveException if one entry in <code>observed1</code> or
+     * <code>observed2</code> is not positive
+     * @throws ZeroException if either all counts of <code>observed1</code> or
+     * <code>observed2</code> are zero, or if the count at the same index is zero
+     * for both arrays
+     * @since 1.2
+     */
+    public double chiSquareDataSetsComparison(long[] observed1, long[] observed2)
+        throws DimensionMismatchException, NotPositiveException, ZeroException {
+
+        // Make sure lengths are same
+        if (observed1.length < 2) {
+            throw new DimensionMismatchException(observed1.length, 2);
+        }
+        if (observed1.length != observed2.length) {
+            throw new DimensionMismatchException(observed1.length, observed2.length);
+        }
+
+        // Ensure non-negative counts
+        checkNonNegative(observed1);
+        checkNonNegative(observed2);
+
+        // Compute and compare count sums
+        long countSum1 = 0;
+        long countSum2 = 0;
+        boolean unequalCounts = false;
+        double weight = 0.0;
+        for (int i = 0; i < observed1.length; i++) {
+            countSum1 += observed1[i];
+            countSum2 += observed2[i];
+        }
+        // Ensure neither sample is uniformly 0
+        if (countSum1 == 0 || countSum2 == 0) {
+            throw new ZeroException();
+        }
+        // Compare and compute weight only if different
+        unequalCounts = countSum1 != countSum2;
+        if (unequalCounts) {
+            weight = FastMath.sqrt((double) countSum1 / (double) countSum2);
+        }
+        // Compute ChiSquare statistic
+        double sumSq = 0.0d;
+        double dev = 0.0d;
+        double obs1 = 0.0d;
+        double obs2 = 0.0d;
+        for (int i = 0; i < observed1.length; i++) {
+            if (observed1[i] == 0 && observed2[i] == 0) {
+                throw new ZeroException(LocalizedFormats.OBSERVED_COUNTS_BOTTH_ZERO_FOR_ENTRY, i);
+            } else {
+                obs1 = observed1[i];
+                obs2 = observed2[i];
+                if (unequalCounts) { // apply weights
+                    dev = obs1/weight - obs2 * weight;
+                } else {
+                    dev = obs1 - obs2;
+                }
+                sumSq += (dev * dev) / (obs1 + obs2);
+            }
+        }
+        return sumSq;
+    }
+
+    /**
+     * <p>Returns the <i>observed significance level</i>, or <a href=
+     * "http://www.cas.lancs.ac.uk/glossary_v1.1/hyptest.html#pvalue">
+     * p-value</a>, associated with a Chi-Square two sample test comparing
+     * bin frequency counts in <code>observed1</code> and
+     * <code>observed2</code>.
+     * </p>
+     * <p>The number returned is the smallest significance level at which one
+     * can reject the null hypothesis that the observed counts conform to the
+     * same distribution.
+     * </p>
+     * <p>See {@link #chiSquareDataSetsComparison(long[], long[])} for details
+     * on the formula used to compute the test statistic. The degrees of
+     * of freedom used to perform the test is one less than the common length
+     * of the input observed count arrays.
+     * </p>
+     * <strong>Preconditions</strong>: <ul>
+     * <li>Observed counts must be non-negative.
+     * </li>
+     * <li>Observed counts for a specific bin must not both be zero.
+     * </li>
+     * <li>Observed counts for a specific sample must not all be 0.
+     * </li>
+     * <li>The arrays <code>observed1</code> and <code>observed2</code> must
+     * have the same length and
+     * their common length must be at least 2.
+     * </li></ul><p>
+     * If any of the preconditions are not met, an
+     * <code>IllegalArgumentException</code> is thrown.</p>
+     *
+     * @param observed1 array of observed frequency counts of the first data set
+     * @param observed2 array of observed frequency counts of the second data set
+     * @return p-value
+     * @throws DimensionMismatchException the the length of the arrays does not match
+     * @throws NotPositiveException if one entry in <code>observed1</code> or
+     * <code>observed2</code> is not positive
+     * @throws ZeroException if either all counts of <code>observed1</code> or
+     * <code>observed2</code> are zero, or if the count at the same index is zero
+     * for both arrays
+     * @throws MaxCountExceededException if an error occurs computing the p-value
+     * @since 1.2
      */
-    boolean chiSquareTest(long[][] counts, double alpha)
-    throws IllegalArgumentException, MathException;
+    public double chiSquareTestDataSetsComparison(long[] observed1, long[] observed2)
+        throws DimensionMismatchException, NotPositiveException, ZeroException,
+        MaxCountExceededException {
+
+        ChiSquaredDistribution distribution;
+        distribution = new ChiSquaredDistribution((double) observed1.length - 1);
+        return 1 - distribution.cumulativeProbability(
+                chiSquareDataSetsComparison(observed1, observed2));
+
+    }
+
+    /**
+     * <p>Performs a Chi-Square two sample test comparing two binned data
+     * sets. The test evaluates the null hypothesis that the two lists of
+     * observed counts conform to the same frequency distribution, with
+     * significance level <code>alpha</code>.  Returns true iff the null
+     * hypothesis can be rejected with 100 * (1 - alpha) percent confidence.
+     * </p>
+     * <p>See {@link #chiSquareDataSetsComparison(long[], long[])} for
+     * details on the formula used to compute the Chisquare statistic used
+     * in the test. The degrees of of freedom used to perform the test is
+     * one less than the common length of the input observed count arrays.
+     * </p>
+     * <strong>Preconditions</strong>: <ul>
+     * <li>Observed counts must be non-negative.
+     * </li>
+     * <li>Observed counts for a specific bin must not both be zero.
+     * </li>
+     * <li>Observed counts for a specific sample must not all be 0.
+     * </li>
+     * <li>The arrays <code>observed1</code> and <code>observed2</code> must
+     * have the same length and their common length must be at least 2.
+     * </li>
+     * <li> <code> 0 < alpha < 0.5 </code>
+     * </li></ul><p>
+     * If any of the preconditions are not met, an
+     * <code>IllegalArgumentException</code> is thrown.</p>
+     *
+     * @param observed1 array of observed frequency counts of the first data set
+     * @param observed2 array of observed frequency counts of the second data set
+     * @param alpha significance level of the test
+     * @return true iff null hypothesis can be rejected with confidence
+     * 1 - alpha
+     * @throws DimensionMismatchException the the length of the arrays does not match
+     * @throws NotPositiveException if one entry in <code>observed1</code> or
+     * <code>observed2</code> is not positive
+     * @throws ZeroException if either all counts of <code>observed1</code> or
+     * <code>observed2</code> are zero, or if the count at the same index is zero
+     * for both arrays
+     * @throws OutOfRangeException if <code>alpha</code> is not in the range (0, 0.5]
+     * @throws MaxCountExceededException if an error occurs performing the test
+     * @since 1.2
+     */
+    public boolean chiSquareTestDataSetsComparison(final long[] observed1,
+                                                   final long[] observed2,
+                                                   final double alpha)
+        throws DimensionMismatchException, NotPositiveException,
+        ZeroException, OutOfRangeException, MaxCountExceededException {
+
+        if (alpha <= 0 ||
+            alpha > 0.5) {
+            throw new OutOfRangeException(LocalizedFormats.OUT_OF_BOUND_SIGNIFICANCE_LEVEL,
+                                          alpha, 0, 0.5);
+        }
+        return chiSquareTestDataSetsComparison(observed1, observed2) < alpha;
+
+    }
+
+    /**
+     * Checks to make sure that the input long[][] array is rectangular,
+     * has at least 2 rows and 2 columns, and has all non-negative entries.
+     *
+     * @param in input 2-way table to check
+     * @throws NullArgumentException if the array is null
+     * @throws DimensionMismatchException if the array is not valid
+     * @throws NotPositiveException if one entry is not positive
+     */
+    private void checkArray(final long[][] in)
+        throws NullArgumentException, DimensionMismatchException,
+        NotPositiveException {
+
+        if (in.length < 2) {
+            throw new DimensionMismatchException(in.length, 2);
+        }
+
+        if (in[0].length < 2) {
+            throw new DimensionMismatchException(in[0].length, 2);
+        }
+
+        checkRectangular(in);
+        checkNonNegative(in);
+
+    }
+
+    //---------------------  Private array methods -- should find a utility home for these
+
+    /**
+     * Throws DimensionMismatchException if the input array is not rectangular.
+     *
+     * @param in array to be tested
+     * @throws NullArgumentException if input array is null
+     * @throws DimensionMismatchException if input array is not rectangular
+     */
+    private void checkRectangular(final long[][] in)
+        throws NullArgumentException, DimensionMismatchException {
+
+        MathUtils.checkNotNull(in);
+        for (int i = 1; i < in.length; i++) {
+            if (in[i].length != in[0].length) {
+                throw new DimensionMismatchException(
+                        LocalizedFormats.DIFFERENT_ROWS_LENGTHS,
+                        in[i].length, in[0].length);
+            }
+        }
+
+    }
+
+    /**
+     * Check all entries of the input array are strictly positive.
+     *
+     * @param in Array to be tested.
+     * @throws NotStrictlyPositiveException if one entry is not strictly positive.
+     */
+    private void checkPositive(final double[] in)
+        throws NotStrictlyPositiveException {
+
+        for (int i = 0; i < in.length; i++) {
+            if (in[i] <= 0) {
+                throw new NotStrictlyPositiveException(in[i]);
+            }
+        }
+
+    }
+
+    /**
+     * Check all entries of the input array are >= 0.
+     *
+     * @param in Array to be tested.
+     * @throws NotPositiveException if one entry is negative.
+     */
+    private void checkNonNegative(final long[] in)
+        throws NotPositiveException {
+
+        for (int i = 0; i < in.length; i++) {
+            if (in[i] < 0) {
+                throw new NotPositiveException(in[i]);
+            }
+        }
+
+    }
+
+    /**
+     * Check all entries of the input array are >= 0.
+     *
+     * @param in Array to be tested.
+     * @throws NotPositiveException if one entry is negative.
+     */
+    private void checkNonNegative(final long[][] in)
+        throws NotPositiveException {
+
+        for (int i = 0; i < in.length; i ++) {
+            for (int j = 0; j < in[i].length; j++) {
+                if (in[i][j] < 0) {
+                    throw new NotPositiveException(in[i][j]);
+                }
+            }
+        }
+
+    }
 
 }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/TestUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/TestUtils.java?rev=1243286&r1=1243285&r2=1243286&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/TestUtils.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/TestUtils.java Sun Feb 12 18:07:53 2012
@@ -21,8 +21,11 @@ import org.apache.commons.math.MathExcep
 import org.apache.commons.math.exception.ConvergenceException;
 import org.apache.commons.math.exception.DimensionMismatchException;
 import org.apache.commons.math.exception.MaxCountExceededException;
+import org.apache.commons.math.exception.NotPositiveException;
+import org.apache.commons.math.exception.NotStrictlyPositiveException;
 import org.apache.commons.math.exception.NullArgumentException;
 import org.apache.commons.math.exception.OutOfRangeException;
+import org.apache.commons.math.exception.ZeroException;
 import org.apache.commons.math.stat.descriptive.StatisticalSummary;
 
 /**
@@ -38,22 +41,11 @@ public class TestUtils  {
     private static final TTest T_TEST = new TTestImpl();
 
     /** Singleton ChiSquareTest instance. */
-    private static final ChiSquareTest CHI_SQUARE_TEST = new ChiSquareTestImpl();
-
-    /** Singleton ChiSquareTest instance. */
-    private static final UnknownDistributionChiSquareTest UNKNOWN_DISTRIBUTION_CHI_SQUARE_TEST =
-        new ChiSquareTestImpl();
+    private static final ChiSquareTest CHI_SQUARE_TEST = new ChiSquareTest();
 
     /** Singleton OneWayAnova instance. */
     private static final OneWayAnova ONE_WAY_ANANOVA = new OneWayAnova();
 
-    /** Singleton MannWhitneyUTest instance using default ranking. */
-    private static final MannWhitneyUTest MANN_WHITNEY_U_TEST = new MannWhitneyUTest();
-
-    /** Singleton WilcoxonSignedRankTest instance. */
-    private static final WilcoxonSignedRankTest WILCOXON_SIGNED_RANK_TEST =
-        new WilcoxonSignedRankTest();
-
     /**
      * Prevent instantiation.
      */
@@ -234,49 +226,55 @@ public class TestUtils  {
     /**
      * @see org.apache.commons.math.stat.inference.ChiSquareTest#chiSquare(double[], long[])
      */
-    public static double chiSquare(double[] expected, long[] observed)
-        throws IllegalArgumentException {
+    public static double chiSquare(final double[] expected, final long[] observed)
+        throws NotPositiveException, NotStrictlyPositiveException,
+        DimensionMismatchException {
         return CHI_SQUARE_TEST.chiSquare(expected, observed);
     }
 
     /**
      * @see org.apache.commons.math.stat.inference.ChiSquareTest#chiSquare(long[][])
      */
-    public static double chiSquare(long[][] counts)
-        throws IllegalArgumentException {
+    public static double chiSquare(final long[][] counts)
+        throws NullArgumentException, NotPositiveException,
+        DimensionMismatchException {
         return CHI_SQUARE_TEST.chiSquare(counts);
     }
 
     /**
      * @see org.apache.commons.math.stat.inference.ChiSquareTest#chiSquareTest(double[], long[], double)
      */
-    public static boolean chiSquareTest(double[] expected, long[] observed,
-        double alpha)
-        throws IllegalArgumentException, MathException {
+    public static boolean chiSquareTest(final double[] expected, final long[] observed,
+                                        final double alpha)
+        throws NotPositiveException, NotStrictlyPositiveException,
+        DimensionMismatchException, OutOfRangeException, MaxCountExceededException {
         return CHI_SQUARE_TEST.chiSquareTest(expected, observed, alpha);
     }
 
     /**
      * @see org.apache.commons.math.stat.inference.ChiSquareTest#chiSquareTest(double[], long[])
      */
-    public static double chiSquareTest(double[] expected, long[] observed)
-        throws IllegalArgumentException, MathException {
+    public static double chiSquareTest(final double[] expected, final long[] observed)
+        throws NotPositiveException, NotStrictlyPositiveException,
+        DimensionMismatchException, MaxCountExceededException {
         return CHI_SQUARE_TEST.chiSquareTest(expected, observed);
     }
 
     /**
      * @see org.apache.commons.math.stat.inference.ChiSquareTest#chiSquareTest(long[][], double)
      */
-    public static boolean chiSquareTest(long[][] counts, double alpha)
-        throws IllegalArgumentException, MathException {
+    public static boolean chiSquareTest(final long[][] counts, final double alpha)
+        throws NullArgumentException, DimensionMismatchException,
+        NotPositiveException, OutOfRangeException, MaxCountExceededException {
         return CHI_SQUARE_TEST.chiSquareTest(counts, alpha);
     }
 
     /**
      * @see org.apache.commons.math.stat.inference.ChiSquareTest#chiSquareTest(long[][])
      */
-    public static double chiSquareTest(long[][] counts)
-        throws IllegalArgumentException, MathException {
+    public static double chiSquareTest(final long[][] counts)
+        throws NullArgumentException, DimensionMismatchException,
+        NotPositiveException, MaxCountExceededException {
         return CHI_SQUARE_TEST.chiSquareTest(counts);
     }
 
@@ -285,9 +283,10 @@ public class TestUtils  {
      *
      * @since 1.2
      */
-    public static double chiSquareDataSetsComparison(long[] observed1, long[] observed2)
-        throws IllegalArgumentException {
-        return UNKNOWN_DISTRIBUTION_CHI_SQUARE_TEST.chiSquareDataSetsComparison(observed1, observed2);
+    public static double chiSquareDataSetsComparison(final long[] observed1,
+                                                     final long[] observed2)
+        throws DimensionMismatchException, NotPositiveException, ZeroException {
+        return CHI_SQUARE_TEST.chiSquareDataSetsComparison(observed1, observed2);
     }
 
     /**
@@ -295,21 +294,24 @@ public class TestUtils  {
      *
      * @since 1.2
      */
-    public static double chiSquareTestDataSetsComparison(long[] observed1, long[] observed2)
-        throws IllegalArgumentException, MathException {
-        return UNKNOWN_DISTRIBUTION_CHI_SQUARE_TEST.chiSquareTestDataSetsComparison(observed1, observed2);
+    public static double chiSquareTestDataSetsComparison(final long[] observed1,
+                                                         final long[] observed2)
+        throws DimensionMismatchException, NotPositiveException, ZeroException,
+        MaxCountExceededException {
+        return CHI_SQUARE_TEST.chiSquareTestDataSetsComparison(observed1, observed2);
     }
 
-
     /**
      * @see org.apache.commons.math.stat.inference.UnknownDistributionChiSquareTest#chiSquareTestDataSetsComparison(long[], long[], double)
      *
      * @since 1.2
      */
-    public static boolean chiSquareTestDataSetsComparison(long[] observed1, long[] observed2,
-        double alpha)
-        throws IllegalArgumentException, MathException {
-        return UNKNOWN_DISTRIBUTION_CHI_SQUARE_TEST.chiSquareTestDataSetsComparison(observed1, observed2, alpha);
+    public static boolean chiSquareTestDataSetsComparison(final long[] observed1,
+                                                          final long[] observed2,
+                                                          final double alpha)
+        throws DimensionMismatchException, NotPositiveException,
+        ZeroException, OutOfRangeException, MaxCountExceededException {
+        return CHI_SQUARE_TEST.chiSquareTestDataSetsComparison(observed1, observed2, alpha);
     }
 
     /**

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java?rev=1243286&r1=1243285&r2=1243286&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java Sun Feb 12 18:07:53 2012
@@ -31,7 +31,6 @@ import org.apache.commons.math.distribut
 import org.apache.commons.math.linear.FieldMatrix;
 import org.apache.commons.math.linear.RealMatrix;
 import org.apache.commons.math.stat.inference.ChiSquareTest;
-import org.apache.commons.math.stat.inference.ChiSquareTestImpl;
 import org.apache.commons.math.util.FastMath;
 import org.apache.commons.math.util.Precision;
 import org.junit.Assert;
@@ -357,7 +356,7 @@ public class TestUtils {
      * @param alpha significance level of the test
      */
     public static void assertChiSquareAccept(String[] valueLabels, double[] expected, long[] observed, double alpha) throws Exception {
-        ChiSquareTest chiSquareTest = new ChiSquareTestImpl();
+        ChiSquareTest chiSquareTest = new ChiSquareTest();
 
         // Fail if we can reject null hypothesis that distributions are the same
         if (chiSquareTest.chiSquareTest(expected, observed, alpha)) {

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java?rev=1243286&r1=1243285&r2=1243286&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java Sun Feb 12 18:07:53 2012
@@ -45,7 +45,6 @@ import org.apache.commons.math.distribut
 import org.apache.commons.math.stat.Frequency;
 import org.apache.commons.math.stat.descriptive.SummaryStatistics;
 import org.apache.commons.math.stat.inference.ChiSquareTest;
-import org.apache.commons.math.stat.inference.ChiSquareTestImpl;
 import org.apache.commons.math.util.FastMath;
 import org.apache.commons.math.exception.MathIllegalArgumentException;
 import org.junit.Assert;
@@ -73,7 +72,7 @@ public class RandomDataTest {
     private final String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
             "a", "b", "c", "d", "e", "f" };
     protected RandomDataImpl randomData = null;
-    protected final ChiSquareTestImpl testStatistic = new ChiSquareTestImpl();
+    protected final ChiSquareTest testStatistic = new ChiSquareTest();
 
     @Test
     public void testNextIntExtremeValues() {
@@ -473,7 +472,7 @@ public class RandomDataTest {
         }
 
         // Use chisquare test to verify that generated values are poisson(mean)-distributed
-        ChiSquareTest chiSquareTest = new ChiSquareTestImpl();
+        ChiSquareTest chiSquareTest = new ChiSquareTest();
             // Fail if we can reject null hypothesis that distributions are the same
         if (chiSquareTest.chiSquareTest(expected, observed, alpha)) {
             StringBuilder msgBuffer = new StringBuilder();

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/ChiSquareTestTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/ChiSquareTestTest.java?rev=1243286&r1=1243285&r2=1243286&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/ChiSquareTestTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/ChiSquareTestTest.java Sun Feb 12 18:07:53 2012
@@ -16,7 +16,11 @@
  */
 package org.apache.commons.math.stat.inference;
 
-import org.apache.commons.math.exception.MathIllegalArgumentException;
+import org.apache.commons.math.exception.DimensionMismatchException;
+import org.apache.commons.math.exception.NotPositiveException;
+import org.apache.commons.math.exception.NotStrictlyPositiveException;
+import org.apache.commons.math.exception.OutOfRangeException;
+import org.apache.commons.math.exception.ZeroException;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -29,7 +33,7 @@ import org.junit.Test;
 
 public class ChiSquareTestTest {
 
-    protected UnknownDistributionChiSquareTest testStatistic = new ChiSquareTestImpl();
+    protected ChiSquareTest testStatistic = new ChiSquareTest();
 
     @Test
     public void testChiSquare() throws Exception {
@@ -53,8 +57,8 @@ public class ChiSquareTestTest {
 
         try {
             testStatistic.chiSquareTest(expected1, observed1, 95);
-            Assert.fail("alpha out of range, MathIllegalArgumentException expected");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("alpha out of range, OutOfRangeException expected");
+        } catch (OutOfRangeException ex) {
             // expected
         }
 
@@ -62,8 +66,8 @@ public class ChiSquareTestTest {
         double[] tooShortEx = { 1 };
         try {
             testStatistic.chiSquare(tooShortEx, tooShortObs);
-            Assert.fail("arguments too short, MathIllegalArgumentException expected");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("arguments too short, DimensionMismatchException expected");
+        } catch (DimensionMismatchException ex) {
             // expected
         }
 
@@ -72,8 +76,8 @@ public class ChiSquareTestTest {
         double[] unMatchedEx = { 1, 1, 2 };
         try {
             testStatistic.chiSquare(unMatchedEx, unMatchedObs);
-            Assert.fail("arrays have different lengths, MathIllegalArgumentException expected");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("arrays have different lengths, DimensionMismatchException expected");
+        } catch (DimensionMismatchException ex) {
             // expected
         }
 
@@ -81,8 +85,8 @@ public class ChiSquareTestTest {
         expected[0] = 0;
         try {
             testStatistic.chiSquareTest(expected, observed, .01);
-            Assert.fail("bad expected count, MathIllegalArgumentException expected");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("bad expected count, NotStrictlyPositiveException expected");
+        } catch (NotStrictlyPositiveException ex) {
             // expected
         }
 
@@ -91,8 +95,8 @@ public class ChiSquareTestTest {
         observed[0] = -1;
         try {
             testStatistic.chiSquareTest(expected, observed, .01);
-            Assert.fail("bad expected count, MathIllegalArgumentException expected");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("bad expected count, NotPositiveException expected");
+        } catch (NotPositiveException ex) {
             // expected
         }
 
@@ -118,8 +122,8 @@ public class ChiSquareTestTest {
         long[][] counts3 = { {40, 22, 43}, {91, 21, 28}, {60, 10}};
         try {
             testStatistic.chiSquare(counts3);
-            Assert.fail("Expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("Expecting DimensionMismatchException");
+        } catch (DimensionMismatchException ex) {
             // expected
         }
 
@@ -127,15 +131,15 @@ public class ChiSquareTestTest {
         long[][] counts4 = {{40, 22, 43}};
         try {
             testStatistic.chiSquare(counts4);
-            Assert.fail("Expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("Expecting DimensionMismatchException");
+        } catch (DimensionMismatchException ex) {
             // expected
         }
         long[][] counts5 = {{40}, {40}, {30}, {10}};
         try {
             testStatistic.chiSquare(counts5);
-            Assert.fail("Expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("Expecting DimensionMismatchException");
+        } catch (DimensionMismatchException ex) {
             // expected
         }
 
@@ -143,16 +147,16 @@ public class ChiSquareTestTest {
         long[][] counts6 = {{10, -2}, {30, 40}, {60, 90} };
         try {
             testStatistic.chiSquare(counts6);
-            Assert.fail("Expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("Expecting NotPositiveException");
+        } catch (NotPositiveException ex) {
             // expected
         }
 
         // bad alpha
         try {
             testStatistic.chiSquareTest(counts, 0);
-            Assert.fail("Expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("Expecting OutOfRangeException");
+        } catch (OutOfRangeException ex) {
             // expected
         }
     }
@@ -167,8 +171,8 @@ public class ChiSquareTestTest {
         long[] obs = new long[] {
             2372383, 584222, 257170, 17750155, 7903832, 489265, 209628, 393899
         };
-        org.apache.commons.math.stat.inference.ChiSquareTestImpl csti =
-            new org.apache.commons.math.stat.inference.ChiSquareTestImpl();
+        org.apache.commons.math.stat.inference.ChiSquareTest csti =
+            new org.apache.commons.math.stat.inference.ChiSquareTest();
         double cst = csti.chiSquareTest(exp, obs);
         Assert.assertEquals("chi-square p-value", 0.0, cst, 1E-3);
         Assert.assertEquals( "chi-square test statistic",
@@ -189,7 +193,7 @@ public class ChiSquareTestTest {
     /** Target values verified using DATAPLOT version 2006.3 */
     @Test
     public void testChiSquareDataSetsComparisonEqualCounts()
-    throws Exception {
+        throws Exception {
         long[] observed1 = {10, 12, 12, 10};
         long[] observed2 = {5, 15, 14, 10};
         Assert.assertEquals("chi-square p value", 0.541096,
@@ -206,7 +210,7 @@ public class ChiSquareTestTest {
     /** Target values verified using DATAPLOT version 2006.3 */
     @Test
     public void testChiSquareDataSetsComparisonUnEqualCounts()
-    throws Exception {
+        throws Exception {
         long[] observed1 = {10, 12, 12, 10, 15};
         long[] observed2 = {15, 10, 10, 15, 5};
         Assert.assertEquals("chi-square p value", 0.124115,
@@ -225,14 +229,14 @@ public class ChiSquareTestTest {
 
     @Test
     public void testChiSquareDataSetsComparisonBadCounts()
-    throws Exception {
+        throws Exception {
         long[] observed1 = {10, -1, 12, 10, 15};
         long[] observed2 = {15, 10, 10, 15, 5};
         try {
             testStatistic.chiSquareTestDataSetsComparison(
                     observed1, observed2);
-            Assert.fail("Expecting MathIllegalArgumentException - negative count");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("Expecting NotPositiveException - negative count");
+        } catch (NotPositiveException ex) {
             // expected
         }
         long[] observed3 = {10, 0, 12, 10, 15};
@@ -240,8 +244,8 @@ public class ChiSquareTestTest {
         try {
             testStatistic.chiSquareTestDataSetsComparison(
                     observed3, observed4);
-            Assert.fail("Expecting MathIllegalArgumentException - double 0's");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("Expecting ZeroException - double 0's");
+        } catch (ZeroException ex) {
             // expected
         }
         long[] observed5 = {10, 10, 12, 10, 15};
@@ -249,8 +253,8 @@ public class ChiSquareTestTest {
         try {
             testStatistic.chiSquareTestDataSetsComparison(
                     observed5, observed6);
-            Assert.fail("Expecting MathIllegalArgumentException - vanishing counts");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("Expecting ZeroException - vanishing counts");
+        } catch (ZeroException ex) {
             // expected
         }
     }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/TestUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/TestUtilsTest.java?rev=1243286&r1=1243285&r2=1243286&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/TestUtilsTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/TestUtilsTest.java Sun Feb 12 18:07:53 2012
@@ -19,7 +19,11 @@ package org.apache.commons.math.stat.inf
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.commons.math.exception.DimensionMismatchException;
 import org.apache.commons.math.exception.MathIllegalArgumentException;
+import org.apache.commons.math.exception.NotPositiveException;
+import org.apache.commons.math.exception.NotStrictlyPositiveException;
+import org.apache.commons.math.exception.OutOfRangeException;
 import org.apache.commons.math.stat.descriptive.SummaryStatistics;
 import org.junit.Assert;
 import org.junit.Test;
@@ -54,8 +58,8 @@ public class TestUtilsTest {
 
         try {
             TestUtils.chiSquareTest(expected1, observed1, 95);
-            Assert.fail("alpha out of range, MathIllegalArgumentException expected");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("alpha out of range, OutOfRangeException expected");
+        } catch (OutOfRangeException ex) {
             // expected
         }
 
@@ -63,8 +67,8 @@ public class TestUtilsTest {
         double[] tooShortEx = { 1 };
         try {
             TestUtils.chiSquare(tooShortEx, tooShortObs);
-            Assert.fail("arguments too short, MathIllegalArgumentException expected");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("arguments too short, DimensionMismatchException expected");
+        } catch (DimensionMismatchException ex) {
             // expected
         }
 
@@ -73,8 +77,8 @@ public class TestUtilsTest {
         double[] unMatchedEx = { 1, 1, 2 };
         try {
             TestUtils.chiSquare(unMatchedEx, unMatchedObs);
-            Assert.fail("arrays have different lengths, MathIllegalArgumentException expected");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("arrays have different lengths, DimensionMismatchException expected");
+        } catch (DimensionMismatchException ex) {
             // expected
         }
 
@@ -82,8 +86,8 @@ public class TestUtilsTest {
         expected[0] = 0;
         try {
             TestUtils.chiSquareTest(expected, observed, .01);
-            Assert.fail("bad expected count, MathIllegalArgumentException expected");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("bad expected count, NotStrictlyPositiveException expected");
+        } catch (NotStrictlyPositiveException ex) {
             // expected
         }
 
@@ -92,8 +96,8 @@ public class TestUtilsTest {
         observed[0] = -1;
         try {
             TestUtils.chiSquareTest(expected, observed, .01);
-            Assert.fail("bad expected count, MathIllegalArgumentException expected");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("bad expected count, NotPositiveException expected");
+        } catch (NotPositiveException ex) {
             // expected
         }
 
@@ -119,8 +123,8 @@ public class TestUtilsTest {
         long[][] counts3 = { {40, 22, 43}, {91, 21, 28}, {60, 10}};
         try {
             TestUtils.chiSquare(counts3);
-            Assert.fail("Expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("Expecting DimensionMismatchException");
+        } catch (DimensionMismatchException ex) {
             // expected
         }
 
@@ -128,15 +132,15 @@ public class TestUtilsTest {
         long[][] counts4 = {{40, 22, 43}};
         try {
             TestUtils.chiSquare(counts4);
-            Assert.fail("Expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("Expecting DimensionMismatchException");
+        } catch (DimensionMismatchException ex) {
             // expected
         }
         long[][] counts5 = {{40}, {40}, {30}, {10}};
         try {
             TestUtils.chiSquare(counts5);
-            Assert.fail("Expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("Expecting DimensionMismatchException");
+        } catch (DimensionMismatchException ex) {
             // expected
         }
 
@@ -144,16 +148,16 @@ public class TestUtilsTest {
         long[][] counts6 = {{10, -2}, {30, 40}, {60, 90} };
         try {
             TestUtils.chiSquare(counts6);
-            Assert.fail("Expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("Expecting NotPositiveException");
+        } catch (NotPositiveException ex) {
             // expected
         }
 
         // bad alpha
         try {
             TestUtils.chiSquareTest(counts, 0);
-            Assert.fail("Expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException ex) {
+            Assert.fail("Expecting OutOfRangeException");
+        } catch (OutOfRangeException ex) {
             // expected
         }
     }
@@ -168,8 +172,8 @@ public class TestUtilsTest {
         long[] obs = new long[] {
                 2372383, 584222, 257170, 17750155, 7903832, 489265, 209628, 393899
         };
-        org.apache.commons.math.stat.inference.ChiSquareTestImpl csti =
-            new org.apache.commons.math.stat.inference.ChiSquareTestImpl();
+        org.apache.commons.math.stat.inference.ChiSquareTest csti =
+            new org.apache.commons.math.stat.inference.ChiSquareTest();
         double cst = csti.chiSquareTest(exp, obs);
         Assert.assertEquals("chi-square p-value", 0.0, cst, 1E-3);
         Assert.assertEquals( "chi-square test statistic",