为啥手动计算的标准差和std()的结果不一样?

tl1=[1,2,2,3,3,3,4,4,5]
tl2=tl1

sum_t1=sum(tl1)
meam_t1=sum_t1/length(tl1)

for i=1:length(tl1)
    tl2[i]=(tl1[i]-meam_t1)^2
end

sum_t2=sum(tl2)
sig=√(sum_t2/length(tl1))

println("手动sig:",sig)

using Statistics

println("sig:",std(tl1))

之前试过,好像是在哪里差了个1还是差了一个元素?现在没印象了。应该是手动计算的结果正确吧。std()是哪的问题呢?

help里有写The algorithm returns an estimator of the generative distribution's standard deviation under the assumption that each entry of itr is an IID drawn from that generative distribution.
差1什么的应该说的是If corrected is true, then the sum is scaled with n-1, whereas the sum is scaled with n if corrected is false with n the number of elements in itr.

julia> itr=[1,2,2,3,3,3,4,4,5]
...

julia> sqrt(sum((itr .- mean(itr)).^2) / (length(itr) - 1))
1.224744871391589

julia> std(itr)
1.224744871391589

julia> sqrt(sum((itr .- mean(itr)).^2) / length(itr))
1.1547005383792515

julia> std(itr,corrected=false)
1.1547005383792515

用std 算的是除以 n-1(这样估计方差是无偏的), 你手动算的是除以 n 的吧。

是差在这了,谢啦

备案号:京ICP备17009874号-2