-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistics.zp
168 lines (146 loc) · 5.5 KB
/
statistics.zp
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
(module "statistics"
(export
(list "mean" mean)
(list "median" median)
(list "average" average)
(list "low-median" low-median)
(list "high-median" high-median)
(list "grouped-median" grouped-median)
(list "variance" variance)
(list "pvariance" pvariance)
(list "stdev" stdev)
(list "pstdev" pstdev))
(_sort (lambda (data)
(if (sort:sorted? data)
data
(sort:sort data))))
(_ss (lambda (data m)
(let ((m (if (nil? m) (mean data) m)))
(- (math:list-sum (map (lambda (x) (pow (- x m) 2)) data))
(/ (pow (math:list-sum (map (lambda (x) (- x m)) data)) 2) (length data))))))
(mean (lambda (data)
"calculate the mean of a collection <par>data</par>. The collection has
to implement the <fun>collec?</fun> protocol and the <par>summable</par>
protocol.
params:
- data: the input data
complexity: O(gsum+length)
returns: the mean as a float"
(/. (gsum data) (length data))))
(median (lambda (data)
"calculate the median of a collection <par>data</par>. The collection has
to implement the <fun>collec?</fun> protocol.
params:
- data: the input data
complexity: O(n+length+index) if sorted, else sort
returns: the median as a float"
(let ((n (length data))
(data (_sort data)))
(if (= n 0)
(nil)
(if (= (mod n 2) 1)
(get-from data (/ n 2))
(let ((mid (/ n 2)))
(/. (+ (get-from data (- mid 1)) (get-from data mid)) 2)))))))
(average (lambda (data)
"calculate the average of a collection <par>data</par>. The collection has
to implement the <fun>collec?</fun> protocol and the <par>summable</par>
protocol.
params:
- data: the input data
complexity: O(gsum+length)
returns: the average as a float"
(/. (gsum data) (length data))))
(low-median (lambda (data)
"calculate the low median of a collection <par>data</par>. The collection
has to implement the <fun>collec?</fun> protocol.
params:
- data: the input data
complexity: O(n+length+index) if sorted, else sort
returns: the low median as a float"
(let ((n (length data))
(data (_sort data)))
(if (= n 0)
(nil)
(if (= (mod n 2) 1)
(get-from data (/ n 2))
(get-from data (- (/ n 2) 1)))))))
(high-median (lambda (data)
"calculate the high median of a collection <par>data</par>. The collection
has to implement the <fun>collec?</fun> protocol.
params:
- data: the input data
complexity: O(n+length+index) if sorted, else sort
returns: the high median as a float"
(let ((n (length data))
(data (_sort data)))
(if (= n 0)
(nil)
(get-from data (/ n 2))))))
(grouped-median (lambda (data . args)
"calculate the grouped median of a collection <par>data</par>. The
collection has to implement the <fun>collec?</fun> protocol.
params:
- data: the input data
complexity: O(n+length+index) if sorted, else sort
returns: the grouped median as a float"
(let ((n (length data))
(data (_sort data))
(interval (get-from args 0 1)))
(cond
((= n 0) (nil))
((= n 1) (get-from data n))
(else
(let* ((x (get-from data (/ n 2)))
(l (- x (/. interval 2)))
(cf (index-of data x))
(f (list:count data x)))
(+ l (/. (* interval (- (/. n 2) cf)) f))))))))
(variance (lambda (data . mean)
"calculate the variance of a collection <par>data</par>. Optionally takes
a precalculated mean to speed up the process of calculation.
params:
- data: the input data
- mean: a precomputed mean (optional)
complexity: O(length+mean) if the mean was not precalulated, else O(length)
returns: a float of the variance"
(let* ((n (length data))
(mean (get-from mean 0 (nil)))
(ss (_ss data mean)))
(/. ss (- n 1)))))
(pvariance (lambda (data . mean)
"calculate the population variance of a collection <par>data</par>.
Optionally takes a precalculated mean to speed up the process of
calculation.
params:
- data: the input data
- mean: a precomputed mean (optional)
complexity: O(length+mean) if the mean was not precalulated, else O(length)
returns: a float of the population variance"
(let* ((n (length data))
(mean (get-from mean 0 (nil)))
(ss (_ss data mean)))
(/. ss n))))
(stdev (lambda (data . mean)
"calculate the standard deviation of a collection <par>data</par>.
Optionally takes a precalculated mean to speed up the process of
calculation.
params:
- data: the input data
- mean: a precomputed mean (optional)
complexity: O(<fun>statistics:variance</fun>)
returns: the standard deviation as float"
(let* ((mean (get-from mean 0 (nil))))
(sqrt (variance data mean)))))
(pstdev (lambda (data . args)
"calculate the standard deviation of a collection <par>data</par>.
As opposed to <fun>statistics:stdev</fun>, this is based on the
population variance. Optionally takes a precalculated mean to speed up
the process of calculation.
params:
- data: the input data
- mean: a precomputed mean (optional)
complexity: O(<fun>statistics:variance</fun>)
returns: the standard deviation as float"
(let* ((mean (get-from args 0 (nil))))
(sqrt (pvariance data mean))))))