问题描述
def m(x,w):
"""Weighted Mean"""
return np.sum(x * w) / np.sum(w)
def cov(x,y,w):
"""Weighted Covariance"""
return np.sum(w * (x - m(x,w)) * (y - m(y,w))) / np.sum(w)
def corr(x,w):
"""Weighted Correlation"""
return cov(x,w) / np.sqrt(cov(x,x,w) * cov(y,w))
这是我正在使用的数据
x = np.arange(78)
y = np.arange(21)
w = np.random.random((21,78)) # this is just an example matrix
# the key is to have a weight matrix that is of size (y.shape,x.shape)
现在,当我尝试计算加权相关时,我遇到了ValueError: operands Could not be broadcast together with shapes
。关于如何修改脚本以使其可用于这样的数据集的任何建议?
作为参考,我正在尝试实现标题Bayesian Replay Analysis
http://www.buzsakilab.com/content/PDFs/Grosmark2016Supp.pdf
解决方法
根据您提到的帖子中提供的Wikipedia链接(请参阅https://en.wikipedia.org/wiki/Pearson_correlation_coefficient#Weighted_correlation_coefficient),向量x
和y
的大小应该相同。