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
11 (in-package :lisp-stat-unittests
)
15 (defun run-lisp-stat-tests ()
16 (run-tests :suite
'lisp-stat-ut
))
18 ;; (run-lisp-stat-tests)
21 (defun run-lisp-stat-test (&rest x
)
24 (deftestsuite lisp-stat-ut
() ())
26 ;; others should be defined in another place...:
27 ;;(deftestsuite lisp-stat-ut-lin-alg (lisp-stat-ut) ())
28 (deftestsuite lisp-stat-ut-spec-fns
(lisp-stat-ut) ())
29 (deftestsuite lisp-stat-ut-probdistn
(lisp-stat-ut) ())
31 (defun almost= (a b
&key
(tol 0.000001))
32 "Numerically compares 2 values to a tolerance."
33 (< (abs (- a b
)) tol
))
35 (defun almost=lists
(a b
&key
(tol 0.000001))
36 "Numerically compare 2 lists using almost=."
37 (if (and (null a
) (null b
))
39 (and (almost= (car a
) (car b
) :tol tol
)
40 (almost=lists
(cdr a
) (cdr b
) :tol tol
))))
42 ;; Need to consider a CLOSy approach for almost= to cover the range of
43 ;; possible data structures that we would like to be equal to a
44 ;; particular tolerance range. For example, fill in a shell like:
46 (defgeneric numerical
= (a b
&key tol
))
48 (defmethod numerical= ((a real
) (b real
) &key
(tol 0.00001)) ;; real))
49 ;;(print (format nil " equality pred for real a=~w real b=~w" a b))
50 (< (abs (- a b
)) tol
))
52 ;; can we just worry about reals if integers are a subclass?
53 (defmethod numerical= ((a integer
) (b integer
) &key
(tol 0.1)) ;; real))
54 ;;(print (format nil " equality pred for int a=~w int b=~w" a b))
55 (< (abs (- a b
)) tol
))
57 (defmethod numerical= ((a complex
) (b complex
) &key
(tol 0.00001))
58 ;;(print (format nil " equality pred for cmplx a=~w cmplx b=~w" a b))
59 (< (abs (- a b
)) tol
))
61 (defmethod numerical= ((a sequence
) (b sequence
) &key
(tol 0.00001))
62 ;; (print (format nil "checking equality for list a ~w list b=~w" a b))
63 ;; using sequence for lists and vectors, but not arrays.
64 ;; FIXME++++ This is too slow, too many comparisons!
65 (if (and (null a
) (null b
))
67 (if (and (= (length a
) (length b
))
69 (numerical= (car a
) (car b
) :tol tol
))
71 (if (= (length (cdr a
)) 0)
73 (numerical= (cdr a
) (cdr b
) :tol tol
)))
78 (defmethod numerical= ((a array
) (b array
) &key
(tol 0.00001))
79 (print (format nil
"checking equality for array a ~w and array b=~w" a b
))
80 ;;; FIXME Warning! Need to generalize past 2-d array!!
81 (if (/= (array-dimensions a
) (array-dimensions b
))
83 (let* ((a-dim (array-dimensions a
))
84 (a-b-elt-eq (loop for i from
0 to
(nth 0 a-dim
)
85 for j from
0 to
(nth 1 a-dim
)
86 collect
(numerical= (apply #'aref a
(list i j
))
87 (apply #'aref b
(list i j
))
89 (every #'(lambda (x) x
) a-b-elt-eq
))))
91 (deftestsuite lisp-stat-ut-testsupport
(lisp-stat-ut)
94 (almost=1 (ensure (almost= 3 3.001 :tol
0.01)))
95 (almost=2 (ensure (almost= 3 3.01 :tol
0.01)))
96 (almost=3 (ensure (not (almost= 3 3.1 :tol
0.01))))
97 (almost=lists1
(ensure (almost=lists nil nil
:tol
0.01)))
98 (almost=lists2
(ensure (almost=lists
(list ) (list ) :tol
0.01)))
99 (almost=lists3
(ensure (almost=lists
(list 1.0) (list 1.0) :tol
0.01)))
100 (almost=lists4
(ensure (almost=lists
(list 1.0 1.0) (list 1.0 1.0) :tol
0.01)))
101 (almost=lists5
(ensure (not (almost=lists
(list 1.0 1.0)
102 (list 1.0 1.1) :tol
0.01))))))
104 (deftestsuite lisp-stat-ut-testsupport2
(lisp-stat-ut)
107 (numerical=1 (ensure (numerical= 3 3.001 :tol
0.01)))
108 (numerical=1.1 (ensure (numerical= 2 2)))
109 (numerical=1.2 (ensure (not (numerical= 2 3))))
110 (numerical=2 (ensure (numerical= 3 3.01 :tol
0.01)))
111 (numerical=3 (ensure (not (numerical= 3 3.1 :tol
0.01))))
112 (numerical=4 (ensure (numerical= nil nil
:tol
0.01)))
113 (numerical=5 (ensure (numerical= (list ) (list ) :tol
0.01)))
114 (numerical=6 (ensure (numerical= (list 1.0) (list 1.0) :tol
0.01)))
115 (numerical=7 (ensure (numerical= (list 1.0 1.0) (list 1.0 1.0) :tol
0.01)))
116 (numerical=7.5 (ensure-error (numerical= 1.0 (list 1.0 1.0) :tol
0.01)))
117 (numerical=8 (ensure (not (numerical= (list 2.0 2.0 2.2) (list 2.1 2.0 2.2)))))
118 (numerical=9 (ensure (numerical= (list 2.1 2.0 2.2) (list 2.1 2.0 2.2)) ))
119 (numerical=10 (ensure (numerical= (list 2.1 2.0 2.2 4.2) (list 2.1 2.0 2.2 4.2))))
120 (numerical=11 (ensure (not (numerical= (list 2.1 2.0 2.3 4.0) (list 2.1 2.0 2.2 4.0)))))
121 (numerical=12 (ensure (not (numerical= (list 1.0 1.0)
122 (list 1.0 1.1) :tol
0.01))))
123 (numerical=C1
(ensure (numerical= #C
(2 3) #C
(2 3))))
124 (numerical=C2
(ensure (not(numerical= #C
(2 3) #C
(2 4)))))
125 (numerical=C3
(ensure (numerical= #C
(2 3) #C
(3 4) :tol
2)))
126 (numerical=C4
(ensure (not(numerical= #C
(2 3) #C
(3 4) :tol
1))))
130 (numerical=A1
(ensure (numerical= #1A
(2 3 4)
133 (numerical=A2
(ensure (numerical= #2A
((2 3 4) (1 2 4) (2 4 5))
134 #2A
((2 3 4) (1 2 4) (2 4 5)))))
136 (numerical=A3
(ensure (not (numerical= #2A
((2 3 4) (1 2 4) (2 5 4))
137 #2A
((2 3 4) (1 2 4) (2 4 5))))))
139 (numerical=A4
(ensure (not (numerical= #1A
(2 2 4)
144 ;; (describe (run-tests :suite 'lisp-stat-ut-testsupport2))
148 ;;;; Log-gamma function
150 (addtest (lisp-stat-ut-spec-fns) log-gamma-fn
157 ;;; Probability distributions
159 ;; This macro should be generalized, but it's a good start now.
160 ;;(defmacro ProbDistnTests (prefixName
161 ;; quant-params quant-answer
162 ;; cdf-params cdf-answer
163 ;; pmf-params pmf-answer
164 ;; rand-params rand-answer)
165 ;; (deftestsuite lisp-stat-ut-probdist-,prefixName (lisp-stat-ut-probdistn)
167 ;; (:documentation "testing for ,testName distribution results")
168 ;; (:test (ensure-same
169 ;; (lisp-stat-ut-basics:,testName-quant ,quant-params) ,quant-answer))
170 ;; (:test (ensure-same
171 ;; (lisp-stat-ut-basics:,testName-cdf ,cdf-params) ,cdf-answer))
172 ;; (:test (ensure-same
173 ;; (lisp-stat-ut-basics:,testName-pmf ,pmf-params) ,pmf-answer))
177 ;; (lisp-stat-ut-basics:,testName-rand ,rand-params) ,rand-answer)))))
179 ;;; Normal distribution
181 (deftestsuite lisp-stat-ut-probdist-f
(lisp-stat-ut-probdistn)
183 (:documentation
"testing for Gaussian distn results")
192 0.17136859204780736))
195 (list -
0.40502015f0 -
0.8091404f0
)))
197 (bivnorm-cdf 0.2 0.4 0.6)
198 0.4736873734160288)))
200 ;;;; Cauchy distribution
202 (deftestsuite lisp-stat-ut-probdist-cauchy
(lisp-stat-ut-probdistn)
204 (:documentation
"testing for Cachy-distn results")
213 0.1183308127104695 ))
216 (list -
1.06224644160405 -
0.4524695943939537))))
218 ;;;; Gamma distribution
220 (deftestsuite lisp-stat-ut-probdist-gamma
(lisp-stat-ut-probdistn)
222 (:documentation
"testing for gamma distn results")
224 (gamma-quant 0.95 4.3)
228 0.028895150986674906))
234 (list 2.454918912880936 4.081365384357454))))
236 ;;;; Chi-square distribution
238 (deftestsuite lisp-stat-ut-probdist-chisq
(lisp-stat-ut-probdistn)
240 (:documentation
"testing for Chi-square distn results")
246 0.03743422675631789))
249 0.08065690818083521))
254 (list 1.968535826180572 2.9988646156942997)))))
256 ;;;; Beta distribution
258 (deftestsuite lisp-stat-ut-probdist-beta
(lisp-stat-ut-probdistn)
260 (:documentation
"testing for beta distn results")
262 (beta-quant 0.95 3 2)
266 0.4247997418541529 ))
268 (beta-dens 0.4 2 2.4)
269 1.5964741858913518 ))
272 (list 0.8014897077282279 0.6516371997922659))))
276 (deftestsuite lisp-stat-ut-probdist-t
(lisp-stat-ut-probdistn)
278 (:documentation
"testing for t-distn results")
290 (list -
0.34303672776089306 -
1.142505872436518))))
294 (deftestsuite lisp-stat-ut-probdist-f
(lisp-stat-ut-probdistn)
296 (:documentation
"testing for f-distn results")
298 (f-quant 0.95 3 5) 5.409451318117459))
304 0.37551128864591415))
309 (list 0.7939093442091963 0.07442694152491144)))))
311 ;;;; Poisson distribution
313 (deftestsuite lisp-stat-ut-probdist-poisson
(lisp-stat-ut-probdistn)
315 (:documentation
"testing for poisson distribution results")
317 (poisson-quant 0.95 3.2) 6))
320 0.17120125672252395))
323 0.13043905274097067))
330 ;; Binomial distribution
332 (deftestsuite lisp-stat-ut-probdist-binomial
(lisp-stat-ut-probdistn)
334 (:documentation
"testing for binomial distribution results")
337 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
340 (binomial-quant 0 3 0.4)
344 (binomial-cdf 1 3 0.4)
348 (binomial-pmf 1 3 0.4)
353 (binomial-rand 5 3 0.4)