Numpy Pytorch:加权协方差

Numpy Pytorch:加权协方差,numpy,statistics,pytorch,covariance,Numpy,Statistics,Pytorch,Covariance,我试图实现一个PyTorch协方差矩阵算子。然而,我注意到Numpy实现和我的尝试之间的结果不一样,但我不理解为什么 我将贝塞尔校正加权协方差矩阵定义为: 我将加权平均数定义为: 我比较NumPy方法和我的方法如下: import numpy as np import torch torch.set_printoptions(precision=8) x = np.random.randn(1000, 3)*1000 w = np.abs(np.random.randn(1000))*1

我试图实现一个PyTorch协方差矩阵算子。然而,我注意到Numpy实现和我的尝试之间的结果不一样,但我不理解为什么

我将贝塞尔校正加权协方差矩阵定义为:

我将加权平均数定义为:

我比较NumPy方法和我的方法如下:

import numpy as np
import torch

torch.set_printoptions(precision=8)

x = np.random.randn(1000, 3)*1000
w = np.abs(np.random.randn(1000))*1000

x_torch = torch.DoubleTensor(x)
w_torch = torch.DoubleTensor(w)

#calculate weighted means
m_w = torch.sum(x_torch.T*w_torch, axis=1)/torch.sum(w_torch)
m_w_np = np.average(x, axis=0, weights=w)

#calculate weighted covariance matrix
Q = (x_torch-m_w).T
cov_w = (1.0 / (torch.sum(w_torch) - 1))*(w_torch*Q).mm(Q.T)
cov_w_np = np.cov(x.T, aweights=w.T)

print("WEIGHTED MEAN")
print("NUMPY = {0}\n\nTORCH = {1}\n\nDIFFERENCE={2}".format(m_w_np, m_w.numpy(), m_w_np-m_w.numpy())) 
print("")
print("")
print("WEIGHTED COVARIANCE")
print("NUMPY = {0}\n\nTORCH = {1}\n\nDIFFERENCE={2}".format(cov_w_np, cov_w.numpy(),cov_w_np-cov_w.numpy()))
这将产生以下输出:

WEIGHTED MEAN
NUMPY = [-21.10537208  -7.70801723  64.4034329 ]

TORCH = [-21.10537208  -7.70801723  64.4034329 ]

DIFFERENCE=[-7.10542736e-15 -1.77635684e-15  1.42108547e-14]


WEIGHTED COVARIANCE
NUMPY = [[ 989468.17457696   13620.54885133   10723.87790683]
         [  13620.54885133  953966.92486133   21407.69378841]
         [  10723.87790683   21407.69378841 1019646.81044077]]

TORCH = [[ 987952.51042915   13599.68493868   10707.45110536]
         [ 13599.68493868  952505.64141296   21374.90155234]
         [ 10707.45110536   21374.90155234 1018084.91875621]]

DIFFERENCE=[[1515.6641478    20.86391265   16.42680147]
            [  20.86391265 1461.28344838   32.79223607]
            [  16.42680147   32.79223607 1561.89168456]]