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