Skip to content

Instantly share code, notes, and snippets.

@endrebak
Forked from dengemann/pearson_vectors.py
Last active November 26, 2019 13:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endrebak/cbb7b8c69ace82a235cc657c7e71242b to your computer and use it in GitHub Desktop.
Save endrebak/cbb7b8c69ace82a235cc657c7e71242b to your computer and use it in GitHub Desktop.
# Author: denis.engemann@gmail.com
# License: simplified BSD (3 clause)
# Note: code is based on scipy.stats.pearsonr
def ss(a, axis):
return np.sum(a * a, axis=axis)
def compute_corr(x, y):
x = np.asarray(x)
y = np.asarray(y)
mx = x.mean(axis=-1)
my = y.mean(axis=-1)
xm, ym = x - mx[..., None], y - my[..., None]
r_num = np.add.reduce(xm * ym, axis=-1)
r_den = np.sqrt(ss(xm, axis=-1) * ss(ym, axis=-1))
r = r_num / r_den
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment