如何在Matlab中计算样本和总体方差? [英] How to calculate sample and population variances in Matlab?
问题描述
我有一个向量a
a = [86 100 41 93 75 61 76 92 88 97]
我想通过以下方式计算std
和mean
我自己:
And I want to calculate the std
and mean
by
myself:
>> mean(a)
ans =
80.9000
>> std(a)^2
ans =
335.2111
但是当我这样做时,我得到了错误的方差:
But when I do it like that I get wrong variance:
>> avg = mean(a)
avg =
80.9000
>> var = sum(a.^2)/length(a) - avg^2
var =
301.6900
我在这里想念什么?
为什么sum(a.^2)/length(a) - avg^2 != std(a)^2
吗?
推荐答案
尝试一下:
var = sum(a.^2)/(length(a)-1) - (length(a))*mean(a)^2/(length(a)-1)
var =
335.2111
var
计算为(无偏)样本,而不是总体方差.
var
is computed as (unbiased) sample, not population variance.
有关完整的解释,您可以在此处阅读.
For a complete explanation you can read here.
从matlab文档中,
From the matlab documentation,
VAR通过N-1归一化Y,其中N是样本大小.这是一 X是的总体的方差的无偏估计量 绘制,只要X由独立且分布均匀的 样本.
VAR normalizes Y by N-1, where N is the sample size. This is an unbiased estimator of the variance of the population from which X is drawn, as long as X consists of independent, identically distributed samples.
但是
Y = VAR(X,1)通过N归一化并产生第二个矩 关于其均值的样本. VAR(X,0)与VAR(X)相同.
Y = VAR(X,1) normalizes by N and produces the second moment of the sample about its mean. VAR(X,0) is the same as VAR(X).
这样
>> var(a,1)
ans =
301.6900
这篇关于如何在Matlab中计算样本和总体方差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!