Documentation, compile, and initialization cleanup.
[CommonLispStat.git] / unittests.lisp
blob545fdb867b7d402723bb38839da00a6db7b0ce24
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 ;;; Need a waz to minimize the possibilities
31 (defun run-lisp-stat-tests ()
32 (run-tests :suite 'lisp-stat))
34 (defun run-lisp-stat-test (&rest x)
35 (run-test x))
39 (deftestsuite lisp-stat () ())
40 (deftestsuite lisp-stat-lin-alg (lisp-stat) ())
41 (deftestsuite lisp-stat-spec-fns (lisp-stat) ())
42 (deftestsuite lisp-stat-probdistn (lisp-stat) ())
44 ;; ;;; and add a test to it
45 ;; (addtest (lisp-stat)
46 ;; (ensure-same (+ 1 1) 2))
47 ;; ;; => #<Test passed>
49 ;; ;;; add another test using ensure-error
50 ;; (addtest (lisp-stat-lin-alg)
51 ;; (ensure-error (let ((x 0)) (/ x))))
52 ;; ;; => #<Test passed>
54 ;; ;;; add another, slightly more specific test
55 ;; (addtest (lisp-stat)
56 ;; (ensure-condition division-by-zero (let ((x 0)) (/ x))))
57 ;; ;; => #<Test passed>
59 (addtest (lisp-stat-lin-alg) cholesky-decomposition
60 (ensure-same
61 (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
62 (values #2A((1.7888543819998317 0.0 0.0)
63 (1.6770509831248424 0.11180339887498929 0.0)
64 (2.23606797749979 2.23606797749979 3.332000937312528e-8))
65 5.000000000000003)))
67 (addtest (lisp-stat-lin-alg) lu-decomposition
68 (ensure-same
69 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
70 (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)))
72 (addtest (lisp-stat-lin-alg) rcondest
73 (ensure-same
74 (rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
75 6.8157451e7 ))
77 (addtest (lisp-stat-lin-alg) lu-solve
78 (ensure-same
79 (lu-solve
80 (lu-decomp
81 #2A((2 3 4) (1 2 4) (2 4 5)))
82 #(2 3 4))
83 #(-2.333333333333333 1.3333333333333335 0.6666666666666666)))
85 (addtest (lisp-stat-lin-alg) inverse
86 (ensure-same
87 (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
88 #2A((2.0 -0.33333333333333326 -1.3333333333333335)
89 (-1.0 -0.6666666666666666 1.3333333333333333)
90 (0.0 0.6666666666666666 -0.3333333333333333))))
92 (addtest (lisp-stat-lin-alg) sv-decomp
93 (ensure-same
94 (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
95 (list #2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
96 (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
97 (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
98 #(9.699290438141343 0.8971681569301373 0.3447525123483081)
99 #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
100 (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
101 (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
102 T)))
104 (addtest (lisp-stat-lin-alg) qr-decomp
105 (ensure-same
106 (qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
107 (list #2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
108 (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
109 (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
110 #2A((-3.0 -5.333333333333334 -7.333333333333332)
111 (0.0 -0.7453559924999292 -1.1925695879998877)
112 (0.0 0.0 -1.3416407864998738)))))
114 (addtest (lisp-stat-lin-alg) eigen
115 (ensure-same
116 (eigen #2A((2 3 4) (1 2 4) (2 4 5)))
117 (list #(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
118 (list #(0.4999999999999998 0.4999999999999997 0.7071067811865475)
119 #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
120 #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
121 NIL)))
123 (addtest (lisp-stat-lin-alg) spline
124 (ensure-same
125 (spline #(1.0 1.2 1.3 1.8 2.1 2.5)
126 #(1.2 2.0 2.1 2.0 1.1 2.8)
127 :xvals 6)
128 (list (list 1.0 1.3 1.6 1.9 2.2 2.5)
129 (list 1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))))
131 (addtest (lisp-stat-lin-alg) kernel-smooth
132 (ensure-same
133 ;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
134 (kernel-smooth
135 #(1.0 1.2 1.3 1.8 2.1 2.5)
136 #(1.2 2.0 2.1 2.0 1.1 2.8)
137 :xvals 5)
138 (list (list 1.0 1.375 1.75 2.125 2.5)
139 (list 1.6603277642110226 1.9471748095239771 1.7938127405752287
140 1.5871511322219498 2.518194783156392))))
142 (addtest (lisp-stat-lin-alg) kernel-dens
143 (ensure-same
144 (kernel-dens
145 #(1.0 1.2 2.5 2.1 1.8 1.2)
146 :xvals 5)
147 (list (list 1.0 1.375 1.75 2.125 2.5)
148 (list 0.7224150453621405 0.5820045548233707 0.38216411702854214
149 0.4829822708587095 0.3485939156929503))))
152 (addtest (lisp-stat-lin-alg) fft
153 (ensure-same
154 (fft #(1.0 1.2 2.5 2.1 1.8))
155 (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)))))
157 (addtest (lisp-stat-lin-alg) lowess
158 (ensure-same
159 (lowess #(1.0 1.2 2.5 2.1 1.8 1.2)
160 #(1.2 2.0 2.1 2.0 1.1 2.8))
161 #(1.0 1.2 1.2 1.8 2.1 2.5)))
165 ;;;; Log-gamma function
167 (addtest (lisp-stat-spec-fns) log-gamma-fn
168 (ensure-same
169 (log-gamma 3.4)
170 1.0923280596789584))
173 #+nil(progn
175 ;;; Probability distributions
177 ;; This macro should be generalized, but it's a good start now.
178 ;;(defmacro ProbDistnTests (prefixName
179 ;; quant-params quant-answer
180 ;; cdf-params cdf-answer
181 ;; pmf-params pmf-answer
182 ;; rand-params rand-answer)
183 ;; (deftestsuite lisp-stat-probdist-,prefixName (lisp-stat-probdistn)
184 ;; ;; (( ))
185 ;; (:documentation "testing for ,testName distribution results")
186 ;; (:test (ensure-same
187 ;; (lisp-stat-basics:,testName-quant ,quant-params) ,quant-answer))
188 ;; (:test (ensure-same
189 ;; (lisp-stat-basics:,testName-cdf ,cdf-params) ,cdf-answer))
190 ;; (:test (ensure-same
191 ;; (lisp-stat-basics:,testName-pmf ,pmf-params) ,pmf-answer))
192 ;; (:test (progn
193 ;; (set-seed 234)
194 ;; (ensure-same
195 ;; (lisp-stat-basics:,testName-rand ,rand-params) ,rand-answer)))))
197 ;;; Normal distribution
199 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
200 (:documentation "testing for Gaussian distn results")
201 (:test (ensure-same
202 (normal-quant 0.95)
203 1.6448536279366268))
204 (:test (ensure-same
205 (normal-cdf 1.3)
206 0.9031995154143897))
207 (:test (ensure-same
208 (normal-dens 1.3)
209 0.17136859204780736))
210 (:test (ensure-same
211 (normal-rand 2)
212 (list -0.40502015f0 -0.8091404f0)))
213 (:test (ensure-same
214 (bivnorm-cdf 0.2 0.4 0.6)
215 0.4736873734160288)))
217 ;;;; Cauchy distribution
219 (deftestsuite lisp-stat-probdist-cauchy (lisp-stat-probdistn)
220 (:documentation "testing for Cachy-distn results")
221 (:test (ensure-same
222 (cauchy-quant 0.95)
223 6.313751514675031))
224 (:test (ensure-same
225 (cauchy-cdf 1.3)
226 0.7912855998398473))
227 (:test (ensure-same
228 (cauchy-dens 1.3)
229 0.1183308127104695 ))
230 (:test (ensure-same
231 (cauchy-rand 2)
232 (-1.06224644160405 -0.4524695943939537))))
234 ;;;; Gamma distribution
236 (deftestsuite lisp-stat-probdist-gamma (lisp-stat-probdistn)
237 (:documentation "testing for gamma distn results")
238 (:test (ensure-same
239 (gamma-quant 0.95 4.3)
240 8.178692439291645))
241 (:test (ensure-same
242 (gamma-cdf 1.3 4.3)
243 0.028895150986674906))
244 (:test (ensure-same
245 (gamma-dens 1.3 4.3)
246 0.0731517686447374))
247 (:test (ensure-same
248 (gamma-rand 2 4.3)
249 (2.454918912880936 4.081365384357454))))
251 ;;;; Chi-square distribution
253 (deftestsuite lisp-stat-probdist-chisq (lisp-stat-probdistn)
255 (:documentation "testing for Chi-square distn results")
256 (:test (ensure-same
257 (chisq-quant 0.95 3)
258 7.814727903379012))
259 (:test (ensure-same
260 (chisq-cdf 1 5)
261 0.03743422675631789))
262 (:test (ensure-same
263 (chisq-dens 1 5)
264 0.08065690818083521))
265 (:test (progn
266 (set-seed 353)
267 (ensure-same
268 (chisq-rand 2 4)
269 (list 1.968535826180572 2.9988646156942997)))))
271 ;;;; Beta distribution
273 (deftestsuite lisp-stat-probdist-beta (lisp-stat-probdistn)
275 (:documentation "testing for beta distn results")
276 (:test (ensure-same
277 (beta-quant 0.95 3 2)
278 0.9023885371149876))
279 (:test (ensure-same
280 (beta-cdf 0.4 2 2.4)
281 0.4247997418541529 ))
282 (:test (ensure-same
283 (beta-dens 0.4 2 2.4)
284 1.5964741858913518 ))
285 (:test (ensure-same
286 (beta-rand 2 2 2.4)
287 (list 0.8014897077282279 0.6516371997922659))))
289 ;;;; t distribution
291 (deftestsuite lisp-stat-probdist-t (lisp-stat-probdistn)
292 (:documentation "testing for t-distn results")
293 (:test (ensure-same
294 (t-quant 0.95 3)
295 2.35336343484194))
296 (:test (ensure-same
297 (t-cdf 1 2.3)
298 0.794733624298342))
299 (:test (ensure-same
300 (t-dens 1 2.3)
301 0.1978163816318102))
302 (:test (ensure-same
303 (t-rand 2 2.3)
304 (list -0.34303672776089306 -1.142505872436518))))
306 ;;;; F distribution
308 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
309 (:documentation "testing for f-distn results")
310 (:test (ensure-same
311 (f-quant 0.95 3 5) 5.409451318117459))
312 (:test (ensure-same
313 (f-cdf 1 3.2 5.4)
314 0.5347130905510765))
315 (:test (ensure-same
316 (f-dens 1 3.2 5.4)
317 0.37551128864591415))
318 (:test (progn
319 (set-seed 234)
320 (ensure-same
321 (f-rand 2 3 2)
322 (list 0.7939093442091963 0.07442694152491144)))))
324 ;;;; Poisson distribution
326 (deftestsuite lisp-stat-probdist-poisson (lisp-stat-probdistn)
327 ;; (( ))
328 (:documentation "testing for poisson distribution results")
329 (:test (ensure-same
330 (poisson-quant 0.95 3.2) 6))
331 (:test (ensure-same
332 (poisson-cdf 1 3.2)
333 0.17120125672252395))
334 (:test (ensure-same
335 (poisson-pmf 1 3.2)
336 0.13043905274097067))
337 (:test (progn
338 (set-seed 234)
339 (ensure-same
340 (poisson-rand 5 3.2)
341 (list 2 1 2 0 3)))))
343 ;; Binomial distribution
345 (deftestsuite lisp-stat-probdist-binomial (lisp-stat-probdistn)
346 ;; (( ))
347 (:documentation "testing for binomial distribution results")
349 (:test (ensure-same
350 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
352 (:test (ensure-same
353 (binomial-quant 0 3 0.4)
354 ;; -2147483648
356 (:test (ensure-same
357 (binomial-cdf 1 3 0.4)
358 0.6479999999965776))
360 (:test (ensure-same
361 (binomial-pmf 1 3 0.4)
362 0.4320000000226171))
363 (:test (progn
364 (set-seed 526)
365 (ensure-same
366 (binomial-rand 5 3 0.4)
367 (list 2 2 0 1 2)))))