document some tasks in dataframe.lisp that need resolution.
[CommonLispStat.git] / src / unittests / unittests-prob.lisp
blob20964741ebb3f43452f7160c09ab5a8f791e79f2
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 :lisp-stat-unittests)
13 ;;; TESTS
15 (deftestsuite lisp-stat-ut-probdistn (lisp-stat-ut) ())
17 ;;; Probability distributions
19 ;; This macro should be generalized, but it's a good start now.
20 ;;(defmacro ProbDistnTests (prefixName
21 ;; quant-params quant-answer
22 ;; cdf-params cdf-answer
23 ;; pmf-params pmf-answer
24 ;; rand-params rand-answer)
25 ;; (deftestsuite lisp-stat-ut-probdist-,prefixName (lisp-stat-ut-probdistn)
26 ;; ;; (( ))
27 ;; (:documentation "testing for ,testName distribution results")
28 ;; (:test (ensure-same
29 ;; (lisp-stat-ut-basics:,testName-quant ,quant-params) ,quant-answer))
30 ;; (:test (ensure-same
31 ;; (lisp-stat-ut-basics:,testName-cdf ,cdf-params) ,cdf-answer))
32 ;; (:test (ensure-same
33 ;; (lisp-stat-ut-basics:,testName-pmf ,pmf-params) ,pmf-answer))
34 ;; (:test (progn
35 ;; (set-seed 234)
36 ;; (ensure-same
37 ;; (lisp-stat-ut-basics:,testName-rand ,rand-params) ,rand-answer)))))
39 ;;; Normal distribution
41 (deftestsuite lisp-stat-ut-probdist-f (lisp-stat-ut-probdistn)
43 (:documentation "testing for Gaussian distn results")
44 (:test (ensure-same
45 (normal-quant 0.95)
46 1.6448536279366268))
47 (:test (ensure-same
48 (normal-cdf 1.3)
49 0.9031995154143897))
50 (:test (ensure-same
51 (normal-dens 1.3)
52 0.17136859204780736))
53 (:test (ensure-same
54 (normal-rand 2)
55 (list -0.40502015f0 -0.8091404f0)))
56 (:test (ensure-same
57 (bivnorm-cdf 0.2 0.4 0.6)
58 0.4736873734160288)))
60 ;;;; Cauchy distribution
62 (deftestsuite lisp-stat-ut-probdist-cauchy (lisp-stat-ut-probdistn)
64 (:documentation "testing for Cachy-distn results")
65 (:test (ensure-same
66 (cauchy-quant 0.95)
67 6.313751514675031))
68 (:test (ensure-same
69 (cauchy-cdf 1.3)
70 0.7912855998398473))
71 (:test (ensure-same
72 (cauchy-dens 1.3)
73 0.1183308127104695 ))
74 (:test (ensure-same
75 (cauchy-rand 2)
76 (list -1.06224644160405 -0.4524695943939537))))
78 ;;;; Gamma distribution
80 (deftestsuite lisp-stat-ut-probdist-gamma (lisp-stat-ut-probdistn)
82 (:documentation "testing for gamma distn results")
83 (:test (ensure-same
84 (gamma-quant 0.95 4.3)
85 8.178692439291645))
86 (:test (ensure-same
87 (gamma-cdf 1.3 4.3)
88 0.028895150986674906))
89 (:test (ensure-same
90 (gamma-dens 1.3 4.3)
91 0.0731517686447374))
92 (:test (ensure-same
93 (gamma-rand 2 4.3)
94 (list 2.454918912880936 4.081365384357454))))
96 ;;;; Chi-square distribution
98 (deftestsuite lisp-stat-ut-probdist-chisq (lisp-stat-ut-probdistn)
100 (:documentation "testing for Chi-square distn results")
101 (:test (ensure-same
102 (chisq-quant 0.95 3)
103 7.814727903379012))
104 (:test (ensure-same
105 (chisq-cdf 1 5)
106 0.03743422675631789))
107 (:test (ensure-same
108 (chisq-dens 1 5)
109 0.08065690818083521))
110 (:test (progn
111 (set-seed 353)
112 (ensure-same
113 (chisq-rand 2 4)
114 (list 1.968535826180572 2.9988646156942997)))))
116 ;;;; Beta distribution
118 (deftestsuite lisp-stat-ut-probdist-beta (lisp-stat-ut-probdistn)
120 (:documentation "testing for beta distn results")
121 (:test (ensure-same
122 (beta-quant 0.95 3 2)
123 0.9023885371149876))
124 (:test (ensure-same
125 (beta-cdf 0.4 2 2.4)
126 0.4247997418541529 ))
127 (:test (ensure-same
128 (beta-dens 0.4 2 2.4)
129 1.5964741858913518 ))
130 (:test (ensure-same
131 (beta-rand 2 2 2.4)
132 (list 0.8014897077282279 0.6516371997922659))))
134 ;;;; t distribution
136 (deftestsuite lisp-stat-ut-probdist-t (lisp-stat-ut-probdistn)
138 (:documentation "testing for t-distn results")
139 (:test (ensure-same
140 (t-quant 0.95 3)
141 2.35336343484194))
142 (:test (ensure-same
143 (t-cdf 1 2.3)
144 0.794733624298342))
145 (:test (ensure-same
146 (t-dens 1 2.3)
147 0.1978163816318102))
148 (:test (ensure-same
149 (t-rand 2 2.3)
150 (list -0.34303672776089306 -1.142505872436518))))
152 ;;;; F distribution
154 (deftestsuite lisp-stat-ut-probdist-f (lisp-stat-ut-probdistn)
156 (:documentation "testing for f-distn results")
157 (:test (ensure-same
158 (f-quant 0.95 3 5) 5.409451318117459))
159 (:test (ensure-same
160 (f-cdf 1 3.2 5.4)
161 0.5347130905510765))
162 (:test (ensure-same
163 (f-dens 1 3.2 5.4)
164 0.37551128864591415))
165 (:test (progn
166 (set-seed 234)
167 (ensure-same
168 (f-rand 2 3 2)
169 (list 0.7939093442091963 0.07442694152491144)))))
171 ;;;; Poisson distribution
173 (deftestsuite lisp-stat-ut-probdist-poisson (lisp-stat-ut-probdistn)
175 (:documentation "testing for poisson distribution results")
176 (:test (ensure-same
177 (poisson-quant 0.95 3.2) 6))
178 (:test (ensure-same
179 (poisson-cdf 1 3.2)
180 0.17120125672252395))
181 (:test (ensure-same
182 (poisson-pmf 1 3.2)
183 0.13043905274097067))
184 (:test (progn
185 (set-seed 234)
186 (ensure-same
187 (poisson-rand 5 3.2)
188 (list 2 1 2 0 3)))))
190 ;; Binomial distribution
192 (deftestsuite lisp-stat-ut-probdist-binomial (lisp-stat-ut-probdistn)
194 (:documentation "testing for binomial distribution results")
196 (:test (ensure-same
197 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
199 (:test (ensure-same
200 (binomial-quant 0 3 0.4)
201 ;; -2147483648
203 (:test (ensure-same
204 (binomial-cdf 1 3 0.4)
205 0.6479999999965776))
207 (:test (ensure-same
208 (binomial-pmf 1 3 0.4)
209 0.4320000000226171))
210 (:test (progn
211 (set-seed 526)
212 (ensure-same
213 (binomial-rand 5 3 0.4)
214 (list 2 2 0 1 2)))))