add a dummy set-seed function. Need to write this!
[CommonLispStat.git] / unittests.lisp
blob40de2ecd2826aa146cf4ebc5dcdd29a44c0981fc
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) ())
43 ;;; and add a test to it
44 (addtest (lisp-stat)
45 (ensure-same (+ 1 1) 2))
46 ;; => #<Test passed>
48 ;;; add another test using ensure-error
49 (addtest (lisp-stat-lin-alg)
50 (ensure-error (let ((x 0)) (/ x))))
51 ;; => #<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>
60 (addtest (lisp-stat-lin-alg) cholesky-decomposition
61 (ensure-same
62 (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
63 (values #2A((1.7888543819998317 0.0 0.0)
64 (1.6770509831248424 0.11180339887498929 0.0)
65 (2.23606797749979 2.23606797749979 3.332000937312528e-8))
66 5.000000000000003)))
68 #+nil(progn
72 (addtest (lisp-stat-lin-alg)
73 lu-decomposition
74 (ensure-same
75 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
76 (#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)))
78 (addtest (lisp-stat-lin-alg)
79 lu-solve
80 (ensure-same
81 (lu-solve
82 (lu-decomp
83 #2A((2 3 4) (1 2 4) (2 4 5)))
84 #(2 3 4))
85 #(-2.333333333333333 1.3333333333333335 0.6666666666666666)))
88 (addtest (lisp-stat-lin-alg)
89 inverse
90 (ensure-same
91 (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
92 #2A((2.0 -0.33333333333333326 -1.3333333333333335)
93 (-1.0 -0.6666666666666666 1.3333333333333333)
94 (0.0 0.6666666666666666 -0.3333333333333333))))
96 (addtest (lisp-stat-lin-alg)
97 sv-decomp
98 (ensure-same
99 (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
100 (#2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
101 (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
102 (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
103 #(9.699290438141343 0.8971681569301373 0.3447525123483081)
104 #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
105 (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
106 (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
107 T)))
109 (addtest (lisp-stat-lin-alg)
110 qr-decomp
111 (ensure-same
112 (qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
113 (#2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
114 (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
115 (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
116 #2A((-3.0 -5.333333333333334 -7.333333333333332)
117 (0.0 -0.7453559924999292 -1.1925695879998877)
118 (0.0 0.0 -1.3416407864998738))) ))
120 (addtest (lisp-stat-lin-alg)
121 rcondest
122 (ensure-same
123 (rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
124 6.8157451e7 ))
126 (addtest (lisp-stat-lin-alg)
127 eigen
128 (ensure-same
129 (eigen #2A((2 3 4) (1 2 4) (2 4 5)))
130 (#(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
131 (#(0.4999999999999998 0.4999999999999997 0.7071067811865475)
132 #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
133 #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
134 NIL) ))
136 (addtest (lisp-stat-lin-alg)
137 spline
138 (ensure-same
139 (spline
140 #(1.0 1.2 1.3 1.8 2.1 2.5)
141 #(1.2 2.0 2.1 2.0 1.1 2.8)
142 :xvals 6)
143 ((1.0 1.3 1.6 1.9 2.2 2.5)
144 (1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))))
147 (addtest (lisp-stat-lin-alg)
148 kernel-smooth
149 (ensure-same
150 ;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
151 (kernel-smooth
152 #(1.0 1.2 1.3 1.8 2.1 2.5)
153 #(1.2 2.0 2.1 2.0 1.1 2.8)
154 :xvals 5)
155 ((1.0 1.375 1.75 2.125 2.5)
156 (1.6603277642110226 1.9471748095239771 1.7938127405752287
157 1.5871511322219498 2.518194783156392))))
159 (addtest (lisp-stat-lin-alg)
160 kernel-dens
161 (ensure-same
162 (kernel-dens
163 #(1.0 1.2 2.5 2.1 1.8 1.2)
164 :xvals 5)
165 ((1.0 1.375 1.75 2.125 2.5)
166 (0.7224150453621405 0.5820045548233707 0.38216411702854214
167 0.4829822708587095 0.3485939156929503)) ))
169 (addtest (lisp-stat-lin-alg)
171 (ensure-same
172 (fft #(1.0 1.2 2.5 2.1 1.8))
173 #(#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)) ))
175 (addtest (lisp-stat-lin-alg)
176 lowess
177 (ensure-same
178 (lisp-stat-descriptive-statistics:lowess
179 #(1.0 1.2 2.5 2.1 1.8 1.2)
180 #(1.2 2.0 2.1 2.0 1.1 2.8))
181 (#(1.0 1.2 1.2 1.8 2.1 2.5))))
184 (deftestsuite lisp-stat-spec-fns () ())
186 ;;;; Log-gamma function
188 (addtest (lisp-stat-spec-fns)
189 (ensure-same
190 (lisp-stat-basics:log-gamma 3.4)
191 1.0923280596789584))
194 ;;; Probability distributions
196 ;; This macro should be generalized, but it's a good start now.
197 (defmacro ProbDistnTests (prefixName
198 quant-params quant-answer
199 cdf-params cdf-answer
200 pmf-params pmf-answer
201 rand-params rand-answer)
203 (deftestsuite lisp-stat-probdist-,prefixName (lisp-stat-probdistn)
204 ;; (( ))
205 (:documentation "testing for ,testName distribution results")
206 (:test (ensure-same
207 (lisp-stat-basics:,testName-quant ,quant-params) ,quant-answer))
208 (:test (ensure-same
209 (lisp-stat-basics:,testName-cdf ,cdf-params) ,cdf-answer))
210 (:test (ensure-same
211 (lisp-stat-basics:,testName-pmf ,pmf-params) ,pmf-answer))
212 (:test (progn
213 (set-seed 234)
214 (ensure-same
215 (lisp-stat-basics:,testName-rand ,rand-params) ,rand-answer)))))
217 ;;; Normal distribution
219 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
220 (:documentation "testing for Gaussian distn results")
221 (:test (ensure-same
222 (lisp-stat-basics:normal-quant 0.95)
223 1.6448536279366268))
224 (:test (ensure-same
225 (lisp-stat-basics:normal-cdf 1.3)
226 0.9031995154143897))
227 (:test (ensure-same
228 (lisp-stat-basics:normal-dens 1.3)
229 0.17136859204780736))
230 (:test (ensure-same
231 (lisp-stat-basics:normal-rand 2)
232 (-0.40502015f0 -0.8091404f0)))
233 (:test (ensure-same
234 (lisp-stat-basics:bivnorm-cdf 0.2 0.4 0.6)
235 0.4736873734160288)))
237 ;;;; Cauchy distribution
239 (deftestsuite lisp-stat-probdist-cauchy (lisp-stat-probdistn)
240 (:documentation "testing for Cachy-distn results")
241 (:test (ensure-same
242 (lisp-stat-basics:cauchy-quant 0.95)
243 6.313751514675031))
244 (:test (ensure-same
245 (lisp-stat-basics:cauchy-cdf 1.3)
246 0.7912855998398473))
247 (:test (ensure-same
248 (lisp-stat-basics:cauchy-dens 1.3)
249 0.1183308127104695 ))
250 (:test (ensure-same
251 (lisp-stat-basics:cauchy-rand 2)
252 (-1.06224644160405 -0.4524695943939537))))
254 ;;;; Gamma distribution
256 (deftestsuite lisp-stat-probdist-gamma (lisp-stat-probdistn)
257 (:documentation "testing for gamma distn results")
258 (:test (ensure-same
259 (lisp-stat-basics:gamma-quant 0.95 4.3)
260 8.178692439291645))
261 (:test (ensure-same
262 (lisp-stat-basics:gamma-cdf 1.3 4.3)
263 0.028895150986674906))
264 (:test (ensure-same
265 (lisp-stat-basics:gamma-dens 1.3 4.3)
266 0.0731517686447374))
267 (:test (ensure-same
268 (lisp-stat-basics:gamma-rand 2 4.3)
269 (2.454918912880936 4.081365384357454))))
271 ;;;; Chi-square distribution
273 (deftestsuite lisp-stat-probdist-chisq (lisp-stat-probdistn)
275 (:documentation "testing for Chi-square distn results")
276 (:test (ensure-same
277 (lisp-stat-basics:chisq-quant 0.95 3)
278 7.814727903379012))
279 (:test (ensure-same
280 (lisp-stat-basics:chisq-cdf 1 5)
281 0.03743422675631789))
282 (:test (ensure-same
283 (lisp-stat-basics:chisq-dens 1 5)
284 0.08065690818083521))
285 (:test (progn
286 (set-seed 353)
287 (ensure-same
288 (lisp-stat-basics:chisq-rand 2 4)
289 (1.968535826180572 2.9988646156942997)))))
291 ;;;; Beta distribution
293 (deftestsuite lisp-stat-probdist-beta (lisp-stat-probdistn)
295 (:documentation "testing for beta distn results")
296 (:test (ensure-same
297 (lisp-stat-basics:beta-quant 0.95 3 2)
298 0.9023885371149876))
299 (:test (ensure-same
300 (lisp-stat-basics:beta-cdf 0.4 2 2.4)
301 0.4247997418541529 ))
302 (:test (ensure-same
303 (lisp-stat-basics:beta-dens 0.4 2 2.4)
304 1.5964741858913518 ))
305 (:test (ensure-same
306 (lisp-stat-basics:beta-rand 2 2 2.4)
307 (0.8014897077282279 0.6516371997922659))))
309 ;;;; t distribution
311 (deftestsuite lisp-stat-probdist-t (lisp-stat-probdistn)
312 (:documentation "testing for t-distn results")
313 (:test (ensure-same
314 (lisp-stat-basics:t-quant 0.95 3)
315 2.35336343484194))
316 (:test (ensure-same
317 (lisp-stat-basics:t-cdf 1 2.3)
318 0.794733624298342))
319 (:test (ensure-same
320 (lisp-stat-basics:t-dens 1 2.3)
321 0.1978163816318102))
322 (:test (ensure-same
323 (lisp-stat-basics:t-rand 2 2.3)
324 (-0.34303672776089306 -1.142505872436518))))
326 ;;;; F distribution
328 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
329 (:documentation "testing for f-distn results")
330 (:test (ensure-same
331 (lisp-stat-basics:f-quant 0.95 3 5) 5.409451318117459))
332 (:test (ensure-same
333 (lisp-stat-basics:f-cdf 1 3.2 5.4)
334 0.5347130905510765))
335 (:test (ensure-same
336 (lisp-stat-basics:f-dens 1 3.2 5.4)
337 0.37551128864591415))
338 (:test (progn
339 (set-seed 234)
340 (ensure-same
341 (lisp-stat-basics:f-rand 2 3 2)
342 (0.7939093442091963 0.07442694152491144)))))
344 ;;;; Poisson distribution
346 (deftestsuite lisp-stat-probdist-poisson (lisp-stat-probdistn)
347 ;; (( ))
348 (:documentation "testing for poisson distribution results")
349 (:test (ensure-same
350 (lisp-stat-basics:poisson-quant 0.95 3.2) 6))
351 (:test (ensure-same
352 (lisp-stat-basics:poisson-cdf 1 3.2)
353 0.17120125672252395))
354 (:test (ensure-same
355 (lisp-stat-basics:poisson-pmf 1 3.2)
356 0.13043905274097067))
357 (:test (progn
358 (set-seed 234)
359 (ensure-same
360 (lisp-stat-basics:poisson-rand 5 3.2)
361 (list 2 1 2 0 3)))))
363 ;; Binomial distribution
365 (deftestsuite lisp-stat-probdist-binomial (lisp-stat-probdistn)
366 ;; (( ))
367 (:documentation "testing for binomial distribution results")
368 (:test (ensure-same
369 (lisp-stat-basics:binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
371 (:test (ensure-same
372 (lisp-stat-basics:binomial-quant 0 3 0.4)
373 ;; -2147483648
375 (:test (ensure-same
376 (lisp-stat-basics:binomial-cdf 1 3 0.4)
377 0.6479999999965776))
379 (:test (ensure-same
380 (lisp-stat-basics:binomial-pmf 1 3 0.4)
381 0.4320000000226171))
382 (:test (progn
383 (set-seed 526)
384 (ensure-same
385 (lisp-stat-basics:binomial-rand 5 3 0.4)
386 (list 2 2 0 1 2)))))
389 ;;; External support for running tests