NAG Library Function Document

nag_mann_whitney (g08amc)


    1  Purpose
    7  Accuracy


nag_mann_whitney (g08amc) performs the Mann–Whitney U  test on two independent samples of possibly unequal size and calculates the exact probability for the Mann–Whitney rank sum test statistic for the case where there are either ties or no ties in the samples pooled together.


#include <nag.h>
#include <nagg08.h>
void  nag_mann_whitney (Integer n1, const double x[], Integer n2, const double y[], Nag_TailProbability tail, Nag_CompProb exact, double *u, double *z, double *p, NagError *fail)


The Mann–Whitney U  test investigates the difference between two populations defined by the distribution functions F x  and G y  respectively. The data consist of two independent samples of size n 1  and n 2 , denoted by x 1 , x 2 , , x n 1  and y 1 , y 2 , , 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 x = G x , and this is to be tested against an alternative hypothesis H 1  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 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 x > G y  we have evidence to suggest that the location, of the distribution defined by F x , is less than the location, of the distribution defined by G y .
The Mann–Whitney U  test differs from the Median test (see nag_median_test (g08acc)) 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 , for i=1,2,, n 1  and r 2j  the rank assigned to y j , for j=1,2,, n 2 . Then the test statistic U  is defined as follows;
U = i=1 n 1 r 1i - n 1 n 1 + 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).
nag_mann_whitney (g08amc) returns:
(a) the test statistic U ;
(b) the approximate Normal test statistic,
z = U - meanU ± 1 2 varU  
meanU = n 1 n 2 2  
varU = n 1 n 2 n 1 + n 2 + 1 12 - n 1 n 2 n 1 + n 2 n 1 + n 2 - 1 × TS  
TS = j=1 τ t j t j - 1 t j + 1 12  
τ  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 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 , corresponding to U , depending on the choice of tail, i.e., the choice of alternative hypothesis, H 1 . An exact probability or a normal approximation may be selected using the exact option. For large values of n 1  and n 2  the normal approximation should be adequate while the exact computation may require extensive calculation, particularly in cases where there are ties in the observations. For small samples the exact probability should be used.
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 α  be the size of the significance test (that is, α  is the probability of rejecting H 0  when H 0  is true). If p<α  then the null hypothesis is rejected. Typically α  might be 0.05 or 0.01.


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


1:     n1 IntegerInput
On entry: the number of non-tied pairs, n 1 .
Constraint: n11 .
2:     x[n1] const doubleInput
On entry: the first vector of observations. x 1 , x 2 , , x n 1 .
3:     n2 IntegerInput
On entry: the size of the second sample, n 2 .
Constraint: n21 .
4:     y[n2] const doubleInput
On entry: the second vector of observations. y 1 , y 2 , , y n 2 .
5:     tail Nag_TailProbabilityInput
On entry: indicates the choice of tail probability, and hence the alternative hypothesis.
A two tailed probability is calculated and the alternative hypothesis is H 1 : F x G y .
An upper tailed probability is calculated and the alternative hypothesis H 1 : F x < G y , i.e., the x 's tend to be greater than the y 's.
A lower tailed probability is calculated and the alternative hypothesis H 1 : F x > G y , i.e., the x 's tend to be less than the y 's.
Constraint: tail=Nag_TwoTail, Nag_UpperTail or Nag_LowerTail.
6:     exact Nag_CompProbInput
On entry: indicates if exact probability, p , is to be computed.
Compute the exact probability.
Compute the approximate probability.
Constraint: exact=Nag_CompProbExact or Nag_CompProbApprox.
7:     u double *Output
On exit: the Mann–Whitney rank sum statistic, U .
8:     z double *Output
On exit: the approximate Normal test statistic, z , as described in Section 3.
9:     p double *Output
On exit: the exact tail probability, p , as specified by the argument tail.
10:   fail NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

Error Indicators and Warnings

Dynamic memory allocation failed.
On entry, argument exact had an illegal value.
On entry, argument tail had an illegal value.
The pooled samples are all the same, that is the variance of U=0.0 .
On entry, n1 must not be less than 1: n1=value .
On entry, n2 must not be less than 1: n2=value .
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
The computed Mann–Whitney statistic, U=value . If the exact tail probability is to be computed, then U0 .


The approximate tail probability, p , returned by nag_mann_whitney (g08amc) is a good approximation to the exact probability for cases where max n 1 , n 2 30  and n 1 + n 2 40 . The relative error of the approximation should be less than 10 percent, for most cases falling in this range.

Parallelism and Performance

nag_mann_whitney (g08amc) is not threaded in any implementation.

Further Comments

The time taken by nag_mann_whitney (g08amc) increases with n 1  and n 2 .


The example program 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.

Program Text

Program Text (g08amce.c)

Program Data

Program Data (g08amce.d)

Program Results

Program Results (g08amce.r)

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