这种计算方差的算法正确吗?
Is this algorithm for calculating the variance correct?
我在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
。
相关文章:
- 为什么一种算法在相同的时间复杂度下比另一种算法更快?
- 是否有一种 STL 算法可以最后找到,但它也适用于指针?
- C++/STL 我应该使用哪种算法来检查容器是否有重复项?
- 字符串::find在c++中使用了哪种算法
- 如何计算摘要/将使用哪种算法?
- 我是 c++ 的新手,有没有一种算法可以找到 3d 数组中最接近的 0 的距离?
- 有没有一种算法可以将 LAPACK 排列更改为真正的排列?
- 为什么"quick sorting"算法的这两种变体在性能上差异如此之大?
- 这是河内算法的递归塔是一种不知情的搜索
- 有没有一种方法可以使用弗洛伊德-沃歇尔算法给出最短路径,其中存在负权重循环而不允许重叠边缘?
- 如何测试两种算法并确定哪种算法更快
- 如何编写一种算法,该算法在哪个行中显示出在哪个行中显示(我使用的是std :: map)
- 了解一种神秘地起作用的递归二进制搜索算法
- 寻找一种单一的定时方法来测试各种算法,不包括它们的输入
- 如何找出哪种算法最快
- 两种算法的Big-O分析
- 这两种算法中有效的一种算法
- 哪种算法需要"visitor"(提升库中的术语)?
- CCACHE中使用了哪种缓存算法
- 是否有一种算法可以查找字符串中任何指定字符的第一次出现