first of the matlisp tests, factored out.
[CommonLispStat.git] / unittests.lisp
blob579c8c4b34ec62baa236167cdd93462afd76aeee
1 ;;; -*- mode: lisp -*-
2 ;;; Copyright (c) 2007, by A.J. Rossini <blindglobe@gmail.com>
3 ;;; See COPYRIGHT file for any additional restrictions (BSD license).
4 ;;; Since 1991, ANSI was finally finished. Edited for ANSI Common Lisp.
6 ;;; This is semi-external to lispstat core packages. The dependency
7 ;;; should be that lispstat packages are dependencies for the unit
8 ;;; tests. However, where they will end up is still to be
9 ;;; determined.
11 (in-package :cl-user)
13 (defpackage :lisp-stat-unittests
14 (:use :common-lisp :lift :lisp-stat)
15 (:shadowing-import-from :lisp-stat
16 slot-value call-method call-next-method ;; objects
17 expt + - * / ** mod rem abs 1+ 1- log exp sqrt sin cos tan ;; lsmath
18 asin acos atan sinh cosh tanh asinh acosh atanh float random
19 truncate floor ceiling round minusp zerop plusp evenp oddp
20 < <= = /= >= > ;; complex
21 conjugate realpart imagpart phase
22 min max logand logior logxor lognot ffloor fceiling
23 ftruncate fround signum cis)
24 (:export run-lisp-stat-tests run-lisp-stat-test scoreboard))
26 (in-package :lisp-stat-unittests)
28 ;;; TESTS
30 (defun run-lisp-stat-tests ()
31 (run-tests :suite 'lisp-stat))
33 (defun run-lisp-stat-test (&rest x)
34 (run-test x))
37 (deftestsuite lisp-stat () ())
38 (deftestsuite lisp-stat-lin-alg (lisp-stat) ())
39 (deftestsuite lisp-stat-spec-fns (lisp-stat) ())
40 (deftestsuite lisp-stat-probdistn (lisp-stat) ())
43 (defun almost= (a b &key (tol 0.000001))
44 "Numerically compares 2 values to a tolerance."
45 (< (abs (- a b)) tol))
47 (defun almost=lists (a b &key (tol 0.000001))
48 "Numerically compare 2 lists using almost=."
49 (if (and (null a) (null b))
51 (and (almost= (car a) (car b) :tol tol)
52 (almost=lists (cdr a) (cdr b) :tol tol))))
56 ;; Need to consider a CLOSy approach for almost= to cover the range of
57 ;; possible data structures that we would like to be equal to a
58 ;; particular tolerance range. For example, fill in a shell like:
60 (defgeneric numerical= (a b &key tol))
62 (defmethod numerical= ((a real) (b real) &key (tol 0.00001)) ;; real))
63 ;;(print (format nil " equality pred for real a=~w real b=~w" a b))
64 (< (abs (- a b)) tol))
66 ;; can we just worry about reals if integers are a subclass?
67 (defmethod numerical= ((a integer) (b integer) &key (tol 0.1)) ;; real))
68 ;;(print (format nil " equality pred for int a=~w int b=~w" a b))
69 (< (abs (- a b)) tol))
71 (defmethod numerical= ((a complex) (b complex) &key (tol 0.00001))
72 ;;(print (format nil " equality pred for cmplx a=~w cmplx b=~w" a b))
73 (< (abs (- a b)) tol))
75 (defmethod numerical= ((a sequence) (b sequence) &key (tol 0.00001))
76 ;; (print (format nil "checking equality for list a ~w list b=~w" a b))
77 ;; using sequence for lists and vectors, but not arrays.
78 ;; FIXME++++ This is too slow, too many comparisons!
79 (if (and (null a) (null b))
81 (if (and (= (length a) (length b))
82 (> (length a) 0)
83 (numerical= (car a) (car b) :tol tol))
84 (progn
85 (if (= (length (cdr a)) 0)
87 (numerical= (cdr a) (cdr b) :tol tol)))
88 nil)))
90 ;; To do.
92 (defmethod numerical= ((a array) (b array) &key (tol 0.00001))
93 (print (format nil "checking equality for array a ~w and array b=~w" a b))
94 ;;; FIXME Warning! Need to generalize past 2-d array!!
95 (if (/= (array-dimensions a) (array-dimensions b))
96 nil
97 (let* ((a-dim (array-dimensions a))
98 (a-b-elt-eq (loop for i from 0 to (nth 0 a-dim)
99 for j from 0 to (nth 1 a-dim)
100 collect (numerical= (apply #'aref a (list i j))
101 (apply #'aref b (list i j))
102 :tol tol))))
103 (every #'(lambda (x) x) a-b-elt-eq))))
105 (deftestsuite lisp-stat-testsupport (lisp-stat)
107 (:tests
108 (almost=1 (ensure (almost= 3 3.001 :tol 0.01)))
109 (almost=2 (ensure (almost= 3 3.01 :tol 0.01)))
110 (almost=3 (ensure (not (almost= 3 3.1 :tol 0.01))))
111 (almost=lists1 (ensure (almost=lists nil nil :tol 0.01)))
112 (almost=lists2 (ensure (almost=lists (list ) (list ) :tol 0.01)))
113 (almost=lists3 (ensure (almost=lists (list 1.0) (list 1.0) :tol 0.01)))
114 (almost=lists4 (ensure (almost=lists (list 1.0 1.0) (list 1.0 1.0) :tol 0.01)))
115 (almost=lists5 (ensure (not (almost=lists (list 1.0 1.0)
116 (list 1.0 1.1) :tol 0.01))))))
118 (deftestsuite lisp-stat-testsupport2 (lisp-stat)
120 (:tests
121 (numerical=1 (ensure (numerical= 3 3.001 :tol 0.01)))
122 (numerical=1.1 (ensure (numerical= 2 2)))
123 (numerical=1.2 (ensure (not (numerical= 2 3))))
124 (numerical=2 (ensure (numerical= 3 3.01 :tol 0.01)))
125 (numerical=3 (ensure (not (numerical= 3 3.1 :tol 0.01))))
126 (numerical=4 (ensure (numerical= nil nil :tol 0.01)))
127 (numerical=5 (ensure (numerical= (list ) (list ) :tol 0.01)))
128 (numerical=6 (ensure (numerical= (list 1.0) (list 1.0) :tol 0.01)))
129 (numerical=7 (ensure (numerical= (list 1.0 1.0) (list 1.0 1.0) :tol 0.01)))
130 (numerical=7.5 (ensure-error (numerical= 1.0 (list 1.0 1.0) :tol 0.01)))
131 (numerical=8 (ensure (not (numerical= (list 2.0 2.0 2.2) (list 2.1 2.0 2.2)))))
132 (numerical=9 (ensure (numerical= (list 2.1 2.0 2.2) (list 2.1 2.0 2.2)) ))
133 (numerical=10 (ensure (numerical= (list 2.1 2.0 2.2 4.2) (list 2.1 2.0 2.2 4.2))))
134 (numerical=11 (ensure (not (numerical= (list 2.1 2.0 2.3 4.0) (list 2.1 2.0 2.2 4.0)))))
135 (numerical=12 (ensure (not (numerical= (list 1.0 1.0)
136 (list 1.0 1.1) :tol 0.01))))
137 (numerical=C1 (ensure (numerical= #C(2 3) #C(2 3))))
138 (numerical=C2 (ensure (not(numerical= #C(2 3) #C(2 4)))))
139 (numerical=C3 (ensure (numerical= #C(2 3) #C(3 4) :tol 2)))
140 (numerical=C4 (ensure (not(numerical= #C(2 3) #C(3 4) :tol 1))))
142 ;;;; Tests to fix
144 (numerical=A1 (ensure (numerical= #1A(2 3 4)
145 #1A(2 3 4))))
147 (numerical=A2 (ensure (numerical= #2A((2 3 4) (1 2 4) (2 4 5))
148 #2A((2 3 4) (1 2 4) (2 4 5)))))
150 (numerical=A3 (ensure (not (numerical= #2A((2 3 4) (1 2 4) (2 5 4))
151 #2A((2 3 4) (1 2 4) (2 4 5))))))
153 (numerical=A4 (ensure (not (numerical= #1A(2 2 4)
154 #1A(2 3 4)))))
158 ;; (describe (run-tests :suite 'lisp-stat-testsupport2))
162 (addtest (lisp-stat-lin-alg) cholesky-decomposition-1
163 (ensure-same
164 (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
165 (list #2A((1.7888543819998317 0.0 0.0)
166 (1.6770509831248424 0.11180339887498929 0.0)
167 (2.23606797749979 2.23606797749979 3.332000937312528e-8))
168 5.000000000000003)
169 :test 'almost=lists))
172 (addtest (lisp-stat-lin-alg) cholesky-decomposition-2
173 (ensure-same
174 (matlisp:chol)))
179 (addtest (lisp-stat-lin-alg) lu-decomposition
180 (ensure-same
181 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
182 (list #2A((2.0 3.0 4.0) (1.0 1.0 1.0) (0.5 0.5 1.5)) #(0 2 2) -1.0 NIL)))
184 (addtest (lisp-stat-lin-alg) rcondest
185 ;; (ensure-same
186 (ensure-error ;; it barfs, FIXME!!
187 (rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
188 6.8157451e7
189 :test 'almost=))
191 (addtest (lisp-stat-lin-alg) lu-solve
192 (ensure-same
193 (lu-solve
194 (lu-decomp
195 #2A((2 3 4) (1 2 4) (2 4 5)))
196 #(2 3 4))
197 #(-2.333333333333333 1.3333333333333335 0.6666666666666666)))
199 (addtest (lisp-stat-lin-alg) inverse
200 (ensure-same
201 (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
202 #2A((2.0 -0.33333333333333326 -1.3333333333333335)
203 (-1.0 -0.6666666666666666 1.3333333333333333)
204 (0.0 0.6666666666666666 -0.3333333333333333))))
206 (addtest (lisp-stat-lin-alg) sv-decomp
207 (ensure-same
208 (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
209 (list #2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
210 (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
211 (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
212 #(9.699290438141343 0.8971681569301373 0.3447525123483081)
213 #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
214 (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
215 (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
217 :test 'almost=lists))
219 (addtest (lisp-stat-lin-alg) qr-decomp
220 (ensure-same
221 (qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
222 (list #2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
223 (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
224 (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
225 #2A((-3.0 -5.333333333333334 -7.333333333333332)
226 (0.0 -0.7453559924999292 -1.1925695879998877)
227 (0.0 0.0 -1.3416407864998738)))
228 :test 'almost=lists))
230 (addtest (lisp-stat-lin-alg) eigen
231 (ensure-same
232 (eigen #2A((2 3 4) (1 2 4) (2 4 5)))
233 (list #(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
234 (list #(0.4999999999999998 0.4999999999999997 0.7071067811865475)
235 #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
236 #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
237 NIL)))
239 (addtest (lisp-stat-lin-alg) spline
240 (ensure-same
241 (spline #(1.0 1.2 1.3 1.8 2.1 2.5)
242 #(1.2 2.0 2.1 2.0 1.1 2.8)
243 :xvals 6)
244 (list (list 1.0 1.3 1.6 1.9 2.2 2.5)
245 (list 1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))
246 :test 'almost=lists))
248 (addtest (lisp-stat-lin-alg) kernel-smooth
249 (ensure-same
250 ;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
251 (kernel-smooth
252 #(1.0 1.2 1.3 1.8 2.1 2.5)
253 #(1.2 2.0 2.1 2.0 1.1 2.8)
254 :xvals 5)
255 (list (list 1.0 1.375 1.75 2.125 2.5)
256 (list 1.6603277642110226 1.9471748095239771 1.7938127405752287
257 1.5871511322219498 2.518194783156392))
258 :test 'almost=lists))
260 (addtest (lisp-stat-lin-alg) kernel-dens
261 (ensure-same
262 (kernel-dens
263 #(1.0 1.2 2.5 2.1 1.8 1.2)
264 :xvals 5)
265 (list (list 1.0 1.375 1.75 2.125 2.5)
266 (list 0.7224150453621405 0.5820045548233707 0.38216411702854214
267 0.4829822708587095 0.3485939156929503))))
270 (addtest (lisp-stat-lin-alg) fft
271 (ensure-same
272 (fft #(1.0 1.2 2.5 2.1 1.8))
273 (list #(#C(1.0 0.0) #C(1.2 0.0) #C(2.5 0.0) #C(2.1 0.0) #C(1.8 0.0)))
274 :test 'almost=lists))
277 (addtest (lisp-stat-lin-alg) lowess
278 (ensure-same
279 (lowess #(1.0 1.2 2.5 2.1 1.8 1.2)
280 #(1.2 2.0 2.1 2.0 1.1 2.8))
281 #(1.0 1.2 1.2 1.8 2.1 2.5)
282 :test 'almost=lists)) ;; result isn't a list!
286 ;;;; Log-gamma function
288 (addtest (lisp-stat-spec-fns) log-gamma-fn
289 (ensure-same
290 (log-gamma 3.4)
291 1.0923280596789584
292 :test 'almost=))
295 ;;; Probability distributions
297 ;; This macro should be generalized, but it's a good start now.
298 ;;(defmacro ProbDistnTests (prefixName
299 ;; quant-params quant-answer
300 ;; cdf-params cdf-answer
301 ;; pmf-params pmf-answer
302 ;; rand-params rand-answer)
303 ;; (deftestsuite lisp-stat-probdist-,prefixName (lisp-stat-probdistn)
304 ;; ;; (( ))
305 ;; (:documentation "testing for ,testName distribution results")
306 ;; (:test (ensure-same
307 ;; (lisp-stat-basics:,testName-quant ,quant-params) ,quant-answer))
308 ;; (:test (ensure-same
309 ;; (lisp-stat-basics:,testName-cdf ,cdf-params) ,cdf-answer))
310 ;; (:test (ensure-same
311 ;; (lisp-stat-basics:,testName-pmf ,pmf-params) ,pmf-answer))
312 ;; (:test (progn
313 ;; (set-seed 234)
314 ;; (ensure-same
315 ;; (lisp-stat-basics:,testName-rand ,rand-params) ,rand-answer)))))
317 ;;; Normal distribution
319 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
321 (:documentation "testing for Gaussian distn results")
322 (:test (ensure-same
323 (normal-quant 0.95)
324 1.6448536279366268))
325 (:test (ensure-same
326 (normal-cdf 1.3)
327 0.9031995154143897))
328 (:test (ensure-same
329 (normal-dens 1.3)
330 0.17136859204780736))
331 (:test (ensure-same
332 (normal-rand 2)
333 (list -0.40502015f0 -0.8091404f0)))
334 (:test (ensure-same
335 (bivnorm-cdf 0.2 0.4 0.6)
336 0.4736873734160288)))
338 ;;;; Cauchy distribution
340 (deftestsuite lisp-stat-probdist-cauchy (lisp-stat-probdistn)
342 (:documentation "testing for Cachy-distn results")
343 (:test (ensure-same
344 (cauchy-quant 0.95)
345 6.313751514675031))
346 (:test (ensure-same
347 (cauchy-cdf 1.3)
348 0.7912855998398473))
349 (:test (ensure-same
350 (cauchy-dens 1.3)
351 0.1183308127104695 ))
352 (:test (ensure-same
353 (cauchy-rand 2)
354 (list -1.06224644160405 -0.4524695943939537))))
356 ;;;; Gamma distribution
358 (deftestsuite lisp-stat-probdist-gamma (lisp-stat-probdistn)
360 (:documentation "testing for gamma distn results")
361 (:test (ensure-same
362 (gamma-quant 0.95 4.3)
363 8.178692439291645))
364 (:test (ensure-same
365 (gamma-cdf 1.3 4.3)
366 0.028895150986674906))
367 (:test (ensure-same
368 (gamma-dens 1.3 4.3)
369 0.0731517686447374))
370 (:test (ensure-same
371 (gamma-rand 2 4.3)
372 (list 2.454918912880936 4.081365384357454))))
374 ;;;; Chi-square distribution
376 (deftestsuite lisp-stat-probdist-chisq (lisp-stat-probdistn)
378 (:documentation "testing for Chi-square distn results")
379 (:test (ensure-same
380 (chisq-quant 0.95 3)
381 7.814727903379012))
382 (:test (ensure-same
383 (chisq-cdf 1 5)
384 0.03743422675631789))
385 (:test (ensure-same
386 (chisq-dens 1 5)
387 0.08065690818083521))
388 (:test (progn
389 (set-seed 353)
390 (ensure-same
391 (chisq-rand 2 4)
392 (list 1.968535826180572 2.9988646156942997)))))
394 ;;;; Beta distribution
396 (deftestsuite lisp-stat-probdist-beta (lisp-stat-probdistn)
398 (:documentation "testing for beta distn results")
399 (:test (ensure-same
400 (beta-quant 0.95 3 2)
401 0.9023885371149876))
402 (:test (ensure-same
403 (beta-cdf 0.4 2 2.4)
404 0.4247997418541529 ))
405 (:test (ensure-same
406 (beta-dens 0.4 2 2.4)
407 1.5964741858913518 ))
408 (:test (ensure-same
409 (beta-rand 2 2 2.4)
410 (list 0.8014897077282279 0.6516371997922659))))
412 ;;;; t distribution
414 (deftestsuite lisp-stat-probdist-t (lisp-stat-probdistn)
416 (:documentation "testing for t-distn results")
417 (:test (ensure-same
418 (t-quant 0.95 3)
419 2.35336343484194))
420 (:test (ensure-same
421 (t-cdf 1 2.3)
422 0.794733624298342))
423 (:test (ensure-same
424 (t-dens 1 2.3)
425 0.1978163816318102))
426 (:test (ensure-same
427 (t-rand 2 2.3)
428 (list -0.34303672776089306 -1.142505872436518))))
430 ;;;; F distribution
432 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
434 (:documentation "testing for f-distn results")
435 (:test (ensure-same
436 (f-quant 0.95 3 5) 5.409451318117459))
437 (:test (ensure-same
438 (f-cdf 1 3.2 5.4)
439 0.5347130905510765))
440 (:test (ensure-same
441 (f-dens 1 3.2 5.4)
442 0.37551128864591415))
443 (:test (progn
444 (set-seed 234)
445 (ensure-same
446 (f-rand 2 3 2)
447 (list 0.7939093442091963 0.07442694152491144)))))
449 ;;;; Poisson distribution
451 (deftestsuite lisp-stat-probdist-poisson (lisp-stat-probdistn)
453 (:documentation "testing for poisson distribution results")
454 (:test (ensure-same
455 (poisson-quant 0.95 3.2) 6))
456 (:test (ensure-same
457 (poisson-cdf 1 3.2)
458 0.17120125672252395))
459 (:test (ensure-same
460 (poisson-pmf 1 3.2)
461 0.13043905274097067))
462 (:test (progn
463 (set-seed 234)
464 (ensure-same
465 (poisson-rand 5 3.2)
466 (list 2 1 2 0 3)))))
468 ;; Binomial distribution
470 (deftestsuite lisp-stat-probdist-binomial (lisp-stat-probdistn)
472 (:documentation "testing for binomial distribution results")
474 (:test (ensure-same
475 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
477 (:test (ensure-same
478 (binomial-quant 0 3 0.4)
479 ;; -2147483648
481 (:test (ensure-same
482 (binomial-cdf 1 3 0.4)
483 0.6479999999965776))
485 (:test (ensure-same
486 (binomial-pmf 1 3 0.4)
487 0.4320000000226171))
488 (:test (progn
489 (set-seed 526)
490 (ensure-same
491 (binomial-rand 5 3 0.4)
492 (list 2 2 0 1 2)))))
496 ;;;; Object System tests
498 ;;(deftestsuite lisp-stat-proto-objects (lisp-stat)
499 ;; ()
500 ;; (:documentation "Make sure the proto object system is valid.")
501 ;; (:tests
502 ;; (create-proto (ensure (object-proto-p (defproto test-me))))
503 ;; (create-proto2 (ensure (object-proto-p (defproto2 test-me2))))
504 ;; (instance1 (ensure (send test-me :isnew)))
505 ;; (instance1-2 (ensure (send test-me2 :isnew)))
506 ;; (instance2 (ensure (send test-me :has-slot 'new)))
507 ;; (instance2-2 (ensure (send test-me2 :has-slot 'new)))
509 ;; (instance5 (ensure (send test-me :has-slot 'new)))
510 ;; (instance5-2 (ensure (send test-me2 :has-slot 'new)))
511 ;; (instance5 (ensure (send test-me :own-slots 'new)))
512 ;; (instance5-2 (ensure (send test-me2 :own-slots 'new)))
513 ;; (instance5 (ensure (send test-me :has-slot 'new)))
514 ;; (instance5-2 (ensure (send test-me2 :has-slot 'new)))
515 ;; (instance5 (ensure (send test-me :has-slot 'new)))
516 ;; (instance5-2 (ensure (send test-me2 :has-slot 'new)))
518 ;; ))