Class MannWhitneyUTest
- java.lang.Object
-
- org.apache.commons.statistics.inference.MannWhitneyUTest
-
public final class MannWhitneyUTest extends Object
Implements the Mann-Whitney U test (also called Wilcoxon rank-sum test).- Since:
- 1.1
- See Also:
- Mann-Whitney U test (Wikipedia)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMannWhitneyUTest.ResultResult for the Mann-Whitney U test.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description doublestatistic(double[] x, double[] y)Computes the Mann-Whitney U statistic comparing two independent samples possibly of different length.MannWhitneyUTest.Resulttest(double[] x, double[] y)Performs a Mann-Whitney U test comparing the location for two independent samples.MannWhitneyUTestwith(AlternativeHypothesis v)Return an instance with the configured alternative hypothesis.MannWhitneyUTestwith(ContinuityCorrection v)Return an instance with the configured continuity correction.MannWhitneyUTestwith(PValueMethod v)Return an instance with the configured p-value method.static MannWhitneyUTestwithDefaults()Return an instance using the default options.MannWhitneyUTestwithMu(double v)Return an instance with the configured location shiftmu.
-
-
-
Method Detail
-
withDefaults
public static MannWhitneyUTest withDefaults()
Return an instance using the default options.- Returns:
- default instance
-
with
public MannWhitneyUTest with(AlternativeHypothesis v)
Return an instance with the configured alternative hypothesis.- Parameters:
v- Value.- Returns:
- an instance
-
with
public MannWhitneyUTest with(PValueMethod v)
Return an instance with the configured p-value method.- Parameters:
v- Value.- Returns:
- an instance
- Throws:
IllegalArgumentException- if the value is not in the allowed options or is null
-
with
public MannWhitneyUTest with(ContinuityCorrection v)
Return an instance with the configured continuity correction.If
ENABLED, adjust the U rank statistic by 0.5 towards the mean value when computing the z-statistic if a normal approximation is used to compute the p-value.- Parameters:
v- Value.- Returns:
- an instance
-
withMu
public MannWhitneyUTest withMu(double v)
Return an instance with the configured location shiftmu.- Parameters:
v- Value.- Returns:
- an instance
- Throws:
IllegalArgumentException- if the value is not finite
-
statistic
public double statistic(double[] x, double[] y)
Computes the Mann-Whitney U statistic comparing two independent samples possibly of different length.This statistic can be used to perform a Mann-Whitney U test evaluating the null hypothesis that the two independent samples differ by a location shift of
mu.This returns the U1 statistic. Compute the U2 statistic using:
u2 = (long) x.length * y.length - u1;
- Parameters:
x- First sample values.y- Second sample values.- Returns:
- Mann-Whitney U1 statistic
- Throws:
IllegalArgumentException- ifxoryare zero-length; or contain NaN values.- See Also:
withMu(double)
-
test
public MannWhitneyUTest.Result test(double[] x, double[] y)
Performs a Mann-Whitney U test comparing the location for two independent samples. The location is specified usingmu.The test is defined by the
AlternativeHypothesis.- 'two-sided': the distribution underlying
(x - mu)is not equal to the distribution underlyingy. - 'greater': the distribution underlying
(x - mu)is stochastically greater than the distribution underlyingy. - 'less': the distribution underlying
(x - mu)is stochastically less than the distribution underlyingy.
If the p-value method is auto an exact p-value is computed if the samples contain less than 50 values; otherwise a normal approximation is used.
Computation of the exact p-value is only valid if there are no tied ranks in the data; otherwise the p-value resorts to the asymptotic approximation using a tie correction and an optional continuity correction.
Note: Exact computation requires tabulation of values not exceeding size
(n+1)*(m+1)*(u+1)whereuis the minimum of the U1 and U2 statistics andnandmare the sample sizes. This may use a very large amount of memory and result in anOutOfMemoryError. Exact computation requires a finite binomial coefficientbinom(n+m, m)which is limited ton+m <= 1029for anynandm, ormin(n, m) <= 37for anymax(n, m). AnOutOfMemoryErroris not expected using the limits configured for the auto p-value computation as the maximum required memory is approximately 23 MiB.- Parameters:
x- First sample values.y- Second sample values.- Returns:
- test result
- Throws:
IllegalArgumentException- ifxoryare zero-length; or contain NaN values.OutOfMemoryError- if the exact computation is user-requested for large samples and there is not enough memory.- See Also:
statistic(double[], double[]),withMu(double),with(AlternativeHypothesis),with(ContinuityCorrection)
- 'two-sided': the distribution underlying
-
-