I don't know a single Python package that implements this, but it should be pretty simple to collapse your own implementation. Using wikipedia article naming conventions:
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, y, w):
"""Weighted Correlation"""
return cov(x, y, w) / np.sqrt(cov(x, x, w) * cov(y, y, w))
, . , @Alberto Garcia-Raboso, m(x, w) np.average(x, weights=w), .
, . , , , .. x = np.asarray(x), , . , ..
:
np.random.seed([3,1415])
n = 10**6
df = pd.DataFrame({
'x': np.random.choice(3, size=n),
'y': np.random.choice(4, size=n),
'w': np.random.random(size=n)
})
r = corr(df['x'], df['y'], df['w'])
p-. , , .