-
Notifications
You must be signed in to change notification settings - Fork 65
/
cluster-validation.cl
executable file
·435 lines (353 loc) · 12.4 KB
/
cluster-validation.cl
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#||
(let ((*read-default-float-format* 'double-float))
(load "defsystem.cl") (excl:load-system :machine-learning :compile t))
(progn (setf cluster-validation:*workspace*
(k-means:k-means
10
(read-data:pick-and-specialize-data
(read-data:read-data-from-file
"sample/norm-interp-feature.sexp") :except '(0)
:data-types (make-list 12 :initial-element :numeric))))nil)
||#
(defpackage :cluster-validation
(:use :cl
:hjs.learn.k-means
:hjs.util.vector :hjs.util.meta
:iterate)
(:export
:*workspace*
:dunn-index
:davies-bouldin-index
:calinski
:hartigan
:ball-and-hall
:global-silhouette-value))
(in-package :cluster-validation)
(defvar *workspace*)
(defdoublefunc v-diff-sum^2 (dvec dvec))
(defun v-diff-sum^2 (x y)
(declare (type dvec x y))
(let ((result 0.0d0))
(declare (type (double-float 0.0) result))
(do-vecs ((ex x :type double-float)
(ey y :type double-float))
(let ((diff (- ex ey)))
(incf result (* diff diff))))
result))
(defparameter *distance* :euclid) ; :manhattan :euclid or :cosine
(defun set-distance (method)
(assert
(or (eq method :euclid) (eq method :manhattan) (eq method :cosine)))
(setf *distance* method))
#-(and)
(defun-speedy d (x y)
(euclid-distance x y))
#-(and)
(defmacro d (x y)
`(locally
(declare (optimize speed (safety 0) (debug 0) (:explain :inlining)))
(vml::sse3-euclid-indirect (length ,x) ,x ,y)))
; TODO
; add manhattan and cosine distance calculation of sse3
#+(and)
(defmacro d (x y)
`(ecase *distance*
(:euclid (euclid-distance ,x ,y))
(:manhattan (manhattan-distance ,x ,y))
(:cosine (cosine-distance ,x ,y))))
(defun foo (x y)
(d x y))
(defdoublefunc d-func (dvec dvec))
(defun-speedy d-func (x y)
(declare (optimize speed (safety 0) (debug 0))
(type (simple-array double-float) x y))
(d x y))
#-(and)
(defun-speedy p-d (x y)
(let ((xn (p-id x)) (yn (p-id y)))
(when (> xn yn)
(let ((tmp xn))
(setf xn yn
yn tmp)))
(p-internal-d (+ (* yn (length (pw-points *workspace*))) xn))))
(defun-speedy p-d (x y)
(d (p-pos x) (p-pos y)))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun symbolicate (&rest args)
(intern
(with-standard-io-syntax
(format nil "~{~A~}" args))))
(defun memo-table-symbol (name)
(let ((*package* (symbol-package name)))
(symbolicate '% name '-%memo-table))))
(defmacro do-memo-update (table params gen-value)
(with-unique-names (key)
`(let ((,key
,(cond ((not (cdr params))
(first params))
((not (cddr params))
`(cons ,(first params) ,(second params)))
(t `(make-array ,(length params))))))
(declare (dynamic-extent ,key))
,(when (cddr params)
`(setf ,@(loop for p in params
for i from 0
collect `(aref ,key ,i)
collect p)))
(gethash-or-set ,key ,table ,gen-value))))
(defmacro defmemo (defun name lambda-list &body body)
(let ((table-name (memo-table-symbol name))
(internal (symbolicate '% name '%-calculate))
(args lambda-list))
`(progn
(defvar ,table-name)
(,defun ,internal ,lambda-list ,@body)
(,defun ,name ,lambda-list
(do-memo-update ,table-name ,args (,internal ,@args))))))
(defmacro with-memo ((&rest memos) &body body)
`(let ,(loop for m in memos collect `(,(memo-table-symbol m) (make-hash-table :test 'equal)))
,@body))
(defdoublefunc ^2 (double-float))
(defun-speedy ^2 (x)
(* x x))
(defun-speedy d-taxi (x y)
"Manhattan distance, taxicab metric, L1 distance or rectilinear distance"
(loop for a across x
for b across y
summing (abs (- a b))))
(defun-speedy d-euclid (x y)
"Euclidean distance, straight line distance"
(sqrt
(loop for a across x
for b across y
summing (^2 (- a b)))))
(defun-speedy d-chebyshev (x y)
(loop for a across x
for b across y
maximizing (abs (- a b))))
(defmemo defun-speedy p-internal-d (coded-points)
(multiple-value-bind (yn xn)
(floor coded-points (length (pw-points *workspace*)))
(let ((x (elt (pw-points *workspace*) xn))
(y (elt (pw-points *workspace*) yn)))
(d (p-pos x) (p-pos y)))))
(defun-speedy d-1000 ()
(let ((x (make-dvec 10)) (y (make-dvec 10)))
(loop repeat 10000 do (d x y))))
(defun-speedy nop ())
(defun-speedy nop-100 ()
(loop repeat 100 do (nop)))
#-(and)
(defun-speedy d (x y)
(declare (optimize speed (safety 0) (debug 0)))
(let ((n (length x)))
(let ((tmp (make-array 1000 :element-type '(unsigned-byte 8))))
(declare (dynamic-extent tmp))
(vml::|%cffi-foreign-function/VDSUB| n x y tmp)
(vml::|%cffi-foreign-function/CBLAS_DNRM2| n tmp 1))))
(defun-speedy second-closest-cluster (p)
(declare (optimize speed))
(let ((clusters (pw-clusters *workspace*)))
(let (closest next-closest (c-d most-positive-double-float) (n-c-d most-positive-double-float))
(iter (for c in-sequence clusters)
(for distance = (d (c-center c) (p-pos p)))
(when (> c-d distance)
(when (> n-c-d c-d)
(setf next-closest closest
n-c-d c-d))
(setf closest c
c-d distance))
(when (and (not (eq c closest)) (> n-c-d distance))
(setf next-closest c
n-c-d distance)))
; (assert (eq closest (p-owner p)))
next-closest)))
(defun p-two-closest-clusters (p)
(values (p-owner p) (second-closest-cluster p)))
(defun silhouette-width (p)
(flet ((average-dissimularity (c)
(loop for p1 in (c-points c)
summing (p-d p1 p) into d
counting t into n
finally (return (/ d n)))))
(multiple-value-bind (closest next-closest)
(p-two-closest-clusters p)
(let ((a (average-dissimularity closest))
(b (average-dissimularity next-closest)))
(/ (- b a) (max a b))))))
(defun silhouette (cluster)
(loop for p in (c-points cluster)
counting t into n
summing (silhouette-width p) into s
finally (return (/ s n))))
(defun intracluster-complete-diameter (c)
(loop for p in (c-points c)
maximizing
(loop for q in (c-points c)
unless (eq p q)
maximizing (p-d p q))))
(defun intracluster-average-diameter (c)
(loop for p in (c-points c)
counting t into n
summing
(loop for q in (c-points c)
unless (eq p q)
summing (p-d p q)) into d
finally (return (/ d (* n (1- n))))))
(defun c-centroid (c)
(c-center c))
(defun intracluster-centroid-diameter (c)
(let* ((points (c-points c))
(n (length points))
(centroid (c-centroid c)))
(* 2
(/ (loop for p in points
summing (d (p-pos p) centroid))
n))))
(defun intercluster-single-linkage (c0 c1)
(let ((c0-points (c-points c0))
(c1-points (c-points c1)))
(iter (for x in-sequence c0-points)
(minimizing
(loop for y in c1-points
minimizing (p-d x y))))))
(defun intercluster-complete-linkage (c0 c1)
(let ((c0-points (c-points c0))
(c1-points (c-points c1)))
(iter (for x in-sequence c0-points)
(maximizing
(loop for y in c1-points
maximizing (p-d x y))))))
(defun intercluster-average-linkage (c0 c1)
(let ((c0-points (c-points c0))
(c1-points (c-points c1)))
(let ((c0-n (length c0-points))
(c1-n (length c1-points)))
(/
(loop for x in c0-points
summing
(loop for y in c1-points
summing (p-d x y)))
c0-n c1-n))))
(defun intercluster-centroid-linkage (c0 c1)
(d (c-centroid c0) (c-centroid c1)))
(defun intercluster-average-to-centroids-linkage (c0 c1)
(let ((c0-points (c-points c0))
(c1-points (c-points c1)))
(let ((c0-n (length c0-points))
(c1-n (length c1-points)))
(/
(flet ((sum-to-c (centroid points)
(loop for x in points
summing (d (p-pos x) centroid))))
(+
(sum-to-c (c-centroid c0) c1-points)
(sum-to-c (c-centroid c1) c0-points)))
(+ c0-n c1-n)))))
(defun intercluster-hausdorff-linkage (c0 c1)
(let ((c0-points (c-points c0))
(c1-points (c-points c1)))
(flet ((max-min-d (xps yps)
(iter (for x in-sequence xps)
(maximizing
(iter (for y in-sequence yps)
(minimizing (p-d x y)))))))
(max (max-min-d c0-points c1-points) (max-min-d c1-points c0-points)))))
(defun intercluster-d (c0 c1 &key (method :centroid))
(ecase method
(:centroid (intercluster-centroid-linkage c0 c1))
(:single (intercluster-single-linkage c0 c1))
(:complete (intercluster-complete-linkage c0 c1))
(:average (intercluster-average-linkage c0 c1))
(:average-to-centroids (intercluster-average-to-centroids-linkage
c0 c1))
(:hausdorff (intercluster-hausdorff-linkage c0 c1))))
(defun intracluster-diameter (c &key (method :centroid))
(ecase method
(:centroid (intracluster-centroid-diameter c))
(:complete (intracluster-complete-diameter c))
(:average (intracluster-average-diameter c))))
(defun ssw ()
(iter (for c in-sequence (pw-clusters *workspace*))
(let ((mu (c-center c)))
(summing
(loop
for p in (c-points c)
summing (v-diff-sum^2 (p-pos p) mu))))))
(defun ssb ()
(let ((mu (centroid)))
(iter (for c in-sequence (pw-clusters *workspace*))
(summing (* (c-size c) (v-diff-sum^2 (c-center c) mu))))))
(defun sst ()
(let ((mu (centroid)))
(iter (for p in-sequence (pw-points *workspace*))
(summing (v-diff-sum^2 (p-pos p) mu)))))
(defun make-zero-dvec ()
(let ((v (copy-seq (p-pos (elt (pw-points *workspace*) 0)))))
(fill-vec v 0d0)
v))
(defun centroid ()
(let ((c (make-zero-dvec))
(n 0)
(pw-points (pw-points *workspace*)))
(do-vec (p pw-points :type point)
(v+ c (p-pos p) c)
(incf n))
(v-scale c (the double-float (/ 1d0 (coerce n 'double-float))) c)
c))
(defun calinski (&optional (*workspace* *workspace*))
(let ((n (length (pw-points *workspace*)))
(k (length (pw-clusters *workspace*))))
(/ (* (ssb) (- (1- n) k)) (* (ssw) (1- k)))))
(defun hartigan (&optional (*workspace* *workspace*))
(- (log (ssb)) (log (ssw))))
(defun ball-and-hall (&optional (*workspace* *workspace*))
(let ((k (length (pw-clusters *workspace*))))
(/ (ssw) k)))
(defun dunn-index (&key (*workspace* *workspace*)
(distance :euclid)
(intercluster :centroid)
(intracluster :centroid))
(set-distance distance)
(let ((clusters (pw-clusters *workspace*)))
(/
(iter (for c0 in-sequence clusters)
(minimizing (iter (for c1 in-sequence clusters)
(unless (eq c0 c1)
(minimizing
(intercluster-d c0 c1
:method intercluster))))))
(iter (for c in-sequence clusters)
(maximizing (intracluster-diameter
c
:method intracluster))))))
(defun davies-bouldin-index (&key (*workspace* *workspace*)
(distance :euclid)
(intercluster :centroid)
(intracluster :centroid))
(set-distance distance)
(let* ((clusters (pw-clusters *workspace*))
(cids (map 'list #'(lambda (c)
(intracluster-diameter
c
:method intracluster)) clusters))
(c (length clusters)))
(/
(iter (for c0 in-sequence clusters)
(for c0id in-sequence cids)
(summing
(iter (for c1 in-sequence clusters)
(for c1id in-sequence cids)
(unless (eq c0 c1)
(maximizing (/ (+ c1id c0id)
(intercluster-d
c0 c1
:method intercluster)))))))
c)))
(defun global-silhouette-value (&key (*workspace* *workspace*)
(distance :euclid))
(set-distance distance)
(iter (for c in-sequence (pw-clusters *workspace*))
(summing (silhouette c) into s)
(counting t into n)
(finally (return (/ s n)))))