regression examples, and use of bind to figure out.
[CommonLispStat.git] / unittests.lisp
blob2c12a25794d0eb5d0a6fb24f8ce3707baf89805e
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)
60 ;; (error "types not matched."))
61 ;; (defmethod numerical= ((real a) (real b) &key (tol 0.00001))
62 ;; (defmethod numerical= ((list a) (list b) &key (tol 0.00001))
63 ;; (defmethod numerical= ((int a) (int b) &key (tol 0.00001))
64 ;; (defmethod numerical= ((complex a) (complex b) &key (tol 0.00001))
66 (deftestsuite lisp-stat-testsupport (lisp-stat)
68 (:tests
69 (almost=1 (ensure (almost= 3 3.001 :tol 0.01)))
70 (almost=2 (ensure (almost= 3 3.01 :tol 0.01)))
71 (almost=3 (ensure (not (almost= 3 3.1 :tol 0.01))))
72 (almost=lists1 (ensure (almost=lists nil nil :tol 0.01)))
73 (almost=lists2 (ensure (almost=lists (list ) (list ) :tol 0.01)))
74 (almost=lists3 (ensure (almost=lists (list 1.0) (list 1.0) :tol 0.01)))
75 (almost=lists4 (ensure (almost=lists (list 1.0 1.0) (list 1.0 1.0) :tol 0.01)))
76 (almost=lists5 (ensure (not (almost=lists (list 1.0 1.0) (list 1.0 1.1) :tol 0.01))))))
80 (addtest (lisp-stat-lin-alg) cholesky-decomposition-1
81 (ensure-same
82 (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
83 (list #2A((1.7888543819998317 0.0 0.0)
84 (1.6770509831248424 0.11180339887498929 0.0)
85 (2.23606797749979 2.23606797749979 3.332000937312528e-8))
86 5.000000000000003)
87 :test 'almost=lists))
92 ;; (print-tests)
93 ;; (run-test :name 'cholesky-decomposition-1)
94 ;; (describe (run-test :name 'cholesky-decomposition-1))
96 (addtest (lisp-stat-lin-alg) lu-decomposition
97 (ensure-same
98 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
99 (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)))
101 (addtest (lisp-stat-lin-alg) rcondest
102 (ensure-same
103 (rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
104 6.8157451e7 ))
106 (addtest (lisp-stat-lin-alg) lu-solve
107 (ensure-same
108 (lu-solve
109 (lu-decomp
110 #2A((2 3 4) (1 2 4) (2 4 5)))
111 #(2 3 4))
112 #(-2.333333333333333 1.3333333333333335 0.6666666666666666)))
114 (addtest (lisp-stat-lin-alg) inverse
115 (ensure-same
116 (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
117 #2A((2.0 -0.33333333333333326 -1.3333333333333335)
118 (-1.0 -0.6666666666666666 1.3333333333333333)
119 (0.0 0.6666666666666666 -0.3333333333333333))))
121 (addtest (lisp-stat-lin-alg) sv-decomp
122 (ensure-same
123 (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
124 (list #2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
125 (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
126 (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
127 #(9.699290438141343 0.8971681569301373 0.3447525123483081)
128 #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
129 (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
130 (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
131 T)))
133 (addtest (lisp-stat-lin-alg) qr-decomp
134 (ensure-same
135 (qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
136 (list #2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
137 (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
138 (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
139 #2A((-3.0 -5.333333333333334 -7.333333333333332)
140 (0.0 -0.7453559924999292 -1.1925695879998877)
141 (0.0 0.0 -1.3416407864998738)))))
143 (addtest (lisp-stat-lin-alg) eigen
144 (ensure-same
145 (eigen #2A((2 3 4) (1 2 4) (2 4 5)))
146 (list #(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
147 (list #(0.4999999999999998 0.4999999999999997 0.7071067811865475)
148 #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
149 #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
150 NIL)))
152 (addtest (lisp-stat-lin-alg) spline
153 (ensure-same
154 (spline #(1.0 1.2 1.3 1.8 2.1 2.5)
155 #(1.2 2.0 2.1 2.0 1.1 2.8)
156 :xvals 6)
157 (list (list 1.0 1.3 1.6 1.9 2.2 2.5)
158 (list 1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))))
160 (addtest (lisp-stat-lin-alg) kernel-smooth
161 (ensure-same
162 ;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
163 (kernel-smooth
164 #(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 5)
167 (list (list 1.0 1.375 1.75 2.125 2.5)
168 (list 1.6603277642110226 1.9471748095239771 1.7938127405752287
169 1.5871511322219498 2.518194783156392))))
171 (addtest (lisp-stat-lin-alg) kernel-dens
172 (ensure-same
173 (kernel-dens
174 #(1.0 1.2 2.5 2.1 1.8 1.2)
175 :xvals 5)
176 (list (list 1.0 1.375 1.75 2.125 2.5)
177 (list 0.7224150453621405 0.5820045548233707 0.38216411702854214
178 0.4829822708587095 0.3485939156929503))))
181 (addtest (lisp-stat-lin-alg) fft
182 (ensure-same
183 (fft #(1.0 1.2 2.5 2.1 1.8))
184 (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)))))
186 (addtest (lisp-stat-lin-alg) lowess
187 (ensure-same
188 (lowess #(1.0 1.2 2.5 2.1 1.8 1.2)
189 #(1.2 2.0 2.1 2.0 1.1 2.8))
190 #(1.0 1.2 1.2 1.8 2.1 2.5)))
194 ;;;; Log-gamma function
196 (addtest (lisp-stat-spec-fns) log-gamma-fn
197 (ensure-same
198 (log-gamma 3.4)
199 1.0923280596789584))
202 #+nil(progn
204 ;;; Probability distributions
206 ;; This macro should be generalized, but it's a good start now.
207 ;;(defmacro ProbDistnTests (prefixName
208 ;; quant-params quant-answer
209 ;; cdf-params cdf-answer
210 ;; pmf-params pmf-answer
211 ;; rand-params rand-answer)
212 ;; (deftestsuite lisp-stat-probdist-,prefixName (lisp-stat-probdistn)
213 ;; ;; (( ))
214 ;; (:documentation "testing for ,testName distribution results")
215 ;; (:test (ensure-same
216 ;; (lisp-stat-basics:,testName-quant ,quant-params) ,quant-answer))
217 ;; (:test (ensure-same
218 ;; (lisp-stat-basics:,testName-cdf ,cdf-params) ,cdf-answer))
219 ;; (:test (ensure-same
220 ;; (lisp-stat-basics:,testName-pmf ,pmf-params) ,pmf-answer))
221 ;; (:test (progn
222 ;; (set-seed 234)
223 ;; (ensure-same
224 ;; (lisp-stat-basics:,testName-rand ,rand-params) ,rand-answer)))))
226 ;;; Normal distribution
228 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
229 (:documentation "testing for Gaussian distn results")
230 (:test (ensure-same
231 (normal-quant 0.95)
232 1.6448536279366268))
233 (:test (ensure-same
234 (normal-cdf 1.3)
235 0.9031995154143897))
236 (:test (ensure-same
237 (normal-dens 1.3)
238 0.17136859204780736))
239 (:test (ensure-same
240 (normal-rand 2)
241 (list -0.40502015f0 -0.8091404f0)))
242 (:test (ensure-same
243 (bivnorm-cdf 0.2 0.4 0.6)
244 0.4736873734160288)))
246 ;;;; Cauchy distribution
248 (deftestsuite lisp-stat-probdist-cauchy (lisp-stat-probdistn)
249 (:documentation "testing for Cachy-distn results")
250 (:test (ensure-same
251 (cauchy-quant 0.95)
252 6.313751514675031))
253 (:test (ensure-same
254 (cauchy-cdf 1.3)
255 0.7912855998398473))
256 (:test (ensure-same
257 (cauchy-dens 1.3)
258 0.1183308127104695 ))
259 (:test (ensure-same
260 (cauchy-rand 2)
261 (-1.06224644160405 -0.4524695943939537))))
263 ;;;; Gamma distribution
265 (deftestsuite lisp-stat-probdist-gamma (lisp-stat-probdistn)
266 (:documentation "testing for gamma distn results")
267 (:test (ensure-same
268 (gamma-quant 0.95 4.3)
269 8.178692439291645))
270 (:test (ensure-same
271 (gamma-cdf 1.3 4.3)
272 0.028895150986674906))
273 (:test (ensure-same
274 (gamma-dens 1.3 4.3)
275 0.0731517686447374))
276 (:test (ensure-same
277 (gamma-rand 2 4.3)
278 (2.454918912880936 4.081365384357454))))
280 ;;;; Chi-square distribution
282 (deftestsuite lisp-stat-probdist-chisq (lisp-stat-probdistn)
284 (:documentation "testing for Chi-square distn results")
285 (:test (ensure-same
286 (chisq-quant 0.95 3)
287 7.814727903379012))
288 (:test (ensure-same
289 (chisq-cdf 1 5)
290 0.03743422675631789))
291 (:test (ensure-same
292 (chisq-dens 1 5)
293 0.08065690818083521))
294 (:test (progn
295 (set-seed 353)
296 (ensure-same
297 (chisq-rand 2 4)
298 (list 1.968535826180572 2.9988646156942997)))))
300 ;;;; Beta distribution
302 (deftestsuite lisp-stat-probdist-beta (lisp-stat-probdistn)
304 (:documentation "testing for beta distn results")
305 (:test (ensure-same
306 (beta-quant 0.95 3 2)
307 0.9023885371149876))
308 (:test (ensure-same
309 (beta-cdf 0.4 2 2.4)
310 0.4247997418541529 ))
311 (:test (ensure-same
312 (beta-dens 0.4 2 2.4)
313 1.5964741858913518 ))
314 (:test (ensure-same
315 (beta-rand 2 2 2.4)
316 (list 0.8014897077282279 0.6516371997922659))))
318 ;;;; t distribution
320 (deftestsuite lisp-stat-probdist-t (lisp-stat-probdistn)
321 (:documentation "testing for t-distn results")
322 (:test (ensure-same
323 (t-quant 0.95 3)
324 2.35336343484194))
325 (:test (ensure-same
326 (t-cdf 1 2.3)
327 0.794733624298342))
328 (:test (ensure-same
329 (t-dens 1 2.3)
330 0.1978163816318102))
331 (:test (ensure-same
332 (t-rand 2 2.3)
333 (list -0.34303672776089306 -1.142505872436518))))
335 ;;;; F distribution
337 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
338 (:documentation "testing for f-distn results")
339 (:test (ensure-same
340 (f-quant 0.95 3 5) 5.409451318117459))
341 (:test (ensure-same
342 (f-cdf 1 3.2 5.4)
343 0.5347130905510765))
344 (:test (ensure-same
345 (f-dens 1 3.2 5.4)
346 0.37551128864591415))
347 (:test (progn
348 (set-seed 234)
349 (ensure-same
350 (f-rand 2 3 2)
351 (list 0.7939093442091963 0.07442694152491144)))))
353 ;;;; Poisson distribution
355 (deftestsuite lisp-stat-probdist-poisson (lisp-stat-probdistn)
356 ;; (( ))
357 (:documentation "testing for poisson distribution results")
358 (:test (ensure-same
359 (poisson-quant 0.95 3.2) 6))
360 (:test (ensure-same
361 (poisson-cdf 1 3.2)
362 0.17120125672252395))
363 (:test (ensure-same
364 (poisson-pmf 1 3.2)
365 0.13043905274097067))
366 (:test (progn
367 (set-seed 234)
368 (ensure-same
369 (poisson-rand 5 3.2)
370 (list 2 1 2 0 3)))))
372 ;; Binomial distribution
374 (deftestsuite lisp-stat-probdist-binomial (lisp-stat-probdistn)
375 ;; (( ))
376 (:documentation "testing for binomial distribution results")
378 (:test (ensure-same
379 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
381 (:test (ensure-same
382 (binomial-quant 0 3 0.4)
383 ;; -2147483648
385 (:test (ensure-same
386 (binomial-cdf 1 3 0.4)
387 0.6479999999965776))
389 (:test (ensure-same
390 (binomial-pmf 1 3 0.4)
391 0.4320000000226171))
392 (:test (progn
393 (set-seed 526)
394 (ensure-same
395 (binomial-rand 5 3 0.4)
396 (list 2 2 0 1 2)))))