forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspearman_rank_correlation_coefficient.py
71 lines (52 loc) · 1.65 KB
/
spearman_rank_correlation_coefficient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def rankdata(arr):
"""
Assigns ranks to elements in the array.
"""
ranked = sorted((value, index) for index, value in enumerate(arr))
ranks = [0] * len(arr)
for pos, (_, index) in enumerate(ranked):
ranks[index] = pos + 1
return ranks
def spearman_rank_correlation(x, y):
"""
Calculates Spearman's rank correlation coefficient.
Example Usage :
>>> x = [1, 2, 3, 4, 5]
>>> y = [5, 4, 3, 2, 1]
>>> spearman_rank_correlation(x, y)
-1.0
>>> x = [1, 2, 3, 4, 5]
>>> y = [2, 4, 6, 8, 10]
>>> spearman_rank_correlation(x, y)
1.0
>>> x = [1, 2, 3, 4, 5]
>>> y = [5, 1, 2, 9, 5]
>>> spearman_rank_correlation(x, y)
0.6
"""
n = len(x)
rank_x = rankdata(x)
rank_y = rankdata(y)
# Calculate differences of ranks
d = [rx - ry for rx, ry in zip(rank_x, rank_y)]
# Calculate the sum of squared differences
d_squared = sum(di**2 for di in d)
# Calculate the Spearman's rank correlation coefficient
rho = 1 - (6 * d_squared) / (n * (n**2 - 1))
return rho
if __name__ == "__main__":
import doctest
doctest.testmod()
# Example usage:
x1 = [1, 2, 3, 4, 5]
y1 = [2, 4, 6, 8, 10]
rho1 = spearman_rank_correlation(x1, y1)
print(f"Spearman's rank correlation coefficient (Example 1): {rho1}")
x2 = [1, 2, 3, 4, 5]
y2 = [5, 4, 3, 2, 1]
rho2 = spearman_rank_correlation(x2, y2)
print(f"Spearman's rank correlation coefficient (Example 2): {rho2}")
x3 = [1, 2, 3, 4, 5]
y3 = [5, 1, 2, 9, 5]
rho3 = spearman_rank_correlation(x3, y3)
print(f"Spearman's rank correlation coefficient (Example 3): {rho3}")