Permalink
Browse files

BUG: fix stats.kendalltau to handle larger lists. Closes #1445.

  • Loading branch information...
1 parent bff6861 commit ce14ddb13d99bad2186d3423f0a4a0133a1e48ca @rgommers rgommers committed Jun 12, 2011
Showing with 6 additions and 5 deletions.
  1. +4 −3 scipy/stats/stats.py
  2. +2 −2 scipy/stats/tests/test_stats.py
View
@@ -2624,7 +2624,7 @@ def kendalltau(x, y, initial_lexsort=True):
x = np.asarray(x).ravel()
y = np.asarray(y).ravel()
- n = len(x)
+ n = np.int64(len(x))
temp = range(n) # support structure used by mergesort
# this closure recursively sorts sections of perm[] by comparing
# elements of y[perm[]] using temp[] as support
@@ -2707,8 +2707,9 @@ def mergesort(offs, length):
if tot == u and tot == v:
return 1 # Special case for all ties in both ranks
- tau = ((tot - (v + u - t)) - 2.0 * exchanges) / \
- np.sqrt((tot - u) * (tot - v))
+ # Prevent overflow; equal to np.sqrt((tot - u) * (tot - v))
+ denom = np.exp(0.5 * (np.log(tot - u) + np.log(tot - v)))
+ tau = ((tot - (v + u - t)) - 2.0 * exchanges) / denom
# what follows reproduces the ending of Gary Strangman's original
# stats.kendalltau() in SciPy
@@ -585,8 +585,8 @@ def test_kendalltau():
# with some ties
x1 = [12, 2, 1, 12, 2]
x2 = [1, 4, 7, 1, 0]
- res = (-0.47140452079103173, 0.24821309157521476)
- expected = stats.kendalltau(x1, x2)
+ expected = (-0.47140452079103173, 0.24821309157521476)
+ res = stats.kendalltau(x1, x2)
assert_approx_equal(res[0], expected[0])
assert_approx_equal(res[1], expected[1])

0 comments on commit ce14ddb

Please sign in to comment.