parent
46e5caa7a2
commit
02776c1e6a
1 changed files with 29 additions and 0 deletions
@ -0,0 +1,29 @@ |
||||
# $Id: avg.py,v 1.1 2010-10-07 15:56:55 wirawan Exp $ |
||||
# Create: 20090112 |
||||
|
||||
import math |
||||
|
||||
class avg(object): |
||||
def __init__(self): |
||||
self.clear() |
||||
def clear(self): |
||||
self.N = 0 |
||||
self.sum1 = 0.0 |
||||
self.sum2 = 0.0 |
||||
def add(self, value): |
||||
self.N += 1 |
||||
self.sum1 += value |
||||
self.sum2 += value*value |
||||
return self |
||||
#__add__ = add << HERETICAL!!! |
||||
def mean(self): |
||||
return self.sum1 / self.N |
||||
def stddev(self): |
||||
N = self.N |
||||
avg1 = self.sum1 / N |
||||
avg2 = self.sum2 / N |
||||
return math.sqrt(abs( (avg2 - avg1**2) * N / (N - 1) )) |
||||
|
||||
# Operator overloading: |
||||
avg.__iadd__ = avg.add |
||||
avg.__call__ = avg.mean |
Loading…
Reference in new issue