-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspearman.py
50 lines (39 loc) · 1.45 KB
/
spearman.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
from transactions import *
def spearman_rank_correlation(x, y):
"""
Compute Spearman's Rank Correlation Coefficient between two lists.
:param x: First list of values.
:param y: Second list of values.
:return: Spearman's rank correlation coefficient.
"""
if len(x) != len(y):
raise ValueError("The two lists must have the same length.")
# Rank the elements in x and y
def rank_elements(values):
sorted_indices = sorted(range(len(values)), key=lambda i: values[i])
print(sorted_indices)
ranks = [0] * len(values)
for rank, index in enumerate(sorted_indices, start=1):
ranks[index] = rank
return ranks
rank_x = rank_elements(x)
rank_y = rank_elements(y)
# print("rankx:", rank_x)
# print(rank_y)
# Compute Spearman's rank correlation
n = len(x)
d_squared_sum = sum((rank_x[i] - rank_y[i])**2 for i in range(n))
spearman_rho = 1 - (6 * d_squared_sum) / (n * (n**2 - 1))
return spearman_rho
def correlation(transactions, deliver_based):
if deliver_based:
transactions.sort(key=lambda x: x.deliver_ID)
else:
transactions.sort(key=lambda x: x.ID)
list_ = [transaction.ID for transaction in transactions]
transactions.sort(key=lambda x: x.pos)
list1 = [transaction.ID for transaction in transactions]
# print(list)
# print(list1)
correlation = spearman_rank_correlation(list_, list1)
return correlation