Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3d180e3

Browse files
authored
Improve accuracy for Spearman's rank correlation coefficient. (#96392)
1 parent 873554e commit 3d180e3

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

Lib/statistics.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def _rank(data, /, *, key=None, reverse=False, ties='average', start=1) -> list[
379379
[2.0, 1.0, 3.0]
380380
381381
Ranks are conventionally numbered starting from one; however,
382-
setting *start* to zero allow the ranks to be used as array indices:
382+
setting *start* to zero allows the ranks to be used as array indices:
383383
384384
>>> prize = ['Gold', 'Silver', 'Bronze', 'Certificate']
385385
>>> scores = [8.1, 7.3, 9.4, 8.3]
@@ -1073,8 +1073,9 @@ def correlation(x, y, /, *, method='linear'):
10731073
if method not in {'linear', 'ranked'}:
10741074
raise ValueError(f'Unknown method: {method!r}')
10751075
if method == 'ranked':
1076-
x = _rank(x)
1077-
y = _rank(y)
1076+
start = (n - 1) / -2 # Center rankings around zero
1077+
x = _rank(x, start=start)
1078+
y = _rank(y, start=start)
10781079
xbar = fsum(x) / n
10791080
ybar = fsum(y) / n
10801081
sxy = fsum((xi - xbar) * (yi - ybar) for xi, yi in zip(x, y))

0 commit comments

Comments
 (0)