documentation edits, some clarifying, some whitespace.
[CommonLispStat.git] / unittests.lisp
blobc7c65bd5a5bcb220612c32bc4842ca97950077b6
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))
34 (defun run-lisp-stat-test (&rest x)
35 (run-test x))
38 (deftestsuite lisp-stat-ut () ())
39 (deftestsuite lisp-stat-ut-lin-alg (lisp-stat-ut) ())
40 (deftestsuite lisp-stat-ut-spec-fns (lisp-stat-ut) ())
41 (deftestsuite lisp-stat-ut-probdistn (lisp-stat-ut) ())
44 (defun almost= (a b &key (tol 0.000001))
45 "Numerically compares 2 values to a tolerance."
46 (< (abs (- a b)) tol))
48 (defun almost=lists (a b &key (tol 0.000001))
49 "Numerically compare 2 lists using almost=."
50 (if (and (null a) (null b))
52 (and (almost= (car a) (car b) :tol tol)
53 (almost=lists (cdr a) (cdr b) :tol tol))))
57 ;; Need to consider a CLOSy approach for almost= to cover the range of
58 ;; possible data structures that we would like to be equal to a
59 ;; particular tolerance range. For example, fill in a shell like:
61 (defgeneric numerical= (a b &key tol))
63 (defmethod numerical= ((a real) (b real) &key (tol 0.00001)) ;; real))
64 ;;(print (format nil " equality pred for real a=~w real b=~w" a b))
65 (< (abs (- a b)) tol))
67 ;; can we just worry about reals if integers are a subclass?
68 (defmethod numerical= ((a integer) (b integer) &key (tol 0.1)) ;; real))
69 ;;(print (format nil " equality pred for int a=~w int b=~w" a b))
70 (< (abs (- a b)) tol))
72 (defmethod numerical= ((a complex) (b complex) &key (tol 0.00001))
73 ;;(print (format nil " equality pred for cmplx a=~w cmplx b=~w" a b))
74 (< (abs (- a b)) tol))
76 (defmethod numerical= ((a sequence) (b sequence) &key (tol 0.00001))
77 ;; (print (format nil "checking equality for list a ~w list b=~w" a b))
78 ;; using sequence for lists and vectors, but not arrays.
79 ;; FIXME++++ This is too slow, too many comparisons!
80 (if (and (null a) (null b))
82 (if (and (= (length a) (length b))
83 (> (length a) 0)
84 (numerical= (car a) (car b) :tol tol))
85 (progn
86 (if (= (length (cdr a)) 0)
88 (numerical= (cdr a) (cdr b) :tol tol)))
89 nil)))
91 ;; To do.
93 (defmethod numerical= ((a array) (b array) &key (tol 0.00001))
94 (print (format nil "checking equality for array a ~w and array b=~w" a b))
95 ;;; FIXME Warning! Need to generalize past 2-d array!!
96 (if (/= (array-dimensions a) (array-dimensions b))
97 nil
98 (let* ((a-dim (array-dimensions a))
99 (a-b-elt-eq (loop for i from 0 to (nth 0 a-dim)
100 for j from 0 to (nth 1 a-dim)
101 collect (numerical= (apply #'aref a (list i j))
102 (apply #'aref b (list i j))
103 :tol tol))))
104 (every #'(lambda (x) x) a-b-elt-eq))))
106 (deftestsuite lisp-stat-ut-testsupport (lisp-stat-ut)
108 (:tests
109 (almost=1 (ensure (almost= 3 3.001 :tol 0.01)))
110 (almost=2 (ensure (almost= 3 3.01 :tol 0.01)))
111 (almost=3 (ensure (not (almost= 3 3.1 :tol 0.01))))
112 (almost=lists1 (ensure (almost=lists nil nil :tol 0.01)))
113 (almost=lists2 (ensure (almost=lists (list ) (list ) :tol 0.01)))
114 (almost=lists3 (ensure (almost=lists (list 1.0) (list 1.0) :tol 0.01)))
115 (almost=lists4 (ensure (almost=lists (list 1.0 1.0) (list 1.0 1.0) :tol 0.01)))
116 (almost=lists5 (ensure (not (almost=lists (list 1.0 1.0)
117 (list 1.0 1.1) :tol 0.01))))))
119 (deftestsuite lisp-stat-ut-testsupport2 (lisp-stat-ut)
121 (:tests
122 (numerical=1 (ensure (numerical= 3 3.001 :tol 0.01)))
123 (numerical=1.1 (ensure (numerical= 2 2)))
124 (numerical=1.2 (ensure (not (numerical= 2 3))))
125 (numerical=2 (ensure (numerical= 3 3.01 :tol 0.01)))
126 (numerical=3 (ensure (not (numerical= 3 3.1 :tol 0.01))))
127 (numerical=4 (ensure (numerical= nil nil :tol 0.01)))
128 (numerical=5 (ensure (numerical= (list ) (list ) :tol 0.01)))
129 (numerical=6 (ensure (numerical= (list 1.0) (list 1.0) :tol 0.01)))
130 (numerical=7 (ensure (numerical= (list 1.0 1.0) (list 1.0 1.0) :tol 0.01)))
131 (numerical=7.5 (ensure-error (numerical= 1.0 (list 1.0 1.0) :tol 0.01)))
132 (numerical=8 (ensure (not (numerical= (list 2.0 2.0 2.2) (list 2.1 2.0 2.2)))))
133 (numerical=9 (ensure (numerical= (list 2.1 2.0 2.2) (list 2.1 2.0 2.2)) ))
134 (numerical=10 (ensure (numerical= (list 2.1 2.0 2.2 4.2) (list 2.1 2.0 2.2 4.2))))
135 (numerical=11 (ensure (not (numerical= (list 2.1 2.0 2.3 4.0) (list 2.1 2.0 2.2 4.0)))))
136 (numerical=12 (ensure (not (numerical= (list 1.0 1.0)
137 (list 1.0 1.1) :tol 0.01))))
138 (numerical=C1 (ensure (numerical= #C(2 3) #C(2 3))))
139 (numerical=C2 (ensure (not(numerical= #C(2 3) #C(2 4)))))
140 (numerical=C3 (ensure (numerical= #C(2 3) #C(3 4) :tol 2)))
141 (numerical=C4 (ensure (not(numerical= #C(2 3) #C(3 4) :tol 1))))
143 ;;;; Tests to fix
145 (numerical=A1 (ensure (numerical= #1A(2 3 4)
146 #1A(2 3 4))))
148 (numerical=A2 (ensure (numerical= #2A((2 3 4) (1 2 4) (2 4 5))
149 #2A((2 3 4) (1 2 4) (2 4 5)))))
151 (numerical=A3 (ensure (not (numerical= #2A((2 3 4) (1 2 4) (2 5 4))
152 #2A((2 3 4) (1 2 4) (2 4 5))))))
154 (numerical=A4 (ensure (not (numerical= #1A(2 2 4)
155 #1A(2 3 4)))))
159 ;; (describe (run-tests :suite 'lisp-stat-ut-testsupport2))
163 ;;;; Log-gamma function
165 (addtest (lisp-stat-ut-spec-fns) log-gamma-fn
166 (ensure-same
167 (log-gamma 3.4)
168 1.0923280596789584
169 :test 'almost=))
172 ;;; Probability distributions
174 ;; This macro should be generalized, but it's a good start now.
175 ;;(defmacro ProbDistnTests (prefixName
176 ;; quant-params quant-answer
177 ;; cdf-params cdf-answer
178 ;; pmf-params pmf-answer
179 ;; rand-params rand-answer)
180 ;; (deftestsuite lisp-stat-ut-probdist-,prefixName (lisp-stat-ut-probdistn)
181 ;; ;; (( ))
182 ;; (:documentation "testing for ,testName distribution results")
183 ;; (:test (ensure-same
184 ;; (lisp-stat-ut-basics:,testName-quant ,quant-params) ,quant-answer))
185 ;; (:test (ensure-same
186 ;; (lisp-stat-ut-basics:,testName-cdf ,cdf-params) ,cdf-answer))
187 ;; (:test (ensure-same
188 ;; (lisp-stat-ut-basics:,testName-pmf ,pmf-params) ,pmf-answer))
189 ;; (:test (progn
190 ;; (set-seed 234)
191 ;; (ensure-same
192 ;; (lisp-stat-ut-basics:,testName-rand ,rand-params) ,rand-answer)))))
194 ;;; Normal distribution
196 (deftestsuite lisp-stat-ut-probdist-f (lisp-stat-ut-probdistn)
198 (:documentation "testing for Gaussian distn results")
199 (:test (ensure-same
200 (normal-quant 0.95)
201 1.6448536279366268))
202 (:test (ensure-same
203 (normal-cdf 1.3)
204 0.9031995154143897))
205 (:test (ensure-same
206 (normal-dens 1.3)
207 0.17136859204780736))
208 (:test (ensure-same
209 (normal-rand 2)
210 (list -0.40502015f0 -0.8091404f0)))
211 (:test (ensure-same
212 (bivnorm-cdf 0.2 0.4 0.6)
213 0.4736873734160288)))
215 ;;;; Cauchy distribution
217 (deftestsuite lisp-stat-ut-probdist-cauchy (lisp-stat-ut-probdistn)
219 (:documentation "testing for Cachy-distn results")
220 (:test (ensure-same
221 (cauchy-quant 0.95)
222 6.313751514675031))
223 (:test (ensure-same
224 (cauchy-cdf 1.3)
225 0.7912855998398473))
226 (:test (ensure-same
227 (cauchy-dens 1.3)
228 0.1183308127104695 ))
229 (:test (ensure-same
230 (cauchy-rand 2)
231 (list -1.06224644160405 -0.4524695943939537))))
233 ;;;; Gamma distribution
235 (deftestsuite lisp-stat-ut-probdist-gamma (lisp-stat-ut-probdistn)
237 (:documentation "testing for gamma distn results")
238 (:test (ensure-same
239 (gamma-quant 0.95 4.3)
240 8.178692439291645))
241 (:test (ensure-same
242 (gamma-cdf 1.3 4.3)
243 0.028895150986674906))
244 (:test (ensure-same
245 (gamma-dens 1.3 4.3)
246 0.0731517686447374))
247 (:test (ensure-same
248 (gamma-rand 2 4.3)
249 (list 2.454918912880936 4.081365384357454))))
251 ;;;; Chi-square distribution
253 (deftestsuite lisp-stat-ut-probdist-chisq (lisp-stat-ut-probdistn)
255 (:documentation "testing for Chi-square distn results")
256 (:test (ensure-same
257 (chisq-quant 0.95 3)
258 7.814727903379012))
259 (:test (ensure-same
260 (chisq-cdf 1 5)
261 0.03743422675631789))
262 (:test (ensure-same
263 (chisq-dens 1 5)
264 0.08065690818083521))
265 (:test (progn
266 (set-seed 353)
267 (ensure-same
268 (chisq-rand 2 4)
269 (list 1.968535826180572 2.9988646156942997)))))
271 ;;;; Beta distribution
273 (deftestsuite lisp-stat-ut-probdist-beta (lisp-stat-ut-probdistn)
275 (:documentation "testing for beta distn results")
276 (:test (ensure-same
277 (beta-quant 0.95 3 2)
278 0.9023885371149876))
279 (:test (ensure-same
280 (beta-cdf 0.4 2 2.4)
281 0.4247997418541529 ))
282 (:test (ensure-same
283 (beta-dens 0.4 2 2.4)
284 1.5964741858913518 ))
285 (:test (ensure-same
286 (beta-rand 2 2 2.4)
287 (list 0.8014897077282279 0.6516371997922659))))
289 ;;;; t distribution
291 (deftestsuite lisp-stat-ut-probdist-t (lisp-stat-ut-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-ut-probdist-f (lisp-stat-ut-probdistn)
311 (:documentation "testing for f-distn results")
312 (:test (ensure-same
313 (f-quant 0.95 3 5) 5.409451318117459))
314 (:test (ensure-same
315 (f-cdf 1 3.2 5.4)
316 0.5347130905510765))
317 (:test (ensure-same
318 (f-dens 1 3.2 5.4)
319 0.37551128864591415))
320 (:test (progn
321 (set-seed 234)
322 (ensure-same
323 (f-rand 2 3 2)
324 (list 0.7939093442091963 0.07442694152491144)))))
326 ;;;; Poisson distribution
328 (deftestsuite lisp-stat-ut-probdist-poisson (lisp-stat-ut-probdistn)
330 (:documentation "testing for poisson distribution results")
331 (:test (ensure-same
332 (poisson-quant 0.95 3.2) 6))
333 (:test (ensure-same
334 (poisson-cdf 1 3.2)
335 0.17120125672252395))
336 (:test (ensure-same
337 (poisson-pmf 1 3.2)
338 0.13043905274097067))
339 (:test (progn
340 (set-seed 234)
341 (ensure-same
342 (poisson-rand 5 3.2)
343 (list 2 1 2 0 3)))))
345 ;; Binomial distribution
347 (deftestsuite lisp-stat-ut-probdist-binomial (lisp-stat-ut-probdistn)
349 (:documentation "testing for binomial distribution results")
351 (:test (ensure-same
352 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
354 (:test (ensure-same
355 (binomial-quant 0 3 0.4)
356 ;; -2147483648
358 (:test (ensure-same
359 (binomial-cdf 1 3 0.4)
360 0.6479999999965776))
362 (:test (ensure-same
363 (binomial-pmf 1 3 0.4)
364 0.4320000000226171))
365 (:test (progn
366 (set-seed 526)
367 (ensure-same
368 (binomial-rand 5 3 0.4)
369 (list 2 2 0 1 2)))))