这种计算方差的算法正确吗?

Is this algorithm for calculating the variance correct?

本文关键字:算法 种计 计算 方差      更新时间:2023-10-16

我在v_samples(1.4, 2.21, 4.21, 2.1, 5.1)中有以下数字

我得到偏差= 2.45122

但是当我查看一些偏差计算器网站时。他们都给了我不同的答案。

double variance(){
        double variance =0 ;
        double average = mean();
        double size = v_samples.size();
        for (size_t i = 0, max = size; i != max; ++i){
            variance += (v_samples[i]-average) * (v_samples[i]-average) / size;
        }
        return variance;
    }

编辑:variance not deviance

edit: mean()返回2.804

edit: mean现在返回正确的值3.004

样本方差现在返回1.97362。

1.4, 2.21, 4.21, 2.1, 5.1
15.02

,意思是 3.004

如果你得到的平均值是2.804

 2.804 * 5 = 14.02
15.02

14.02 是1。你有一个错误的地方:)

它应该工作,所以你在mean()有问题

你的在线资料中的混淆几乎肯定是在"样本方差"answers"偏差校正样本方差"之间。后者需要用size-1代替size

http://mathworld.wolfram.com/Variance.html