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-ut
))
34 ;; (run-lisp-stat-tests)
37 (defun run-lisp-stat-test (&rest 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
))
87 (numerical= (car a
) (car b
) :tol tol
))
89 (if (= (length (cdr a
)) 0)
91 (numerical= (cdr a
) (cdr b
) :tol tol
)))
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
))
107 (every #'(lambda (x) x
) a-b-elt-eq
))))
109 (deftestsuite lisp-stat-ut-testsupport
(lisp-stat-ut)
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)
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))))
148 (numerical=A1
(ensure (numerical= #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)
162 ;; (describe (run-tests :suite 'lisp-stat-ut-testsupport2))
166 ;;;; Log-gamma function
168 (addtest (lisp-stat-ut-spec-fns) log-gamma-fn
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)
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))
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")
210 0.17136859204780736))
213 (list -
0.40502015f0 -
0.8091404f0
)))
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")
231 0.1183308127104695 ))
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")
242 (gamma-quant 0.95 4.3)
246 0.028895150986674906))
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")
264 0.03743422675631789))
267 0.08065690818083521))
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")
280 (beta-quant 0.95 3 2)
284 0.4247997418541529 ))
286 (beta-dens 0.4 2 2.4)
287 1.5964741858913518 ))
290 (list 0.8014897077282279 0.6516371997922659))))
294 (deftestsuite lisp-stat-ut-probdist-t
(lisp-stat-ut-probdistn)
296 (:documentation
"testing for t-distn results")
308 (list -
0.34303672776089306 -
1.142505872436518))))
312 (deftestsuite lisp-stat-ut-probdist-f
(lisp-stat-ut-probdistn)
314 (:documentation
"testing for f-distn results")
316 (f-quant 0.95 3 5) 5.409451318117459))
322 0.37551128864591415))
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")
335 (poisson-quant 0.95 3.2) 6))
338 0.17120125672252395))
341 0.13043905274097067))
348 ;; Binomial distribution
350 (deftestsuite lisp-stat-ut-probdist-binomial
(lisp-stat-ut-probdistn)
352 (:documentation
"testing for binomial distribution results")
355 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
358 (binomial-quant 0 3 0.4)
362 (binomial-cdf 1 3 0.4)
366 (binomial-pmf 1 3 0.4)
371 (binomial-rand 5 3 0.4)