-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats.py
60 lines (50 loc) · 1.42 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from __future__ import division
import math
class stats:
"""
A helper class for tracking the mean and variance
of a random variable.
"""
def __init__(self):
self.mean = 0
self.M2 = 0
self.n = 0
def add(self, dx):
# Wikipedia!
self.n += 1
delta = dx - self.mean
self.mean += delta / self.n
self.M2 += delta * (dx - self.mean)
def params(self):
if self.n < 2:
return (0, 0) # adhoc for our application
else:
return self.mean, math.sqrt(self.M2 / (self.n - 1))
def ci_mean(self):
if self.n < 2:
return (0, 0, 0) # adhoc for our application
else:
return (
self.mean
- 1.96 * math.sqrt(self.M2 / (self.n - 1)) / math.sqrt(self.n),
self.mean,
self.mean
+ 1.96 * math.sqrt(self.M2 / (self.n - 1)) / math.sqrt(self.n),
)
if __name__ == "__main__":
import random
N = 100000000
A = 1
B = 1000000
mu_, sigma_ = B + A / 2.0, A / 12 ** 0.5
s = stats()
for _ in range(0, N):
r = A * random.random() + B
s.add(r)
print("Confidence interval for true mean=%f" % mu_)
print(s.ci_mean())
print("Sample mean and stdev:")
mu, sigma = s.params()
print(mu, sigma)
print("Relative error:")
print((mu - mu_) / mu_, (sigma - sigma_) / sigma_)