from scipy.stats import pearsonr, spearmanr
x1 = [4, 5, 6, 7]
y1 = [5, 6, 7, 8]
p1 = pearsonr(x1, y1)
print('Test case 1 pearson coeficient: \033[1m{0:.8}.'.format(p1[0]))
Test case 1 pearson coeficient: 1.0.
x2 = [9, 8, 7, 6]
y2 = [8, 7, 6, 5]
p2 = pearsonr(x2, y2)
print('Test case 2 pearson coeficient: \033[1m{0:.8}.'.format(p2[0]))
Test case 2 pearson coeficient: 1.0.
x3 = [48, 84, 39, 54]
y3 = [84, 39, 54, 77]
p3 = pearsonr(x3, y3)
print('Test case 3 pearson coeficient: \033[1m{0:.8}.'.format(p3[0]))
Test case 3 pearson coeficient: -0.58913542.
x4 = [86, 97, 99, 100, 101, 103, 106, 110, 112, 113]
y4 = [0, 20, 28, 27, 50, 29, 7, 17, 6, 12]
p4 = pearsonr(x4, y4)
print('Test case 4 pearson coeficient: \033[1m{0:.8}.'.format(p4[0]))
Test case 4 pearson coeficient: -0.037601474.
data = [93, 76, 18, 38, 27, 48, 22, 73, 54, 68]
n = len(data)
window = 5
for i in range(n-window-1):
x = data[i:i+window]
y = data[i+1:i+window+1]
p = pearsonr(x, y)
print('Test case ', i, '- x=[', *x, '] y=[', *y, '] r=', p[0])
Test case 0 - x=[ 93 76 18 38 27 ] y=[ 76 18 38 27 48 ] r= 0.34670807201 Test case 1 - x=[ 76 18 38 27 48 ] y=[ 18 38 27 48 22 ] r= -0.821747728214 Test case 2 - x=[ 18 38 27 48 22 ] y=[ 38 27 48 22 73 ] r= -0.701175297732 Test case 3 - x=[ 38 27 48 22 73 ] y=[ 27 48 22 73 54 ] r= -0.233376493759
x5 = [86, 97, 99, 100, 101, 103, 106, 110, 112, 113]
y5 = [0, 20, 28, 27, 50, 29, 7, 17, 6, 12]
p5 = pearsonr(x5, y5)
s = spearmanr(x5, y5)
print('Test case from Wikipedia Article PEARSON coeficient: \033[1m{0:.8}.'.format(p5[0]))
print('Test case from Wikipedia Article SPEARMAN coeficient: \033[1m{0:.8}.'.format(s[0]))
Test case from Wikipedia Article PEARSON coeficient: -0.037601474. Test case from Wikipedia Article SPEARMAN coeficient: -0.17575758.