PDF version (NAG web site
, 64bit version, 64bit version)
NAG Toolbox: nag_nonpar_test_mwu (g08ah)
Purpose
nag_nonpar_test_mwu (g08ah) performs the Mann–Whitney U$U$ test on two independent samples of possibly unequal size.
Syntax
[
u,
unor,
p,
ties,
ranks,
ifail] = g08ah(
x,
y,
tail, 'n1',
n1, 'n2',
n2)
[
u,
unor,
p,
ties,
ranks,
ifail] = nag_nonpar_test_mwu(
x,
y,
tail, 'n1',
n1, 'n2',
n2)
Description
The Mann–Whitney U$U$ test investigates the difference between two populations defined by the distribution functions F(x)$F\left(x\right)$ and G(y)$G\left(y\right)$ respectively. The data consist of two independent samples of size n_{1}${n}_{1}$ and n_{2}${n}_{2}$, denoted by x_{1},x_{2}, … ,x_{n1}${x}_{1},{x}_{2},\dots ,{x}_{{n}_{1}}$ and y_{1},y_{2}, … ,y_{n2}${y}_{1},{y}_{2},\dots ,{y}_{{n}_{2}}$, taken from the two populations.
The hypothesis under test,
H_{0}${H}_{0}$, often called the null hypothesis, is that the two distributions are the same, that is
F(x) = G(x)$F\left(x\right)=G\left(x\right)$, and this is to be tested against an alternative hypothesis
H_{1}${H}_{1}$ which is
 H_{1}${H}_{1}$: F(x) ≠ G(y)$F\left(x\right)\ne G\left(y\right)$; or
 H_{1}${H}_{1}$: F(x) < G(y)$F\left(x\right)<G\left(y\right)$, i.e., the x$x$'s tend to be greater than the y$y$'s; or
 H_{1}${H}_{1}$: F(x) > G(y)$F\left(x\right)>G\left(y\right)$, i.e., the x$x$'s tend to be less than the y$y$'s,
using a two tailed, upper tailed or lower tailed probability respectively. You select the alternative hypothesis by choosing the appropriate tail probability to be computed (see the description of parameter
tail in
Section [Parameters]).
Note that when using this test to test for differences in the distributions one is primarily detecting differences in the location of the two distributions. That is to say, if we reject the null hypothesis H_{0}${H}_{0}$ in favour of the alternative hypothesis H_{1}${H}_{1}$: F(x) > G(y)$F\left(x\right)>G\left(y\right)$ we have evidence to suggest that the location, of the distribution defined by F(x)$F\left(x\right)$, is less than the location, of the distribution defined by G(y)$G\left(y\right)$.
The Mann–Whitney
U$U$ test differs from the Median test (see
nag_nonpar_test_median (g08ac)) in that the ranking of the individual scores within the pooled sample is taken into account, rather than simply the position of a score relative to the median of the pooled sample. It is therefore a more powerful test if score differences are meaningful.
The test procedure involves ranking the pooled sample, average ranks being used for ties. Let
r_{1i}${r}_{1i}$ be the rank assigned to
x_{i}${x}_{i}$,
i = 1,2, … ,n_{1}$i=1,2,\dots ,{n}_{1}$ and
r_{2j}${r}_{2j}$ the rank assigned to
y_{j}${y}_{j}$,
j = 1,2, … ,n_{2}$j=1,2,\dots ,{n}_{2}$. Then the test statistic
U$U$ is defined as follows;
U$U$ is also the number of times a score in the second sample precedes a score in the first sample (where we only count a half if a score in the second sample actually equals a score in the first sample).
nag_nonpar_test_mwu (g08ah) returns:
(a) 
The test statistic U$U$. 
(b) 
The approximate Normal test statistic,
where
and
where
τ$\tau $ is the number of groups of ties in the sample and t_{j}${t}_{j}$ is the number of ties in the j$j$th group.
Note that if no ties are present the variance of U$U$ reduces to
(n_{1}n_{2})/12(n_{1} + n_{2} + 1)$\frac{{n}_{1}{n}_{2}}{12}({n}_{1}+{n}_{2}+1)$. 
(c) 
An indicator as to whether ties were present in the pooled sample or not. 
(d) 
The tail probability, p$p$, corresponding to U$U$ (adjusted to allow the complement to be used in an upper one tailed or a two tailed test), depending on the choice of tail, i.e., the choice of alternative hypothesis, H_{1}${H}_{1}$. The tail probability returned is an approximation of p$p$ is based on an approximate Normal statistic corrected for continuity according to the tail specified. If n_{1}${n}_{1}$ and n_{2}${n}_{2}$ are not very large an exact probability may be desired. For the calculation of the exact probability see nag_nonpar_prob_mwu_noties (g08aj) (no ties in the pooled sample) or nag_nonpar_prob_mwu_ties (g08ak) (ties in the pooled sample).
The value of p$p$ can be used to perform a significance test on the null hypothesis H_{0}${H}_{0}$ against the alternative hypothesis H_{1}${H}_{1}$. Let α$\alpha $ be the size of the significance test (that is, α$\alpha $ is the probability of rejecting H_{0}${H}_{0}$ when H_{0}${H}_{0}$ is true). If p < α$p<\alpha $ then the null hypothesis is rejected. Typically α$\alpha $ might be 0.05$0.05$ or 0.01$0.01$. 
References
Conover W J (1980) Practical Nonparametric Statistics Wiley
Neumann N (1988) Some procedures for calculating the distributions of elementary nonparametric teststatistics Statistical Software Newsletter 14(3) 120–126
Siegel S (1956) Nonparametric Statistics for the Behavioral Sciences McGraw–Hill
Parameters
Compulsory Input Parameters
 1:
x(n1) – double array
n1, the dimension of the array, must satisfy the constraint
n1 ≥ 1${\mathbf{n1}}\ge 1$.
The first vector of observations, x_{1},x_{2}, … ,x_{n1}${x}_{1},{x}_{2},\dots ,{x}_{{n}_{1}}$.
 2:
y(n2) – double array
n2, the dimension of the array, must satisfy the constraint
n2 ≥ 1${\mathbf{n2}}\ge 1$.
The second vector of observations. y_{1},y_{2}, … ,y_{n2}${y}_{1},{y}_{2},\dots ,{y}_{{n}_{2}}$.
 3:
tail – string (length ≥ 1)
Indicates the choice of tail probability, and hence the alternative hypothesis.
 tail = 'T'${\mathbf{tail}}=\text{'T'}$
 A two tailed probability is calculated and the alternative hypothesis is H_{1} : F(x) ≠ G(y)${H}_{1}:F\left(x\right)\ne G\left(y\right)$.
 tail = 'U'${\mathbf{tail}}=\text{'U'}$
 An upper tailed probability is calculated and the alternative hypothesis H_{1} : F(x) < G(y)${H}_{1}:F\left(x\right)<G\left(y\right)$, i.e., the x$x$'s tend to be greater than the y$y$'s.
 tail = 'L'${\mathbf{tail}}=\text{'L'}$
 A lower tailed probability is calculated and the alternative hypothesis H_{1} : F(x) > G(y)${H}_{1}:F\left(x\right)>G\left(y\right)$, i.e., the x$x$'s tend to be less than the y$y$'s.
Constraint:
tail = 'T'${\mathbf{tail}}=\text{'T'}$,
'U'$\text{'U'}$ or
'L'$\text{'L'}$.
Optional Input Parameters
 1:
n1 – int64int32nag_int scalar
Default:
The dimension of the array
x.
The size of the first sample, n_{1}${n}_{1}$.
Constraint:
n1 ≥ 1${\mathbf{n1}}\ge 1$.
 2:
n2 – int64int32nag_int scalar
Default:
The dimension of the array
y.
The size of the second sample, n_{2}${\mathit{n}}_{2}$.
Constraint:
n2 ≥ 1${\mathbf{n2}}\ge 1$.
Input Parameters Omitted from the MATLAB Interface
 wrk
Output Parameters
 1:
u – double scalar
The Mann–Whitney rank sum statistic, U$U$.
 2:
unor – double scalar
The approximate Normal test statistic,
z$z$, as described in
Section [Description].
 3:
p – double scalar
The tail probability,
p$p$, as specified by the parameter
tail.
 4:
ties – logical scalar
Indicates whether the pooled sample contained ties or not. This will be useful in checking which function to use should one wish to calculate an exact tail probability.
ties = false${\mathbf{ties}}=\mathbf{false}$, no ties were present (use
nag_nonpar_prob_mwu_noties (g08aj) for an exact probability).
ties = true${\mathbf{ties}}=\mathbf{true}$, ties were present (use
nag_nonpar_prob_mwu_ties (g08ak) for an exact probability).
 5:
ranks(n1 + n2${\mathbf{n1}}+{\mathbf{n2}}$) – double array
Contains the ranks of the pooled sample. The ranks of the first sample are contained in the first
n1 elements and those of the second sample are contained in the next
n2 elements.
 6:
ifail – int64int32nag_int scalar
ifail = 0${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see
[Error Indicators and Warnings]).
Error Indicators and Warnings
Errors or warnings detected by the function:
Cases prefixed with W are classified as warnings and
do not generate an error of type NAG:error_n. See nag_issue_warnings.
 ifail = 1${\mathbf{ifail}}=1$

On entry,  n1 < 1${\mathbf{n1}}<1$, 
or  n2 < 1${\mathbf{n2}}<1$. 
 ifail = 2${\mathbf{ifail}}=2$

On entry,  tail ≠ 'T'${\mathbf{tail}}\ne \text{'T'}$, 'U'$\text{'U'}$ or 'L'$\text{'L'}$. 
 W ifail = 3${\mathbf{ifail}}=3$
The pooled sample values are all the same, that is the variance of
u = 0.0${\mathbf{u}}=0.0$.
Accuracy
The approximate tail probability, p$p$, returned by nag_nonpar_test_mwu (g08ah) is a good approximation to the exact probability for cases where max (n_{1},n_{2}) ≥ 30$\mathrm{max}\phantom{\rule{0.125em}{0ex}}({n}_{1},{n}_{2})\ge 30$ and (n_{1} + n_{2}) ≥ 40$({n}_{1}+{n}_{2})\ge 40$. The relative error of the approximation should be less than 10%$10\%$, for most cases falling in this range.
Further Comments
The time taken by nag_nonpar_test_mwu (g08ah) increases with n_{1}${n}_{1}$ and n_{2}${n}_{2}$.
Example
Open in the MATLAB editor:
nag_nonpar_test_mwu_example
function nag_nonpar_test_mwu_example
x = [13;
6;
12;
7;
12;
7;
10;
7;
10;
7;
16;
7;
10;
8;
9;
8];
y = [17;
6;
10;
8;
15;
8;
15;
10;
15;
10;
14;
10;
14;
11;
14;
11;
13;
12;
13;
12;
13;
12;
12];
tail = 'Lowertail';
[u, unor, p, ties, ranks, ifail] = nag_nonpar_test_mwu(x, y, tail)
u =
86
unor =
2.8039
p =
0.0025
ties =
1
ranks =
29.5000
1.5000
24.5000
5.0000
24.5000
5.0000
16.0000
5.0000
16.0000
5.0000
38.0000
5.0000
16.0000
9.5000
12.0000
9.5000
39.0000
1.5000
16.0000
9.5000
36.0000
9.5000
36.0000
16.0000
36.0000
16.0000
33.0000
16.0000
33.0000
20.5000
33.0000
20.5000
29.5000
24.5000
29.5000
24.5000
29.5000
24.5000
24.5000
ifail =
0
Open in the MATLAB editor:
g08ah_example
function g08ah_example
x = [13;
6;
12;
7;
12;
7;
10;
7;
10;
7;
16;
7;
10;
8;
9;
8];
y = [17;
6;
10;
8;
15;
8;
15;
10;
15;
10;
14;
10;
14;
11;
14;
11;
13;
12;
13;
12;
13;
12;
12];
tail = 'Lowertail';
[u, unor, p, ties, ranks, ifail] = g08ah(x, y, tail)
u =
86
unor =
2.8039
p =
0.0025
ties =
1
ranks =
29.5000
1.5000
24.5000
5.0000
24.5000
5.0000
16.0000
5.0000
16.0000
5.0000
38.0000
5.0000
16.0000
9.5000
12.0000
9.5000
39.0000
1.5000
16.0000
9.5000
36.0000
9.5000
36.0000
16.0000
36.0000
16.0000
33.0000
16.0000
33.0000
20.5000
33.0000
20.5000
29.5000
24.5000
29.5000
24.5000
29.5000
24.5000
24.5000
ifail =
0
PDF version (NAG web site
, 64bit version, 64bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013