removed complex type from CLS.
[CommonLispStat.git] / unittests.lisp
blobfa43fd1827c839ebfd63c64622782be556241434
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 (defmethod numerical= ((a integer) (b integer) &key (tol 0.1)) ;; real))
67 (print (format nil " equality pred for int a=~w int b=~w" a b))
68 (< (abs (- a b)) tol))
70 ;;(defmethod numerical= ((a complex) (b complex) &key (tol 0.00001)) ;; real))
71 ;; (< (abs (- a b)) tol))
73 ;; can we use sequence for both array and list? I think so.
74 (defmethod numerical= ((a sequence) (b sequence) &key (tol 0.00001))
75 (print (format nil "checking equality for list a ~w list b=~w" a b))
76 (if (and (= (length a) (length b))
77 (> (length a) 0)
78 (numerical= (car a) (car b) :tol tol))
79 (progn
80 (numerical= (cdr a) (cdr b) :tol tol))
81 nil))
82 ;; FIXME++++ This is too slow, a few too many comparisons.
84 (numerical= (list 2.0 2.0 2.2) (list 2.1 2.0 2.2))
85 (numerical= (list 2.1 2.0 2.2) (list 2.1 2.0 2.2))
87 (numerical= (list 2.1 2.0 2.2 4.2) (list 2.1 2.0 2.2 4.2))
88 (numerical= (list 2.1 2.0 2.3 4.0) (list 2.1 2.0 2.2 4.0))
90 (let ((a (list 2.1 2.0 2.2 4.2))
91 (b (list 2.0 2.1 2.2 4.2)))
92 (and (= (length a) (length b))
93 (numerical= (car a) (car b))))
96 ;; (defmethod numerical= ((complex a) (complex b) &key (tol 0.00001))
97 ;; (defmethod numerical= ((list a) (list b) &key (tol 0.00001))
98 ;; (defmethod numerical= ((array a) (array b) &key (tol 0.00001))
102 (deftestsuite lisp-stat-testsupport (lisp-stat)
104 (:tests
105 (almost=1 (ensure (almost= 3 3.001 :tol 0.01)))
106 (almost=2 (ensure (almost= 3 3.01 :tol 0.01)))
107 (almost=3 (ensure (not (almost= 3 3.1 :tol 0.01))))
108 (almost=lists1 (ensure (almost=lists nil nil :tol 0.01)))
109 (almost=lists2 (ensure (almost=lists (list ) (list ) :tol 0.01)))
110 (almost=lists3 (ensure (almost=lists (list 1.0) (list 1.0) :tol 0.01)))
111 (almost=lists4 (ensure (almost=lists (list 1.0 1.0) (list 1.0 1.0) :tol 0.01)))
112 (almost=lists5 (ensure (not (almost=lists (list 1.0 1.0)
113 (list 1.0 1.1) :tol 0.01))))))
115 (deftestsuite lisp-stat-testsupport2 (lisp-stat)
117 (:tests
118 (numerical=1 (ensure (numerical= 3 3.001 :tol 0.01)))
119 (numerical=2 (ensure (numerical= 3 3.01 :tol 0.01)))
120 (numerical=3 (ensure (not (numerical= 3 3.1 :tol 0.01))))
121 (numerical=4 (ensure (numerical= nil nil :tol 0.01)))
122 (numerical=5 (ensure (numerical= (list ) (list ) :tol 0.01)))
123 (numerical=6 (ensure (numerical= (list 1.0) (list 1.0) :tol 0.01)))
124 (numerical=7 (ensure (numerical= (list 1.0 1.0) (list 1.0 1.0) :tol 0.01)))
125 (numerical=8 (ensure (not (numerical= (list 1.0 1.0)
126 (list 1.0 1.1) :tol 0.01))))))
129 (numerical= 2.0 2.0)
130 (numerical= 2.0 2.1)
131 (numerical= 2.0 2.1 :tol 0.5)
132 (numerical= 2 2)
133 (numerical= 2 3)
134 ;;(numerical= 2.0 (list 2.1 2.0 2.2))
139 (addtest (lisp-stat-lin-alg) cholesky-decomposition-1
140 (ensure-same
141 (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
142 (list #2A((1.7888543819998317 0.0 0.0)
143 (1.6770509831248424 0.11180339887498929 0.0)
144 (2.23606797749979 2.23606797749979 3.332000937312528e-8))
145 5.000000000000003)
146 :test 'almost=lists))
148 (addtest (lisp-stat-lin-alg) lu-decomposition
149 (ensure-same
150 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
151 (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)))
153 (addtest (lisp-stat-lin-alg) rcondest
154 ;; (ensure-same
155 (ensure-error ;; it barfs, FIXME!!
156 (rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
157 6.8157451e7
158 :test 'almost=))
160 (addtest (lisp-stat-lin-alg) lu-solve
161 (ensure-same
162 (lu-solve
163 (lu-decomp
164 #2A((2 3 4) (1 2 4) (2 4 5)))
165 #(2 3 4))
166 #(-2.333333333333333 1.3333333333333335 0.6666666666666666)))
168 (addtest (lisp-stat-lin-alg) inverse
169 (ensure-same
170 (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
171 #2A((2.0 -0.33333333333333326 -1.3333333333333335)
172 (-1.0 -0.6666666666666666 1.3333333333333333)
173 (0.0 0.6666666666666666 -0.3333333333333333))))
175 (addtest (lisp-stat-lin-alg) sv-decomp
176 (ensure-same
177 (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
178 (list #2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
179 (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
180 (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
181 #(9.699290438141343 0.8971681569301373 0.3447525123483081)
182 #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
183 (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
184 (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
186 :test 'almost=lists))
188 (addtest (lisp-stat-lin-alg) qr-decomp
189 (ensure-same
190 (qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
191 (list #2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
192 (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
193 (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
194 #2A((-3.0 -5.333333333333334 -7.333333333333332)
195 (0.0 -0.7453559924999292 -1.1925695879998877)
196 (0.0 0.0 -1.3416407864998738)))
197 :test 'almost=lists))
199 (addtest (lisp-stat-lin-alg) eigen
200 (ensure-same
201 (eigen #2A((2 3 4) (1 2 4) (2 4 5)))
202 (list #(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
203 (list #(0.4999999999999998 0.4999999999999997 0.7071067811865475)
204 #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
205 #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
206 NIL)))
208 (addtest (lisp-stat-lin-alg) spline
209 (ensure-same
210 (spline #(1.0 1.2 1.3 1.8 2.1 2.5)
211 #(1.2 2.0 2.1 2.0 1.1 2.8)
212 :xvals 6)
213 (list (list 1.0 1.3 1.6 1.9 2.2 2.5)
214 (list 1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))
215 :test 'almost=lists))
217 (addtest (lisp-stat-lin-alg) kernel-smooth
218 (ensure-same
219 ;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
220 (kernel-smooth
221 #(1.0 1.2 1.3 1.8 2.1 2.5)
222 #(1.2 2.0 2.1 2.0 1.1 2.8)
223 :xvals 5)
224 (list (list 1.0 1.375 1.75 2.125 2.5)
225 (list 1.6603277642110226 1.9471748095239771 1.7938127405752287
226 1.5871511322219498 2.518194783156392))
227 :test 'almost=lists))
229 (addtest (lisp-stat-lin-alg) kernel-dens
230 (ensure-same
231 (kernel-dens
232 #(1.0 1.2 2.5 2.1 1.8 1.2)
233 :xvals 5)
234 (list (list 1.0 1.375 1.75 2.125 2.5)
235 (list 0.7224150453621405 0.5820045548233707 0.38216411702854214
236 0.4829822708587095 0.3485939156929503))))
239 (addtest (lisp-stat-lin-alg) fft
240 (ensure-same
241 (fft #(1.0 1.2 2.5 2.1 1.8))
242 (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)))
243 :test 'almost=lists))
246 (addtest (lisp-stat-lin-alg) lowess
247 (ensure-same
248 (lowess #(1.0 1.2 2.5 2.1 1.8 1.2)
249 #(1.2 2.0 2.1 2.0 1.1 2.8))
250 #(1.0 1.2 1.2 1.8 2.1 2.5)
251 :test 'almost=lists)) ;; result isn't a list!
255 ;;;; Log-gamma function
257 (addtest (lisp-stat-spec-fns) log-gamma-fn
258 (ensure-same
259 (log-gamma 3.4)
260 1.0923280596789584
261 :test 'almost=))
264 ;;; Probability distributions
266 ;; This macro should be generalized, but it's a good start now.
267 ;;(defmacro ProbDistnTests (prefixName
268 ;; quant-params quant-answer
269 ;; cdf-params cdf-answer
270 ;; pmf-params pmf-answer
271 ;; rand-params rand-answer)
272 ;; (deftestsuite lisp-stat-probdist-,prefixName (lisp-stat-probdistn)
273 ;; ;; (( ))
274 ;; (:documentation "testing for ,testName distribution results")
275 ;; (:test (ensure-same
276 ;; (lisp-stat-basics:,testName-quant ,quant-params) ,quant-answer))
277 ;; (:test (ensure-same
278 ;; (lisp-stat-basics:,testName-cdf ,cdf-params) ,cdf-answer))
279 ;; (:test (ensure-same
280 ;; (lisp-stat-basics:,testName-pmf ,pmf-params) ,pmf-answer))
281 ;; (:test (progn
282 ;; (set-seed 234)
283 ;; (ensure-same
284 ;; (lisp-stat-basics:,testName-rand ,rand-params) ,rand-answer)))))
286 ;;; Normal distribution
288 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
290 (:documentation "testing for Gaussian distn results")
291 (:test (ensure-same
292 (normal-quant 0.95)
293 1.6448536279366268))
294 (:test (ensure-same
295 (normal-cdf 1.3)
296 0.9031995154143897))
297 (:test (ensure-same
298 (normal-dens 1.3)
299 0.17136859204780736))
300 (:test (ensure-same
301 (normal-rand 2)
302 (list -0.40502015f0 -0.8091404f0)))
303 (:test (ensure-same
304 (bivnorm-cdf 0.2 0.4 0.6)
305 0.4736873734160288)))
307 ;;;; Cauchy distribution
309 (deftestsuite lisp-stat-probdist-cauchy (lisp-stat-probdistn)
311 (:documentation "testing for Cachy-distn results")
312 (:test (ensure-same
313 (cauchy-quant 0.95)
314 6.313751514675031))
315 (:test (ensure-same
316 (cauchy-cdf 1.3)
317 0.7912855998398473))
318 (:test (ensure-same
319 (cauchy-dens 1.3)
320 0.1183308127104695 ))
321 (:test (ensure-same
322 (cauchy-rand 2)
323 (list -1.06224644160405 -0.4524695943939537))))
325 ;;;; Gamma distribution
327 (deftestsuite lisp-stat-probdist-gamma (lisp-stat-probdistn)
329 (:documentation "testing for gamma distn results")
330 (:test (ensure-same
331 (gamma-quant 0.95 4.3)
332 8.178692439291645))
333 (:test (ensure-same
334 (gamma-cdf 1.3 4.3)
335 0.028895150986674906))
336 (:test (ensure-same
337 (gamma-dens 1.3 4.3)
338 0.0731517686447374))
339 (:test (ensure-same
340 (gamma-rand 2 4.3)
341 (list 2.454918912880936 4.081365384357454))))
343 ;;;; Chi-square distribution
345 (deftestsuite lisp-stat-probdist-chisq (lisp-stat-probdistn)
347 (:documentation "testing for Chi-square distn results")
348 (:test (ensure-same
349 (chisq-quant 0.95 3)
350 7.814727903379012))
351 (:test (ensure-same
352 (chisq-cdf 1 5)
353 0.03743422675631789))
354 (:test (ensure-same
355 (chisq-dens 1 5)
356 0.08065690818083521))
357 (:test (progn
358 (set-seed 353)
359 (ensure-same
360 (chisq-rand 2 4)
361 (list 1.968535826180572 2.9988646156942997)))))
363 ;;;; Beta distribution
365 (deftestsuite lisp-stat-probdist-beta (lisp-stat-probdistn)
367 (:documentation "testing for beta distn results")
368 (:test (ensure-same
369 (beta-quant 0.95 3 2)
370 0.9023885371149876))
371 (:test (ensure-same
372 (beta-cdf 0.4 2 2.4)
373 0.4247997418541529 ))
374 (:test (ensure-same
375 (beta-dens 0.4 2 2.4)
376 1.5964741858913518 ))
377 (:test (ensure-same
378 (beta-rand 2 2 2.4)
379 (list 0.8014897077282279 0.6516371997922659))))
381 ;;;; t distribution
383 (deftestsuite lisp-stat-probdist-t (lisp-stat-probdistn)
385 (:documentation "testing for t-distn results")
386 (:test (ensure-same
387 (t-quant 0.95 3)
388 2.35336343484194))
389 (:test (ensure-same
390 (t-cdf 1 2.3)
391 0.794733624298342))
392 (:test (ensure-same
393 (t-dens 1 2.3)
394 0.1978163816318102))
395 (:test (ensure-same
396 (t-rand 2 2.3)
397 (list -0.34303672776089306 -1.142505872436518))))
399 ;;;; F distribution
401 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
403 (:documentation "testing for f-distn results")
404 (:test (ensure-same
405 (f-quant 0.95 3 5) 5.409451318117459))
406 (:test (ensure-same
407 (f-cdf 1 3.2 5.4)
408 0.5347130905510765))
409 (:test (ensure-same
410 (f-dens 1 3.2 5.4)
411 0.37551128864591415))
412 (:test (progn
413 (set-seed 234)
414 (ensure-same
415 (f-rand 2 3 2)
416 (list 0.7939093442091963 0.07442694152491144)))))
418 ;;;; Poisson distribution
420 (deftestsuite lisp-stat-probdist-poisson (lisp-stat-probdistn)
422 (:documentation "testing for poisson distribution results")
423 (:test (ensure-same
424 (poisson-quant 0.95 3.2) 6))
425 (:test (ensure-same
426 (poisson-cdf 1 3.2)
427 0.17120125672252395))
428 (:test (ensure-same
429 (poisson-pmf 1 3.2)
430 0.13043905274097067))
431 (:test (progn
432 (set-seed 234)
433 (ensure-same
434 (poisson-rand 5 3.2)
435 (list 2 1 2 0 3)))))
437 ;; Binomial distribution
439 (deftestsuite lisp-stat-probdist-binomial (lisp-stat-probdistn)
441 (:documentation "testing for binomial distribution results")
443 (:test (ensure-same
444 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
446 (:test (ensure-same
447 (binomial-quant 0 3 0.4)
448 ;; -2147483648
450 (:test (ensure-same
451 (binomial-cdf 1 3 0.4)
452 0.6479999999965776))
454 (:test (ensure-same
455 (binomial-pmf 1 3 0.4)
456 0.4320000000226171))
457 (:test (progn
458 (set-seed 526)
459 (ensure-same
460 (binomial-rand 5 3 0.4)
461 (list 2 2 0 1 2)))))