为什么 MATLAB 本机函数 cov(协方差矩阵计算)使用与我预期不同的除数?
假设给定 M 维和 N 个样本的数据矩阵数据,
data = randn(N, M);
我可以计算协方差矩阵:
data_mu = data - ones(N, 1)*mean(data);
cov_matrix = (data_mu'*data_mu)./N
如果我使用本机 MATLAB 函数,
cov_matrix2 = cov(data)
则该协方差矩阵将始终等于
cov_matrix = (data_mu'*data_mu)./(N-1)
即,分母为 (N - 1) 减一。
为什么??你能重现它吗?这是一个错误吗?
我使用 MATLAB 版本 7.6.0.324 (2008)。
Given a data matrix data of M dimensions and N samples, say,
data = randn(N, M);
I could compute the covariance matrix with
data_mu = data - ones(N, 1)*mean(data);
cov_matrix = (data_mu'*data_mu)./N
If I use the native MATLAB function
cov_matrix2 = cov(data)
this will always be equal to
cov_matrix = (data_mu'*data_mu)./(N-1)
That is, the denominator is (N - 1) is one less.
Why?? Can you reproduce it? Is this a bug??
I use MATLAB version 7.6.0.324 (2008).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅 cov 文档。它与总体方差与样本方差有关。
另请注意,如果您希望使用分母 N 而不是 N-1,则可以在调用中添加尾随
1
参数,即cov(x,y,1)
或cov(x,1)
根据文档。See the cov documentation. It has to do with population variance vs. sample variance.
Note also that if you wish to use the denominator N instead of N-1, you can add a trailing
1
argument to the call, i.e.cov(x,y,1)
orcov(x,1)
as per the documentation.n-1 是计算方差时使用的正确分母。
这就是所谓的贝塞尔校正 (http://en.wikipedia.org/wiki/Bessel%27s_ Correction)
简而言之,1/(n-1) 产生的方差预期估计比 1/n 更准确。
n-1 is the correct denominator to use in computation of variance.
It is what's known as Bessel's correction (http://en.wikipedia.org/wiki/Bessel%27s_correction)
Simply put, 1/(n-1) produces a more accurate expected estimate of the variance than 1/n.