Equivalence of Pearson correlation and cosine similarity

x = [5,12, 34, 6, 11, 16, 23, 35] y = [7, 16, 21, 9, 14, 12, 8, 31] def mean(x): return sum(x) / len(x) def center(x): return [xc - mean(x) for xc in x] def dot(x,y): return sum(xc*yc for xc, yc in zip(x,y)) def norm(v): return sum(vc**2 for vc in v) ** 0.5 def cosine_similarity(x,y): return dot(x,y) / (norm(x) * norm(y)) def pearson_correlation(x,y): rootx = sum(xc**2 for xc in center(x)) ** 0.5 rooty = sum(yc**2 for yc in center(y)) ** 0.5 return dot(center(x), center(y)) / (rootx * rooty) print(pearson_correlation(x, y) == cosine_similarity(center(x), center(y))) # Both vary between -1 and 1