-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsigmarunner.py
59 lines (48 loc) · 1.29 KB
/
sigmarunner.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from math import *
class sigmarunner:
def __init__(self):
self.n = 0
self.mean = 0.
self.s = 0.
self.mold = 0.
self.sold = 0.
def push(self, x):
self.n += 1
if self.n == 1:
self.mold = x
self.mean = x
else:
self.mean = self.mold + (x-self.mold)/(self.n+0.)
self.s = self.sold + (x-self.mold)*(x-self.mean)
self.mold = self.mean
self.sold = self.s
def variance(self):
return self.n > 1 and (self.s/(self.n-1.)) or 0.
def stdev(self):
return sqrt(self.variance())
def marginerror95(self):
return 1.96*self.stdev()/sqrt(self.n)
def conflo95(self):
return self.mean-self.marginerror95()
def confhi95(self):
return self.mean+self.marginerror95()
def __str__(self):
return "n=%d mean=%g stdev=%g E=%g" % (self.n, self.mean, self.stdev(), self.marginerror95())
#def __repr__(self):
#return "n=%d mean=%g stdev=%g" % (self.n, self.mean, self.stdev())
if __name__=='__main__':
s = sigmarunner()
s.push(4)
print(s)
s.push(4)
print(s)
s.push(4)
print(s)
s.push(4)
print(s)
s.push(4)
print(s)
s.push(5)
print(s)
s.push(6)
print(s)