8000 BUG: fix stats.kendalltau to handle larger lists. Closes #1445. · scipy/scipy@ce14ddb · GitHub
[go: up one dir, main page]

Skip to content

Commit ce14ddb

Browse files
author
rgommers
committed
BUG: fix stats.kendalltau to handle larger lists. Closes #1445.
1 parent bff6861 commit ce14ddb

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

scipy/stats/stats.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,7 +2624,7 @@ def kendalltau(x, y, initial_lexsort=True):
26242624

26252625
x = np.asarray(x).ravel()
26262626
y = np.asarray(y).ravel()
2627-
n = len(x)
2627+
n = np.int64(len(x))
26282628
temp = range(n) # support structure used by mergesort
26292629
# this closure recursively sorts sections of perm[] by comparing
26302630
# elements of y[perm[]] using temp[] as support
@@ -2707,8 +2707,9 @@ def mergesort(offs, length):
27072707
if tot == u and tot == v:
27082708
return 1 # Special case for all ties in both ranks
27092709

2710-
tau = ((tot - (v + u - t)) - 2.0 * exchanges) / \
2711-
np.sqrt((tot - u) * (tot - v))
2710+
# Prevent overflow; equal to np.sqrt((tot - u) * (tot - v))
2711+
denom = np.exp(0.5 * (np.log(tot - u) + np.log(tot - v)))
2712+
tau = ((tot - (v + u - t)) - 2.0 * exchanges) / denom
27122713

27132714
# what follows reproduces the ending of Gary Strangman's original
27142715
# stats.kendalltau() in SciPy

scipy/stats/tests/test_stats.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,8 @@ def test_kendalltau():
585585
# with some ties
586586
x1 = [12, 2, 1, 12, 2]
587587
x2 = [1, 4, 7, 1, 0]
588-
res = (-0.47140452079103173, 0.24821309157521476)
589-
expected = stats.kendalltau(x1, x2)
588+
expected = (-0.47140452079103173, 0.24821309157521476)
589+
res = stats.kendalltau(x1, x2)
590590
assert_approx_equal(res[0], expected[0])
591591
assert_approx_equal(res[1], expected[1])
592592

0 commit comments

Comments
 (0)
0