org.apache.commons.math3.stat.inference
Class MannWhitneyUTest

java.lang.Object
  extended by org.apache.commons.math3.stat.inference.MannWhitneyUTest

public class MannWhitneyUTest
extends Object

An implementation of the Mann-Whitney U test (also called Wilcoxon rank-sum test).

Version:
$Id: MannWhitneyUTest.java 1416643 2012-12-03 19:37:14Z tn $

Field Summary
private  NaturalRanking naturalRanking
          Ranking algorithm.
 
Constructor Summary
MannWhitneyUTest()
          Create a test instance using where NaN's are left in place and ties get the average of applicable ranks.
MannWhitneyUTest(NaNStrategy nanStrategy, TiesStrategy tiesStrategy)
          Create a test instance using the given strategies for NaN's and ties.
 
Method Summary
private  double calculateAsymptoticPValue(double Umin, int n1, int n2)
           
private  double[] concatenateSamples(double[] x, double[] y)
          Concatenate the samples into one array.
private  void ensureDataConformance(double[] x, double[] y)
          Ensures that the provided arrays fulfills the assumptions.
 double mannWhitneyU(double[] x, double[] y)
          Computes the Mann-Whitney U statistic comparing mean for two independent samples possibly of different length.
 double mannWhitneyUTest(double[] x, double[] y)
          Returns the asymptotic observed significance level, or p-value, associated with a Mann-Whitney U statistic comparing mean for two independent samples.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

naturalRanking

private NaturalRanking naturalRanking
Ranking algorithm.

Constructor Detail

MannWhitneyUTest

public MannWhitneyUTest()
Create a test instance using where NaN's are left in place and ties get the average of applicable ranks. Use this unless you are very sure of what you are doing.


MannWhitneyUTest

public MannWhitneyUTest(NaNStrategy nanStrategy,
                        TiesStrategy tiesStrategy)
Create a test instance using the given strategies for NaN's and ties. Only use this if you are sure of what you are doing.

Parameters:
nanStrategy - specifies the strategy that should be used for Double.NaN's
tiesStrategy - specifies the strategy that should be used for ties
Method Detail

ensureDataConformance

private void ensureDataConformance(double[] x,
                                   double[] y)
                            throws NullArgumentException,
                                   NoDataException
Ensures that the provided arrays fulfills the assumptions.

Parameters:
x - first sample
y - second sample
Throws:
NullArgumentException - if x or y are null.
NoDataException - if x or y are zero-length.

concatenateSamples

private double[] concatenateSamples(double[] x,
                                    double[] y)
Concatenate the samples into one array.

Parameters:
x - first sample
y - second sample
Returns:
concatenated array

mannWhitneyU

public double mannWhitneyU(double[] x,
                           double[] y)
                    throws NullArgumentException,
                           NoDataException
Computes the Mann-Whitney U statistic comparing mean for 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 has equal mean.

Let Xi denote the i'th individual of the first sample and Yj the j'th individual in the second sample. Note that the samples would often have different length.

Preconditions:

Parameters:
x - the first sample
y - the second sample
Returns:
Mann-Whitney U statistic (maximum of Ux and Uy)
Throws:
NullArgumentException - if x or y are null.
NoDataException - if x or y are zero-length.

calculateAsymptoticPValue

private double calculateAsymptoticPValue(double Umin,
                                         int n1,
                                         int n2)
                                  throws ConvergenceException,
                                         MaxCountExceededException
Parameters:
Umin - smallest Mann-Whitney U value
n1 - number of subjects in first sample
n2 - number of subjects in second sample
Returns:
two-sided asymptotic p-value
Throws:
ConvergenceException - if the p-value can not be computed due to a convergence error
MaxCountExceededException - if the maximum number of iterations is exceeded

mannWhitneyUTest

public double mannWhitneyUTest(double[] x,
                               double[] y)
                        throws NullArgumentException,
                               NoDataException,
                               ConvergenceException,
                               MaxCountExceededException
Returns the asymptotic observed significance level, or p-value, associated with a Mann-Whitney U statistic comparing mean for two independent samples.

Let Xi denote the i'th individual of the first sample and Yj the j'th individual in the second sample. Note that the samples would often have different length.

Preconditions:

Ties give rise to biased variance at the moment. See e.g. http://mlsc.lboro.ac.uk/resources/statistics/Mannwhitney.pdf.

Parameters:
x - the first sample
y - the second sample
Returns:
asymptotic p-value
Throws:
NullArgumentException - if x or y are null.
NoDataException - if x or y are zero-length.
ConvergenceException - if the p-value can not be computed due to a convergence error
MaxCountExceededException - if the maximum number of iterations is exceeded


Copyright (c) 2003-2013 Apache Software Foundation