Exact null distribution of a rank sum (source code)

= Exact null distribution of a rank sum
{title2=$\Pr(W_X=w)=\binom{m+n}{m}^{-1}[z^mt^w]\prod_{k=1}^{m+n}(1+zt^k)$}

Under the continuous common-distribution null of the <Wilcoxon rank sum test>, pooled labels are exchangeable. The product records every choice of ranks: choosing $zt^k$ assigns rank $k$ to the first sample. Its indicated coefficient counts subsets of size $m$ and rank sum $w$. Divide by $\binom{m+n}{m}$ and sum the lower tail for the one-sided <P-value>. Equivalently the counts satisfy $a_{k,h,w}=a_{k-1,h,w}+a_{k-1,h-1,w-k}$ with $a_{0,0,0}=1$, allowing an exact integer computation.