为什么numpy cov对角元素和var函数具有不同的值? [英] Why do numpy cov diagonal elements and var functions have different values?
本文介绍了为什么numpy cov对角元素和var函数具有不同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
In [127]: x = np.arange(2)
In [128]: np.cov(x,x)
Out[128]:
array([[ 0.5, 0.5],
[ 0.5, 0.5]])
In [129]: x.var()
Out[129]: 0.25
为什么这是行为?我相信协方差矩阵对角线元素应该是级数的方差.
Why is this the behavior? I believe that covariance matrix diagonal elements should be the variance of the series.
推荐答案
在numpy中,cov
的默认"delta自由度"为1,而var
的默认ddof为0. c2>
In numpy, cov
defaults to a "delta degree of freedom" of 1 while var
defaults to a ddof of 0. From the notes to numpy.var
Notes
-----
The variance is the average of the squared deviations from the mean,
i.e., ``var = mean(abs(x - x.mean())**2)``.
The mean is normally calculated as ``x.sum() / N``, where ``N = len(x)``.
If, however, `ddof` is specified, the divisor ``N - ddof`` is used
instead. In standard statistical practice, ``ddof=1`` provides an
unbiased estimator of the variance of a hypothetical infinite population.
``ddof=0`` provides a maximum likelihood estimate of the variance for
normally distributed variables.
因此,您可以通过以下方式使他们同意:
So you can get them to agree by taking:
In [69]: cov(x,x)#defaulting to ddof=1
Out[69]:
array([[ 0.5, 0.5],
[ 0.5, 0.5]])
In [70]: x.var(ddof=1)
Out[70]: 0.5
In [71]: cov(x,x,ddof=0)
Out[71]:
array([[ 0.25, 0.25],
[ 0.25, 0.25]])
In [72]: x.var()#defaulting to ddof=0
Out[72]: 0.25
这篇关于为什么numpy cov对角元素和var函数具有不同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文