# Problem with Mann-Whitney U test in scipy

I am trying to perform Mann-Whitney test on a real data set using scipy. I extracted an example where the problem occurs. It seems that the function tiecorrect returns negative value which is the source of problem in my opinion. Here is my case

import scipy.stats

x = [1.] * 163 + [2.] * 81 + [3.] * 40 + [4.] * 6 + [5.] * 2
y = [1.] * 1007 + [2.] * 362 + [3.] * 99 + [4.] * 27 + [5.] * 13 # real-world example

print scipy.stats.mannwhitneyu(x,y)


I have successfully performed the MW test on the same case using SPSS and R but I would like to figure out how to do with in scipy.

There might be a bug in the package, but if you store the u and the prob output separately, you'll see the u value, although the prob is missing for some reason.

u, prob=scipy.stats.mannwhitneyu(x,y)

u
Out[18]: 193405.5

prob
Out[19]: nan


You could then use the normal approximation of $U$ to get a p-value, though. For large samples, $$z = \frac{U-m_U}{\sigma_U}$$ where $m_U = \frac{n_1n_2}{2}$ and $\sigma_U=\sqrt{\frac{n_1n_2(n_1+n_2+1)}{12}}$ has approximately a standard Normal distribution.

m_u = len(x)*len(y)/2

sigma_u = np.sqrt(len(x)*len(y)*(len(x)+len(y)+1)/12)

z = (u - m_u)/sigma_u

z
Out[23]: -3.2920646126227546


Then you can compute a p-value.

pval = 2*scipy.stats.norm.cdf(z)

pval
Out[27]: 0.00099454759456888472


Scipy might be trying to compute the direct null hypothesis distribution of $U$, but the Normal approximation should work fine given the number of observations that you have.

• In my opinion, the problem is in computation of sigma_u. It is computed as sd = np.sqrt(T*n1*n2*(n1+n2+1)/12.0) where T = tiecorrect(ranked) and ranked = rankdata(np.concatenate((x,y))) The problem is that the quantity T is negative so the standard error cannot be computed. – Lan Sep 22 '14 at 15:58
• @Lan I had the same problem and I came to the same conclusion after going through the source code. T is indeed negative and therefore the np.sqrt spits out a NaN. Problem seems to be the tiecorrect function. I've found your issue on github, seems it's still open... – Julien Marrec Jul 20 '15 at 14:51

This thread is old, but for those like me who encounter this scipy bug and find themselves here -

The issue is indeed the tiecorrect function in scipy.stats.mannwhitneyu(x,y)

If you do not need tie correcting, the scipy.stats.ranksums(x,y) test works fine.

import scipy.stats