-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathstats.py
More file actions
64 lines (52 loc) · 1.34 KB
/
stats.py
File metadata and controls
64 lines (52 loc) · 1.34 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
'''
stats.py
Python module with functions for calculating common statistical measures
'''
from collections import Counter
def mean(numbers):
s = sum(numbers)
N = len(numbers)
mean = s/N
return mean
def median(numbers):
# find the numnber of items
N = len(numbers)
# sort the list in ascending order
numbers.sort()
# find the median
if N % 2 == 0:
# if N is even
m1 = N/2
m2 = (N/2) + 1
# convert to integer, match position
m1 = int(m1) - 1
m2 = int(m2) - 1
median = (numbers[m1] + numbers[m2])/2
else:
m = (N+1)/2
# convert to integer, match position
m = int(m) - 1
median = numbers[m]
return median
def mode(numbers):
c = Counter(numbers)
mode = c.most_common(1)
return mode[0][0]
def find_differences(numbers):
m = mean(numbers)
# find the differences from the mean
diff = []
for num in numbers:
diff.append(num-m)
return diff
def variance_sd(numbers):
# find the list of differences
diff = find_differences(numbers)
# find the squared differences
squared_diff = []
for d in diff:
squared_diff.append(d**2)
# find the variance
sum_squared_diff = sum(squared_diff)
variance = sum_squared_diff/len(numbers)
return variance, variance**0.5