Doc cleanup
[CommonLispStat.git] / unittests.lisp
blob6faddf579a5585d98216bb05728d4316eb2c5cdd
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)) ;; real))
72 ;; (< (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 both array and lists -- need to check multi-dim 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 ;; (defmethod numerical= ((complex a) (complex b) &key (tol 0.00001))
91 ;; (defmethod numerical= ((list a) (list b) &key (tol 0.00001))
92 ;; (defmethod numerical= ((array a) (array b) &key (tol 0.00001))
96 (deftestsuite lisp-stat-testsupport (lisp-stat)
98 (:tests
99 (almost=1 (ensure (almost= 3 3.001 :tol 0.01)))
100 (almost=2 (ensure (almost= 3 3.01 :tol 0.01)))
101 (almost=3 (ensure (not (almost= 3 3.1 :tol 0.01))))
102 (almost=lists1 (ensure (almost=lists nil nil :tol 0.01)))
103 (almost=lists2 (ensure (almost=lists (list ) (list ) :tol 0.01)))
104 (almost=lists3 (ensure (almost=lists (list 1.0) (list 1.0) :tol 0.01)))
105 (almost=lists4 (ensure (almost=lists (list 1.0 1.0) (list 1.0 1.0) :tol 0.01)))
106 (almost=lists5 (ensure (not (almost=lists (list 1.0 1.0)
107 (list 1.0 1.1) :tol 0.01))))))
109 (deftestsuite lisp-stat-testsupport2 (lisp-stat)
111 (:tests
112 (numerical=1 (ensure (numerical= 3 3.001 :tol 0.01)))
113 (numerical=1.1 (ensure (numerical= 2 2)))
114 (numerical=1.2 (ensure (not (numerical= 2 3))))
115 (numerical=2 (ensure (numerical= 3 3.01 :tol 0.01)))
116 (numerical=3 (ensure (not (numerical= 3 3.1 :tol 0.01))))
117 (numerical=4 (ensure (numerical= nil nil :tol 0.01)))
118 (numerical=5 (ensure (numerical= (list ) (list ) :tol 0.01)))
119 (numerical=6 (ensure (numerical= (list 1.0) (list 1.0) :tol 0.01)))
120 (numerical=7 (ensure (numerical= (list 1.0 1.0) (list 1.0 1.0) :tol 0.01)))
121 (numerical=7.5 (ensure-error (numerical= 1.0 (list 1.0 1.0) :tol 0.01)))
122 (numerical=8 (ensure (not (numerical= (list 2.0 2.0 2.2) (list 2.1 2.0 2.2)))))
123 (numerical=9 (ensure (numerical= (list 2.1 2.0 2.2) (list 2.1 2.0 2.2)) ))
124 (numerical=10 (ensure (numerical= (list 2.1 2.0 2.2 4.2) (list 2.1 2.0 2.2 4.2))))
125 (numerical=11 (ensure (not (numerical= (list 2.1 2.0 2.3 4.0) (list 2.1 2.0 2.2 4.0)))))
126 (numerical=12 (ensure (not (numerical= (list 1.0 1.0)
127 (list 1.0 1.1) :tol 0.01))))))
131 (addtest (lisp-stat-lin-alg) cholesky-decomposition-1
132 (ensure-same
133 (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
134 (list #2A((1.7888543819998317 0.0 0.0)
135 (1.6770509831248424 0.11180339887498929 0.0)
136 (2.23606797749979 2.23606797749979 3.332000937312528e-8))
137 5.000000000000003)
138 :test 'almost=lists))
140 (addtest (lisp-stat-lin-alg) lu-decomposition
141 (ensure-same
142 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
143 (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)))
145 (addtest (lisp-stat-lin-alg) rcondest
146 ;; (ensure-same
147 (ensure-error ;; it barfs, FIXME!!
148 (rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
149 6.8157451e7
150 :test 'almost=))
152 (addtest (lisp-stat-lin-alg) lu-solve
153 (ensure-same
154 (lu-solve
155 (lu-decomp
156 #2A((2 3 4) (1 2 4) (2 4 5)))
157 #(2 3 4))
158 #(-2.333333333333333 1.3333333333333335 0.6666666666666666)))
160 (addtest (lisp-stat-lin-alg) inverse
161 (ensure-same
162 (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
163 #2A((2.0 -0.33333333333333326 -1.3333333333333335)
164 (-1.0 -0.6666666666666666 1.3333333333333333)
165 (0.0 0.6666666666666666 -0.3333333333333333))))
167 (addtest (lisp-stat-lin-alg) sv-decomp
168 (ensure-same
169 (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
170 (list #2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
171 (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
172 (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
173 #(9.699290438141343 0.8971681569301373 0.3447525123483081)
174 #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
175 (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
176 (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
178 :test 'almost=lists))
180 (addtest (lisp-stat-lin-alg) qr-decomp
181 (ensure-same
182 (qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
183 (list #2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
184 (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
185 (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
186 #2A((-3.0 -5.333333333333334 -7.333333333333332)
187 (0.0 -0.7453559924999292 -1.1925695879998877)
188 (0.0 0.0 -1.3416407864998738)))
189 :test 'almost=lists))
191 (addtest (lisp-stat-lin-alg) eigen
192 (ensure-same
193 (eigen #2A((2 3 4) (1 2 4) (2 4 5)))
194 (list #(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
195 (list #(0.4999999999999998 0.4999999999999997 0.7071067811865475)
196 #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
197 #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
198 NIL)))
200 (addtest (lisp-stat-lin-alg) spline
201 (ensure-same
202 (spline #(1.0 1.2 1.3 1.8 2.1 2.5)
203 #(1.2 2.0 2.1 2.0 1.1 2.8)
204 :xvals 6)
205 (list (list 1.0 1.3 1.6 1.9 2.2 2.5)
206 (list 1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))
207 :test 'almost=lists))
209 (addtest (lisp-stat-lin-alg) kernel-smooth
210 (ensure-same
211 ;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
212 (kernel-smooth
213 #(1.0 1.2 1.3 1.8 2.1 2.5)
214 #(1.2 2.0 2.1 2.0 1.1 2.8)
215 :xvals 5)
216 (list (list 1.0 1.375 1.75 2.125 2.5)
217 (list 1.6603277642110226 1.9471748095239771 1.7938127405752287
218 1.5871511322219498 2.518194783156392))
219 :test 'almost=lists))
221 (addtest (lisp-stat-lin-alg) kernel-dens
222 (ensure-same
223 (kernel-dens
224 #(1.0 1.2 2.5 2.1 1.8 1.2)
225 :xvals 5)
226 (list (list 1.0 1.375 1.75 2.125 2.5)
227 (list 0.7224150453621405 0.5820045548233707 0.38216411702854214
228 0.4829822708587095 0.3485939156929503))))
231 (addtest (lisp-stat-lin-alg) fft
232 (ensure-same
233 (fft #(1.0 1.2 2.5 2.1 1.8))
234 (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)))
235 :test 'almost=lists))
238 (addtest (lisp-stat-lin-alg) lowess
239 (ensure-same
240 (lowess #(1.0 1.2 2.5 2.1 1.8 1.2)
241 #(1.2 2.0 2.1 2.0 1.1 2.8))
242 #(1.0 1.2 1.2 1.8 2.1 2.5)
243 :test 'almost=lists)) ;; result isn't a list!
247 ;;;; Log-gamma function
249 (addtest (lisp-stat-spec-fns) log-gamma-fn
250 (ensure-same
251 (log-gamma 3.4)
252 1.0923280596789584
253 :test 'almost=))
256 ;;; Probability distributions
258 ;; This macro should be generalized, but it's a good start now.
259 ;;(defmacro ProbDistnTests (prefixName
260 ;; quant-params quant-answer
261 ;; cdf-params cdf-answer
262 ;; pmf-params pmf-answer
263 ;; rand-params rand-answer)
264 ;; (deftestsuite lisp-stat-probdist-,prefixName (lisp-stat-probdistn)
265 ;; ;; (( ))
266 ;; (:documentation "testing for ,testName distribution results")
267 ;; (:test (ensure-same
268 ;; (lisp-stat-basics:,testName-quant ,quant-params) ,quant-answer))
269 ;; (:test (ensure-same
270 ;; (lisp-stat-basics:,testName-cdf ,cdf-params) ,cdf-answer))
271 ;; (:test (ensure-same
272 ;; (lisp-stat-basics:,testName-pmf ,pmf-params) ,pmf-answer))
273 ;; (:test (progn
274 ;; (set-seed 234)
275 ;; (ensure-same
276 ;; (lisp-stat-basics:,testName-rand ,rand-params) ,rand-answer)))))
278 ;;; Normal distribution
280 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
282 (:documentation "testing for Gaussian distn results")
283 (:test (ensure-same
284 (normal-quant 0.95)
285 1.6448536279366268))
286 (:test (ensure-same
287 (normal-cdf 1.3)
288 0.9031995154143897))
289 (:test (ensure-same
290 (normal-dens 1.3)
291 0.17136859204780736))
292 (:test (ensure-same
293 (normal-rand 2)
294 (list -0.40502015f0 -0.8091404f0)))
295 (:test (ensure-same
296 (bivnorm-cdf 0.2 0.4 0.6)
297 0.4736873734160288)))
299 ;;;; Cauchy distribution
301 (deftestsuite lisp-stat-probdist-cauchy (lisp-stat-probdistn)
303 (:documentation "testing for Cachy-distn results")
304 (:test (ensure-same
305 (cauchy-quant 0.95)
306 6.313751514675031))
307 (:test (ensure-same
308 (cauchy-cdf 1.3)
309 0.7912855998398473))
310 (:test (ensure-same
311 (cauchy-dens 1.3)
312 0.1183308127104695 ))
313 (:test (ensure-same
314 (cauchy-rand 2)
315 (list -1.06224644160405 -0.4524695943939537))))
317 ;;;; Gamma distribution
319 (deftestsuite lisp-stat-probdist-gamma (lisp-stat-probdistn)
321 (:documentation "testing for gamma distn results")
322 (:test (ensure-same
323 (gamma-quant 0.95 4.3)
324 8.178692439291645))
325 (:test (ensure-same
326 (gamma-cdf 1.3 4.3)
327 0.028895150986674906))
328 (:test (ensure-same
329 (gamma-dens 1.3 4.3)
330 0.0731517686447374))
331 (:test (ensure-same
332 (gamma-rand 2 4.3)
333 (list 2.454918912880936 4.081365384357454))))
335 ;;;; Chi-square distribution
337 (deftestsuite lisp-stat-probdist-chisq (lisp-stat-probdistn)
339 (:documentation "testing for Chi-square distn results")
340 (:test (ensure-same
341 (chisq-quant 0.95 3)
342 7.814727903379012))
343 (:test (ensure-same
344 (chisq-cdf 1 5)
345 0.03743422675631789))
346 (:test (ensure-same
347 (chisq-dens 1 5)
348 0.08065690818083521))
349 (:test (progn
350 (set-seed 353)
351 (ensure-same
352 (chisq-rand 2 4)
353 (list 1.968535826180572 2.9988646156942997)))))
355 ;;;; Beta distribution
357 (deftestsuite lisp-stat-probdist-beta (lisp-stat-probdistn)
359 (:documentation "testing for beta distn results")
360 (:test (ensure-same
361 (beta-quant 0.95 3 2)
362 0.9023885371149876))
363 (:test (ensure-same
364 (beta-cdf 0.4 2 2.4)
365 0.4247997418541529 ))
366 (:test (ensure-same
367 (beta-dens 0.4 2 2.4)
368 1.5964741858913518 ))
369 (:test (ensure-same
370 (beta-rand 2 2 2.4)
371 (list 0.8014897077282279 0.6516371997922659))))
373 ;;;; t distribution
375 (deftestsuite lisp-stat-probdist-t (lisp-stat-probdistn)
377 (:documentation "testing for t-distn results")
378 (:test (ensure-same
379 (t-quant 0.95 3)
380 2.35336343484194))
381 (:test (ensure-same
382 (t-cdf 1 2.3)
383 0.794733624298342))
384 (:test (ensure-same
385 (t-dens 1 2.3)
386 0.1978163816318102))
387 (:test (ensure-same
388 (t-rand 2 2.3)
389 (list -0.34303672776089306 -1.142505872436518))))
391 ;;;; F distribution
393 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
395 (:documentation "testing for f-distn results")
396 (:test (ensure-same
397 (f-quant 0.95 3 5) 5.409451318117459))
398 (:test (ensure-same
399 (f-cdf 1 3.2 5.4)
400 0.5347130905510765))
401 (:test (ensure-same
402 (f-dens 1 3.2 5.4)
403 0.37551128864591415))
404 (:test (progn
405 (set-seed 234)
406 (ensure-same
407 (f-rand 2 3 2)
408 (list 0.7939093442091963 0.07442694152491144)))))
410 ;;;; Poisson distribution
412 (deftestsuite lisp-stat-probdist-poisson (lisp-stat-probdistn)
414 (:documentation "testing for poisson distribution results")
415 (:test (ensure-same
416 (poisson-quant 0.95 3.2) 6))
417 (:test (ensure-same
418 (poisson-cdf 1 3.2)
419 0.17120125672252395))
420 (:test (ensure-same
421 (poisson-pmf 1 3.2)
422 0.13043905274097067))
423 (:test (progn
424 (set-seed 234)
425 (ensure-same
426 (poisson-rand 5 3.2)
427 (list 2 1 2 0 3)))))
429 ;; Binomial distribution
431 (deftestsuite lisp-stat-probdist-binomial (lisp-stat-probdistn)
433 (:documentation "testing for binomial distribution results")
435 (:test (ensure-same
436 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
438 (:test (ensure-same
439 (binomial-quant 0 3 0.4)
440 ;; -2147483648
442 (:test (ensure-same
443 (binomial-cdf 1 3 0.4)
444 0.6479999999965776))
446 (:test (ensure-same
447 (binomial-pmf 1 3 0.4)
448 0.4320000000226171))
449 (:test (progn
450 (set-seed 526)
451 (ensure-same
452 (binomial-rand 5 3 0.4)
453 (list 2 2 0 1 2)))))