-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscaled_correlation.py
More file actions
31 lines (24 loc) · 847 Bytes
/
scaled_correlation.py
File metadata and controls
31 lines (24 loc) · 847 Bytes
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
from scipy import stats
# Consider these two arrays
x = [2, 1, 3, 4, 5, 6, 7, 8]
y = [2, 1, 13, 14, 1, 2, 3, 4]
# They are not strongly correlated
coefficient, _ = stats.pearsonr(x, y)
print(coefficient)
# Here is a function to compute scaled correlation between two variables
def scaled_correlation(a, b, scale):
sum_correlations = 0
count = 0
for i in range(len(a)//scale):
r, _ = stats.pearsonr(
a[i * scale: (i + 1) * scale],
b[i * scale: (i + 1) * scale]
)
sum_correlations += r
count += 1
return sum_correlations/count
# At this shorter scale correlation is strong
print(scaled_correlation(x, y, 4))
# And when the scale is even shorter, each individual segment is perfectly correlated
# and so is thus the overall correlation
print(scaled_correlation(x, y, 2))