/
core.clj
307 lines (276 loc) · 11 KB
/
core.clj
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
(ns histogram.test.core
(:import [java.lang Math]
[java.util Random])
(:use [histogram.core]
[clojure.test]))
(defn- about= [v1 v2 epsilon]
(>= epsilon (Math/abs (double (- v1 v2)))))
(defn- normal-data [size]
(let [^Random rnd (Random.)]
(repeatedly size #(.nextGaussian rnd))))
(defn- rand-data [size]
(repeatedly size #(rand)))
(defn- cat-data [size with-missing]
(repeatedly size
#(let [x (rand)
y (cond (< x (/ 1 3)) :apple
(< x (/ 2 3)) :orange
:else :grape)]
[x (if (or (not with-missing) (> 0.5 (rand)))
y nil)])))
(defn- group-data [size with-missing]
(map list (repeatedly #(rand)) (cat-data size with-missing)))
(deftest sum-test
(let [points 10000]
(is (about= (sum (reduce insert! (create) (normal-data points)) 0)
(/ points 2)
(/ points 50)))))
(deftest median-mean-test
(let [points 10000]
(is (about= (median (reduce insert! (create) (rand-data points)))
0.5 0.05))
(is (about= (median (reduce insert! (create) (normal-data points)))
0 0.05))
(is (about= (mean (reduce insert! (create) (normal-data points)))
0 0.05))))
(deftest mean-test
(let [points 1001]
(is (== (/ (dec points) 2)
(mean (reduce insert! (create) (range points)))))))
(deftest merge-test
(is (empty? (bins (merge! (create) (create)))))
(is (seq (bins (merge! (insert! (create) 1) (create)))))
(is (seq (bins (merge! (create) (insert! (create) 1)))))
(let [points 1000
hist-count 10
hists (repeatedly hist-count
#(reduce insert! (create) (normal-data points)))
merged-hist (reduce merge! hists)]
(is (about= (sum merged-hist 0)
(/ (* points hist-count) 2)
(/ (* points hist-count) 50))))
(let [h1 (-> (create)
(insert! 1 1)
(insert! nil 1))
h2 (-> (create)
(insert! 2 2)
(insert! nil 2))]
(is (== 2 (total-count (merge! h1 h2))))))
(deftest mixed-test
(let [insert-pair #(apply insert! (apply insert! (create) %1) %2)
vals [[1] [1 2] [1 :a] [1 [:a]]]]
(doseq [x vals y vals]
(if (= x y)
(is (insert-pair x y))
(is (thrown? Throwable (insert-pair x y))))))
(is (thrown? Throwable (insert! (create :categories [:a :b]) :c)))
(is (thrown? Throwable (merge! (create :categories [:a :b])
(create :categories [:b :c])))))
(deftest density-test
(let [hist (reduce insert! (create) [1 2 2 3])]
(is (= 0.0 (density hist 0.0)))
(is (= 0.0 (density hist 0.5)))
(is (= 0.5 (density hist 1.0)))
(is (= 1.0 (density hist 1.5)))
(is (= 1.0 (density hist 2.0)))
(is (= 1.0 (density hist 2.5)))
(is (= 0.5 (density hist 3.0)))
(is (= 0.0 (density hist 3.5)))
(is (= 0.0 (density hist 4.0)))))
(deftest categorical-test
(let [points 10000
hist (reduce (fn [h [x y]] (insert! h x y))
(create)
(cat-data points false))
ext-sum (extended-sum hist 0.5)]
(is (about= (:apple (:counts (:target ext-sum)))
(/ points 3)
(/ points 50)))
(is (about= (:orange (:counts (:target ext-sum)))
(/ points 6)
(/ points 50)))))
(deftest categorical-array-test
(let [points 10000
hist (reduce (fn [h [x y]] (insert! h x y))
(create :categories [:apple :orange :grape])
(cat-data points false))
ext-sum (extended-sum hist 0.5)]
(is (about= (:apple (:counts (:target ext-sum)))
(/ points 3)
(/ points 50)))
(is (about= (:orange (:counts (:target ext-sum)))
(/ points 6)
(/ points 50)))
(is (about= 3333 (:orange (:counts (total-target-sum hist))) 150))
(is (about= 3333 (:grape (:counts (total-target-sum hist))) 150))
(is (about= 3333 (:apple (:counts (total-target-sum hist))) 150))))
(deftest group-test
(let [points 10000
data (group-data points false)
hist (reduce (fn [h [x y]] (insert! h x y))
(create)
data)
target (:target (extended-sum hist 0.5))]
(is (= (target-type hist) :group))
(is (= (group-types hist) '(:numeric :categorical)))
(is (about= (:sum (first target))
(/ points 4)
(/ points 50)))
(is (about= (:sum-squares (first target))
(reduce + (map #(* % %)
(take (int (/ (count data) 2))
(map first data))))
150))
(is (about= (:orange (:counts (second target)))
(/ points 6)
(/ points 50)))))
(deftest weighted-gap-test
;; Histograms using weighted gaps are less eager to merge bins with
;; large counts. This test builds weighted and non-weighted
;; histograms using samples from a normal distribution. The
;; non-weighted histogram should spend more of its bins capturing
;; the tails of the distribution. With that in mind this test makes
;; sure the bins bracketing the weighted histogram have larger
;; counts than the bins bracketing the non-weighted histogram.
(let [points 10000
weighted (bins (reduce insert!
(create :bins 32 :gap-weighted? true)
(normal-data points)))
classic (bins (reduce insert!
(create :bins 32 :gap-weighted? false)
(normal-data points)))]
(> (+ (:count (first weighted) (last weighted)))
(+ (:count (first classic) (last classic))))))
(deftest round-trip-test
(let [points 10000
hist1 (reduce (fn [h [x y]] (insert! h x y))
(create)
(cat-data points false))
hist2 (reduce insert-bin! (create) (bins hist1))]
(= (bins hist1) (bins hist2))))
(deftest hist-test
(is (histogram? (create)))
(is (not (histogram? "forjao"))))
(deftest weighted-test
(let [data [1 2 2 3 4]
hist (reduce insert!
(create :bins 3 :gap-weighted? true)
data)]
(is (== (total-count hist) (count data)))))
(deftest numeric-missing-test
(let [data [[1 1] [1 nil] [4 2] [6 nil]]
result (bins (reduce (partial apply insert-numeric!)
(create :bins 2)
data))]
(is (= result
'({:mean 1.0
:count 2
:target {:sum 1.0 :sum-squares 1.0 :missing-count 1.0}}
{:mean 5.0
:count 2
:target {:sum 2.0 :sum-squares 4.0 :missing-count 1.0}})))))
(deftest categorical-missing-test
(let [data [[1 :foo] [1 nil] [4 :bar] [6 nil]]
result (bins (reduce (partial apply insert-categorical!)
(create :bins 2 :categories [:foo :bar])
data))]
(is (= result
'({:mean 1.0
:count 2
:target {:counts {:foo 1.0 :bar 0.0} :missing-count 1.0}}
{:mean 5.0
:count 2
:target {:counts {:foo 0.0 :bar 1.0} :missing-count 1.0}})))))
(deftest group-missing-test
(let [points 10000
hist (reduce (fn [h [x y]] (insert! h x y))
(create :bins 4 :group-types [:numeric :categorical])
(group-data points true))
ext-sum (extended-sum hist 0.5)]
(is (about= (:sum (first (:target ext-sum)))
(/ points 4)
(/ points 50)))
(is (about= (:missing-count (second (:target ext-sum)))
(/ points 4)
(/ points 50)))
(is (about= (:orange (:counts (second (:target ext-sum))))
(/ points 12)
(/ points 50)))))
(deftest input-missing-test
(let [data [[1 :foo] [nil :bar] [4 :bar] [nil :foo] [nil nil]]
hist (reduce (partial apply insert-categorical!)
(create :bins 2 :categories [:foo :bar])
data)]
(is (= (missing-bin hist)
{:count 3
:target {:counts {:bar 1.0, :foo 1.0}
:missing-count 1.0}}))))
(deftest missing-merge-test
(is (merge! (insert-numeric! (create :bins 8) nil 4)
(create :bins 8)))
(let [hist1 (insert! (create) nil 1)
hist2 (insert! (create) nil 2)
merged (merge! (merge! (create) hist1) hist2)]
(is (== 2 (:count (missing-bin merged))))
(is (== 3 (:sum (:target (missing-bin merged)))))))
(deftest missing-bin-test
(let [bin1 (-> (create)
(insert-numeric! nil 3)
(missing-bin))
bin2 (-> (create)
(insert-bin! bin1)
(missing-bin))]
(is (= bin1 bin2))))
(deftest min-max-test
(let [hist (create)]
(is (nil? (minimum hist)))
(is (nil? (maximum hist))))
(let [hist (reduce insert!
(create)
(repeatedly 1000 #(rand-int 10)))]
(is (== 0 (minimum hist)))
(is (== 9 (maximum hist))))
(let [hist1 (reduce insert! (create) (range 0 4))
hist2 (reduce insert! (create) (range 2 6))
merged (-> (create) (merge! hist1) (merge! hist2))]
(is (== 0 (minimum merged)))
(is (== 5 (maximum merged)))))
(deftest transform-test
(let [hist (-> (create)
(insert! 1 [2 3 :a])
(insert! 1 [9 2 :b])
(insert! 4 [5 nil nil]))]
(is (= (hist-to-clj hist)
(hist-to-clj (clj-to-hist (hist-to-clj hist))))))
(let [hist1 (reduce (fn [h [x y]] (insert! h x y))
(create :bins 8 :gap-weighted? true
:categories [:apple :orange :grape])
(cat-data 1000 false))
hist1 (insert! hist1 nil :apple)
hist2 (clj-to-hist (hist-to-clj hist1))]
(is (= (bins hist1) (bins hist2)))
(is (= (missing-bin hist1) (missing-bin hist2)))
(is (= (minimum hist1) (minimum hist2)))
(is (= (maximum hist1) (maximum hist2)))))
(deftest variance-test
(is (nil? (variance (insert! (create) 1))))
(is (= 3.5 (variance (reduce insert! (create) [1 2 3 4 5 6]))))
(is (about= (variance (reduce insert! (create) (normal-data 10000)))
1 0.05)))
(deftest negative-zero-test
(is (= 1 (count (bins (reduce insert! (create) [0.0 -0.0]))))))
(deftest freeze-test
(let [points 100000
hist (reduce insert! (create :freeze 500) (normal-data points))]
(is (about= (sum hist 0) (/ points 2) (/ points 50)))
(is (about= (median hist) 0 0.05))
(is (about= (mean hist) 0 0.05))
(is (about= (variance hist) 1 0.05))))
(deftest correct-counts
(let [data [605 760 610 615 605 780 605 905]
hist (reduce insert!
(create :bins 4 :gap-weighted? true)
data)]
(is (== (count data)
(total-count hist)
(reduce + (map :count (bins hist)))))))