-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.py
41 lines (35 loc) · 1.45 KB
/
stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from math import sqrt
class Stats:
def __init__(self):
self.z = 0.0 # location parameter
self.sum = 0.0
self.sumSq = 0.0
self.n = 0
self.min = float('inf')
self.max = float('-inf')
def add(self, x):
if self.n < 2:
if self.n == 0:
self.z = x
else:
# get closer to center of distribution
self.z = 0.5 * (self.z + x)
self.sumSq = 0.5 * x * x
else:
xz = x - self.z # shift around location
self.sum += xz
self.sumSq += xz * xz
self.n += 1
self.min = min(self.min, x)
self.max = max(self.max, x)
def calcMean(self):
mean, sigma, error = self.z, 0.0, 0.0
if self.n > 0:
mean = self.sum / self.n + self.z
variance = (self.sumSq - self.sum * self.sum / self.n) / self.n
sigma = sqrt(variance)
error = sigma / sqrt(self.n)
return mean, sigma, error
def __str__(self):
mean, sigma, error = self.calcMean()
return '%.15f +/- %.15f [%d]' % (mean, error, self.n)