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 conjugate realpart imagpart phase
21 min max logand logior logxor lognot ffloor fceiling
22 ftruncate fround signum cis
)
23 (:export run-lisp-stat-tests run-lisp-stat-test scoreboard
))
25 (in-package :lisp-stat-unittests
)
29 ;;; Need a waz to minimize the possibilities
31 (defun run-lisp-stat-tests ()
32 (run-tests :suite
'lisp-stat
))
34 (defun run-lisp-stat-test (&rest x
)
39 (deftestsuite lisp-stat
() ())
40 (deftestsuite lisp-stat-lin-alg
(lisp-stat) ())
43 ;;; and add a test to it
45 (ensure-same (+ 1 1) 2))
48 ;;; add another test using ensure-error
49 (addtest (lisp-stat-lin-alg)
50 (ensure-error (let ((x 0)) (/ x
))))
54 ;;; add another, slightly more specific test
56 (ensure-condition division-by-zero
(let ((x 0)) (/ x
))))
60 (addtest (lisp-stat-lin-alg) cholesky-decomposition
62 (chol-decomp #2A
((2 3 4) (1 2 4) (2 4 5)))
63 (values #2A
((1.7888543819998317
0.0 0.0)
64 (1.6770509831248424
0.11180339887498929 0.0)
65 (2.23606797749979
2.23606797749979 3.332000937312528e-8))
72 (addtest (lisp-stat-lin-alg)
75 (lu-decomp #2A
((2 3 4) (1 2 4) (2 4 5)))
76 (#2A
((2.0
3.0 4.0) (1.0
1.0 1.0) (0.5
0.5 1.5)) #(0 2 2) -
1.0 NIL
)))
78 (addtest (lisp-stat-lin-alg)
83 #2A
((2 3 4) (1 2 4) (2 4 5)))
85 #(-2.333333333333333
1.3333333333333335 0.6666666666666666)))
88 (addtest (lisp-stat-lin-alg)
91 (inverse #2A
((2 3 4) (1 2 4) (2 4 5)))
92 #2A
((2.0 -
0.33333333333333326 -
1.3333333333333335)
93 (-1.0 -
0.6666666666666666 1.3333333333333333)
94 (0.0
0.6666666666666666 -
0.3333333333333333))))
96 (addtest (lisp-stat-lin-alg)
99 (sv-decomp #2A
((2 3 4) (1 2 4) (2 4 5)))
100 (#2A
((-0.5536537653489974
0.34181191712789266 -
0.7593629708013371)
101 (-0.4653437312661058 -
0.8832095891230851 -
0.05827549615722014)
102 (-0.6905959164998124
0.3211003503429828 0.6480523475178517))
103 #(9.699290438141343
0.8971681569301373 0.3447525123483081)
104 #2A
((-0.30454218417339873
0.49334669582252344 -
0.8147779426198863)
105 (-0.5520024849987308
0.6057035911404464 0.5730762743603965)
106 (-0.7762392122368734 -
0.6242853493399995 -
0.08786630745236332))
109 (addtest (lisp-stat-lin-alg)
112 (qr-decomp #2A
((2 3 4) (1 2 4) (2 4 5)))
113 (#2A
((-0.6666666666666665
0.7453559924999298 5.551115123125783e-17)
114 (-0.3333333333333333 -
0.2981423969999719 -
0.894427190999916)
115 (-0.6666666666666666 -
0.5962847939999439 0.44721359549995787))
116 #2A
((-3.0 -
5.333333333333334 -
7.333333333333332)
117 (0.0 -
0.7453559924999292 -
1.1925695879998877)
118 (0.0
0.0 -
1.3416407864998738))) ))
120 (addtest (lisp-stat-lin-alg)
123 (rcondest #2A
((2 3 4) (1 2 4) (2 4 5)))
126 (addtest (lisp-stat-lin-alg)
129 (eigen #2A
((2 3 4) (1 2 4) (2 4 5)))
130 (#(10.656854249492381 -
0.6568542494923802 -
0.9999999999999996)
131 (#(0.4999999999999998
0.4999999999999997 0.7071067811865475)
132 #(-0.49999999999999856 -
0.5000000000000011 0.7071067811865474)
133 #(0.7071067811865483 -
0.7071067811865466 -
1.2560739669470215e-15))
136 (addtest (lisp-stat-lin-alg)
140 #(1.0
1.2 1.3 1.8 2.1 2.5)
141 #(1.2
2.0 2.1 2.0 1.1 2.8)
143 ((1.0
1.3 1.6 1.9 2.2 2.5)
144 (1.2
2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))))
147 (addtest (lisp-stat-lin-alg)
150 ;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
152 #(1.0
1.2 1.3 1.8 2.1 2.5)
153 #(1.2
2.0 2.1 2.0 1.1 2.8)
155 ((1.0
1.375 1.75 2.125 2.5)
156 (1.6603277642110226
1.9471748095239771 1.7938127405752287
157 1.5871511322219498 2.518194783156392))))
159 (addtest (lisp-stat-lin-alg)
163 #(1.0
1.2 2.5 2.1 1.8 1.2)
165 ((1.0
1.375 1.75 2.125 2.5)
166 (0.7224150453621405
0.5820045548233707 0.38216411702854214
167 0.4829822708587095 0.3485939156929503)) ))
169 (addtest (lisp-stat-lin-alg)
172 (fft #(1.0
1.2 2.5 2.1 1.8))
173 #(#C
(1.0
0.0) #C
(1.2
0.0) #C
(2.5
0.0) #C
(2.1
0.0) #C
(1.8
0.0)) ))
175 (addtest (lisp-stat-lin-alg)
178 (lisp-stat-descriptive-statistics:lowess
179 #(1.0
1.2 2.5 2.1 1.8 1.2)
180 #(1.2
2.0 2.1 2.0 1.1 2.8))
181 (#(1.0
1.2 1.2 1.8 2.1 2.5))))
184 (deftestsuite lisp-stat-spec-fns
() ())
186 ;;;; Log-gamma function
188 (addtest (lisp-stat-spec-fns)
190 (lisp-stat-basics:log-gamma
3.4)
194 ;;; Probability distributions
196 ;; This macro should be generalized, but it's a good start now.
197 (defmacro ProbDistnTests
(prefixName
198 quant-params quant-answer
199 cdf-params cdf-answer
200 pmf-params pmf-answer
201 rand-params rand-answer
)
203 (deftestsuite lisp-stat-probdist-
,prefixName
(lisp-stat-probdistn)
205 (:documentation
"testing for ,testName distribution results")
207 (lisp-stat-basics:,testName-quant
,quant-params
) ,quant-answer
))
209 (lisp-stat-basics:,testName-cdf
,cdf-params
) ,cdf-answer
))
211 (lisp-stat-basics:,testName-pmf
,pmf-params
) ,pmf-answer
))
215 (lisp-stat-basics:,testName-rand
,rand-params
) ,rand-answer
)))))
217 ;;; Normal distribution
219 (deftestsuite lisp-stat-probdist-f
(lisp-stat-probdistn)
220 (:documentation
"testing for Gaussian distn results")
222 (lisp-stat-basics:normal-quant
0.95)
225 (lisp-stat-basics:normal-cdf
1.3)
228 (lisp-stat-basics:normal-dens
1.3)
229 0.17136859204780736))
231 (lisp-stat-basics:normal-rand
2)
232 (-0.40502015f0 -
0.8091404f0
)))
234 (lisp-stat-basics:bivnorm-cdf
0.2 0.4 0.6)
235 0.4736873734160288)))
237 ;;;; Cauchy distribution
239 (deftestsuite lisp-stat-probdist-cauchy
(lisp-stat-probdistn)
240 (:documentation
"testing for Cachy-distn results")
242 (lisp-stat-basics:cauchy-quant
0.95)
245 (lisp-stat-basics:cauchy-cdf
1.3)
248 (lisp-stat-basics:cauchy-dens
1.3)
249 0.1183308127104695 ))
251 (lisp-stat-basics:cauchy-rand
2)
252 (-1.06224644160405 -
0.4524695943939537))))
254 ;;;; Gamma distribution
256 (deftestsuite lisp-stat-probdist-gamma
(lisp-stat-probdistn)
257 (:documentation
"testing for gamma distn results")
259 (lisp-stat-basics:gamma-quant
0.95 4.3)
262 (lisp-stat-basics:gamma-cdf
1.3 4.3)
263 0.028895150986674906))
265 (lisp-stat-basics:gamma-dens
1.3 4.3)
268 (lisp-stat-basics:gamma-rand
2 4.3)
269 (2.454918912880936
4.081365384357454))))
271 ;;;; Chi-square distribution
273 (deftestsuite lisp-stat-probdist-chisq
(lisp-stat-probdistn)
275 (:documentation
"testing for Chi-square distn results")
277 (lisp-stat-basics:chisq-quant
0.95 3)
280 (lisp-stat-basics:chisq-cdf
1 5)
281 0.03743422675631789))
283 (lisp-stat-basics:chisq-dens
1 5)
284 0.08065690818083521))
288 (lisp-stat-basics:chisq-rand
2 4)
289 (1.968535826180572
2.9988646156942997)))))
291 ;;;; Beta distribution
293 (deftestsuite lisp-stat-probdist-beta
(lisp-stat-probdistn)
295 (:documentation
"testing for beta distn results")
297 (lisp-stat-basics:beta-quant
0.95 3 2)
300 (lisp-stat-basics:beta-cdf
0.4 2 2.4)
301 0.4247997418541529 ))
303 (lisp-stat-basics:beta-dens
0.4 2 2.4)
304 1.5964741858913518 ))
306 (lisp-stat-basics:beta-rand
2 2 2.4)
307 (0.8014897077282279
0.6516371997922659))))
311 (deftestsuite lisp-stat-probdist-t
(lisp-stat-probdistn)
312 (:documentation
"testing for t-distn results")
314 (lisp-stat-basics:t-quant
0.95 3)
317 (lisp-stat-basics:t-cdf
1 2.3)
320 (lisp-stat-basics:t-dens
1 2.3)
323 (lisp-stat-basics:t-rand
2 2.3)
324 (-0.34303672776089306 -
1.142505872436518))))
328 (deftestsuite lisp-stat-probdist-f
(lisp-stat-probdistn)
329 (:documentation
"testing for f-distn results")
331 (lisp-stat-basics:f-quant
0.95 3 5) 5.409451318117459))
333 (lisp-stat-basics:f-cdf
1 3.2 5.4)
336 (lisp-stat-basics:f-dens
1 3.2 5.4)
337 0.37551128864591415))
341 (lisp-stat-basics:f-rand
2 3 2)
342 (0.7939093442091963
0.07442694152491144)))))
344 ;;;; Poisson distribution
346 (deftestsuite lisp-stat-probdist-poisson
(lisp-stat-probdistn)
348 (:documentation
"testing for poisson distribution results")
350 (lisp-stat-basics:poisson-quant
0.95 3.2) 6))
352 (lisp-stat-basics:poisson-cdf
1 3.2)
353 0.17120125672252395))
355 (lisp-stat-basics:poisson-pmf
1 3.2)
356 0.13043905274097067))
360 (lisp-stat-basics:poisson-rand
5 3.2)
363 ;; Binomial distribution
365 (deftestsuite lisp-stat-probdist-binomial
(lisp-stat-probdistn)
367 (:documentation
"testing for binomial distribution results")
369 (lisp-stat-basics:binomial-quant
0.95 3 0.4) ;;; DOESN'T RETURN
372 (lisp-stat-basics:binomial-quant
0 3 0.4)
376 (lisp-stat-basics:binomial-cdf
1 3 0.4)
380 (lisp-stat-basics:binomial-pmf
1 3 0.4)
385 (lisp-stat-basics:binomial-rand
5 3 0.4)
389 ;;; External support for running tests