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/08 10:27:08 UTC
svn commit: r1241831 - 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: Wed Feb 8 09:27:07 2012
New Revision: 1241831
URL: http://svn.apache.org/viewvc?rev=1241831&view=rev
Log:
Removed MathException from MannWhitneyUTest, improved javadoc, exceptions.
JIRA: MATH-488
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/MannWhitneyUTest.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/MannWhitneyUTestImpl.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/WilcoxonSignedRankTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/MannWhitneyUTestTest.java
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/MannWhitneyUTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/MannWhitneyUTest.java?rev=1241831&r1=1241830&r2=1241831&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/MannWhitneyUTest.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/MannWhitneyUTest.java Wed Feb 8 09:27:07 2012
@@ -16,7 +16,10 @@
*/
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.MaxCountExceededException;
+import org.apache.commons.math.exception.NoDataException;
+import org.apache.commons.math.exception.NullArgumentException;
/**
* An interface for Mann-Whitney U test (also called Wilcoxon rank-sum test).
@@ -47,16 +50,14 @@ public interface MannWhitneyUTest {
* </ul>
* </p>
*
- * @param x
- * the first sample
- * @param y
- * the second sample
- * @return mannWhitneyU statistic
- * @throws IllegalArgumentException
- * if preconditions are not met
+ * @param x the first sample
+ * @param y the second sample
+ * @return Mann-Whitney U statistic (maximum of U<sup>x</sup> and U<sup>y</sup>)
+ * @throws NullArgumentException if {@code x} or {@code y} are {@code null}.
+ * @throws NoDataException if {@code x} or {@code y} are zero-length.
*/
double mannWhitneyU(final double[] x, final double[] y)
- throws IllegalArgumentException;
+ throws NullArgumentException, NoDataException;
/**
* Returns the asymptotic <i>observed significance level</i>, or <a href=
@@ -77,16 +78,17 @@ public interface MannWhitneyUTest {
* </ul>
* </p>
*
- * @param x
- * the first sample
- * @param y
- * the second sample
+ * @param x the first sample
+ * @param y the second sample
* @return asymptotic 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 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 mannWhitneyUTest(final double[] x, final double[] y)
- throws IllegalArgumentException, MathException;
+ throws NullArgumentException, NoDataException,
+ ConvergenceException, MaxCountExceededException;
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/MannWhitneyUTestImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/MannWhitneyUTestImpl.java?rev=1241831&r1=1241830&r2=1241831&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/MannWhitneyUTestImpl.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/inference/MannWhitneyUTestImpl.java Wed Feb 8 09:27:07 2012
@@ -16,8 +16,11 @@
*/
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.MaxCountExceededException;
+import org.apache.commons.math.exception.NoDataException;
+import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.stat.ranking.NaNStrategy;
import org.apache.commons.math.stat.ranking.NaturalRanking;
import org.apache.commons.math.stat.ranking.TiesStrategy;
@@ -63,27 +66,19 @@ public class MannWhitneyUTestImpl implem
*
* @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.
*/
private void ensureDataConformance(final double[] x, final double[] y)
- throws IllegalArgumentException {
- if (x == null) {
- throw new IllegalArgumentException("x must not be null");
- }
+ throws NullArgumentException, NoDataException {
- if (y == null) {
- throw new IllegalArgumentException("y must not be null");
+ if (x == null ||
+ y == null) {
+ throw new NullArgumentException();
}
-
- if (x.length == 0) {
- throw new IllegalArgumentException(
- "x must contain at least one element");
- }
-
- if (y.length == 0) {
- throw new IllegalArgumentException(
- "y must contain at least one element");
+ if (x.length == 0 ||
+ y.length == 0) {
+ throw new NoDataException();
}
}
@@ -101,16 +96,9 @@ public class MannWhitneyUTestImpl implem
return z;
}
- /**
- * {@inheritDoc}
- *
- * @param x the first sample
- * @param y the second sample
- * @return mannWhitneyU statistic U (maximum of U<sup>x</sup> and U<sup>y</sup>)
- * @throws IllegalArgumentException if preconditions are not met
- */
+ /** {@inheritDoc} */
public double mannWhitneyU(final double[] x, final double[] y)
- throws IllegalArgumentException {
+ throws NullArgumentException, NoDataException {
ensureDataConformance(x, y);
@@ -146,10 +134,15 @@ public class MannWhitneyUTestImpl implem
* @param n1 number of subjects in first sample
* @param n2 number of subjects in second sample
* @return two-sided asymptotic p-value
- * @throws MathException if an error occurs computing the p-value
- */
- private double calculateAsymptoticPValue(final double Umin, final int n1,
- final int n2) throws MathException {
+ * @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
+ */
+ private double calculateAsymptoticPValue(final double Umin,
+ final int n1,
+ final int n2)
+ throws ConvergenceException, MaxCountExceededException {
final int n1n2prod = n1 * n2;
@@ -159,8 +152,7 @@ public class MannWhitneyUTestImpl implem
final double z = (Umin - EU) / FastMath.sqrt(VarU);
- final NormalDistribution standardNormal = new NormalDistribution(
- 0, 1);
+ final NormalDistribution standardNormal = new NormalDistribution(0, 1);
return 2 * standardNormal.cumulativeProbability(z);
}
@@ -171,15 +163,10 @@ public class MannWhitneyUTestImpl implem
* >http://mlsc.lboro.ac.uk/resources/statistics/Mannwhitney.pdf</a>.
*
* {@inheritDoc}
- *
- * @param x the first sample
- * @param y the second sample
- * @return asymptotic p-value (biased for samples with ties)
- * @throws IllegalArgumentException if preconditions are not met
- * @throws MathException if an error occurs computing the p-value
*/
public double mannWhitneyUTest(final double[] x, final double[] y)
- throws IllegalArgumentException, MathException {
+ throws NullArgumentException, NoDataException,
+ ConvergenceException, MaxCountExceededException {
ensureDataConformance(x, y);
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=1241831&r1=1241830&r2=1241831&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 Wed Feb 8 09:27:07 2012
@@ -103,10 +103,12 @@ public interface WilcoxonSignedRankTest
* @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
+ * @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 NullArgumentException, NoDataException, DimensionMismatchException,
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/MannWhitneyUTestTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/MannWhitneyUTestTest.java?rev=1241831&r1=1241830&r2=1241831&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/MannWhitneyUTestTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/inference/MannWhitneyUTestTest.java Wed Feb 8 09:27:07 2012
@@ -16,12 +16,14 @@
*/
package org.apache.commons.math.stat.inference;
+import org.apache.commons.math.exception.NoDataException;
+import org.apache.commons.math.exception.NullArgumentException;
import org.junit.Assert;
import org.junit.Test;
/**
- * Test cases for the ChiSquareTestImpl class.
+ * Test cases for the MannWhitneyUTestImpl class.
*
* @version $Id$
*/
@@ -52,15 +54,15 @@ public class MannWhitneyUTestTest {
*/
try {
testStatistic.mannWhitneyUTest(new double[] { }, new double[] { 1.0 });
- 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.mannWhitneyUTest(new double[] { 1.0 }, new double[] { });
- 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
}
@@ -69,15 +71,15 @@ public class MannWhitneyUTestTest {
*/
try {
testStatistic.mannWhitneyUTest(null, null);
- 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.mannWhitneyUTest(null, null);
- 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
}
@@ -86,15 +88,15 @@ public class MannWhitneyUTestTest {
*/
try {
testStatistic.mannWhitneyUTest(null, new double[] { 1.0 });
- 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.mannWhitneyUTest(new double[] { 1.0 }, null);
- Assert.fail("y is null (exact), IllegalArgumentException expected");
- } catch (IllegalArgumentException ex) {
+ Assert.fail("y is null (exact), NullArgumentException expected");
+ } catch (NullArgumentException ex) {
// expected
}
}