/
stdstats.py
executable file
·137 lines (112 loc) · 3.23 KB
/
stdstats.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
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
126
127
128
129
130
131
132
133
134
135
136
137
"""
stdstats.py
The stdstats module defines functions related to statistical analysis
and graphical data display.
"""
#-----------------------------------------------------------------------
import math
import stddraw
#-----------------------------------------------------------------------
def min(a):
"""
Return the minimum value in array 'a'. Could call the built-in
min() function instead.
"""
minumum = float('inf')
for x in a:
if x < minumum:
minumum = x
return minumum
#-----------------------------------------------------------------------
def max(a):
"""
Return the maximum value in array 'a'. Could call the built-in
max() function instead.
"""
maximum = float('-inf')
for x in a:
if x > maximum:
maximum = x
return maximum
#-----------------------------------------------------------------------
def mean(a):
"""
Return the average of the values in array 'a'.
Could call the built-in sum() and len() functions instead.
"""
sum = 0.0
for x in a:
sum += x
return sum / float(len(a))
#-----------------------------------------------------------------------
def var(a):
"""
Return the sample variance of the values in array 'a'.
"""
avg = mean(a)
sum = 0.0
for x in a:
sum += (x - avg) * (x - avg)
return sum / (float(len(a)) - 1.0)
#-----------------------------------------------------------------------
def stddev(a):
"""
Return the standard deviation of the values in array 'a'.
"""
return math.sqrt(var(a))
#-----------------------------------------------------------------------
def median(a):
"""
Return the median of the values in array 'a'.
"""
b = list(a) # Make a copy of a.
b.sort()
length = len(b)
if length % 2 == 1:
return b[length//2]
else:
return float(b[length//2 - 1] + b[length//2]) / 2.0
#-----------------------------------------------------------------------
def plotPoints(a):
"""
Plot the values of array 'a' as points.
"""
N = len(a)
stddraw.setXscale(0, N-1)
stddraw.setPenRadius(1.0 / (3.0 * N))
for i in range(N):
stddraw.point(i, a[i])
#-----------------------------------------------------------------------
def plotLines(a):
"""
Plot the values of array 'a' as line end-points.
"""
N = len(a)
stddraw.setXscale(0, N-1)
stddraw.setPenRadius()
for i in range(1, N):
stddraw.line(i-1, a[i-1], i, a[i])
#-----------------------------------------------------------------------
def plotBars(a):
"""
Plot the values of array 'a' as bars.
"""
N = len(a)
stddraw.setXscale(0, N-1)
for i in range(N):
stddraw.filledRectangle(i, a[i]/2, .25, a[i])
#-----------------------------------------------------------------------
def main():
"""
For testing:
"""
import stdarray
import stdio
a = stdarray.readFloat1D()
stdio.writef(' min %7.3f\n', min(a))
stdio.writef(' mean %7.3f\n', mean(a))
stdio.writef(' max %7.3f\n', max(a))
stdio.writef(' std dev %7.3f\n', stddev(a))
stdio.writef(' median %7.3f\n', median(a))
if __name__ == '__main__':
main()