2014-01-09 46 views

回答

7

在numpy的,cov默認爲1,而var默認「自由的增量程度」來的0。從筆記ddof到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. 

所以,你可以讓他們通過採取一致認爲:

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 
+0

比我更清晰的答案還值得一提的是,基本上所有其他numpy函數默認爲'ddof = 0'。 'numpy.cov'是個例外,可能是出於歷史原因。 –

+0

謝謝你們兩位!這確實令人困惑。在Excel中,我們有一個用於不同目的的人口版本和樣本版本,這更清晰。 – zsljulius

相關問題