0

I want to calculate the Standard Deviation for every value which comes to the system in run time.

I'm not sure how to implement it.

I implemented the online average

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

I want the prototype of the online standard deviation to be as:

def online_std(last_avg, last_N, last_std, new_val):
    pass
Bush
  • 2,433
  • 5
  • 34
  • 57
  • 1
    http://stackoverflow.com/questions/1174984/how-to-efficiently-calculate-a-running-standard-deviation (already solved, different ways) , http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm (all about variance, including such an algorithm) – user2246674 Jun 23 '13 at 17:44
  • thanks @unutbu - The referenced article in the answer is well explained. – Bush Jun 23 '13 at 18:08

1 Answers1

0

@unutbu mentioned some question in StackOverflow.

I found the answer in the article mentioned in the answer there:

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
Bush
  • 2,433
  • 5
  • 34
  • 57