How to calculate sample and population variances in Matlab?

后端 未结 2 1517
心在旅途
心在旅途 2021-01-18 02:21

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

相关标签:
2条回答
  • 2021-01-18 02:44

    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
    
    0 讨论(0)
  • 2021-01-18 02:52

    An unbiased sample variance is given by:

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

    enter image description here

    0 讨论(0)
提交回复
热议问题