A few more silly examples for lin alg handling.
[CommonLispStat.git] / unittests.lisp
bloba7da677100562bb3799481449f7dad28c17e263a
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))
37 (defun almost= (a b &key (tol 0.000001))
38 (< (abs (- a b)) tol))
40 (deftestsuite lisp-stat () ())
41 (deftestsuite lisp-stat-lin-alg (lisp-stat) ())
42 (deftestsuite lisp-stat-spec-fns (lisp-stat) ())
43 (deftestsuite lisp-stat-probdistn (lisp-stat) ())
45 ;; ;;; and add a test to it
46 ;; (addtest (lisp-stat)
47 ;; (ensure-same (+ 1 1) 2))
48 ;; ;; => #<Test passed>
50 ;; ;;; add another test using ensure-error
51 ;; (addtest (lisp-stat-lin-alg)
52 ;; (ensure-error (let ((x 0)) (/ x))))
53 ;; ;; => #<Test passed>
55 ;; ;;; add another, slightly more specific test
56 ;; (addtest (lisp-stat)
57 ;; (ensure-condition division-by-zero (let ((x 0)) (/ x))))
58 ;; ;; => #<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 (addtest (lisp-stat-lin-alg) lu-decomposition
69 (ensure-same
70 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
71 (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)))
73 (addtest (lisp-stat-lin-alg) rcondest
74 (ensure-same
75 (rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
76 6.8157451e7 ))
78 (addtest (lisp-stat-lin-alg) lu-solve
79 (ensure-same
80 (lu-solve
81 (lu-decomp
82 #2A((2 3 4) (1 2 4) (2 4 5)))
83 #(2 3 4))
84 #(-2.333333333333333 1.3333333333333335 0.6666666666666666)))
86 (addtest (lisp-stat-lin-alg) inverse
87 (ensure-same
88 (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
89 #2A((2.0 -0.33333333333333326 -1.3333333333333335)
90 (-1.0 -0.6666666666666666 1.3333333333333333)
91 (0.0 0.6666666666666666 -0.3333333333333333))))
93 (addtest (lisp-stat-lin-alg) sv-decomp
94 (ensure-same
95 (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
96 (list #2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
97 (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
98 (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
99 #(9.699290438141343 0.8971681569301373 0.3447525123483081)
100 #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
101 (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
102 (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
103 T)))
105 (addtest (lisp-stat-lin-alg) qr-decomp
106 (ensure-same
107 (qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
108 (list #2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
109 (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
110 (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
111 #2A((-3.0 -5.333333333333334 -7.333333333333332)
112 (0.0 -0.7453559924999292 -1.1925695879998877)
113 (0.0 0.0 -1.3416407864998738)))))
115 (addtest (lisp-stat-lin-alg) eigen
116 (ensure-same
117 (eigen #2A((2 3 4) (1 2 4) (2 4 5)))
118 (list #(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
119 (list #(0.4999999999999998 0.4999999999999997 0.7071067811865475)
120 #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
121 #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
122 NIL)))
124 (addtest (lisp-stat-lin-alg) spline
125 (ensure-same
126 (spline #(1.0 1.2 1.3 1.8 2.1 2.5)
127 #(1.2 2.0 2.1 2.0 1.1 2.8)
128 :xvals 6)
129 (list (list 1.0 1.3 1.6 1.9 2.2 2.5)
130 (list 1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))))
132 (addtest (lisp-stat-lin-alg) kernel-smooth
133 (ensure-same
134 ;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
135 (kernel-smooth
136 #(1.0 1.2 1.3 1.8 2.1 2.5)
137 #(1.2 2.0 2.1 2.0 1.1 2.8)
138 :xvals 5)
139 (list (list 1.0 1.375 1.75 2.125 2.5)
140 (list 1.6603277642110226 1.9471748095239771 1.7938127405752287
141 1.5871511322219498 2.518194783156392))))
143 (addtest (lisp-stat-lin-alg) kernel-dens
144 (ensure-same
145 (kernel-dens
146 #(1.0 1.2 2.5 2.1 1.8 1.2)
147 :xvals 5)
148 (list (list 1.0 1.375 1.75 2.125 2.5)
149 (list 0.7224150453621405 0.5820045548233707 0.38216411702854214
150 0.4829822708587095 0.3485939156929503))))
153 (addtest (lisp-stat-lin-alg) fft
154 (ensure-same
155 (fft #(1.0 1.2 2.5 2.1 1.8))
156 (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)))))
158 (addtest (lisp-stat-lin-alg) lowess
159 (ensure-same
160 (lowess #(1.0 1.2 2.5 2.1 1.8 1.2)
161 #(1.2 2.0 2.1 2.0 1.1 2.8))
162 #(1.0 1.2 1.2 1.8 2.1 2.5)))
166 ;;;; Log-gamma function
168 (addtest (lisp-stat-spec-fns) log-gamma-fn
169 (ensure-same
170 (log-gamma 3.4)
171 1.0923280596789584))
174 #+nil(progn
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)
184 ;; (deftestsuite lisp-stat-probdist-,prefixName (lisp-stat-probdistn)
185 ;; ;; (( ))
186 ;; (:documentation "testing for ,testName distribution results")
187 ;; (:test (ensure-same
188 ;; (lisp-stat-basics:,testName-quant ,quant-params) ,quant-answer))
189 ;; (:test (ensure-same
190 ;; (lisp-stat-basics:,testName-cdf ,cdf-params) ,cdf-answer))
191 ;; (:test (ensure-same
192 ;; (lisp-stat-basics:,testName-pmf ,pmf-params) ,pmf-answer))
193 ;; (:test (progn
194 ;; (set-seed 234)
195 ;; (ensure-same
196 ;; (lisp-stat-basics:,testName-rand ,rand-params) ,rand-answer)))))
198 ;;; Normal distribution
200 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
201 (:documentation "testing for Gaussian distn results")
202 (:test (ensure-same
203 (normal-quant 0.95)
204 1.6448536279366268))
205 (:test (ensure-same
206 (normal-cdf 1.3)
207 0.9031995154143897))
208 (:test (ensure-same
209 (normal-dens 1.3)
210 0.17136859204780736))
211 (:test (ensure-same
212 (normal-rand 2)
213 (list -0.40502015f0 -0.8091404f0)))
214 (:test (ensure-same
215 (bivnorm-cdf 0.2 0.4 0.6)
216 0.4736873734160288)))
218 ;;;; Cauchy distribution
220 (deftestsuite lisp-stat-probdist-cauchy (lisp-stat-probdistn)
221 (:documentation "testing for Cachy-distn results")
222 (:test (ensure-same
223 (cauchy-quant 0.95)
224 6.313751514675031))
225 (:test (ensure-same
226 (cauchy-cdf 1.3)
227 0.7912855998398473))
228 (:test (ensure-same
229 (cauchy-dens 1.3)
230 0.1183308127104695 ))
231 (:test (ensure-same
232 (cauchy-rand 2)
233 (-1.06224644160405 -0.4524695943939537))))
235 ;;;; Gamma distribution
237 (deftestsuite lisp-stat-probdist-gamma (lisp-stat-probdistn)
238 (:documentation "testing for gamma distn results")
239 (:test (ensure-same
240 (gamma-quant 0.95 4.3)
241 8.178692439291645))
242 (:test (ensure-same
243 (gamma-cdf 1.3 4.3)
244 0.028895150986674906))
245 (:test (ensure-same
246 (gamma-dens 1.3 4.3)
247 0.0731517686447374))
248 (:test (ensure-same
249 (gamma-rand 2 4.3)
250 (2.454918912880936 4.081365384357454))))
252 ;;;; Chi-square distribution
254 (deftestsuite lisp-stat-probdist-chisq (lisp-stat-probdistn)
256 (:documentation "testing for Chi-square distn results")
257 (:test (ensure-same
258 (chisq-quant 0.95 3)
259 7.814727903379012))
260 (:test (ensure-same
261 (chisq-cdf 1 5)
262 0.03743422675631789))
263 (:test (ensure-same
264 (chisq-dens 1 5)
265 0.08065690818083521))
266 (:test (progn
267 (set-seed 353)
268 (ensure-same
269 (chisq-rand 2 4)
270 (list 1.968535826180572 2.9988646156942997)))))
272 ;;;; Beta distribution
274 (deftestsuite lisp-stat-probdist-beta (lisp-stat-probdistn)
276 (:documentation "testing for beta distn results")
277 (:test (ensure-same
278 (beta-quant 0.95 3 2)
279 0.9023885371149876))
280 (:test (ensure-same
281 (beta-cdf 0.4 2 2.4)
282 0.4247997418541529 ))
283 (:test (ensure-same
284 (beta-dens 0.4 2 2.4)
285 1.5964741858913518 ))
286 (:test (ensure-same
287 (beta-rand 2 2 2.4)
288 (list 0.8014897077282279 0.6516371997922659))))
290 ;;;; t distribution
292 (deftestsuite lisp-stat-probdist-t (lisp-stat-probdistn)
293 (:documentation "testing for t-distn results")
294 (:test (ensure-same
295 (t-quant 0.95 3)
296 2.35336343484194))
297 (:test (ensure-same
298 (t-cdf 1 2.3)
299 0.794733624298342))
300 (:test (ensure-same
301 (t-dens 1 2.3)
302 0.1978163816318102))
303 (:test (ensure-same
304 (t-rand 2 2.3)
305 (list -0.34303672776089306 -1.142505872436518))))
307 ;;;; F distribution
309 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
310 (:documentation "testing for f-distn results")
311 (:test (ensure-same
312 (f-quant 0.95 3 5) 5.409451318117459))
313 (:test (ensure-same
314 (f-cdf 1 3.2 5.4)
315 0.5347130905510765))
316 (:test (ensure-same
317 (f-dens 1 3.2 5.4)
318 0.37551128864591415))
319 (:test (progn
320 (set-seed 234)
321 (ensure-same
322 (f-rand 2 3 2)
323 (list 0.7939093442091963 0.07442694152491144)))))
325 ;;;; Poisson distribution
327 (deftestsuite lisp-stat-probdist-poisson (lisp-stat-probdistn)
328 ;; (( ))
329 (:documentation "testing for poisson distribution results")
330 (:test (ensure-same
331 (poisson-quant 0.95 3.2) 6))
332 (:test (ensure-same
333 (poisson-cdf 1 3.2)
334 0.17120125672252395))
335 (:test (ensure-same
336 (poisson-pmf 1 3.2)
337 0.13043905274097067))
338 (:test (progn
339 (set-seed 234)
340 (ensure-same
341 (poisson-rand 5 3.2)
342 (list 2 1 2 0 3)))))
344 ;; Binomial distribution
346 (deftestsuite lisp-stat-probdist-binomial (lisp-stat-probdistn)
347 ;; (( ))
348 (:documentation "testing for binomial distribution results")
350 (:test (ensure-same
351 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
353 (:test (ensure-same
354 (binomial-quant 0 3 0.4)
355 ;; -2147483648
357 (:test (ensure-same
358 (binomial-cdf 1 3 0.4)
359 0.6479999999965776))
361 (:test (ensure-same
362 (binomial-pmf 1 3 0.4)
363 0.4320000000226171))
364 (:test (progn
365 (set-seed 526)
366 (ensure-same
367 (binomial-rand 5 3 0.4)
368 (list 2 2 0 1 2)))))