Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Author: denis.engemann@gmail.com
# License: simplified BSD (3 clause)
# Note: code is based on scipy.stats.pearsonr
from scipy import stats
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(stats.ss(xm, axis=-1) * stats.ss(ym, axis=-1))
r = r_num / r_den
return r
@endrebak
Copy link

endrebak commented Nov 26, 2019

ss has been removed from scipy, but it just did this:

def ss(a, axis):
    return np.sum(a * a, axis=axis)

Thanks for the function btw :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment