-
Notifications
You must be signed in to change notification settings - Fork 4
/
mean_sigma.py
44 lines (35 loc) · 841 Bytes
/
mean_sigma.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
#!/usr/bin/env python
import math
import sys
n = mean = m2 = 0
def welford(x):
global n
global mean
global m2
n += 1
delta = x - mean
mean += delta/n
delta2 = x - mean
m2 += delta*delta2
# get filename
if sys.argv > 0:
filename = sys.argv[1]
file = open(filename,"r")
else:
file = sys.stdin
for line in file.readlines():
try:
value = float(line)
welford(value)
except:
pass
if n < 2:
print "Too vew samples to compute standard deviation"
print "Mean = %f" % mean
else:
print "Population Variance = %f" % (m2 / n)
print "Population std dev = %f" % math.sqrt(m2 / n)
print "Sample Variance = %f" % (m2 / (n-1))
print "Sample std dev = %f" % math.sqrt(m2 / (n-1))
print "Mean = %f" % mean
file.close()