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
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
)
31 (defun run-lisp-stat-tests ()
32 (run-tests :suite
'lisp-stat
))
34 (defun run-lisp-stat-test (&rest 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
))
84 (numerical= (car a
) (car b
) :tol tol
))
86 (if (= (length (cdr a
)) 0)
88 (numerical= (cdr a
) (cdr b
) :tol tol
)))
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
))
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
))
104 (every #'(lambda (x) x
) a-b-elt-eq
))))
106 (deftestsuite lisp-stat-ut-testsupport
(lisp-stat-ut)
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)
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))))
145 (numerical=A1
(ensure (numerical= #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)
159 ;; (describe (run-tests :suite 'lisp-stat-ut-testsupport2))
163 ;;;; Log-gamma function
165 (addtest (lisp-stat-ut-spec-fns) log-gamma-fn
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)
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))
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")
207 0.17136859204780736))
210 (list -
0.40502015f0 -
0.8091404f0
)))
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")
228 0.1183308127104695 ))
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")
239 (gamma-quant 0.95 4.3)
243 0.028895150986674906))
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")
261 0.03743422675631789))
264 0.08065690818083521))
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")
277 (beta-quant 0.95 3 2)
281 0.4247997418541529 ))
283 (beta-dens 0.4 2 2.4)
284 1.5964741858913518 ))
287 (list 0.8014897077282279 0.6516371997922659))))
291 (deftestsuite lisp-stat-ut-probdist-t
(lisp-stat-ut-probdistn)
293 (:documentation
"testing for t-distn results")
305 (list -
0.34303672776089306 -
1.142505872436518))))
309 (deftestsuite lisp-stat-ut-probdist-f
(lisp-stat-ut-probdistn)
311 (:documentation
"testing for f-distn results")
313 (f-quant 0.95 3 5) 5.409451318117459))
319 0.37551128864591415))
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")
332 (poisson-quant 0.95 3.2) 6))
335 0.17120125672252395))
338 0.13043905274097067))
345 ;; Binomial distribution
347 (deftestsuite lisp-stat-ut-probdist-binomial
(lisp-stat-ut-probdistn)
349 (:documentation
"testing for binomial distribution results")
352 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
355 (binomial-quant 0 3 0.4)
359 (binomial-cdf 1 3 0.4)
363 (binomial-pmf 1 3 0.4)
368 (binomial-rand 5 3 0.4)