为什么 MATLAB 本机函数 cov(协方差矩阵计算)使用与我预期不同的除数?

发布于 2024-09-09 06:28:14 字数 455 浏览 2 评论 0原文

假设给定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

药祭#氼 2024-09-16 06:28:14

即分母为(N - 1) 减一。
为什么??你能重现它吗?这是一个错误吗??

请参阅 cov 文档。它与总体方差与样本方差有关。

另请注意,如果您希望使用分母 N 而不是 N-1,则可以在调用中添加尾随 1 参数,即 cov(x,y,1)cov(x,1) 根据文档。

That is, the denominator is (N - 1) is one less.
Why?? Can you reproduce it? Is this a bug??

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) or cov(x,1) as per the documentation.

旧城烟雨 2024-09-16 06:28:14

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文