testsupport unit tests work and verify numerical equality approach.
[CommonLispStat.git] / ls-demo.lisp
blob7611f2fd7f3e7ea43b9c4cc75dfdc2af8f4790ca
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 ;;; File: ls-demo.lisp
7 ;;; Author: AJ Rossini <blindglobe@gmail.com>
8 ;;; Copyright: (c) 2007, AJ Rossini. BSD.
9 ;;; Purpose: demonstrations of how one might use CLS.
10 ;;; Time-stamp: <>
11 ;;; Creation:
13 ;;; What is this talk of 'release'? Klingons do not make software
14 ;;; 'releases'. Our software 'escapes', leaving a bloody trail of
15 ;;; designers and quality assurance people in its wake.
17 (load "init.lisp") ;; init needs to be more like the asdf-loader for lisp-stat, though it is pretty close.
19 ;;; checking exports.
20 ;;; This is generally not how I expect it to be used.
22 (in-package :cl-user)
23 (lisp-stat:binomial-quant 0.95 3 0.4) ;;; 3
24 (lisp-stat:binomial-quant 0 3 0.4) ;;; 0
25 (lisp-stat:normal-rand 20) ;;; DOESN'T RETURN
28 ;;; This is how I expect it to be used, either with work in ls-user,
29 ;;; or a cloned package similar to ls-user.
31 (in-package :ls-user)
33 ;; Probability
35 (binomial-quant 0.95 3 0.4) ; 3
36 (binomial-quant 0 3 0.4) ; 0
37 (normal-rand 20)
39 ;; Matrix algebra.
41 (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
42 ;; (#2A((1.7888543819998317 0.0 0.0)
43 ;; (1.6770509831248424 0.11180339887498929 0.0)
44 ;; (2.23606797749979 2.23606797749979 3.332000937312528e-8))
45 ;; 5.000000000000003)
47 (defvar my-chol-decomp-test (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5))))
48 my-chol-decomp-test
49 (nth 0 my-chol-decomp-test)
50 (nth 1 my-chol-decomp-test)
55 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
56 ;; (#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)
58 (lu-solve
59 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
60 #(2 3 4))
61 ;; #(-2.333333333333333 1.3333333333333335 0.6666666666666666)
63 (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
64 ;; #2A((2.0 -0.33333333333333326 -1.3333333333333335)
65 ;; (-1.0 -0.6666666666666666 1.3333333333333333)
66 ;; (0.0 0.6666666666666666 -0.3333333333333333))
68 (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
69 ;; (#2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
70 ;; (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
71 ;; (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
72 ;; #(9.699290438141343 0.8971681569301373 0.3447525123483081)
73 ;; #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
74 ;; (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
75 ;; (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
76 ;; T)
78 (qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
79 ;; (#2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
80 ;; (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
81 ;; (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
82 ;; #2A((-3.0 -5.333333333333334 -7.333333333333332)
83 ;; (0.0 -0.7453559924999292 -1.1925695879998877)
84 ;; (0.0 0.0 -1.3416407864998738)))
86 (rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
87 ;; 6.8157451e7
89 (eigen #2A((2 3 4) (1 2 4) (2 4 5)))
90 ;; (#(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
91 ;; (#(0.4999999999999998 0.4999999999999997 0.7071067811865475)
92 ;; #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
93 ;; #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
94 ;; NIL)
96 (spline #(1.0 1.2 1.3 1.8 2.1 2.5)
97 #(1.2 2.0 2.1 2.0 1.1 2.8) :xvals 6)
98 ;; ((1.0 1.3 1.6 1.9 2.2 2.5)
99 ;; (1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))
101 ;;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
102 (kernel-smooth #(1.0 1.2 1.3 1.8 2.1 2.5)
103 #(1.2 2.0 2.1 2.0 1.1 2.8) :xvals 5)
104 ;; ((1.0 1.375 1.75 2.125 2.5)
105 ;; (1.6603277642110226 1.9471748095239771 1.7938127405752287
106 ;; 1.5871511322219498 2.518194783156392))
108 (kernel-dens #(1.0 1.2 2.5 2.1 1.8 1.2) :xvals 5)
109 ;; ((1.0 1.375 1.75 2.125 2.5)
110 ;; (0.7224150453621405 0.5820045548233707 0.38216411702854214
111 ;; 0.4829822708587095 0.3485939156929503))
113 (fft #(1.0 1.2 2.5 2.1 1.8))
114 ;; #(#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))
116 (lowess #(1.0 1.2 2.5 2.1 1.8 1.2) #(1.2 2.0 2.1 2.0 1.1 2.8))
117 ;; (#(1.0 1.2 1.2 1.8 2.1 2.5))
121 ;;; Log-gamma function
123 (log-gamma 3.4) ;;1.0923280596789584
125 ;;; Probability functions
127 ;;; looking at these a bit more, perhaps a more CLOSy style is needed, i.e.
128 ;;; (quantile :list-or-cons loc :type type (one of 'empirical 'normal 'cauchy, etc...))
129 ;;; similar for the cdf, density, and rand.
130 ;;; Probably worth figuring out how to add a new distribution
131 ;;; efficiently, i.e. by keeping some kind of list.
133 ;;; Normal distribution
135 (normal-quant 0.95) ;;1.6448536279366268
136 (normal-cdf 1.3) ;;0.9031995154143897
137 (normal-dens 1.3) ;;0.17136859204780736
138 (normal-rand 2) ;;(-0.40502015f0 -0.8091404f0)
140 (bivnorm-cdf 0.2 0.4 0.6) ;;0.4736873734160288
142 ;;; Cauchy distribution
144 (cauchy-quant 0.95) ;;6.313751514675031
145 (cauchy-cdf 1.3) ;;0.7912855998398473
146 (cauchy-dens 1.3) ;;0.1183308127104695
147 (cauchy-rand 2) ;;(-1.06224644160405 -0.4524695943939537)
149 ;;; Gamma distribution
151 (gamma-quant 0.95 4.3) ;;8.178692439291645
152 (gamma-cdf 1.3 4.3) ;;0.028895150986674906
153 (gamma-dens 1.3 4.3) ;;0.0731517686447374
154 (gamma-rand 2 4.3) ;;(2.454918912880936 4.081365384357454)
156 ;;; Chi-square distribution
158 (chisq-quant 0.95 3) ;;7.814727903379012
159 (chisq-cdf 1 5) ;;0.03743422675631789
160 (chisq-dens 1 5) ;;0.08065690818083521
161 (chisq-rand 2 4) ;;(1.968535826180572 2.9988646156942997)
163 ;;; Beta distribution
165 (beta-quant 0.95 3 2) ;;0.9023885371149876
166 (beta-cdf 0.4 2 2.4) ;;0.4247997418541529
167 (beta-dens 0.4 2 2.4) ;;1.5964741858913518
168 (beta-rand 2 2 2.4) ;;(0.8014897077282279 0.6516371997922659)
170 ;;; t distribution
172 (t-quant 0.95 3) ;;2.35336343484194
173 (t-cdf 1 2.3) ;;0.794733624298342
174 (t-dens 1 2.3) ;;0.1978163816318102
175 (t-rand 2 2.3) ;;(-0.34303672776089306 -1.142505872436518)
177 ;;; F distribution
179 (f-quant 0.95 3 5) ;;5.409451318117459
180 (f-cdf 1 3.2 5.4) ;;0.5347130905510765
181 (f-dens 1 3.2 5.4) ;;0.37551128864591415
182 (f-rand 2 3 2) ;;(0.7939093442091963 0.07442694152491144)
184 ;;; Poisson distribution
186 (poisson-quant 0.95 3.2) ;;6
187 (poisson-cdf 1 3.2) ;;0.17120125672252395
188 (poisson-pmf 1 3.2) ;;0.13043905274097067
189 (poisson-rand 5 3.2) ;;(2 1 2 0 3)
191 ;;; Binomial distribution
193 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
194 (binomial-quant 0 3 0.4) ;;; -2147483648
195 (binomial-cdf 1 3 0.4) ;;0.6479999999965776
196 (binomial-pmf 1 3 0.4) ;;0.4320000000226171
197 (binomial-rand 5 3 0.4) ;;(2 2 0 1 2)
200 ;;; OBJECT SYSTEM
202 (in-package :ls-user)
203 (defproto *test-proto*)
204 *test-proto*
205 (defmeth *test-proto* :make-data (&rest args) nil)
206 (send *test-proto* :make-data)
209 ;;; Testing
211 (in-package :lisp-stat-unittests)
212 (testsuites)
213 (print-tests)
214 (run-tests)
215 (last-test-status)
216 ;;(failures)
218 (testsuite-tests 'lisp-stat)
219 (run-tests :suite 'lisp-stat)
220 (describe (run-tests :suite 'lisp-stat))
222 (describe (run-tests :suite 'lisp-stat-testsupport))
224 (run-tests :suite 'lisp-stat-probdistn)
225 (describe (run-tests :suite 'lisp-stat-probdistn))
229 (run-tests :suite 'lisp-stat-spec-fns)
230 (describe (run-tests :suite 'lisp-stat-spec-fns))
233 (find-testsuite 'lisp-stat-lin-alg)
234 (testsuite-tests 'lisp-stat-lin-alg)
235 (run-tests :suite 'lisp-stat-lin-alg)
236 (describe (run-tests :suite 'lisp-stat-lin-alg))
239 ;;; Data Analysis test
242 (in-package :ls-user)
244 (def iron (list 61 175 111 124 130 173 169 169 160 224 257 333 199))
245 iron
246 (def aluminum (list 13 21 24 23 64 38 33 61 39 71 112 88 54))
247 aluminum
248 (def absorbtion (list 4 18 14 18 26 26 21 30 28 36 65 62 40))
249 absorbtion
251 (fivnum absorbtion)
252 (median absorbtion) (sort absorbtion) (rank absorbtion)
253 (standard-deviation absorbtion)
254 (interquartile-range absorbtion)
257 *variables*
259 (variables)
260 (undef 'iron)
261 (variables)