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/07 23:03:52 UTC
svn commit: r1241657 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math/stat/inference/
test/java/org/apache/commons/math/stat/inference/
Author: tn
Date: Tue Feb 7 22:03:51 2012
New Revision: 1241657
URL: http://svn.apache.org/viewvc?rev=1241657&view=rev
Log:
Exception handling in WilcoxonSignedRankTest made consistent with CM.
JIRA: MATH-488
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/WilcoxonSignedRankTest.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/WilcoxonSignedRankTestImpl.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/OneWayAnovaTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/WilcoxonSignedRankTestTest.java
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/WilcoxonSignedRankTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/WilcoxonSignedRankTest.java?rev=1241657&r1=1241656&r2=1241657&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/WilcoxonSignedRankTest.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/WilcoxonSignedRankTest.java Tue Feb 7 22:03:51 2012
@@ -16,7 +16,12 @@
*/
package org.apache.commons.math.stat.inference;
-import org.apache.commons.math.MathException;
+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.NoDataException;
+import org.apache.commons.math.exception.NullArgumentException;
+import org.apache.commons.math.exception.NumberIsTooLargeException;
/**
* An interface for Wilcoxon signed-rank test.
@@ -52,16 +57,16 @@ public interface WilcoxonSignedRankTest
* </ul>
* </p>
*
- * @param x
- * the first sample
- * @param y
- * the second sample
- * @return wilcoxonSignedRank statistic
- * @throws IllegalArgumentException
- * if preconditions are not met
+ * @param x the first sample
+ * @param y the second sample
+ * @return wilcoxonSignedRank statistic (the larger of W+ and W-)
+ * @throws NullArgumentException if {@code x} or {@code y} are {@code null}.
+ * @throws NoDataException if {@code x} or {@code y} are zero-length.
+ * @throws DimensionMismatchException if {@code x} and {@code y} do not
+ * have the same length.
*/
double wilcoxonSignedRank(final double[] x, final double[] y)
- throws IllegalArgumentException;
+ throws NullArgumentException, NoDataException, DimensionMismatchException;
/**
* Returns the <i>observed significance level</i>, or <a href=
@@ -87,21 +92,23 @@ public interface WilcoxonSignedRankTest
* </ul>
* </p>
*
- * @param x
- * the first sample
- * @param y
- * the second sample
+ * @param x the first sample
+ * @param y the second sample
* @param exactPValue
* if the exact p-value is wanted (only works for x.length <= 30,
* if true and x.length > 30, this is ignored because
* calculations may take too long)
* @return p-value
- * @throws IllegalArgumentException
- * if preconditions are not met
- * @throws MathException
- * if an error occurs computing the p-value
+ * @throws NullArgumentException if {@code x} or {@code y} are {@code null}.
+ * @throws NoDataException if {@code x} or {@code y} are zero-length.
+ * @throws DimensionMismatchException if {@code x} and {@code y} do not
+ * have the same length.
+ * @throws NumberIsTooLargeException if {@code exactPValue} is {@code true} and
+ * {@code x.length} > 30
+ * @throws ConvergenceException if the p-value can not be computed due to a convergence error
+ * @throws MaxCountExceededException if the maximum number of iterations is exceeded
*/
- double wilcoxonSignedRankTest(final double[] x, final double[] y,
- boolean exactPValue) throws IllegalArgumentException,
- MathException;
+ double wilcoxonSignedRankTest(final double[] x, final double[] y, boolean exactPValue)
+ throws NullArgumentException, NoDataException, DimensionMismatchException,
+ NumberIsTooLargeException, ConvergenceException, MaxCountExceededException;
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/WilcoxonSignedRankTestImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/WilcoxonSignedRankTestImpl.java?rev=1241657&r1=1241656&r2=1241657&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/WilcoxonSignedRankTestImpl.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/WilcoxonSignedRankTestImpl.java Tue Feb 7 22:03:51 2012
@@ -16,8 +16,13 @@
*/
package org.apache.commons.math.stat.inference;
-import org.apache.commons.math.MathException;
import org.apache.commons.math.distribution.NormalDistribution;
+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.NoDataException;
+import org.apache.commons.math.exception.NullArgumentException;
+import org.apache.commons.math.exception.NumberIsTooLargeException;
import org.apache.commons.math.stat.ranking.NaNStrategy;
import org.apache.commons.math.stat.ranking.NaturalRanking;
import org.apache.commons.math.stat.ranking.TiesStrategy;
@@ -53,7 +58,7 @@ public class WilcoxonSignedRankTestImpl
* specifies the strategy that should be used for ties
*/
public WilcoxonSignedRankTestImpl(NaNStrategy nanStrategy,
- TiesStrategy tiesStrategy) {
+ TiesStrategy tiesStrategy) {
naturalRanking = new NaturalRanking(nanStrategy, tiesStrategy);
}
@@ -62,27 +67,24 @@ public class WilcoxonSignedRankTestImpl
*
* @param x first sample
* @param y second sample
- * @throws IllegalArgumentException
- * if assumptions are not met
+ * @throws NullArgumentException if {@code x} or {@code y} are {@code null}.
+ * @throws NoDataException if {@code x} or {@code y} are zero-length.
+ * @throws DimensionMismatchException if {@code x} and {@code y} do not
+ * have the same length.
*/
private void ensureDataConformance(final double[] x, final double[] y)
- throws IllegalArgumentException {
- if (x == null) {
- throw new IllegalArgumentException("x must not be null");
- }
-
- if (y == null) {
- throw new IllegalArgumentException("y must not be null");
- }
+ throws NullArgumentException, NoDataException, DimensionMismatchException {
- if (x.length != y.length) {
- throw new IllegalArgumentException(
- "x and y must contain the same number of elements");
+ if (x == null ||
+ y == null) {
+ throw new NullArgumentException();
+ }
+ if (x.length == 0 ||
+ y.length == 0) {
+ throw new NoDataException();
}
-
- if (x.length == 0) {
- throw new IllegalArgumentException(
- "x and y must contain at least one element");
+ if (y.length != x.length) {
+ throw new DimensionMismatchException(y.length, x.length);
}
}
@@ -109,18 +111,18 @@ public class WilcoxonSignedRankTestImpl
*
* @param z sample
* @return |z|
- * @throws IllegalArgumentException
- * if assumptions are not met
+ * @throws NullArgumentException if {@code z} is {@code null}
+ * @throws NoDataException if {@code z} is zero-length.
*/
private double[] calculateAbsoluteDifferences(final double[] z)
- throws IllegalArgumentException {
+ throws NullArgumentException, NoDataException {
+
if (z == null) {
- throw new IllegalArgumentException("z must not be null");
+ throw new NullArgumentException();
}
if (z.length == 0) {
- throw new IllegalArgumentException(
- "z must contain at least one element");
+ throw new NoDataException();
}
final double[] zAbs = new double[z.length];
@@ -132,19 +134,9 @@ public class WilcoxonSignedRankTestImpl
return zAbs;
}
- /**
- * {@inheritDoc}
- *
- * @param x
- * the first sample
- * @param y
- * the second sample
- * @return wilcoxonSignedRank statistic (the larger of W+ and W-)
- * @throws IllegalArgumentException
- * if preconditions are not met
- */
+ /** {@inheritDoc} */
public double wilcoxonSignedRank(final double[] x, final double[] y)
- throws IllegalArgumentException {
+ throws NullArgumentException, NoDataException, DimensionMismatchException {
ensureDataConformance(x, y);
@@ -214,9 +206,8 @@ public class WilcoxonSignedRankTestImpl
* @param Wmin smallest Wilcoxon signed rank value
* @param N number of subjects (corresponding to x.length)
* @return two-sided asymptotic p-value
- * @throws MathException if an error occurs computing the p-value
*/
- private double calculateAsymptoticPValue(final double Wmin, final int N) throws MathException {
+ private double calculateAsymptoticPValue(final double Wmin, final int N) {
final double ES = (double) (N * (N + 1)) / 4.0;
@@ -233,25 +224,11 @@ public class WilcoxonSignedRankTestImpl
return 2*standardNormal.cumulativeProbability(z);
}
- /**
- * {@inheritDoc}
- *
- * @param x
- * the first sample
- * @param y
- * the second sample
- * @param exactPValue
- * if the exact p-value is wanted (only for x.length <= 30)
- * @return p-value
- * @throws IllegalArgumentException
- * if preconditions are not met or exact p-value is wanted for
- * when x.length > 30
- * @throws MathException
- * if an error occurs computing the p-value
- */
+ /** {@inheritDoc} */
public double wilcoxonSignedRankTest(final double[] x, final double[] y,
- boolean exactPValue) throws IllegalArgumentException,
- MathException {
+ boolean exactPValue)
+ throws NullArgumentException, NoDataException, DimensionMismatchException,
+ NumberIsTooLargeException, ConvergenceException, MaxCountExceededException {
ensureDataConformance(x, y);
@@ -259,7 +236,7 @@ public class WilcoxonSignedRankTestImpl
final double Wmax = wilcoxonSignedRank(x, y);
if (exactPValue && N > 30) {
- throw new IllegalArgumentException("Exact test can only be made for N <= 30.");
+ throw new NumberIsTooLargeException(N, 30, true);
}
if (exactPValue) {
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/OneWayAnovaTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/OneWayAnovaTest.java?rev=1241657&r1=1241656&r2=1241657&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/OneWayAnovaTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/OneWayAnovaTest.java Tue Feb 7 22:03:51 2012
@@ -19,6 +19,7 @@ package org.apache.commons.math.stat.inf
import java.util.ArrayList;
import java.util.List;
+import org.apache.commons.math.exception.MathIllegalArgumentException;
import org.junit.Assert;
import org.junit.Test;
@@ -65,8 +66,8 @@ public class OneWayAnovaTest {
emptyContents.add(classC);
try {
testStatistic.anovaFValue(emptyContents);
- Assert.fail("empty array for key classX, IllegalArgumentException expected");
- } catch (IllegalArgumentException ex) {
+ Assert.fail("empty array for key classX, MathIllegalArgumentException expected");
+ } catch (MathIllegalArgumentException ex) {
// expected
}
@@ -74,8 +75,8 @@ public class OneWayAnovaTest {
tooFew.add(classA);
try {
testStatistic.anovaFValue(tooFew);
- Assert.fail("less than two classes, IllegalArgumentException expected");
- } catch (IllegalArgumentException ex) {
+ Assert.fail("less than two classes, MathIllegalArgumentException expected");
+ } catch (MathIllegalArgumentException ex) {
// expected
}
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/WilcoxonSignedRankTestTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/WilcoxonSignedRankTestTest.java?rev=1241657&r1=1241656&r2=1241657&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/WilcoxonSignedRankTestTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/WilcoxonSignedRankTestTest.java Tue Feb 7 22:03:51 2012
@@ -16,6 +16,10 @@
*/
package org.apache.commons.math.stat.inference;
+import org.apache.commons.math.exception.DimensionMismatchException;
+import org.apache.commons.math.exception.NoDataException;
+import org.apache.commons.math.exception.NullArgumentException;
+import org.apache.commons.math.exception.NumberIsTooLargeException;
import org.junit.Assert;
import org.junit.Test;
@@ -78,8 +82,8 @@ public class WilcoxonSignedRankTestTest
try {
testStatistic.wilcoxonSignedRankTest(x2, y2, true);
- Assert.fail("More than 30 samples and exact chosen, IllegalArgumentException expected");
- } catch (IllegalArgumentException ex) {
+ Assert.fail("More than 30 samples and exact chosen, NumberIsTooLargeException expected");
+ } catch (NumberIsTooLargeException ex) {
// expected
}
@@ -87,29 +91,29 @@ public class WilcoxonSignedRankTestTest
*/
try {
testStatistic.wilcoxonSignedRankTest(new double[] { }, new double[] { 1.0 }, true);
- Assert.fail("x does not contain samples (exact), IllegalArgumentException expected");
- } catch (IllegalArgumentException ex) {
+ Assert.fail("x does not contain samples (exact), NoDataException expected");
+ } catch (NoDataException ex) {
// expected
}
try {
testStatistic.wilcoxonSignedRankTest(new double[] { }, new double[] { 1.0 }, false);
- Assert.fail("x does not contain samples (asymptotic), IllegalArgumentException expected");
- } catch (IllegalArgumentException ex) {
+ Assert.fail("x does not contain samples (asymptotic), NoDataException expected");
+ } catch (NoDataException ex) {
// expected
}
try {
testStatistic.wilcoxonSignedRankTest(new double[] { 1.0 }, new double[] { }, true);
- Assert.fail("y does not contain samples (exact), IllegalArgumentException expected");
- } catch (IllegalArgumentException ex) {
+ Assert.fail("y does not contain samples (exact), NoDataException expected");
+ } catch (NoDataException ex) {
// expected
}
try {
testStatistic.wilcoxonSignedRankTest(new double[] { 1.0 }, new double[] { }, false);
- Assert.fail("y does not contain samples (asymptotic), IllegalArgumentException expected");
- } catch (IllegalArgumentException ex) {
+ Assert.fail("y does not contain samples (asymptotic), NoDataException expected");
+ } catch (NoDataException ex) {
// expected
}
@@ -117,15 +121,15 @@ public class WilcoxonSignedRankTestTest
*/
try {
testStatistic.wilcoxonSignedRankTest(new double[] { 1.0, 2.0 }, new double[] { 3.0 }, true);
- Assert.fail("x and y not same size (exact), IllegalArgumentException expected");
- } catch (IllegalArgumentException ex) {
+ Assert.fail("x and y not same size (exact), DimensionMismatchException expected");
+ } catch (DimensionMismatchException ex) {
// expected
}
try {
testStatistic.wilcoxonSignedRankTest(new double[] { 1.0, 2.0 }, new double[] { 3.0 }, false);
- Assert.fail("x and y not same size (asymptotic), IllegalArgumentException expected");
- } catch (IllegalArgumentException ex) {
+ Assert.fail("x and y not same size (asymptotic), DimensionMismatchException expected");
+ } catch (DimensionMismatchException ex) {
// expected
}
@@ -134,15 +138,15 @@ public class WilcoxonSignedRankTestTest
*/
try {
testStatistic.wilcoxonSignedRankTest(null, null, true);
- Assert.fail("x and y is null (exact), IllegalArgumentException expected");
- } catch (IllegalArgumentException ex) {
+ Assert.fail("x and y is null (exact), NullArgumentException expected");
+ } catch (NullArgumentException ex) {
// expected
}
try {
testStatistic.wilcoxonSignedRankTest(null, null, false);
- Assert.fail("x and y is null (asymptotic), IllegalArgumentException expected");
- } catch (IllegalArgumentException ex) {
+ Assert.fail("x and y is null (asymptotic), NullArgumentException expected");
+ } catch (NullArgumentException ex) {
// expected
}
@@ -151,29 +155,29 @@ public class WilcoxonSignedRankTestTest
*/
try {
testStatistic.wilcoxonSignedRankTest(null, new double[] { 1.0 }, true);
- Assert.fail("x is null (exact), IllegalArgumentException expected");
- } catch (IllegalArgumentException ex) {
+ Assert.fail("x is null (exact), NullArgumentException expected");
+ } catch (NullArgumentException ex) {
// expected
}
try {
testStatistic.wilcoxonSignedRankTest(null, new double[] { 1.0 }, false);
- Assert.fail("x is null (asymptotic), IllegalArgumentException expected");
- } catch (IllegalArgumentException ex) {
+ Assert.fail("x is null (asymptotic), NullArgumentException expected");
+ } catch (NullArgumentException ex) {
// expected
}
try {
testStatistic.wilcoxonSignedRankTest(new double[] { 1.0 }, null, true);
- Assert.fail("y is null (exact), IllegalArgumentException expected");
- } catch (IllegalArgumentException ex) {
+ Assert.fail("y is null (exact), NullArgumentException expected");
+ } catch (NullArgumentException ex) {
// expected
}
try {
testStatistic.wilcoxonSignedRankTest(new double[] { 1.0 }, null, false);
- Assert.fail("y is null (asymptotic), IllegalArgumentException expected");
- } catch (IllegalArgumentException ex) {
+ Assert.fail("y is null (asymptotic), NullArgumentException expected");
+ } catch (NullArgumentException ex) {
// expected
}
}