计算在线标准差

时间:2013-06-23 17:42:21

标签: python algorithm statistics standard-deviation

我想计算运行时系统中每个值的标准偏差。

我不确定如何实施它。

我实现了在线平均值

def online_avg(last_avg, last_N, new_val):
    return ((last_avg*last_N)+new_val)/(last_N+1)

我希望在线标准偏差的原型如下:

def online_std(last_avg, last_N, last_std, new_val):
    pass

1 个答案:

答案 0 :(得分:0)

@unutbu在StackOverflow中提到了一些问题。

我在答案中提到了答案:

def online_avg(last_avg, last_N, new_val):
    return ((last_avg*last_N)+new_val)/(last_N+1)

def online_std(last_avg, last_N, last_std, new_val):
    if last_N == 0:
        return 0
    new_avg = online_avg(last_avg, last_N, new_val)
    new_std = last_std + (new_val - last_avg)*(new_val - new_avg)
    return new_std