numpy中的加权协方差矩阵 [英] weighted covariance matrix in numpy
问题描述
我想计算p
个数量的n
个测量值的协方差C
,其中每个单独的数量测量值都有自己的权重.也就是说,我的权重数组W
与我的数量数组Q
具有相同的形状(n
除以p
).原生np.cov()
函数仅支持赋予各个度量的权重(即长度为n
的向量).
I want to compute the covariance C
of n
measurements of p
quantities, where each individual quantity measurement is given its own weight. That is, my weight array W
has the same shape as my quantity array Q
(n
by p
). The native np.cov()
function only supports weights given to individual measurements (i.e., a vector of length n
).
我可以通过p
矩阵初始化p
并进行迭代,但是如果p
很大,那么这将是一个非常缓慢的过程.
I can initialize a p
by p
matrix and iterate, but if p
is large, then it's a very slow process.
由于已知Q
对于每个量(Q
的列)均值为零,所以C
的每个元素的显式公式为
Since Q
is known to have mean zero for each quantity (column of Q
), the explicit formula for each element of C
is
C[i,j] = np.sum(
Q[:, i] * Q[:, j] * W[:, i] * W[:, j]) / np.sum(W[:, i] * W[:, j])
如果我将分子重新设置为Q[:, i] * W[:, i] * Q[:, j] * W[:, j]
,看来我应该能够对Q * W
的列进行乘法和求和,然后类似地进行分母(使用W * W
除外).
If I rearrange the numerator to be Q[:, i] * W[:, i] * Q[:, j] * W[:, j]
, it seems like I should be able to multiply and sum columns of Q * W
, and then do the denominator similarly (except using W * W
).
有没有办法用np.einsum()
做到这一点?
Is there a way to do this with np.einsum()
?
对于测试,让我们定义以下内容:
For testing, let's define the following:
C = array([[ 1. , 0.1 , 0.2 ], # set this beforehand, to test whether
[ 0.1 , 0.5 , 0.15], # we get the correct result
[ 0.2 , 0.15, 0.75]])
Q = array([[-0.6084634 , 0.16656143, -1.04490324],
[-1.51164337, -0.96403094, -2.37051952],
[-0.32781346, -0.19616374, -1.32591578],
[-0.88371729, 0.20877833, -0.52074272],
[-0.67987913, -0.84458226, 0.02897935],
[-2.01924756, -0.51877396, -0.68483981],
[ 1.64600477, 0.67620595, 1.24559591],
[ 0.82554885, 0.14884613, -0.15211434],
[-0.88119527, 0.11663335, -0.31522598],
[-0.14830668, 1.26906561, -0.49686309]])
W = array([[ 1.01133857, 0.91962164, 1.01897898],
[ 1.09467975, 0.91191381, 0.90150961],
[ 0.96334661, 1.00759046, 1.01638749],
[ 1.04827001, 0.95861001, 1.01248969],
[ 0.91572506, 1.09388218, 1.03616461],
[ 0.9418178 , 1.07210878, 0.90431879],
[ 1.0093642 , 1.00408472, 1.07570172],
[ 0.92203074, 1.00022631, 1.09705542],
[ 0.99775598, 0.01000000, 0.94996408],
[ 1.02996389, 1.01224303, 1.00331465]])
推荐答案
You can use the very efficient matrix-multiplication with np.dot
-
QW = Q*W
C = QW.T.dot(QW)/W.T.dot(W)
这篇关于numpy中的加权协方差矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!