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