Online algorithm for calculating standard deviation

后端 未结 2 1388
时光取名叫无心
时光取名叫无心 2020-12-18 12:25

Normally, I have a more technical problem but I will simplify it for you with an example of counting balls.

Assume I have balls of different colors and one index of

2条回答
  •  北海茫月
    2020-12-18 13:05

    Since average and standard deviation are calculated using sums, you can easily implement appropriate accumulators for these. Then when you want the actual values, finish the rest of the calculation (particularly, the divisions).

    The sum of squares is the tricky part since you increment one of the frequencies for each input. One way to deal with this is to maintain a count of each color seen so far (using an appropriate data structure). Then when you see a color in the input, you can subtract out its previous square and add the new square back in (or equivalently add the difference of the two squares to your accumulator).

    I'll leave it to the reader to implement the algorithm described here.

提交回复
热议问题