# NAG FL Interfaceg08ahf (test_​mwu)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

g08ahf performs the Mann–Whitney $U$ test on two independent samples of possibly unequal size.

## 2Specification

Fortran Interface
 Subroutine g08ahf ( n1, x, n2, y, tail, u, unor, p, ties, wrk,
 Integer, Intent (In) :: n1, n2 Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: x(n1), y(n2) Real (Kind=nag_wp), Intent (Out) :: u, unor, p, ranks(n1+n2), wrk(n1+n2) Logical, Intent (Out) :: ties Character (1), Intent (In) :: tail
C Header Interface
#include <nag.h>
 void g08ahf_ (const Integer *n1, const double x[], const Integer *n2, const double y[], const char *tail, double *u, double *unor, double *p, logical *ties, double ranks[], double wrk[], Integer *ifail, const Charlen length_tail)
The routine may be called by the names g08ahf or nagf_nonpar_test_mwu.

## 3Description

The Mann–Whitney $U$ test investigates the difference between two populations defined by the distribution functions $F\left(x\right)$ and $G\left(y\right)$ respectively. The data consist of two independent samples of size ${n}_{1}$ and ${n}_{2}$, denoted by ${x}_{1},{x}_{2},\dots ,{x}_{{n}_{1}}$ and ${y}_{1},{y}_{2},\dots ,{y}_{{n}_{2}}$, taken from the two populations.
The hypothesis under test, ${H}_{0}$, often called the null hypothesis, is that the two distributions are the same, that is $F\left(x\right)=G\left(x\right)$, and this is to be tested against an alternative hypothesis ${H}_{1}$ which is
• ${H}_{1}$: $F\left(x\right)\ne G\left(y\right)$; or
• ${H}_{1}$: $F\left(x\right), i.e., the $x$'s tend to be greater than the $y$'s; or
• ${H}_{1}$: $F\left(x\right)>G\left(y\right)$, i.e., the $x$'s tend to be less than the $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 argument tail in Section 5).
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}$ in favour of the alternative hypothesis ${H}_{1}$: $F\left(x\right)>G\left(y\right)$ we have evidence to suggest that the location, of the distribution defined by $F\left(x\right)$, is less than the location, of the distribution defined by $G\left(y\right)$.
The Mann–Whitney $U$ test differs from the Median test (see g08acf) 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}$ be the rank assigned to ${x}_{i}$, $i=1,2,\dots ,{n}_{1}$ and ${r}_{2j}$ the rank assigned to ${y}_{j}$, $j=1,2,\dots ,{n}_{2}$. Then the test statistic $U$ is defined as follows;
 $U=∑i=1n1r1i-n1(n1+1)2$
$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).
g08ahf returns:
1. (a)The test statistic $U$.
2. (b)The approximate Normal test statistic,
 $z=U-mean(U)±12 var(U)$
where
 $mean(U)=n1n22$
and
 $var(U)=n1n2(n1+n2+1)12-n1n2 (n1+n2)(n1+n2-1) ×TS$
where
 $TS=∑j= 1 τ(tj)(tj-1)(tj+1)12$
$\tau$ is the number of groups of ties in the sample and ${t}_{j}$ is the number of ties in the $j$th group.
Note that if no ties are present the variance of $U$ reduces to $\frac{{n}_{1}{n}_{2}}{12}\left({n}_{1}+{n}_{2}+1\right)$.
3. (c)An indicator as to whether ties were present in the pooled sample or not.
4. (d)The tail probability, $p$, corresponding to $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}$. The tail probability returned is an approximation of $p$ is based on an approximate Normal statistic corrected for continuity according to the tail specified. If ${n}_{1}$ and ${n}_{2}$ are not very large an exact probability may be desired. For the calculation of the exact probability see g08ajf (no ties in the pooled sample) or g08akf (ties in the pooled sample).
The value of $p$ can be used to perform a significance test on the null hypothesis ${H}_{0}$ against the alternative hypothesis ${H}_{1}$. Let $\alpha$ be the size of the significance test (that is, $\alpha$ is the probability of rejecting ${H}_{0}$ when ${H}_{0}$ is true). If $p<\alpha$ then the null hypothesis is rejected. Typically $\alpha$ might be $0.05$ or $0.01$.

## 4References

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) Non-parametric Statistics for the Behavioral Sciences McGraw–Hill

## 5Arguments

1: $\mathbf{n1}$Integer Input
On entry: the size of the first sample, ${n}_{1}$.
Constraint: ${\mathbf{n1}}\ge 1$.
2: $\mathbf{x}\left({\mathbf{n1}}\right)$Real (Kind=nag_wp) array Input
On entry: the first vector of observations, ${x}_{1},{x}_{2},\dots ,{x}_{{n}_{1}}$.
3: $\mathbf{n2}$Integer Input
On entry: the size of the second sample, ${\mathit{n}}_{2}$.
Constraint: ${\mathbf{n2}}\ge 1$.
4: $\mathbf{y}\left({\mathbf{n2}}\right)$Real (Kind=nag_wp) array Input
On entry: the second vector of observations. ${y}_{1},{y}_{2},\dots ,{y}_{{n}_{2}}$.
5: $\mathbf{tail}$Character(1) Input
On entry: indicates the choice of tail probability, and hence the alternative hypothesis.
${\mathbf{tail}}=\text{'T'}$
A two tailed probability is calculated and the alternative hypothesis is ${H}_{1}:F\left(x\right)\ne G\left(y\right)$.
${\mathbf{tail}}=\text{'U'}$
An upper tailed probability is calculated and the alternative hypothesis ${H}_{1}:F\left(x\right), i.e., the $x$'s tend to be greater than the $y$'s.
${\mathbf{tail}}=\text{'L'}$
A lower tailed probability is calculated and the alternative hypothesis ${H}_{1}:F\left(x\right)>G\left(y\right)$, i.e., the $x$'s tend to be less than the $y$'s.
Constraint: ${\mathbf{tail}}=\text{'T'}$, $\text{'U'}$ or $\text{'L'}$.
6: $\mathbf{u}$Real (Kind=nag_wp) Output
On exit: the Mann–Whitney rank sum statistic, $U$.
7: $\mathbf{unor}$Real (Kind=nag_wp) Output
On exit: the approximate Normal test statistic, $z$, as described in Section 3.
8: $\mathbf{p}$Real (Kind=nag_wp) Output
On exit: the tail probability, $p$, as specified by the argument tail.
9: $\mathbf{ties}$Logical Output
On exit: indicates whether the pooled sample contained ties or not. This will be useful in checking which routine to use should one wish to calculate an exact tail probability.
${\mathbf{ties}}=\mathrm{.FALSE.}$, no ties were present (use g08ajf for an exact probability).
${\mathbf{ties}}=\mathrm{.TRUE.}$, ties were present (use g08akf for an exact probability).
10: $\mathbf{ranks}\left({\mathbf{n1}}+{\mathbf{n2}}\right)$Real (Kind=nag_wp) array Output
On exit: 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.
11: $\mathbf{wrk}\left({\mathbf{n1}}+{\mathbf{n2}}\right)$Real (Kind=nag_wp) array Workspace
12: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{n1}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n1}}\ge 1$.
On entry, ${\mathbf{n2}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n2}}\ge 1$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{tail}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{tail}}=\text{'T'}$, $\text{'U'}$ or $\text{'L'}$.
${\mathbf{ifail}}=3$
The pooled sample values are all the same, i.e., the variance of ${\mathbf{u}}=0.0$.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

The approximate tail probability, $p$, returned by g08ahf is a good approximation to the exact probability for cases where $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({n}_{1},{n}_{2}\right)\ge 30$ and $\left({n}_{1}+{n}_{2}\right)\ge 40$. The relative error of the approximation should be less than $10%$, for most cases falling in this range.

## 8Parallelism and Performance

g08ahf is not threaded in any implementation.

## 9Further Comments

The time taken by g08ahf increases with ${n}_{1}$ and ${n}_{2}$.

## 10Example

This example performs the Mann–Whitney test on two independent samples of sizes $16$ and $23$ respectively. This is used to test the null hypothesis that the distributions of the two populations from which the samples were taken are the same against the alternative hypothesis that the distributions are different. The test statistic, the approximate Normal statistic and the approximate two tail probability are printed. An exact tail probability is also calculated and printed depending on whether ties were found in the pooled sample or not.

### 10.1Program Text

Program Text (g08ahfe.f90)

### 10.2Program Data

Program Data (g08ahfe.d)

### 10.3Program Results

Program Results (g08ahfe.r)