How to calculate sample and population variances in Matlab?

偶尔善良 提交于 2019-12-30 18:29:28

问题


I have a vector a

a = [86 100 41 93 75 61 76 92 88 97]

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

What do I miss here ?

why sum(a.^2)/length(a) - avg^2 != std(a)^2 ?


回答1:


Try this:

var = sum(a.^2)/(length(a)-1) - (length(a))*mean(a)^2/(length(a)-1)


var =

  335.2111

var is computed as (unbiased) sample, not population variance.

For a complete explanation you can read here.

From the matlab documentation,

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.

but

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).

so that

>> var(a,1)

ans =

  301.6900



回答2:


An unbiased sample variance is given by:

>> 1/(length(a)-1) * sum((a-mean(a)).^2)

ans =

  335.2111



来源:https://stackoverflow.com/questions/18134371/how-to-calculate-sample-and-population-variances-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!