Skip to content

Commit 3773233

Browse files
cyrixninjapre-commit-ci[bot]cclauss
authored andcommitted
Create Spearman's rank correlation coefficient (TheAlgorithms#11155)
* Create spearman_rank_correlation_coefficient.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fixed Issues * Added More Description * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fixed Issues * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Tried Fixing Issues * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Tried Fixing Issues * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fixed Issues * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fixed Issues * Apply suggestions from code review * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update maths/spearman_rank_correlation_coefficient.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 2c42b73 commit 3773233

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from collections.abc import Sequence
2+
3+
4+
def assign_ranks(data: Sequence[float]) -> list[int]:
5+
"""
6+
Assigns ranks to elements in the array.
7+
8+
:param data: List of floats.
9+
:return: List of ints representing the ranks.
10+
11+
Example:
12+
>>> assign_ranks([3.2, 1.5, 4.0, 2.7, 5.1])
13+
[3, 1, 4, 2, 5]
14+
15+
>>> assign_ranks([10.5, 8.1, 12.4, 9.3, 11.0])
16+
[3, 1, 5, 2, 4]
17+
"""
18+
ranked_data = sorted((value, index) for index, value in enumerate(data))
19+
ranks = [0] * len(data)
20+
21+
for position, (_, index) in enumerate(ranked_data):
22+
ranks[index] = position + 1
23+
24+
return ranks
25+
26+
27+
def calculate_spearman_rank_correlation(
28+
variable_1: Sequence[float], variable_2: Sequence[float]
29+
) -> float:
30+
"""
31+
Calculates Spearman's rank correlation coefficient.
32+
33+
:param variable_1: List of floats representing the first variable.
34+
:param variable_2: List of floats representing the second variable.
35+
:return: Spearman's rank correlation coefficient.
36+
37+
Example Usage:
38+
39+
>>> x = [1, 2, 3, 4, 5]
40+
>>> y = [5, 4, 3, 2, 1]
41+
>>> calculate_spearman_rank_correlation(x, y)
42+
-1.0
43+
44+
>>> x = [1, 2, 3, 4, 5]
45+
>>> y = [2, 4, 6, 8, 10]
46+
>>> calculate_spearman_rank_correlation(x, y)
47+
1.0
48+
49+
>>> x = [1, 2, 3, 4, 5]
50+
>>> y = [5, 1, 2, 9, 5]
51+
>>> calculate_spearman_rank_correlation(x, y)
52+
0.6
53+
"""
54+
n = len(variable_1)
55+
rank_var1 = assign_ranks(variable_1)
56+
rank_var2 = assign_ranks(variable_2)
57+
58+
# Calculate differences of ranks
59+
d = [rx - ry for rx, ry in zip(rank_var1, rank_var2)]
60+
61+
# Calculate the sum of squared differences
62+
d_squared = sum(di**2 for di in d)
63+
64+
# Calculate the Spearman's rank correlation coefficient
65+
rho = 1 - (6 * d_squared) / (n * (n**2 - 1))
66+
67+
return rho
68+
69+
70+
if __name__ == "__main__":
71+
import doctest
72+
73+
doctest.testmod()
74+
75+
# Example usage:
76+
print(
77+
f"{calculate_spearman_rank_correlation([1, 2, 3, 4, 5], [2, 4, 6, 8, 10]) = }"
78+
)
79+
80+
print(f"{calculate_spearman_rank_correlation([1, 2, 3, 4, 5], [5, 4, 3, 2, 1]) = }")
81+
82+
print(f"{calculate_spearman_rank_correlation([1, 2, 3, 4, 5], [5, 1, 2, 9, 5]) = }")

0 commit comments

Comments
 (0)