clean up, fixed exports and functions except for proto. compiles and first version...
[CommonLispStat.git] / unittests.lisp
bloba884fb7b9970db94e392272d40dae9191fb37a29
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 (in-package #:cl-user)
8 ;;; This is semi-external to lispstat core packages. The dependency
9 ;;; should be that lispstat packages are dependencies for the unit
10 ;;; tests. However, where they will end up is still to be
11 ;;; determined.
13 (defpackage #:lisp-stat-unittests
14 (:use :lift :lisp-stat)
15 (:export run-ls-tests run-ls-test scoreboard))
17 (in-package #:lisp-stat-unittests)
19 ;;; TESTS
21 ;; optimization-related functions not tested
22 ;;(lisp-stat-optimize:numgrad
23 ;;(lisp-stat-optimize:numhess
24 ;;(lisp-stat-optimize::minfo-maximize
26 ;; the rest return reasonable values, except BINOMIAL-QUANT:
27 ;; (lisp-stat-basics:binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
28 ;; (lisp-stat-basics:binomial-quant 0 3 0.4) ;;; -2147483648
31 (deftestsuite ls-lin-alg () ())
33 (addtest (ls-lin-alg)
34 cholesky-decomposition
35 (ensure-same
36 (lisp-stat-linalg:chol-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
37 (#2A((1.7888543819998317 0.0 0.0)
38 (1.6770509831248424 0.11180339887498929 0.0)
39 (2.23606797749979 2.23606797749979 3.332000937312528e-8))
40 5.000000000000003)))
42 (addtest (ls-lin-alg)
43 lu-decomposition
44 (ensure-same
45 (lisp-stat-linalg:lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
46 (#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)))
48 (addtest (ls-lin-alg)
49 lu-solve
50 (ensure-same
51 (lisp-stat-linalg:lu-solve
52 (lisp-stat-linalg:lu-decomp
53 #2A((2 3 4) (1 2 4) (2 4 5)))
54 #(2 3 4))
55 #(-2.333333333333333 1.3333333333333335 0.6666666666666666)))
58 (addtest (ls-lin-alg)
59 inverse
60 (ensure-same
61 (lisp-stat-linalg:inverse #2A((2 3 4) (1 2 4) (2 4 5)))
62 #2A((2.0 -0.33333333333333326 -1.3333333333333335)
63 (-1.0 -0.6666666666666666 1.3333333333333333)
64 (0.0 0.6666666666666666 -0.3333333333333333))))
66 (addtest (ls-lin-alg)
67 sv-decomp
68 (ensure-same
69 (lisp-stat-linalg:sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
70 (#2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
71 (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
72 (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
73 #(9.699290438141343 0.8971681569301373 0.3447525123483081)
74 #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
75 (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
76 (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
77 T)))
79 (addtest (ls-lin-alg)
80 qr-decomp
81 (ensure-same
82 (lisp-stat-linalg:qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
83 (#2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
84 (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
85 (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
86 #2A((-3.0 -5.333333333333334 -7.333333333333332)
87 (0.0 -0.7453559924999292 -1.1925695879998877)
88 (0.0 0.0 -1.3416407864998738))) ))
90 (addtest (ls-lin-alg)
91 rcondest
92 (ensure-same
93 (lisp-stat-linalg:rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
94 6.8157451e7 ))
96 (addtest (ls-lin-alg)
97 eigen
98 (ensure-same
99 (lisp-stat-linalg:eigen #2A((2 3 4) (1 2 4) (2 4 5)))
100 (#(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
101 (#(0.4999999999999998 0.4999999999999997 0.7071067811865475)
102 #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
103 #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
104 NIL) ))
106 (addtest (ls-lin-alg)
107 spline
108 (ensure-same
109 (lisp-stat-linalg:spline
110 #(1.0 1.2 1.3 1.8 2.1 2.5)
111 #(1.2 2.0 2.1 2.0 1.1 2.8)
112 :xvals 6)
113 ((1.0 1.3 1.6 1.9 2.2 2.5)
114 (1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))))
117 (addtest (ls-lin-alg)
118 kernel-smooth
119 (ensure-same
120 ;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
121 (lisp-stat-linalg:kernel-smooth
122 #(1.0 1.2 1.3 1.8 2.1 2.5)
123 #(1.2 2.0 2.1 2.0 1.1 2.8)
124 :xvals 5)
125 ((1.0 1.375 1.75 2.125 2.5)
126 (1.6603277642110226 1.9471748095239771 1.7938127405752287
127 1.5871511322219498 2.518194783156392))))
129 (addtest (ls-lin-alg)
130 kernel-dens
131 (ensure-same
132 (lisp-stat-linalg:kernel-dens
133 #(1.0 1.2 2.5 2.1 1.8 1.2)
134 :xvals 5)
135 ((1.0 1.375 1.75 2.125 2.5)
136 (0.7224150453621405 0.5820045548233707 0.38216411702854214
137 0.4829822708587095 0.3485939156929503)) ))
139 (addtest (ls-lin-alg)
141 (ensure-same
142 (lisp-stat-linalg:fft #(1.0 1.2 2.5 2.1 1.8))
143 #(#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)) ))
145 (addtest (ls-lin-alg)
146 lowess
147 (ensure-same
148 (lisp-stat-descriptive-statistics:lowess
149 #(1.0 1.2 2.5 2.1 1.8 1.2)
150 #(1.2 2.0 2.1 2.0 1.1 2.8))
151 (#(1.0 1.2 1.2 1.8 2.1 2.5))))
154 (deftestsuite ls-spec-fns () ())
156 ;;;; Log-gamma function
158 (addtest (ls-spec-fns)
159 (ensure-same
160 (lisp-stat-basics:log-gamma 3.4)
161 1.0923280596789584))
164 ;;; Probability distributions
166 ;; This macro should be generalized, but it's a good start now.
167 (defmacro ProbDistnTests (prefixName
168 quant-params quant-answer
169 cdf-params cdf-answer
170 pmf-params pmf-answer
171 rand-params rand-answer)
173 (deftestsuite ls-probdist-,prefixName (ls-probdistn)
174 ;; (( ))
175 (:documentation "testing for ,testName distribution results")
176 (:test (ensure-same
177 (lisp-stat-basics:,testName-quant ,quant-params) ,quant-answer))
178 (:test (ensure-same
179 (lisp-stat-basics:,testName-cdf ,cdf-params) ,cdf-answer))
180 (:test (ensure-same
181 (lisp-stat-basics:,testName-pmf ,pmf-params) ,pmf-answer))
182 (:test (progn
183 (set-seed 234)
184 (ensure-same
185 (lisp-stat-basics:,testName-rand ,rand-params) ,rand-answer)))))
187 ;;; Normal distribution
189 (deftestsuite ls-probdist-f (ls-probdistn)
190 (:documentation "testing for Gaussian distn results")
191 (:test (ensure-same
192 (lisp-stat-basics:normal-quant 0.95)
193 1.6448536279366268))
194 (:test (ensure-same
195 (lisp-stat-basics:normal-cdf 1.3)
196 0.9031995154143897))
197 (:test (ensure-same
198 (lisp-stat-basics:normal-dens 1.3)
199 0.17136859204780736))
200 (:test (ensure-same
201 (lisp-stat-basics:normal-rand 2)
202 (-0.40502015f0 -0.8091404f0)))
203 (:test (ensure-same
204 (lisp-stat-basics:bivnorm-cdf 0.2 0.4 0.6)
205 0.4736873734160288)))
207 ;;;; Cauchy distribution
209 (deftestsuite ls-probdist-cauchy (ls-probdistn)
210 (:documentation "testing for Cachy-distn results")
211 (:test (ensure-same
212 (lisp-stat-basics:cauchy-quant 0.95)
213 6.313751514675031))
214 (:test (ensure-same
215 (lisp-stat-basics:cauchy-cdf 1.3)
216 0.7912855998398473))
217 (:test (ensure-same
218 (lisp-stat-basics:cauchy-dens 1.3)
219 0.1183308127104695 ))
220 (:test (ensure-same
221 (lisp-stat-basics:cauchy-rand 2)
222 (-1.06224644160405 -0.4524695943939537))))
224 ;;;; Gamma distribution
226 (deftestsuite ls-probdist-gamma (ls-probdistn)
227 (:documentation "testing for gamma distn results")
228 (:test (ensure-same
229 (lisp-stat-basics:gamma-quant 0.95 4.3)
230 8.178692439291645))
231 (:test (ensure-same
232 (lisp-stat-basics:gamma-cdf 1.3 4.3)
233 0.028895150986674906))
234 (:test (ensure-same
235 (lisp-stat-basics:gamma-dens 1.3 4.3)
236 0.0731517686447374))
237 (:test (ensure-same
238 (lisp-stat-basics:gamma-rand 2 4.3)
239 (2.454918912880936 4.081365384357454))))
241 ;;;; Chi-square distribution
243 (deftestsuite ls-probdist-chisq (ls-probdistn)
245 (:documentation "testing for Chi-square distn results")
246 (:test (ensure-same
247 (lisp-stat-basics:chisq-quant 0.95 3)
248 7.814727903379012))
249 (:test (ensure-same
250 (lisp-stat-basics:chisq-cdf 1 5)
251 0.03743422675631789))
252 (:test (ensure-same
253 (lisp-stat-basics:chisq-dens 1 5)
254 0.08065690818083521))
255 (:test (progn
256 (set-seed 353)
257 (ensure-same
258 (lisp-stat-basics:chisq-rand 2 4)
259 (1.968535826180572 2.9988646156942997)))))
261 ;;;; Beta distribution
263 (deftestsuite ls-probdist-beta (ls-probdistn)
265 (:documentation "testing for beta distn results")
266 (:test (ensure-same
267 (lisp-stat-basics:beta-quant 0.95 3 2)
268 0.9023885371149876))
269 (:test (ensure-same
270 (lisp-stat-basics:beta-cdf 0.4 2 2.4)
271 0.4247997418541529 ))
272 (:test (ensure-same
273 (lisp-stat-basics:beta-dens 0.4 2 2.4)
274 1.5964741858913518 ))
275 (:test (ensure-same
276 (lisp-stat-basics:beta-rand 2 2 2.4)
277 (0.8014897077282279 0.6516371997922659))))
279 ;;;; t distribution
281 (deftestsuite ls-probdist-t (ls-probdistn)
282 (:documentation "testing for t-distn results")
283 (:test (ensure-same
284 (lisp-stat-basics:t-quant 0.95 3)
285 2.35336343484194))
286 (:test (ensure-same
287 (lisp-stat-basics:t-cdf 1 2.3)
288 0.794733624298342))
289 (:test (ensure-same
290 (lisp-stat-basics:t-dens 1 2.3)
291 0.1978163816318102))
292 (:test (ensure-same
293 (lisp-stat-basics:t-rand 2 2.3)
294 (-0.34303672776089306 -1.142505872436518))))
296 ;;;; F distribution
298 (deftestsuite ls-probdist-f (ls-probdistn)
299 (:documentation "testing for f-distn results")
300 (:test (ensure-same
301 (lisp-stat-basics:f-quant 0.95 3 5) 5.409451318117459))
302 (:test (ensure-same
303 (lisp-stat-basics:f-cdf 1 3.2 5.4)
304 0.5347130905510765))
305 (:test (ensure-same
306 (lisp-stat-basics:f-dens 1 3.2 5.4)
307 0.37551128864591415))
308 (:test (progn
309 (set-seed 234)
310 (ensure-same
311 (lisp-stat-basics:f-rand 2 3 2)
312 (0.7939093442091963 0.07442694152491144)))))
314 ;;;; Poisson distribution
316 (deftestsuite ls-probdist-poisson (ls-probdistn)
317 ;; (( ))
318 (:documentation "testing for poisson distribution results")
319 (:test (ensure-same
320 (lisp-stat-basics:poisson-quant 0.95 3.2) 6))
321 (:test (ensure-same
322 (lisp-stat-basics:poisson-cdf 1 3.2)
323 0.17120125672252395))
324 (:test (ensure-same
325 (lisp-stat-basics:poisson-pmf 1 3.2)
326 0.13043905274097067))
327 (:test (progn
328 (set-seed 234)
329 (ensure-same
330 (lisp-stat-basics:poisson-rand 5 3.2)
331 (list 2 1 2 0 3)))))
333 ;; Binomial distribution
335 (deftestsuite ls-probdist-binomial (ls-probdistn)
336 ;; (( ))
337 (:documentation "testing for binomial distribution results")
338 (:test (ensure-same
339 (lisp-stat-basics:binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
341 (:test (ensure-same
342 (lisp-stat-basics:binomial-quant 0 3 0.4)
343 ;; -2147483648
345 (:test (ensure-same
346 (lisp-stat-basics:binomial-cdf 1 3 0.4)
347 0.6479999999965776))
349 (:test (ensure-same
350 (lisp-stat-basics:binomial-pmf 1 3 0.4)
351 0.4320000000226171))
352 (:test (progn
353 (set-seed 526)
354 (ensure-same
355 (lisp-stat-basics:binomial-rand 5 3 0.4)
356 (list 2 2 0 1 2)))))
359 ;;; External support for running tests
361 (defun run-ls-tests)