clean up unittests, but does not compile.
[CommonLispStat.git] / unittests.lisp
blobf4ecc0550612d411fe0207444104e640c2c2beb7
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 :lift :lisp-stat)
15 (:export run-lisp-stat-tests run-lisp-stat-test scoreboard))
17 (in-package #:lisp-stat-unittests)
19 ;;; TESTS
22 (deftestsuite lisp-stat () ())
24 (deftestsuite lisp-stat-lin-alg (lisp-stat) ())
26 (addtest (lisp-stat-lin-alg)
27 cholesky-decomposition
28 (ensure-same
29 (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
30 (#2A((1.7888543819998317 0.0 0.0)
31 (1.6770509831248424 0.11180339887498929 0.0)
32 (2.23606797749979 2.23606797749979 3.332000937312528e-8))
33 5.000000000000003)))
35 (addtest (lisp-stat-lin-alg)
36 lu-decomposition
37 (ensure-same
38 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
39 (#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)))
41 (addtest (lisp-stat-lin-alg)
42 lu-solve
43 (ensure-same
44 (lu-solve
45 (lu-decomp
46 #2A((2 3 4) (1 2 4) (2 4 5)))
47 #(2 3 4))
48 #(-2.333333333333333 1.3333333333333335 0.6666666666666666)))
51 (addtest (lisp-stat-lin-alg)
52 inverse
53 (ensure-same
54 (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
55 #2A((2.0 -0.33333333333333326 -1.3333333333333335)
56 (-1.0 -0.6666666666666666 1.3333333333333333)
57 (0.0 0.6666666666666666 -0.3333333333333333))))
59 (addtest (lisp-stat-lin-alg)
60 sv-decomp
61 (ensure-same
62 (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
63 (#2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
64 (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
65 (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
66 #(9.699290438141343 0.8971681569301373 0.3447525123483081)
67 #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
68 (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
69 (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
70 T)))
72 (addtest (lisp-stat-lin-alg)
73 qr-decomp
74 (ensure-same
75 (qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
76 (#2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
77 (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
78 (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
79 #2A((-3.0 -5.333333333333334 -7.333333333333332)
80 (0.0 -0.7453559924999292 -1.1925695879998877)
81 (0.0 0.0 -1.3416407864998738))) ))
83 (addtest (lisp-stat-lin-alg)
84 rcondest
85 (ensure-same
86 (rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
87 6.8157451e7 ))
89 (addtest (lisp-stat-lin-alg)
90 eigen
91 (ensure-same
92 (eigen #2A((2 3 4) (1 2 4) (2 4 5)))
93 (#(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
94 (#(0.4999999999999998 0.4999999999999997 0.7071067811865475)
95 #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
96 #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
97 NIL) ))
99 (addtest (lisp-stat-lin-alg)
100 spline
101 (ensure-same
102 (spline
103 #(1.0 1.2 1.3 1.8 2.1 2.5)
104 #(1.2 2.0 2.1 2.0 1.1 2.8)
105 :xvals 6)
106 ((1.0 1.3 1.6 1.9 2.2 2.5)
107 (1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))))
110 (addtest (lisp-stat-lin-alg)
111 kernel-smooth
112 (ensure-same
113 ;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
114 (kernel-smooth
115 #(1.0 1.2 1.3 1.8 2.1 2.5)
116 #(1.2 2.0 2.1 2.0 1.1 2.8)
117 :xvals 5)
118 ((1.0 1.375 1.75 2.125 2.5)
119 (1.6603277642110226 1.9471748095239771 1.7938127405752287
120 1.5871511322219498 2.518194783156392))))
122 (addtest (lisp-stat-lin-alg)
123 kernel-dens
124 (ensure-same
125 (kernel-dens
126 #(1.0 1.2 2.5 2.1 1.8 1.2)
127 :xvals 5)
128 ((1.0 1.375 1.75 2.125 2.5)
129 (0.7224150453621405 0.5820045548233707 0.38216411702854214
130 0.4829822708587095 0.3485939156929503)) ))
132 (addtest (lisp-stat-lin-alg)
134 (ensure-same
135 (fft #(1.0 1.2 2.5 2.1 1.8))
136 #(#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)) ))
138 (addtest (lisp-stat-lin-alg)
139 lowess
140 (ensure-same
141 (lisp-stat-descriptive-statistics:lowess
142 #(1.0 1.2 2.5 2.1 1.8 1.2)
143 #(1.2 2.0 2.1 2.0 1.1 2.8))
144 (#(1.0 1.2 1.2 1.8 2.1 2.5))))
147 (deftestsuite lisp-stat-spec-fns () ())
149 ;;;; Log-gamma function
151 (addtest (lisp-stat-spec-fns)
152 (ensure-same
153 (lisp-stat-basics:log-gamma 3.4)
154 1.0923280596789584))
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)
166 (deftestsuite lisp-stat-probdist-,prefixName (lisp-stat-probdistn)
167 ;; (( ))
168 (:documentation "testing for ,testName distribution results")
169 (:test (ensure-same
170 (lisp-stat-basics:,testName-quant ,quant-params) ,quant-answer))
171 (:test (ensure-same
172 (lisp-stat-basics:,testName-cdf ,cdf-params) ,cdf-answer))
173 (:test (ensure-same
174 (lisp-stat-basics:,testName-pmf ,pmf-params) ,pmf-answer))
175 (:test (progn
176 (set-seed 234)
177 (ensure-same
178 (lisp-stat-basics:,testName-rand ,rand-params) ,rand-answer)))))
180 ;;; Normal distribution
182 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
183 (:documentation "testing for Gaussian distn results")
184 (:test (ensure-same
185 (lisp-stat-basics:normal-quant 0.95)
186 1.6448536279366268))
187 (:test (ensure-same
188 (lisp-stat-basics:normal-cdf 1.3)
189 0.9031995154143897))
190 (:test (ensure-same
191 (lisp-stat-basics:normal-dens 1.3)
192 0.17136859204780736))
193 (:test (ensure-same
194 (lisp-stat-basics:normal-rand 2)
195 (-0.40502015f0 -0.8091404f0)))
196 (:test (ensure-same
197 (lisp-stat-basics:bivnorm-cdf 0.2 0.4 0.6)
198 0.4736873734160288)))
200 ;;;; Cauchy distribution
202 (deftestsuite lisp-stat-probdist-cauchy (lisp-stat-probdistn)
203 (:documentation "testing for Cachy-distn results")
204 (:test (ensure-same
205 (lisp-stat-basics:cauchy-quant 0.95)
206 6.313751514675031))
207 (:test (ensure-same
208 (lisp-stat-basics:cauchy-cdf 1.3)
209 0.7912855998398473))
210 (:test (ensure-same
211 (lisp-stat-basics:cauchy-dens 1.3)
212 0.1183308127104695 ))
213 (:test (ensure-same
214 (lisp-stat-basics:cauchy-rand 2)
215 (-1.06224644160405 -0.4524695943939537))))
217 ;;;; Gamma distribution
219 (deftestsuite lisp-stat-probdist-gamma (lisp-stat-probdistn)
220 (:documentation "testing for gamma distn results")
221 (:test (ensure-same
222 (lisp-stat-basics:gamma-quant 0.95 4.3)
223 8.178692439291645))
224 (:test (ensure-same
225 (lisp-stat-basics:gamma-cdf 1.3 4.3)
226 0.028895150986674906))
227 (:test (ensure-same
228 (lisp-stat-basics:gamma-dens 1.3 4.3)
229 0.0731517686447374))
230 (:test (ensure-same
231 (lisp-stat-basics:gamma-rand 2 4.3)
232 (2.454918912880936 4.081365384357454))))
234 ;;;; Chi-square distribution
236 (deftestsuite lisp-stat-probdist-chisq (lisp-stat-probdistn)
238 (:documentation "testing for Chi-square distn results")
239 (:test (ensure-same
240 (lisp-stat-basics:chisq-quant 0.95 3)
241 7.814727903379012))
242 (:test (ensure-same
243 (lisp-stat-basics:chisq-cdf 1 5)
244 0.03743422675631789))
245 (:test (ensure-same
246 (lisp-stat-basics:chisq-dens 1 5)
247 0.08065690818083521))
248 (:test (progn
249 (set-seed 353)
250 (ensure-same
251 (lisp-stat-basics:chisq-rand 2 4)
252 (1.968535826180572 2.9988646156942997)))))
254 ;;;; Beta distribution
256 (deftestsuite lisp-stat-probdist-beta (lisp-stat-probdistn)
258 (:documentation "testing for beta distn results")
259 (:test (ensure-same
260 (lisp-stat-basics:beta-quant 0.95 3 2)
261 0.9023885371149876))
262 (:test (ensure-same
263 (lisp-stat-basics:beta-cdf 0.4 2 2.4)
264 0.4247997418541529 ))
265 (:test (ensure-same
266 (lisp-stat-basics:beta-dens 0.4 2 2.4)
267 1.5964741858913518 ))
268 (:test (ensure-same
269 (lisp-stat-basics:beta-rand 2 2 2.4)
270 (0.8014897077282279 0.6516371997922659))))
272 ;;;; t distribution
274 (deftestsuite lisp-stat-probdist-t (lisp-stat-probdistn)
275 (:documentation "testing for t-distn results")
276 (:test (ensure-same
277 (lisp-stat-basics:t-quant 0.95 3)
278 2.35336343484194))
279 (:test (ensure-same
280 (lisp-stat-basics:t-cdf 1 2.3)
281 0.794733624298342))
282 (:test (ensure-same
283 (lisp-stat-basics:t-dens 1 2.3)
284 0.1978163816318102))
285 (:test (ensure-same
286 (lisp-stat-basics:t-rand 2 2.3)
287 (-0.34303672776089306 -1.142505872436518))))
289 ;;;; F distribution
291 (deftestsuite lisp-stat-probdist-f (lisp-stat-probdistn)
292 (:documentation "testing for f-distn results")
293 (:test (ensure-same
294 (lisp-stat-basics:f-quant 0.95 3 5) 5.409451318117459))
295 (:test (ensure-same
296 (lisp-stat-basics:f-cdf 1 3.2 5.4)
297 0.5347130905510765))
298 (:test (ensure-same
299 (lisp-stat-basics:f-dens 1 3.2 5.4)
300 0.37551128864591415))
301 (:test (progn
302 (set-seed 234)
303 (ensure-same
304 (lisp-stat-basics:f-rand 2 3 2)
305 (0.7939093442091963 0.07442694152491144)))))
307 ;;;; Poisson distribution
309 (deftestsuite lisp-stat-probdist-poisson (lisp-stat-probdistn)
310 ;; (( ))
311 (:documentation "testing for poisson distribution results")
312 (:test (ensure-same
313 (lisp-stat-basics:poisson-quant 0.95 3.2) 6))
314 (:test (ensure-same
315 (lisp-stat-basics:poisson-cdf 1 3.2)
316 0.17120125672252395))
317 (:test (ensure-same
318 (lisp-stat-basics:poisson-pmf 1 3.2)
319 0.13043905274097067))
320 (:test (progn
321 (set-seed 234)
322 (ensure-same
323 (lisp-stat-basics:poisson-rand 5 3.2)
324 (list 2 1 2 0 3)))))
326 ;; Binomial distribution
328 (deftestsuite lisp-stat-probdist-binomial (lisp-stat-probdistn)
329 ;; (( ))
330 (:documentation "testing for binomial distribution results")
331 (:test (ensure-same
332 (lisp-stat-basics:binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
334 (:test (ensure-same
335 (lisp-stat-basics:binomial-quant 0 3 0.4)
336 ;; -2147483648
338 (:test (ensure-same
339 (lisp-stat-basics:binomial-cdf 1 3 0.4)
340 0.6479999999965776))
342 (:test (ensure-same
343 (lisp-stat-basics:binomial-pmf 1 3 0.4)
344 0.4320000000226171))
345 (:test (progn
346 (set-seed 526)
347 (ensure-same
348 (lisp-stat-basics:binomial-rand 5 3 0.4)
349 (list 2 2 0 1 2)))))
352 ;;; External support for running tests
354 (defun run-lisp-stat-tests ())