docs and whitespace.
[CommonLispStat.git] / src / unittests / unittests.lisp
blobabd643284985f3c9aa9431de8d256a9944f6392b
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
21 conjugate realpart imagpart phase
22 min max logand logior logxor lognot ffloor fceiling
23 ftruncate fround signum cis)
24 (:export run-lisp-stat-tests run-lisp-stat-test scoreboard ; exec
25 almost= almost=lists numerical=)) ; compare
27 (in-package :lisp-stat-unittests)
29 ;;; TESTS
31 (defun run-lisp-stat-tests ()
32 (run-tests :suite 'lisp-stat-ut))
34 ;; (run-lisp-stat-tests)
37 (defun run-lisp-stat-test (&rest x)
38 (run-test x))
41 (deftestsuite lisp-stat-ut () ())
42 (deftestsuite lisp-stat-ut-lin-alg (lisp-stat-ut) ())
43 (deftestsuite lisp-stat-ut-spec-fns (lisp-stat-ut) ())
44 (deftestsuite lisp-stat-ut-probdistn (lisp-stat-ut) ())
47 (defun almost= (a b &key (tol 0.000001))
48 "Numerically compares 2 values to a tolerance."
49 (< (abs (- a b)) tol))
51 (defun almost=lists (a b &key (tol 0.000001))
52 "Numerically compare 2 lists using almost=."
53 (if (and (null a) (null b))
55 (and (almost= (car a) (car b) :tol tol)
56 (almost=lists (cdr a) (cdr b) :tol tol))))
60 ;; Need to consider a CLOSy approach for almost= to cover the range of
61 ;; possible data structures that we would like to be equal to a
62 ;; particular tolerance range. For example, fill in a shell like:
64 (defgeneric numerical= (a b &key tol))
66 (defmethod numerical= ((a real) (b real) &key (tol 0.00001)) ;; real))
67 ;;(print (format nil " equality pred for real a=~w real b=~w" a b))
68 (< (abs (- a b)) tol))
70 ;; can we just worry about reals if integers are a subclass?
71 (defmethod numerical= ((a integer) (b integer) &key (tol 0.1)) ;; real))
72 ;;(print (format nil " equality pred for int a=~w int b=~w" a b))
73 (< (abs (- a b)) tol))
75 (defmethod numerical= ((a complex) (b complex) &key (tol 0.00001))
76 ;;(print (format nil " equality pred for cmplx a=~w cmplx b=~w" a b))
77 (< (abs (- a b)) tol))
79 (defmethod numerical= ((a sequence) (b sequence) &key (tol 0.00001))
80 ;; (print (format nil "checking equality for list a ~w list b=~w" a b))
81 ;; using sequence for lists and vectors, but not arrays.
82 ;; FIXME++++ This is too slow, too many comparisons!
83 (if (and (null a) (null b))
85 (if (and (= (length a) (length b))
86 (> (length a) 0)
87 (numerical= (car a) (car b) :tol tol))
88 (progn
89 (if (= (length (cdr a)) 0)
91 (numerical= (cdr a) (cdr b) :tol tol)))
92 nil)))
94 ;; To do.
96 (defmethod numerical= ((a array) (b array) &key (tol 0.00001))
97 (print (format nil "checking equality for array a ~w and array b=~w" a b))
98 ;;; FIXME Warning! Need to generalize past 2-d array!!
99 (if (/= (array-dimensions a) (array-dimensions b))
101 (let* ((a-dim (array-dimensions a))
102 (a-b-elt-eq (loop for i from 0 to (nth 0 a-dim)
103 for j from 0 to (nth 1 a-dim)
104 collect (numerical= (apply #'aref a (list i j))
105 (apply #'aref b (list i j))
106 :tol tol))))
107 (every #'(lambda (x) x) a-b-elt-eq))))
109 (deftestsuite lisp-stat-ut-testsupport (lisp-stat-ut)
111 (:tests
112 (almost=1 (ensure (almost= 3 3.001 :tol 0.01)))
113 (almost=2 (ensure (almost= 3 3.01 :tol 0.01)))
114 (almost=3 (ensure (not (almost= 3 3.1 :tol 0.01))))
115 (almost=lists1 (ensure (almost=lists nil nil :tol 0.01)))
116 (almost=lists2 (ensure (almost=lists (list ) (list ) :tol 0.01)))
117 (almost=lists3 (ensure (almost=lists (list 1.0) (list 1.0) :tol 0.01)))
118 (almost=lists4 (ensure (almost=lists (list 1.0 1.0) (list 1.0 1.0) :tol 0.01)))
119 (almost=lists5 (ensure (not (almost=lists (list 1.0 1.0)
120 (list 1.0 1.1) :tol 0.01))))))
122 (deftestsuite lisp-stat-ut-testsupport2 (lisp-stat-ut)
124 (:tests
125 (numerical=1 (ensure (numerical= 3 3.001 :tol 0.01)))
126 (numerical=1.1 (ensure (numerical= 2 2)))
127 (numerical=1.2 (ensure (not (numerical= 2 3))))
128 (numerical=2 (ensure (numerical= 3 3.01 :tol 0.01)))
129 (numerical=3 (ensure (not (numerical= 3 3.1 :tol 0.01))))
130 (numerical=4 (ensure (numerical= nil nil :tol 0.01)))
131 (numerical=5 (ensure (numerical= (list ) (list ) :tol 0.01)))
132 (numerical=6 (ensure (numerical= (list 1.0) (list 1.0) :tol 0.01)))
133 (numerical=7 (ensure (numerical= (list 1.0 1.0) (list 1.0 1.0) :tol 0.01)))
134 (numerical=7.5 (ensure-error (numerical= 1.0 (list 1.0 1.0) :tol 0.01)))
135 (numerical=8 (ensure (not (numerical= (list 2.0 2.0 2.2) (list 2.1 2.0 2.2)))))
136 (numerical=9 (ensure (numerical= (list 2.1 2.0 2.2) (list 2.1 2.0 2.2)) ))
137 (numerical=10 (ensure (numerical= (list 2.1 2.0 2.2 4.2) (list 2.1 2.0 2.2 4.2))))
138 (numerical=11 (ensure (not (numerical= (list 2.1 2.0 2.3 4.0) (list 2.1 2.0 2.2 4.0)))))
139 (numerical=12 (ensure (not (numerical= (list 1.0 1.0)
140 (list 1.0 1.1) :tol 0.01))))
141 (numerical=C1 (ensure (numerical= #C(2 3) #C(2 3))))
142 (numerical=C2 (ensure (not(numerical= #C(2 3) #C(2 4)))))
143 (numerical=C3 (ensure (numerical= #C(2 3) #C(3 4) :tol 2)))
144 (numerical=C4 (ensure (not(numerical= #C(2 3) #C(3 4) :tol 1))))
146 ;;;; Tests to fix
148 (numerical=A1 (ensure (numerical= #1A(2 3 4)
149 #1A(2 3 4))))
151 (numerical=A2 (ensure (numerical= #2A((2 3 4) (1 2 4) (2 4 5))
152 #2A((2 3 4) (1 2 4) (2 4 5)))))
154 (numerical=A3 (ensure (not (numerical= #2A((2 3 4) (1 2 4) (2 5 4))
155 #2A((2 3 4) (1 2 4) (2 4 5))))))
157 (numerical=A4 (ensure (not (numerical= #1A(2 2 4)
158 #1A(2 3 4)))))
162 ;; (describe (run-tests :suite 'lisp-stat-ut-testsupport2))
166 ;;;; Log-gamma function
168 (addtest (lisp-stat-ut-spec-fns) log-gamma-fn
169 (ensure-same
170 (log-gamma 3.4)
171 1.0923280596789584
172 :test 'almost=))
175 ;;; Probability distributions
177 ;; This macro should be generalized, but it's a good start now.
178 ;;(defmacro ProbDistnTests (prefixName
179 ;; quant-params quant-answer
180 ;; cdf-params cdf-answer
181 ;; pmf-params pmf-answer
182 ;; rand-params rand-answer)
183 ;; (deftestsuite lisp-stat-ut-probdist-,prefixName (lisp-stat-ut-probdistn)
184 ;; ;; (( ))
185 ;; (:documentation "testing for ,testName distribution results")
186 ;; (:test (ensure-same
187 ;; (lisp-stat-ut-basics:,testName-quant ,quant-params) ,quant-answer))
188 ;; (:test (ensure-same
189 ;; (lisp-stat-ut-basics:,testName-cdf ,cdf-params) ,cdf-answer))
190 ;; (:test (ensure-same
191 ;; (lisp-stat-ut-basics:,testName-pmf ,pmf-params) ,pmf-answer))
192 ;; (:test (progn
193 ;; (set-seed 234)
194 ;; (ensure-same
195 ;; (lisp-stat-ut-basics:,testName-rand ,rand-params) ,rand-answer)))))
197 ;;; Normal distribution
199 (deftestsuite lisp-stat-ut-probdist-f (lisp-stat-ut-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-ut-probdist-cauchy (lisp-stat-ut-probdistn)
222 (:documentation "testing for Cachy-distn results")
223 (:test (ensure-same
224 (cauchy-quant 0.95)
225 6.313751514675031))
226 (:test (ensure-same
227 (cauchy-cdf 1.3)
228 0.7912855998398473))
229 (:test (ensure-same
230 (cauchy-dens 1.3)
231 0.1183308127104695 ))
232 (:test (ensure-same
233 (cauchy-rand 2)
234 (list -1.06224644160405 -0.4524695943939537))))
236 ;;;; Gamma distribution
238 (deftestsuite lisp-stat-ut-probdist-gamma (lisp-stat-ut-probdistn)
240 (:documentation "testing for gamma distn results")
241 (:test (ensure-same
242 (gamma-quant 0.95 4.3)
243 8.178692439291645))
244 (:test (ensure-same
245 (gamma-cdf 1.3 4.3)
246 0.028895150986674906))
247 (:test (ensure-same
248 (gamma-dens 1.3 4.3)
249 0.0731517686447374))
250 (:test (ensure-same
251 (gamma-rand 2 4.3)
252 (list 2.454918912880936 4.081365384357454))))
254 ;;;; Chi-square distribution
256 (deftestsuite lisp-stat-ut-probdist-chisq (lisp-stat-ut-probdistn)
258 (:documentation "testing for Chi-square distn results")
259 (:test (ensure-same
260 (chisq-quant 0.95 3)
261 7.814727903379012))
262 (:test (ensure-same
263 (chisq-cdf 1 5)
264 0.03743422675631789))
265 (:test (ensure-same
266 (chisq-dens 1 5)
267 0.08065690818083521))
268 (:test (progn
269 (set-seed 353)
270 (ensure-same
271 (chisq-rand 2 4)
272 (list 1.968535826180572 2.9988646156942997)))))
274 ;;;; Beta distribution
276 (deftestsuite lisp-stat-ut-probdist-beta (lisp-stat-ut-probdistn)
278 (:documentation "testing for beta distn results")
279 (:test (ensure-same
280 (beta-quant 0.95 3 2)
281 0.9023885371149876))
282 (:test (ensure-same
283 (beta-cdf 0.4 2 2.4)
284 0.4247997418541529 ))
285 (:test (ensure-same
286 (beta-dens 0.4 2 2.4)
287 1.5964741858913518 ))
288 (:test (ensure-same
289 (beta-rand 2 2 2.4)
290 (list 0.8014897077282279 0.6516371997922659))))
292 ;;;; t distribution
294 (deftestsuite lisp-stat-ut-probdist-t (lisp-stat-ut-probdistn)
296 (:documentation "testing for t-distn results")
297 (:test (ensure-same
298 (t-quant 0.95 3)
299 2.35336343484194))
300 (:test (ensure-same
301 (t-cdf 1 2.3)
302 0.794733624298342))
303 (:test (ensure-same
304 (t-dens 1 2.3)
305 0.1978163816318102))
306 (:test (ensure-same
307 (t-rand 2 2.3)
308 (list -0.34303672776089306 -1.142505872436518))))
310 ;;;; F distribution
312 (deftestsuite lisp-stat-ut-probdist-f (lisp-stat-ut-probdistn)
314 (:documentation "testing for f-distn results")
315 (:test (ensure-same
316 (f-quant 0.95 3 5) 5.409451318117459))
317 (:test (ensure-same
318 (f-cdf 1 3.2 5.4)
319 0.5347130905510765))
320 (:test (ensure-same
321 (f-dens 1 3.2 5.4)
322 0.37551128864591415))
323 (:test (progn
324 (set-seed 234)
325 (ensure-same
326 (f-rand 2 3 2)
327 (list 0.7939093442091963 0.07442694152491144)))))
329 ;;;; Poisson distribution
331 (deftestsuite lisp-stat-ut-probdist-poisson (lisp-stat-ut-probdistn)
333 (:documentation "testing for poisson distribution results")
334 (:test (ensure-same
335 (poisson-quant 0.95 3.2) 6))
336 (:test (ensure-same
337 (poisson-cdf 1 3.2)
338 0.17120125672252395))
339 (:test (ensure-same
340 (poisson-pmf 1 3.2)
341 0.13043905274097067))
342 (:test (progn
343 (set-seed 234)
344 (ensure-same
345 (poisson-rand 5 3.2)
346 (list 2 1 2 0 3)))))
348 ;; Binomial distribution
350 (deftestsuite lisp-stat-ut-probdist-binomial (lisp-stat-ut-probdistn)
352 (:documentation "testing for binomial distribution results")
354 (:test (ensure-same
355 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
357 (:test (ensure-same
358 (binomial-quant 0 3 0.4)
359 ;; -2147483648
361 (:test (ensure-same
362 (binomial-cdf 1 3 0.4)
363 0.6479999999965776))
365 (:test (ensure-same
366 (binomial-pmf 1 3 0.4)
367 0.4320000000226171))
368 (:test (progn
369 (set-seed 526)
370 (ensure-same
371 (binomial-rand 5 3 0.4)
372 (list 2 2 0 1 2)))))