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