G08AHF (PDF version)
G08 Chapter Contents
G08 Chapter Introduction
NAG Library Manual

NAG Library Routine Document


Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

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

2  Specification

REAL (KIND=nag_wp)  X(N1), Y(N2), U, UNOR, P, RANKS(N1+N2), WRK(N1+N2)

3  Description

The Mann–Whitney U test investigates the difference between two populations defined by the distribution functions Fx and Gy respectively. The data consist of two independent samples of size n1 and n2, denoted by x1,x2,,xn1 and y1,y2,,yn2, taken from the two populations.
The hypothesis under test, H0, often called the null hypothesis, is that the two distributions are the same, that is Fx=Gx, and this is to be tested against an alternative hypothesis H1 which is 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 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 H0 in favour of the alternative hypothesis H1: Fx>Gy we have evidence to suggest that the location, of the distribution defined by Fx, is less than the location, of the distribution defined by Gy.
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 r1i be the rank assigned to xi, i=1,2,,n1 and r2j the rank assigned to yj, j=1,2,,n2. Then the test statistic U is defined as follows;
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:
(a) The test statistic U.
(b) The approximate Normal test statistic,
z=U-meanU±12 varU
varU=n1n2n1+n2+112-n1n2 n1+n2n1+n2-1 ×TS
TS=j= 1 τtjtj- 1tj+ 112
τ is the number of groups of ties in the sample and tj is the number of ties in the jth group.
Note that if no ties are present the variance of U reduces to n1n212n1+n2+1.
(c) An indicator as to whether ties were present in the pooled sample or not.
(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, H1. 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 n1 and n2 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 H0 against the alternative hypothesis H1. Let α be the size of the significance test (that is, α is the probability of rejecting H0 when H0 is true). If p<α then the null hypothesis is rejected. Typically α might be 0.05 or 0.01.

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

5  Parameters

1:     N1 – INTEGERInput
On entry: the size of the first sample, n1.
Constraint: N11.
2:     X(N1) – REAL (KIND=nag_wp) arrayInput
On entry: the first vector of observations, x1,x2,,xn1.
3:     N2 – INTEGERInput
On entry: the size of the second sample, n2.
Constraint: N21.
4:     Y(N2) – REAL (KIND=nag_wp) arrayInput
On entry: the second vector of observations. y1,y2,,yn2.
5:     TAIL – CHARACTER(1)Input
On entry: indicates the choice of tail probability, and hence the alternative hypothesis.
A two tailed probability is calculated and the alternative hypothesis is H1:FxGy.
An upper tailed probability is calculated and the alternative hypothesis H1:Fx<Gy, i.e., the x's tend to be greater than the y's.
A lower tailed probability is calculated and the alternative hypothesis H1:Fx>Gy, i.e., the x's tend to be less than the y's.
Constraint: TAIL='T', 'U' or 'L'.
6:     U – REAL (KIND=nag_wp)Output
On exit: the Mann–Whitney rank sum statistic, U.
7:     UNOR – REAL (KIND=nag_wp)Output
On exit: the approximate Normal test statistic, z, as described in Section 3.
8:     P – REAL (KIND=nag_wp)Output
On exit: the tail probability, p, as specified by the parameter TAIL.
9:     TIES – LOGICALOutput
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.
TIES=.FALSE., no ties were present (use G08AJF for an exact probability).
TIES=.TRUE., ties were present (use G08AKF for an exact probability).
10:   RANKS(N1+N2) – REAL (KIND=nag_wp) arrayOutput
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:   WRK(N1+N2) – REAL (KIND=nag_wp) arrayWorkspace
12:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ or ​1. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is 0. When the value -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry 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:
On entry,N1<1,
On entry,TAIL'T', 'U' or 'L'.
The pooled sample values are all the same, that is the variance of U=0.0.

7  Accuracy

The approximate tail probability, p, returned by G08AHF is a good approximation to the exact probability for cases where maxn1,n230 and n1+n240. The relative error of the approximation should be less than 10%, for most cases falling in this range.

8  Further Comments

The time taken by G08AHF increases with n1 and n2.

9  Example

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.

9.1  Program Text

Program Text (g08ahfe.f90)

9.2  Program Data

Program Data (g08ahfe.d)

9.3  Program Results

Program Results (g08ahfe.r)

G08AHF (PDF version)
G08 Chapter Contents
G08 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2012