working on a generics approach for numerical testing.
[CommonLispStat.git] / unittests.lisp
blobcf004dabfa967161b177ce3f5a58c9ad89e64ca6
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 conjugate realpart imagpart phase
21 min max logand logior logxor lognot ffloor fceiling
22 ftruncate fround signum cis)
23 (:export run-lisp-stat-tests run-lisp-stat-test scoreboard))
25 (in-package :lisp-stat-unittests)
27 ;;; TESTS
29 (defun run-lisp-stat-tests ()
30 (run-tests :suite 'lisp-stat))
32 (defun run-lisp-stat-test (&rest x)
33 (run-test x))
36 (deftestsuite lisp-stat () ())
37 (deftestsuite lisp-stat-lin-alg (lisp-stat) ())
38 (deftestsuite lisp-stat-spec-fns (lisp-stat) ())
39 (deftestsuite lisp-stat-probdistn (lisp-stat) ())
42 (defun almost= (a b &key (tol 0.000001))
43 "Numerically compares 2 values to a tolerance."
44 (< (abs (- a b)) tol))
46 (defun almost=lists (a b &key (tol 0.000001))
47 "Numerically compare 2 lists using almost=."
48 (if (and (null a) (null b))
50 (and (almost= (car a) (car b) :tol tol)
51 (almost=lists (cdr a) (cdr b) :tol tol))))
55 ;; Need to consider a CLOSy approach for almost= to cover the range of
56 ;; possible data structures that we would like to be equal to a
57 ;; particular tolerance range. For example, fill in a shell like:
59 (defgeneric numerical= (a b &key tol))
61 (defmethod numerical= ((a real) (b real) &key (tol 0.00001)) ;; real))
62 (print (format nil " equality pred for real a=%l real b=%l" a b))
63 (< (abs (- a b)) tol))
65 (defmethod numerical= ((a integer) (b integer) &key (tol 0.1)) ;; real))
66 (print (format nil " equality pred for int a=~l int b=~l" (list a b)))
67 (< (abs (- a b)) tol))
69 (defmethod numerical= ((a complex) (b complex) &key (tol 0.00001)) ;; real))
70 (< (abs (- a b)) tol))
72 ;; can we use sequence for both array and list? I think so.
73 (defmethod numerical= ((a sequence) (b sequence) &key (tol 0.00001))
74 (print (format nil "checking equality for list a %l list b=%l" a b))
75 (if (and (= (length a) (length b))
76 (> (length a) 0)
77 (numerical= (car a) (car b) :tol tol))
78 (progn
79 (numerical= (cdr a) (cdr b) :tol tol))
80 nil))
81 ;; FIXME++++ This is too slow, a few too many comparisons.
83 (numerical= (list 2.0 2.0 2.2) (list 2.1 2.0 2.2))
84 (numerical= (list 2.1 2.0 2.2) (list 2.1 2.0 2.2))
86 (numerical= (list 2.1 2.0 2.2 4.2) (list 2.1 2.0 2.2 4.2))
87 (numerical= (list 2.1 2.0 2.3 4.0) (list 2.1 2.0 2.2 4.0))
89 (let ((a (list 2.1 2.0 2.2 4.2))
90 (b (list 2.0 2.1 2.2 4.2)))
91 (and (= (length a) (length b))
92 (numerical= (car a) (car b))))
95 ;; (defmethod numerical= ((complex a) (complex b) &key (tol 0.00001))
96 ;; (defmethod numerical= ((list a) (list b) &key (tol 0.00001))
97 ;; (defmethod numerical= ((array a) (array b) &key (tol 0.00001))
101 (deftestsuite lisp-stat-testsupport (lisp-stat)
103 (:tests
104 (almost=1 (ensure (almost= 3 3.001 :tol 0.01)))
105 (almost=2 (ensure (almost= 3 3.01 :tol 0.01)))
106 (almost=3 (ensure (not (almost= 3 3.1 :tol 0.01))))
107 (almost=lists1 (ensure (almost=lists nil nil :tol 0.01)))
108 (almost=lists2 (ensure (almost=lists (list ) (list ) :tol 0.01)))
109 (almost=lists3 (ensure (almost=lists (list 1.0) (list 1.0) :tol 0.01)))
110 (almost=lists4 (ensure (almost=lists (list 1.0 1.0) (list 1.0 1.0) :tol 0.01)))
111 (almost=lists5 (ensure (not (almost=lists (list 1.0 1.0)
112 (list 1.0 1.1) :tol 0.01))))))
114 (deftestsuite lisp-stat-testsupport2 (lisp-stat)
116 (:tests
117 (numerical=1 (ensure (numerical= 3 3.001 :tol 0.01)))
118 (numerical=2 (ensure (numerical= 3 3.01 :tol 0.01)))
119 (numerical=3 (ensure (not (numerical= 3 3.1 :tol 0.01))))
120 (numerical=4 (ensure (numerical= nil nil :tol 0.01)))
121 (numerical=5 (ensure (numerical= (list ) (list ) :tol 0.01)))
122 (numerical=6 (ensure (numerical= (list 1.0) (list 1.0) :tol 0.01)))
123 (numerical=7 (ensure (numerical= (list 1.0 1.0) (list 1.0 1.0) :tol 0.01)))
124 (numerical=8 (ensure (not (numerical= (list 1.0 1.0)
125 (list 1.0 1.1) :tol 0.01))))))
128 (numerical= 2.0 2.0)
129 (numerical= 2.0 2.1)
130 (numerical= 2.0 2.1 :tol 0.5)
131 (numerical= 2 2)
132 (numerical= 2 3)
133 (numerical= 2.0 (list 2.1 2.0 2.2))
138 (addtest (lisp-stat-lin-alg) cholesky-decomposition-1
139 (ensure-same
140 (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
141 (list #2A((1.7888543819998317 0.0 0.0)
142 (1.6770509831248424 0.11180339887498929 0.0)
143 (2.23606797749979 2.23606797749979 3.332000937312528e-8))
144 5.000000000000003)
145 :test 'almost=lists))
147 (addtest (lisp-stat-lin-alg) lu-decomposition
148 (ensure-same
149 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
150 (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)))
152 (addtest (lisp-stat-lin-alg) rcondest
153 ;; (ensure-same
154 (ensure-error ;; it barfs, FIXME!!
155 (rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
156 6.8157451e7
157 :test 'almost=))
159 (addtest (lisp-stat-lin-alg) lu-solve
160 (ensure-same
161 (lu-solve
162 (lu-decomp
163 #2A((2 3 4) (1 2 4) (2 4 5)))
164 #(2 3 4))
165 #(-2.333333333333333 1.3333333333333335 0.6666666666666666)))
167 (addtest (lisp-stat-lin-alg) inverse
168 (ensure-same
169 (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
170 #2A((2.0 -0.33333333333333326 -1.3333333333333335)
171 (-1.0 -0.6666666666666666 1.3333333333333333)
172 (0.0 0.6666666666666666 -0.3333333333333333))))
174 (addtest (lisp-stat-lin-alg) sv-decomp
175 (ensure-same
176 (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
177 (list #2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
178 (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
179 (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
180 #(9.699290438141343 0.8971681569301373 0.3447525123483081)
181 #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
182 (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
183 (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
185 :test 'almost=lists))
187 (addtest (lisp-stat-lin-alg) qr-decomp
188 (ensure-same
189 (qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
190 (list #2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
191 (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
192 (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
193 #2A((-3.0 -5.333333333333334 -7.333333333333332)
194 (0.0 -0.7453559924999292 -1.1925695879998877)
195 (0.0 0.0 -1.3416407864998738)))
196 :test 'almost=lists))
198 (addtest (lisp-stat-lin-alg) eigen
199 (ensure-same
200 (eigen #2A((2 3 4) (1 2 4) (2 4 5)))
201 (list #(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
202 (list #(0.4999999999999998 0.4999999999999997 0.7071067811865475)
203 #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
204 #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
205 NIL)))
207 (addtest (lisp-stat-lin-alg) spline
208 (ensure-same
209 (spline #(1.0 1.2 1.3 1.8 2.1 2.5)
210 #(1.2 2.0 2.1 2.0 1.1 2.8)
211 :xvals 6)
212 (list (list 1.0 1.3 1.6 1.9 2.2 2.5)
213 (list 1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))
214 :test 'almost=lists))
216 (addtest (lisp-stat-lin-alg) kernel-smooth
217 (ensure-same
218 ;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
219 (kernel-smooth
220 #(1.0 1.2 1.3 1.8 2.1 2.5)
221 #(1.2 2.0 2.1 2.0 1.1 2.8)
222 :xvals 5)
223 (list (list 1.0 1.375 1.75 2.125 2.5)
224 (list 1.6603277642110226 1.9471748095239771 1.7938127405752287
225 1.5871511322219498 2.518194783156392))
226 :test 'almost=lists))
228 (addtest (lisp-stat-lin-alg) kernel-dens
229 (ensure-same
230 (kernel-dens
231 #(1.0 1.2 2.5 2.1 1.8 1.2)
232 :xvals 5)
233 (list (list 1.0 1.375 1.75 2.125 2.5)
234 (list 0.7224150453621405 0.5820045548233707 0.38216411702854214
235 0.4829822708587095 0.3485939156929503))))
238 (addtest (lisp-stat-lin-alg) fft
239 (ensure-same
240 (fft #(1.0 1.2 2.5 2.1 1.8))
241 (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)))
242 :test 'almost=lists))
245 (addtest (lisp-stat-lin-alg) lowess
246 (ensure-same
247 (lowess #(1.0 1.2 2.5 2.1 1.8 1.2)
248 #(1.2 2.0 2.1 2.0 1.1 2.8))
249 #(1.0 1.2 1.2 1.8 2.1 2.5)
250 :test 'almost=lists)) ;; result isn't a list!
254 ;;;; Log-gamma function
256 (addtest (lisp-stat-spec-fns) log-gamma-fn
257 (ensure-same
258 (log-gamma 3.4)
259 1.0923280596789584
260 :test 'almost=))
263 ;;; Probability distributions
265 ;; This macro should be generalized, but it's a good start now.
266 ;;(defmacro ProbDistnTests (prefixName
267 ;; quant-params quant-answer
268 ;; cdf-params cdf-answer
269 ;; pmf-params pmf-answer
270 ;; rand-params rand-answer)
271 ;; (deftestsuite lisp-stat-probdist-,prefixName (lisp-stat-probdistn)
272 ;; ;; (( ))
273 ;; (:documentation "testing for ,testName distribution results")
274 ;; (:test (ensure-same
275 ;; (lisp-stat-basics:,testName-quant ,quant-params) ,quant-answer))
276 ;; (:test (ensure-same
277 ;; (lisp-stat-basics:,testName-cdf ,cdf-params) ,cdf-answer))
278 ;; (:test (ensure-same
279 ;; (lisp-stat-basics:,testName-pmf ,pmf-params) ,pmf-answer))
280 ;; (:test (progn
281 ;; (set-seed 234)
282 ;; (ensure-same
283 ;; (lisp-stat-basics:,testName-rand ,rand-params) ,rand-answer)))))
285 ;;; Normal distribution
287 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
289 (:documentation "testing for Gaussian distn results")
290 (:test (ensure-same
291 (normal-quant 0.95)
292 1.6448536279366268))
293 (:test (ensure-same
294 (normal-cdf 1.3)
295 0.9031995154143897))
296 (:test (ensure-same
297 (normal-dens 1.3)
298 0.17136859204780736))
299 (:test (ensure-same
300 (normal-rand 2)
301 (list -0.40502015f0 -0.8091404f0)))
302 (:test (ensure-same
303 (bivnorm-cdf 0.2 0.4 0.6)
304 0.4736873734160288)))
306 ;;;; Cauchy distribution
308 (deftestsuite lisp-stat-probdist-cauchy (lisp-stat-probdistn)
310 (:documentation "testing for Cachy-distn results")
311 (:test (ensure-same
312 (cauchy-quant 0.95)
313 6.313751514675031))
314 (:test (ensure-same
315 (cauchy-cdf 1.3)
316 0.7912855998398473))
317 (:test (ensure-same
318 (cauchy-dens 1.3)
319 0.1183308127104695 ))
320 (:test (ensure-same
321 (cauchy-rand 2)
322 (list -1.06224644160405 -0.4524695943939537))))
324 ;;;; Gamma distribution
326 (deftestsuite lisp-stat-probdist-gamma (lisp-stat-probdistn)
328 (:documentation "testing for gamma distn results")
329 (:test (ensure-same
330 (gamma-quant 0.95 4.3)
331 8.178692439291645))
332 (:test (ensure-same
333 (gamma-cdf 1.3 4.3)
334 0.028895150986674906))
335 (:test (ensure-same
336 (gamma-dens 1.3 4.3)
337 0.0731517686447374))
338 (:test (ensure-same
339 (gamma-rand 2 4.3)
340 (list 2.454918912880936 4.081365384357454))))
342 ;;;; Chi-square distribution
344 (deftestsuite lisp-stat-probdist-chisq (lisp-stat-probdistn)
346 (:documentation "testing for Chi-square distn results")
347 (:test (ensure-same
348 (chisq-quant 0.95 3)
349 7.814727903379012))
350 (:test (ensure-same
351 (chisq-cdf 1 5)
352 0.03743422675631789))
353 (:test (ensure-same
354 (chisq-dens 1 5)
355 0.08065690818083521))
356 (:test (progn
357 (set-seed 353)
358 (ensure-same
359 (chisq-rand 2 4)
360 (list 1.968535826180572 2.9988646156942997)))))
362 ;;;; Beta distribution
364 (deftestsuite lisp-stat-probdist-beta (lisp-stat-probdistn)
366 (:documentation "testing for beta distn results")
367 (:test (ensure-same
368 (beta-quant 0.95 3 2)
369 0.9023885371149876))
370 (:test (ensure-same
371 (beta-cdf 0.4 2 2.4)
372 0.4247997418541529 ))
373 (:test (ensure-same
374 (beta-dens 0.4 2 2.4)
375 1.5964741858913518 ))
376 (:test (ensure-same
377 (beta-rand 2 2 2.4)
378 (list 0.8014897077282279 0.6516371997922659))))
380 ;;;; t distribution
382 (deftestsuite lisp-stat-probdist-t (lisp-stat-probdistn)
384 (:documentation "testing for t-distn results")
385 (:test (ensure-same
386 (t-quant 0.95 3)
387 2.35336343484194))
388 (:test (ensure-same
389 (t-cdf 1 2.3)
390 0.794733624298342))
391 (:test (ensure-same
392 (t-dens 1 2.3)
393 0.1978163816318102))
394 (:test (ensure-same
395 (t-rand 2 2.3)
396 (list -0.34303672776089306 -1.142505872436518))))
398 ;;;; F distribution
400 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
402 (:documentation "testing for f-distn results")
403 (:test (ensure-same
404 (f-quant 0.95 3 5) 5.409451318117459))
405 (:test (ensure-same
406 (f-cdf 1 3.2 5.4)
407 0.5347130905510765))
408 (:test (ensure-same
409 (f-dens 1 3.2 5.4)
410 0.37551128864591415))
411 (:test (progn
412 (set-seed 234)
413 (ensure-same
414 (f-rand 2 3 2)
415 (list 0.7939093442091963 0.07442694152491144)))))
417 ;;;; Poisson distribution
419 (deftestsuite lisp-stat-probdist-poisson (lisp-stat-probdistn)
421 (:documentation "testing for poisson distribution results")
422 (:test (ensure-same
423 (poisson-quant 0.95 3.2) 6))
424 (:test (ensure-same
425 (poisson-cdf 1 3.2)
426 0.17120125672252395))
427 (:test (ensure-same
428 (poisson-pmf 1 3.2)
429 0.13043905274097067))
430 (:test (progn
431 (set-seed 234)
432 (ensure-same
433 (poisson-rand 5 3.2)
434 (list 2 1 2 0 3)))))
436 ;; Binomial distribution
438 (deftestsuite lisp-stat-probdist-binomial (lisp-stat-probdistn)
440 (:documentation "testing for binomial distribution results")
442 (:test (ensure-same
443 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
445 (:test (ensure-same
446 (binomial-quant 0 3 0.4)
447 ;; -2147483648
449 (:test (ensure-same
450 (binomial-cdf 1 3 0.4)
451 0.6479999999965776))
453 (:test (ensure-same
454 (binomial-pmf 1 3 0.4)
455 0.4320000000226171))
456 (:test (progn
457 (set-seed 526)
458 (ensure-same
459 (binomial-rand 5 3 0.4)
460 (list 2 2 0 1 2)))))