-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStats.py
More file actions
executable file
·125 lines (103 loc) · 2.93 KB
/
Stats.py
File metadata and controls
executable file
·125 lines (103 loc) · 2.93 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import math
class Stats:
def __init__(self):
self.clear()
def append(self, val):
"""Append a value to the data set.
"""
val = float(val)
self._n += 1
self._sum += val
self._sumSq += val*val
if self._minVal is None or val < self._minVal:
self._minVal = val
if self._maxVal is None or val > self._maxVal:
self._maxVal = val
def clear(self):
"""Clear the data.
"""
self._n = 0.0
self._sum = 0.0
self._sumSq = 0.0
self._minVal = None
self._maxVal = None
def min(self):
"""Return the minimum value.
"""
self._checkPoints()
return self._minVal
def max(self):
"""Return the maximum value.
"""
self._checkPoints()
return self._maxVal
def mean(self):
"""Return the mean value.
"""
self._checkPoints()
return self._sum / float(self._n)
def nPoints(self):
"""Return the number of points.
"""
return self._n
def var(self):
"""Return the variance:
(sum(val^2) - mean^2) / N-1
"""
self._checkPoints(2)
return (self._sumSq - (self.mean()**2)) / float(self._n - 1)
def stdDev(self):
"""Return the stanard deviation: sqrt(var).
"""
return math.sqrt(self.var())
def rms(self):
"""Return the RMS value.
"""
self._checkPoints()
return math.sqrt(self._sumSq / float(self._n))
def _checkPoints(self, minReq=1):
"""Check that we have enough data points.
Raise a RuntimeError if we do not.
"""
if self._n < minReq:
if minReq <= 1:
raise RuntimeError("No data points")
raise RuntimeError(
"Need at least %d data points, but only have %d" % (minReq, self._n)
)
if __name__ == "__main__":
s = Stats()
NaN = float("NaN")
def printRes():
try:
minVal = s.min()
except RuntimeError:
minVal = NaN
try:
maxVal = s.max()
except RuntimeError:
maxVal = NaN
try:
mean = s.mean()
except RuntimeError:
mean = NaN
try:
stdDev = s.stdDev()
except RuntimeError:
stdDev = NaN
try:
var = s.var()
except RuntimeError:
var = NaN
try:
rms = s.rms()
except RuntimeError:
rms = NaN
print ("%2d %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f" % \
(s.nPoints(), minVal, maxVal, mean, stdDev, var, rms))
print (" N min max mean stdDev var RMS")
testVals = (1, 2, 3, 4, 5, 6, 7, 8, 9)
printRes()
for val in testVals:
s.append(val)
printRes()