Yak*_*kov 3 algorithm math statistics uniform
你好假设我有一组数字,我想快速计算一些均匀度.我知道方差是最明显的答案,但我担心天真算法的复杂性太高任何人有什么建议吗?
用于计算方差的"直观"算法通常会遇到以下一种或两种情况:
一个好的算法,只有一个循环和数值稳定是由D. Knuth(一如既往).
n = 0
mean = 0
M2 = 0
def calculate_online_variance(x):
n = n + 1
delta = x - mean
mean = mean + delta/n
M2 = M2 + delta*(x - mean) # This expression uses the new value of mean
variance_n = M2/n
variance = M2/(n - 1) #note on the first pass with n=1 this will fail (should return Inf)
return variance
Run Code Online (Sandbox Code Playgroud)
您应该为每个点调用calculate_online_variance(x),并返回到目前为止计算的方差.