# The exact distribution of Wilcoxon rank-sum statistic U

The distribition of the rank-sum statistic U is assumed to be normal for large number of samples being considered. What is the exact distribution? I want to compare and sometimes fuse results from various tests wherein some tests might not have large number of samples. I want to have a the exact distributions in cases where, say $n_1 n_2 < 30$. Is there a closed form that can be used or calculated?

Update: So apparently, people cite Streitberg, B. and J. Rohmel, Exact distributions for permutation and rank tests: An introduction to some recently published algorithms, Statist. Software Newsletter 1 (1986) 10-17. for the exact distribution, but I have not been able to find either the paper or the result yet.

• Why not just tabulate them? Mathematica 8 computed and plotted every case covered by the condition $n_1n_2 \lt 30$ in less than 1/20 second. The trick is to look at all possible rank sums that can be attained by the smaller of the two datasets; this requires only $\min(n_1,n_2) \binom{n_1+n_2}{n_1}$ sums. – whuber Jul 19 '11 at 20:56
• Regarding the Streitberg and Rohmel paper, see stats.stackexchange.com/questions/25565/… – Joel Reyes Noche Apr 1 '12 at 7:50

AFAIK, there is no closed form for the distribution. Using R, the naive implementation of getting the exact distribution works for me up to group sizes of at least 12 - that takes less than 1 minute on a Core i5 using Windows7 64bit and current R. For R's own more clever algorithm in C that's used in pwilcox(), you can check the source file src/nmath/wilcox.c

n1 <- 12                                # size group 1
n2 <- 12                                # size group 2
N  <- n1 + n2                           # total number of subjects


Now generate all possible cases for the ranks within group 1. These are all ${N \choose n_{1}}$ different samples from the numbers $1, \ldots, N$ of size $n_{1}$. Then calculate the rank sum (= test statistic) for each of these cases. Tabulate these rank sums to get the probability density function from the relative frequencies, the cumulative sum of these relative frequencies is the cumulative distribution function.

rankMat <- combn(1:N, n1)               # all possible ranks within group 1
LnPl    <- colSums(rankMat)             # all possible rank sums for group 1
dWRS    <- table(LnPl) / choose(N, n1)  # relative frequencies of rank sums: pdf
pWRS    <- cumsum(dWRS)                 # cumulative sums: cdf


Compare the exact distribution against the asymptotically correct normal distribution.

muLnPl  <- (n1    * (N+1)) /  2         # expected value
varLnPl <- (n1*n2 * (N+1)) / 12         # variance

plot(names(pWRS), pWRS, main="Wilcoxon RS, N=(12, 12): exact vs. asymptotic",
type="n", xlab="ln+", ylab="P(Ln+ <= ln+)", cex.lab=1.4)
curve(pnorm(x, mean=muLnPl, sd=sqrt(varLnPl)), lwd=4, n=200, add=TRUE)
points(names(pWRS), pWRS, pch=16, col="red", cex=0.7)
abline(h=0.95, col="blue")
legend(x="bottomright", legend=c("exact", "asymptotic"),
pch=c(16, NA), col=c("red", "black"), lty=c(NA, 1), lwd=c(NA, 2)) Caracal's answer is nice, but it's important to consider that the large sample approximation works best for equal sample sizes, and can perform considerably worse for unbalanced samples.

The paper you (and I) are looking for is for more general statistics than the Wilcoxon (Jonckheere-Terpstra, Umbrella tests, etc...).

Mehta has some papers around 1984 that should speed up the calculation of the distribution, but I agree with Caracal that pwilcox() should do the trick for you unless your samples are quite large.

Also, consider looking at the probability generating function for the Wilcoxon, which a closed form solution does exist and shows up as early as Jonckheere's original paper, and many times after that. This may or may not be useful, depending on your application.

• Eh... the asymptotic rapidly converges to the exact even with unbalanced sample sizes. See, for example, Bellera, C. A., Julien, M., and Hanley, J. A. (2010). Normal approximations to the distributions of the Wilcoxon statistics: Accurate to what n? Graphical insights. Journal of Statistics Education, 18(2):1–17. – Alexis Jan 21 '15 at 3:18