Skip to content

Commit 8154e45

Browse files
committed
Add test
1 parent 292a971 commit 8154e45

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

Lib/statistics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,7 @@ def correlation(x, y, /, *, by_rank=False):
10411041
raise StatisticsError('correlation requires that both inputs have same number of data points')
10421042
if n < 2:
10431043
raise StatisticsError('correlation requires at least two data points')
1044-
if ranked:
1044+
if by_rank:
10451045
x = _rank(x)
10461046
y = _rank(y)
10471047
xbar = fsum(x) / n

Lib/test/test_statistics.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2565,6 +2565,19 @@ def test_different_scales(self):
25652565
self.assertAlmostEqual(statistics.covariance(x, y), 0.1)
25662566

25672567

2568+
def test_correlation_spearman(self):
2569+
# https://statistics.laerd.com/statistical-guides/spearmans-rank-order-correlation-statistical-guide-2.php
2570+
# Compare with:
2571+
# >>> import scipy.stats.mstats
2572+
# >>> scipy.stats.mstats.spearmanr(reading, mathematics)
2573+
# SpearmanrResult(correlation=0.6686960980480712, pvalue=0.03450954165178532)
2574+
# And Wolfram Alpha gives: 0.668696
2575+
# https://www.wolframalpha.com/input?i=SpearmanRho%5B%7B56%2C+75%2C+45%2C+71%2C+61%2C+64%2C+58%2C+80%2C+76%2C+61%7D%2C+%7B66%2C+70%2C+40%2C+60%2C+65%2C+56%2C+59%2C+77%2C+67%2C+63%7D%5D
2576+
reading = [56, 75, 45, 71, 61, 64, 58, 80, 76, 61]
2577+
mathematics = [66, 70, 40, 60, 65, 56, 59, 77, 67, 63]
2578+
self.assertAlmostEqual(statistics.correlation(reading, mathematics, by_rank=True),
2579+
0.6686960980480712)
2580+
25682581
class TestLinearRegression(unittest.TestCase):
25692582

25702583
def test_constant_input_error(self):

0 commit comments

Comments
 (0)