From 02776c1e6ac9832755e9f972c9b9eb467f26f1b6 Mon Sep 17 00:00:00 2001 From: wirawan Date: Thu, 7 Oct 2010 15:56:55 +0000 Subject: [PATCH] * Moving pyqmc.stats.avg to wpylib.math.stats.avg . --- math/stats/avg.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 math/stats/avg.py diff --git a/math/stats/avg.py b/math/stats/avg.py new file mode 100644 index 0000000..2d36ffe --- /dev/null +++ b/math/stats/avg.py @@ -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