-
Notifications
You must be signed in to change notification settings - Fork 0
/
normalstats2.py
90 lines (61 loc) · 2 KB
/
normalstats2.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
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
#!/usr/local/bin/python3
#############################################################################################
# Program by Mohammed Faisal Khan #
# Email: faisalkhan91@outlook.com #
# Date: 8/10/2019 #
#############################################################################################
# Importing system module
import random
import math
def mean(numbers, num):
sum = 0
for val in numbers:
sum += val
return sum / num
def stddev(numbers, mean, num):
sum = 0
for val in numbers:
sum += (val - mean) ** 2
return math.sqrt(sum / num)
def median(numbers, num):
numbers.sort()
return numbers[num // 2]
def mode(numbers, num):
modval = []
modcount = 0
index = 0
while index < num:
c = 1
while index+c < num and numbers[index] == numbers[index+c]:
c += 1
if c == modcount:
modval.append(numbers[index])
elif c > modcount:
modcount = c
modval = [numbers[index]]
index += c
return modval
def getsample(numbers, num):
i = 0
while i < num:
sample = random.normalvariate(75, 50)
numbers.append(int(sample))
i += 1
random.seed()
for n in [100, 1000, 10000, 100000, 1000000]:
samples = []
getsample(samples, n)
print("N =", n)
avg = mean(samples, n)
print("Avg =", avg)
std = stddev(samples, avg, n)
print("Std =", std)
med = median(samples, n)
print("Median = ", med)
mod = mode(samples, n)
print("Mode =", mod)
#print("N =", n, "Avg =", avg, "Std =", std, "Median =", med, "Mode =", mod)
#############################################################################################
# End of Program #
# Copyright 2019 #
#############################################################################################