-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.py
35 lines (28 loc) · 1.02 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
from typing import Any, Dict, Iterable, List, Tuple
def streaming_statistics(stream: Iterable) -> List[Dict[str, Any]]:
'''
Calculates cumulative arithmetic mean and Standard Deviation over the numbers in a stream passed in
There is actually `statistics.stdev` in standard Python 3.4+ library, that could be used as well.
'''
stats = []
cum_sum = 0.0
cum_len = 0
cum_items = []
mean = None
std = None
for item in stream:
cum_items.append(item)
# I could as well use list comprehension instead of cumulatively adding the values, but this should be faster
cum_sum += item
cum_len += 1
if cum_len > 1:
# Calculate arithmetic mean
mean = cum_sum / cum_len
# Sum of all deviations, i.e. squared difference of the numbers less the mean
deviations = sum((n - mean) ** 2 for n in cum_items)
# Variance is 1/length * sum of deviations
variance = deviations / (cum_len - 1)
# And Standard Deviation is Square-root of Variance
std = variance ** 0.5
stats.append({'mean': mean, 'std': std})
return stats