Unittests needs infrastructure for numerical comparisons to be fixed.
[CommonLispStat.git] / unittests.lisp
blobe00613113911538477ff3ae21ad11b6f82aef5b9
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 ;; Need to consider a CLOSy approach for almost= to cover the range of
43 ;; possible data structures that we would like to be equal to a
44 ;; particular tolerance range.
46 (defun almost= (a b &key (tol 0.000001))
47 (< (abs (- a b)) tol))
49 (defun almost=lists (a b &key (tol 0.000001))
50 "Numerically compare 2 lists using almost=."
51 (if (and (null a) (null b))
53 (and (almost= (car a) (car b) :tol tol)
54 (almost=lists (cdr a) (cdr b) :tol tol))))
56 (deftestsuite lisp-stat-unitsupport (lisp-stat) ())
58 ;;; FIXME THIS IS WRONG
59 (add-test (lisp-stat-testsupport) almost=-1
60 (almost= 3 3.001 :tol 0.01)
61 (almost= 3 3.01 :tol 0.01)
62 (almost= 3 3.1 :tol 0.01)
65 ;;; FIXME THIS IS WRONG
66 (add-test (lisp-stat-testsupport) almost=lists-1
67 (almost=lists nil nil :tol 0.01)
68 (almost=lists (list ) (list ) :tol 0.01)
69 (almost=lists (list 1.0) (list 1.0) :tol 0.01)
70 (almost=lists (list 1.0 1.0) (list 1.0 1.0) :tol 0.01)
71 (almost=lists (list 1.0 1.0) (list 1.0 1.1) :tol 0.01)
72 (almost=lists testlist1 testlist3 :tol 0.01)
75 ;; ;;; and add a test to it
76 ;; (addtest (lisp-stat)
77 ;; (ensure-same (+ 1 1) 2))
78 ;; ;; => #<Test passed>
80 ;; ;;; add another test using ensure-error
81 ;; (addtest (lisp-stat-lin-alg)
82 ;; (ensure-error (let ((x 0)) (/ x))))
83 ;; ;; => #<Test passed>
85 ;; ;;; add another, slightly more specific test
86 ;; (addtest (lisp-stat)
87 ;; (ensure-condition division-by-zero (let ((x 0)) (/ x))))
88 ;; ;; => #<Test passed>
90 (addtest (lisp-stat-lin-alg) cholesky-decomposition-1
91 (ensure-same
92 (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
93 (list #2A((1.7888543819998317 0.0 0.0)
94 (1.6770509831248424 0.11180339887498929 0.0)
95 (2.23606797749979 2.23606797749979 3.332000937312528e-8))
96 5.000000000000003)
97 :test 'almost=lists))
102 ;; (print-tests)
103 ;; (run-test :name 'cholesky-decomposition-1)
104 ;; (describe (run-test :name 'cholesky-decomposition-1))
106 (addtest (lisp-stat-lin-alg) lu-decomposition
107 (ensure-same
108 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
109 (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)))
111 (addtest (lisp-stat-lin-alg) rcondest
112 (ensure-same
113 (rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
114 6.8157451e7 ))
116 (addtest (lisp-stat-lin-alg) lu-solve
117 (ensure-same
118 (lu-solve
119 (lu-decomp
120 #2A((2 3 4) (1 2 4) (2 4 5)))
121 #(2 3 4))
122 #(-2.333333333333333 1.3333333333333335 0.6666666666666666)))
124 (addtest (lisp-stat-lin-alg) inverse
125 (ensure-same
126 (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
127 #2A((2.0 -0.33333333333333326 -1.3333333333333335)
128 (-1.0 -0.6666666666666666 1.3333333333333333)
129 (0.0 0.6666666666666666 -0.3333333333333333))))
131 (addtest (lisp-stat-lin-alg) sv-decomp
132 (ensure-same
133 (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
134 (list #2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
135 (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
136 (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
137 #(9.699290438141343 0.8971681569301373 0.3447525123483081)
138 #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
139 (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
140 (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
141 T)))
143 (addtest (lisp-stat-lin-alg) qr-decomp
144 (ensure-same
145 (qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
146 (list #2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
147 (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
148 (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
149 #2A((-3.0 -5.333333333333334 -7.333333333333332)
150 (0.0 -0.7453559924999292 -1.1925695879998877)
151 (0.0 0.0 -1.3416407864998738)))))
153 (addtest (lisp-stat-lin-alg) eigen
154 (ensure-same
155 (eigen #2A((2 3 4) (1 2 4) (2 4 5)))
156 (list #(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
157 (list #(0.4999999999999998 0.4999999999999997 0.7071067811865475)
158 #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
159 #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
160 NIL)))
162 (addtest (lisp-stat-lin-alg) spline
163 (ensure-same
164 (spline #(1.0 1.2 1.3 1.8 2.1 2.5)
165 #(1.2 2.0 2.1 2.0 1.1 2.8)
166 :xvals 6)
167 (list (list 1.0 1.3 1.6 1.9 2.2 2.5)
168 (list 1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))))
170 (addtest (lisp-stat-lin-alg) kernel-smooth
171 (ensure-same
172 ;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
173 (kernel-smooth
174 #(1.0 1.2 1.3 1.8 2.1 2.5)
175 #(1.2 2.0 2.1 2.0 1.1 2.8)
176 :xvals 5)
177 (list (list 1.0 1.375 1.75 2.125 2.5)
178 (list 1.6603277642110226 1.9471748095239771 1.7938127405752287
179 1.5871511322219498 2.518194783156392))))
181 (addtest (lisp-stat-lin-alg) kernel-dens
182 (ensure-same
183 (kernel-dens
184 #(1.0 1.2 2.5 2.1 1.8 1.2)
185 :xvals 5)
186 (list (list 1.0 1.375 1.75 2.125 2.5)
187 (list 0.7224150453621405 0.5820045548233707 0.38216411702854214
188 0.4829822708587095 0.3485939156929503))))
191 (addtest (lisp-stat-lin-alg) fft
192 (ensure-same
193 (fft #(1.0 1.2 2.5 2.1 1.8))
194 (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)))))
196 (addtest (lisp-stat-lin-alg) lowess
197 (ensure-same
198 (lowess #(1.0 1.2 2.5 2.1 1.8 1.2)
199 #(1.2 2.0 2.1 2.0 1.1 2.8))
200 #(1.0 1.2 1.2 1.8 2.1 2.5)))
204 ;;;; Log-gamma function
206 (addtest (lisp-stat-spec-fns) log-gamma-fn
207 (ensure-same
208 (log-gamma 3.4)
209 1.0923280596789584))
212 #+nil(progn
214 ;;; Probability distributions
216 ;; This macro should be generalized, but it's a good start now.
217 ;;(defmacro ProbDistnTests (prefixName
218 ;; quant-params quant-answer
219 ;; cdf-params cdf-answer
220 ;; pmf-params pmf-answer
221 ;; rand-params rand-answer)
222 ;; (deftestsuite lisp-stat-probdist-,prefixName (lisp-stat-probdistn)
223 ;; ;; (( ))
224 ;; (:documentation "testing for ,testName distribution results")
225 ;; (:test (ensure-same
226 ;; (lisp-stat-basics:,testName-quant ,quant-params) ,quant-answer))
227 ;; (:test (ensure-same
228 ;; (lisp-stat-basics:,testName-cdf ,cdf-params) ,cdf-answer))
229 ;; (:test (ensure-same
230 ;; (lisp-stat-basics:,testName-pmf ,pmf-params) ,pmf-answer))
231 ;; (:test (progn
232 ;; (set-seed 234)
233 ;; (ensure-same
234 ;; (lisp-stat-basics:,testName-rand ,rand-params) ,rand-answer)))))
236 ;;; Normal distribution
238 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
239 (:documentation "testing for Gaussian distn results")
240 (:test (ensure-same
241 (normal-quant 0.95)
242 1.6448536279366268))
243 (:test (ensure-same
244 (normal-cdf 1.3)
245 0.9031995154143897))
246 (:test (ensure-same
247 (normal-dens 1.3)
248 0.17136859204780736))
249 (:test (ensure-same
250 (normal-rand 2)
251 (list -0.40502015f0 -0.8091404f0)))
252 (:test (ensure-same
253 (bivnorm-cdf 0.2 0.4 0.6)
254 0.4736873734160288)))
256 ;;;; Cauchy distribution
258 (deftestsuite lisp-stat-probdist-cauchy (lisp-stat-probdistn)
259 (:documentation "testing for Cachy-distn results")
260 (:test (ensure-same
261 (cauchy-quant 0.95)
262 6.313751514675031))
263 (:test (ensure-same
264 (cauchy-cdf 1.3)
265 0.7912855998398473))
266 (:test (ensure-same
267 (cauchy-dens 1.3)
268 0.1183308127104695 ))
269 (:test (ensure-same
270 (cauchy-rand 2)
271 (-1.06224644160405 -0.4524695943939537))))
273 ;;;; Gamma distribution
275 (deftestsuite lisp-stat-probdist-gamma (lisp-stat-probdistn)
276 (:documentation "testing for gamma distn results")
277 (:test (ensure-same
278 (gamma-quant 0.95 4.3)
279 8.178692439291645))
280 (:test (ensure-same
281 (gamma-cdf 1.3 4.3)
282 0.028895150986674906))
283 (:test (ensure-same
284 (gamma-dens 1.3 4.3)
285 0.0731517686447374))
286 (:test (ensure-same
287 (gamma-rand 2 4.3)
288 (2.454918912880936 4.081365384357454))))
290 ;;;; Chi-square distribution
292 (deftestsuite lisp-stat-probdist-chisq (lisp-stat-probdistn)
294 (:documentation "testing for Chi-square distn results")
295 (:test (ensure-same
296 (chisq-quant 0.95 3)
297 7.814727903379012))
298 (:test (ensure-same
299 (chisq-cdf 1 5)
300 0.03743422675631789))
301 (:test (ensure-same
302 (chisq-dens 1 5)
303 0.08065690818083521))
304 (:test (progn
305 (set-seed 353)
306 (ensure-same
307 (chisq-rand 2 4)
308 (list 1.968535826180572 2.9988646156942997)))))
310 ;;;; Beta distribution
312 (deftestsuite lisp-stat-probdist-beta (lisp-stat-probdistn)
314 (:documentation "testing for beta distn results")
315 (:test (ensure-same
316 (beta-quant 0.95 3 2)
317 0.9023885371149876))
318 (:test (ensure-same
319 (beta-cdf 0.4 2 2.4)
320 0.4247997418541529 ))
321 (:test (ensure-same
322 (beta-dens 0.4 2 2.4)
323 1.5964741858913518 ))
324 (:test (ensure-same
325 (beta-rand 2 2 2.4)
326 (list 0.8014897077282279 0.6516371997922659))))
328 ;;;; t distribution
330 (deftestsuite lisp-stat-probdist-t (lisp-stat-probdistn)
331 (:documentation "testing for t-distn results")
332 (:test (ensure-same
333 (t-quant 0.95 3)
334 2.35336343484194))
335 (:test (ensure-same
336 (t-cdf 1 2.3)
337 0.794733624298342))
338 (:test (ensure-same
339 (t-dens 1 2.3)
340 0.1978163816318102))
341 (:test (ensure-same
342 (t-rand 2 2.3)
343 (list -0.34303672776089306 -1.142505872436518))))
345 ;;;; F distribution
347 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
348 (:documentation "testing for f-distn results")
349 (:test (ensure-same
350 (f-quant 0.95 3 5) 5.409451318117459))
351 (:test (ensure-same
352 (f-cdf 1 3.2 5.4)
353 0.5347130905510765))
354 (:test (ensure-same
355 (f-dens 1 3.2 5.4)
356 0.37551128864591415))
357 (:test (progn
358 (set-seed 234)
359 (ensure-same
360 (f-rand 2 3 2)
361 (list 0.7939093442091963 0.07442694152491144)))))
363 ;;;; Poisson distribution
365 (deftestsuite lisp-stat-probdist-poisson (lisp-stat-probdistn)
366 ;; (( ))
367 (:documentation "testing for poisson distribution results")
368 (:test (ensure-same
369 (poisson-quant 0.95 3.2) 6))
370 (:test (ensure-same
371 (poisson-cdf 1 3.2)
372 0.17120125672252395))
373 (:test (ensure-same
374 (poisson-pmf 1 3.2)
375 0.13043905274097067))
376 (:test (progn
377 (set-seed 234)
378 (ensure-same
379 (poisson-rand 5 3.2)
380 (list 2 1 2 0 3)))))
382 ;; Binomial distribution
384 (deftestsuite lisp-stat-probdist-binomial (lisp-stat-probdistn)
385 ;; (( ))
386 (:documentation "testing for binomial distribution results")
388 (:test (ensure-same
389 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
391 (:test (ensure-same
392 (binomial-quant 0 3 0.4)
393 ;; -2147483648
395 (:test (ensure-same
396 (binomial-cdf 1 3 0.4)
397 0.6479999999965776))
399 (:test (ensure-same
400 (binomial-pmf 1 3 0.4)
401 0.4320000000226171))
402 (:test (progn
403 (set-seed 526)
404 (ensure-same
405 (binomial-rand 5 3 0.4)
406 (list 2 2 0 1 2)))))