more tests to track down multiple regression failures
[CommonLispStat.git] / unittests.lisp
blob85e27269be61231064005b455bfff778329cb78e
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))
171 ;(addtest (lisp-stat-lin-alg) cholesky-decomposition-1a
172 ; (ensure-same
173 ; (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
174 ; (list #2A((1.7888543819998317 0.0 0.0)
175 ; (1.6770509831248424 0.11180339887498929 0.0)
176 ; (2.23606797749979 2.23606797749979 3.332000937312528e-8))
177 ; 5.000000000000003)
178 ; :test 'numerical=))
180 (addtest (lisp-stat-lin-alg) lu-decomposition
181 (ensure-same
182 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
183 (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)))
185 (addtest (lisp-stat-lin-alg) rcondest
186 ;; (ensure-same
187 (ensure-error ;; it barfs, FIXME!!
188 (rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
189 6.8157451e7
190 :test 'almost=))
192 (addtest (lisp-stat-lin-alg) lu-solve
193 (ensure-same
194 (lu-solve
195 (lu-decomp
196 #2A((2 3 4) (1 2 4) (2 4 5)))
197 #(2 3 4))
198 #(-2.333333333333333 1.3333333333333335 0.6666666666666666)))
200 (addtest (lisp-stat-lin-alg) inverse
201 (ensure-same
202 (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
203 #2A((2.0 -0.33333333333333326 -1.3333333333333335)
204 (-1.0 -0.6666666666666666 1.3333333333333333)
205 (0.0 0.6666666666666666 -0.3333333333333333))))
207 (addtest (lisp-stat-lin-alg) sv-decomp
208 (ensure-same
209 (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
210 (list #2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
211 (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
212 (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
213 #(9.699290438141343 0.8971681569301373 0.3447525123483081)
214 #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
215 (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
216 (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
218 :test 'almost=lists))
220 (addtest (lisp-stat-lin-alg) qr-decomp
221 (ensure-same
222 (qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
223 (list #2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
224 (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
225 (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
226 #2A((-3.0 -5.333333333333334 -7.333333333333332)
227 (0.0 -0.7453559924999292 -1.1925695879998877)
228 (0.0 0.0 -1.3416407864998738)))
229 :test 'almost=lists))
231 (addtest (lisp-stat-lin-alg) eigen
232 (ensure-same
233 (eigen #2A((2 3 4) (1 2 4) (2 4 5)))
234 (list #(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
235 (list #(0.4999999999999998 0.4999999999999997 0.7071067811865475)
236 #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
237 #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
238 NIL)))
240 (addtest (lisp-stat-lin-alg) spline
241 (ensure-same
242 (spline #(1.0 1.2 1.3 1.8 2.1 2.5)
243 #(1.2 2.0 2.1 2.0 1.1 2.8)
244 :xvals 6)
245 (list (list 1.0 1.3 1.6 1.9 2.2 2.5)
246 (list 1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))
247 :test 'almost=lists))
249 (addtest (lisp-stat-lin-alg) kernel-smooth
250 (ensure-same
251 ;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
252 (kernel-smooth
253 #(1.0 1.2 1.3 1.8 2.1 2.5)
254 #(1.2 2.0 2.1 2.0 1.1 2.8)
255 :xvals 5)
256 (list (list 1.0 1.375 1.75 2.125 2.5)
257 (list 1.6603277642110226 1.9471748095239771 1.7938127405752287
258 1.5871511322219498 2.518194783156392))
259 :test 'almost=lists))
261 (addtest (lisp-stat-lin-alg) kernel-dens
262 (ensure-same
263 (kernel-dens
264 #(1.0 1.2 2.5 2.1 1.8 1.2)
265 :xvals 5)
266 (list (list 1.0 1.375 1.75 2.125 2.5)
267 (list 0.7224150453621405 0.5820045548233707 0.38216411702854214
268 0.4829822708587095 0.3485939156929503))))
271 (addtest (lisp-stat-lin-alg) fft
272 (ensure-same
273 (fft #(1.0 1.2 2.5 2.1 1.8))
274 (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)))
275 :test 'almost=lists))
278 (addtest (lisp-stat-lin-alg) lowess
279 (ensure-same
280 (lowess #(1.0 1.2 2.5 2.1 1.8 1.2)
281 #(1.2 2.0 2.1 2.0 1.1 2.8))
282 #(1.0 1.2 1.2 1.8 2.1 2.5)
283 :test 'almost=lists)) ;; result isn't a list!
287 ;;;; Log-gamma function
289 (addtest (lisp-stat-spec-fns) log-gamma-fn
290 (ensure-same
291 (log-gamma 3.4)
292 1.0923280596789584
293 :test 'almost=))
296 ;;; Probability distributions
298 ;; This macro should be generalized, but it's a good start now.
299 ;;(defmacro ProbDistnTests (prefixName
300 ;; quant-params quant-answer
301 ;; cdf-params cdf-answer
302 ;; pmf-params pmf-answer
303 ;; rand-params rand-answer)
304 ;; (deftestsuite lisp-stat-probdist-,prefixName (lisp-stat-probdistn)
305 ;; ;; (( ))
306 ;; (:documentation "testing for ,testName distribution results")
307 ;; (:test (ensure-same
308 ;; (lisp-stat-basics:,testName-quant ,quant-params) ,quant-answer))
309 ;; (:test (ensure-same
310 ;; (lisp-stat-basics:,testName-cdf ,cdf-params) ,cdf-answer))
311 ;; (:test (ensure-same
312 ;; (lisp-stat-basics:,testName-pmf ,pmf-params) ,pmf-answer))
313 ;; (:test (progn
314 ;; (set-seed 234)
315 ;; (ensure-same
316 ;; (lisp-stat-basics:,testName-rand ,rand-params) ,rand-answer)))))
318 ;;; Normal distribution
320 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
322 (:documentation "testing for Gaussian distn results")
323 (:test (ensure-same
324 (normal-quant 0.95)
325 1.6448536279366268))
326 (:test (ensure-same
327 (normal-cdf 1.3)
328 0.9031995154143897))
329 (:test (ensure-same
330 (normal-dens 1.3)
331 0.17136859204780736))
332 (:test (ensure-same
333 (normal-rand 2)
334 (list -0.40502015f0 -0.8091404f0)))
335 (:test (ensure-same
336 (bivnorm-cdf 0.2 0.4 0.6)
337 0.4736873734160288)))
339 ;;;; Cauchy distribution
341 (deftestsuite lisp-stat-probdist-cauchy (lisp-stat-probdistn)
343 (:documentation "testing for Cachy-distn results")
344 (:test (ensure-same
345 (cauchy-quant 0.95)
346 6.313751514675031))
347 (:test (ensure-same
348 (cauchy-cdf 1.3)
349 0.7912855998398473))
350 (:test (ensure-same
351 (cauchy-dens 1.3)
352 0.1183308127104695 ))
353 (:test (ensure-same
354 (cauchy-rand 2)
355 (list -1.06224644160405 -0.4524695943939537))))
357 ;;;; Gamma distribution
359 (deftestsuite lisp-stat-probdist-gamma (lisp-stat-probdistn)
361 (:documentation "testing for gamma distn results")
362 (:test (ensure-same
363 (gamma-quant 0.95 4.3)
364 8.178692439291645))
365 (:test (ensure-same
366 (gamma-cdf 1.3 4.3)
367 0.028895150986674906))
368 (:test (ensure-same
369 (gamma-dens 1.3 4.3)
370 0.0731517686447374))
371 (:test (ensure-same
372 (gamma-rand 2 4.3)
373 (list 2.454918912880936 4.081365384357454))))
375 ;;;; Chi-square distribution
377 (deftestsuite lisp-stat-probdist-chisq (lisp-stat-probdistn)
379 (:documentation "testing for Chi-square distn results")
380 (:test (ensure-same
381 (chisq-quant 0.95 3)
382 7.814727903379012))
383 (:test (ensure-same
384 (chisq-cdf 1 5)
385 0.03743422675631789))
386 (:test (ensure-same
387 (chisq-dens 1 5)
388 0.08065690818083521))
389 (:test (progn
390 (set-seed 353)
391 (ensure-same
392 (chisq-rand 2 4)
393 (list 1.968535826180572 2.9988646156942997)))))
395 ;;;; Beta distribution
397 (deftestsuite lisp-stat-probdist-beta (lisp-stat-probdistn)
399 (:documentation "testing for beta distn results")
400 (:test (ensure-same
401 (beta-quant 0.95 3 2)
402 0.9023885371149876))
403 (:test (ensure-same
404 (beta-cdf 0.4 2 2.4)
405 0.4247997418541529 ))
406 (:test (ensure-same
407 (beta-dens 0.4 2 2.4)
408 1.5964741858913518 ))
409 (:test (ensure-same
410 (beta-rand 2 2 2.4)
411 (list 0.8014897077282279 0.6516371997922659))))
413 ;;;; t distribution
415 (deftestsuite lisp-stat-probdist-t (lisp-stat-probdistn)
417 (:documentation "testing for t-distn results")
418 (:test (ensure-same
419 (t-quant 0.95 3)
420 2.35336343484194))
421 (:test (ensure-same
422 (t-cdf 1 2.3)
423 0.794733624298342))
424 (:test (ensure-same
425 (t-dens 1 2.3)
426 0.1978163816318102))
427 (:test (ensure-same
428 (t-rand 2 2.3)
429 (list -0.34303672776089306 -1.142505872436518))))
431 ;;;; F distribution
433 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
435 (:documentation "testing for f-distn results")
436 (:test (ensure-same
437 (f-quant 0.95 3 5) 5.409451318117459))
438 (:test (ensure-same
439 (f-cdf 1 3.2 5.4)
440 0.5347130905510765))
441 (:test (ensure-same
442 (f-dens 1 3.2 5.4)
443 0.37551128864591415))
444 (:test (progn
445 (set-seed 234)
446 (ensure-same
447 (f-rand 2 3 2)
448 (list 0.7939093442091963 0.07442694152491144)))))
450 ;;;; Poisson distribution
452 (deftestsuite lisp-stat-probdist-poisson (lisp-stat-probdistn)
454 (:documentation "testing for poisson distribution results")
455 (:test (ensure-same
456 (poisson-quant 0.95 3.2) 6))
457 (:test (ensure-same
458 (poisson-cdf 1 3.2)
459 0.17120125672252395))
460 (:test (ensure-same
461 (poisson-pmf 1 3.2)
462 0.13043905274097067))
463 (:test (progn
464 (set-seed 234)
465 (ensure-same
466 (poisson-rand 5 3.2)
467 (list 2 1 2 0 3)))))
469 ;; Binomial distribution
471 (deftestsuite lisp-stat-probdist-binomial (lisp-stat-probdistn)
473 (:documentation "testing for binomial distribution results")
475 (:test (ensure-same
476 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
478 (:test (ensure-same
479 (binomial-quant 0 3 0.4)
480 ;; -2147483648
482 (:test (ensure-same
483 (binomial-cdf 1 3 0.4)
484 0.6479999999965776))
486 (:test (ensure-same
487 (binomial-pmf 1 3 0.4)
488 0.4320000000226171))
489 (:test (progn
490 (set-seed 526)
491 (ensure-same
492 (binomial-rand 5 3 0.4)
493 (list 2 2 0 1 2)))))
497 ;;;; Object System tests
499 ;;(deftestsuite lisp-stat-proto-objects (lisp-stat)
500 ;; ()
501 ;; (:documentation "Make sure the proto object system is valid.")
502 ;; (:tests
503 ;; (create-proto (ensure (object-proto-p (defproto test-me))))
504 ;; (create-proto2 (ensure (object-proto-p (defproto2 test-me2))))
505 ;; (instance1 (ensure (send test-me :isnew)))
506 ;; (instance1-2 (ensure (send test-me2 :isnew)))
507 ;; (instance2 (ensure (send test-me :has-slot 'new)))
508 ;; (instance2-2 (ensure (send test-me2 :has-slot 'new)))
510 ;; (instance5 (ensure (send test-me :has-slot 'new)))
511 ;; (instance5-2 (ensure (send test-me2 :has-slot 'new)))
512 ;; (instance5 (ensure (send test-me :own-slots 'new)))
513 ;; (instance5-2 (ensure (send test-me2 :own-slots 'new)))
514 ;; (instance5 (ensure (send test-me :has-slot 'new)))
515 ;; (instance5-2 (ensure (send test-me2 :has-slot 'new)))
516 ;; (instance5 (ensure (send test-me :has-slot 'new)))
517 ;; (instance5-2 (ensure (send test-me2 :has-slot 'new)))
519 ;; ))