Removed MathException from MannWhitneyUTest, improved javadoc, exceptions.

JIRA: MATH-488

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1241831 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2012-02-08 09:27:07 +00:00
parent 4a8f04b498
commit eec73bbca2
4 changed files with 67 additions and 74 deletions

View File

@ -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;
}

View File

@ -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 implements MannWhitneyUTest {
*
* @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 implements MannWhitneyUTest {
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 implements MannWhitneyUTest {
* @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
* @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 MathException {
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 implements MannWhitneyUTest {
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 implements MannWhitneyUTest {
* >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);

View File

@ -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,

View File

@ -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
}
}