Skip to content

Commit

Permalink
Fix off-by-one bug in mann_whitney_u
Browse files Browse the repository at this point in the history
  • Loading branch information
pv committed Oct 21, 2018
1 parent eed9e6d commit f0f94c7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion asv/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def mann_whitney_u(x, y, method='auto'):
# Get p-value
if method == 'exact':
p1 = mann_whitney_u_cdf(m, n, ux, memo)
p2 = 1.0 - mann_whitney_u_cdf(m, n, m*n - ux, memo)
p2 = 1.0 - mann_whitney_u_cdf(m, n, m*n - ux - 1, memo)
p = p1 + p2
elif method == 'normal':
N = m + n
Expand Down
13 changes: 13 additions & 0 deletions test/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,19 @@ def check(x, y):
check(x, y - 2.5)


def test_mann_whitney_u_R():
a = [1, 2, 3, 4]
b = [0.9, 1.1, 0.7]

# wilcox.test(a, b, exact=TRUE)
u_expected = 11
p_expected = 0.11428571428571428

u, p = statistics.mann_whitney_u(a, b, method='exact')
assert u == u_expected
assert p == pytest.approx(p_expected, abs=0, rel=1e-10)


def test_binom():
for n in range(10):
for k in range(10):
Expand Down

0 comments on commit f0f94c7

Please sign in to comment.