-
Notifications
You must be signed in to change notification settings - Fork 0
/
Statistics.py
45 lines (34 loc) · 1005 Bytes
/
Statistics.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
from math import sqrt
class Statistics:
def __init__(self, points):
self._points = points[:]
def __iadd__(self, point):
if isinstance(point, (int, float)):
self._points += [point]
else:
self._points += point
return self
def __str__(self):
return str(self._points)
def __len__(self):
return len(self._points)
def __iter__(self):
yield from self._points
def mean(self):
return sum(self) / len(self)
def variance(self):
m = self.mean()
return sum((x - m)**2 for x in self._points) / len(self)
def std_deviation(self):
return sqrt(self.variance())
if __name__ == "__main__":
b = [3, 4, 5]
a = Statistics(b)
print(a)
a += 6
print(a)
a += [5, 6]
print(a, sum(a), a.mean(), a.variance(), a.std_deviation())
a = Statistics([1, 2, 3])
print(a, sum(a), a.mean(), a.variance(), a.std_deviation(), tuple(a))
print(sum(a))