Update local CFFI to darcs from 1.6.08
[CommonLispStat.git] / ls-demo.lisp
blob19c32a4b3bed6902d3a61c7c7d94a7e11a2e1936
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 ;;; Time-stamp: <2008-05-25 20:21:38 tony>
7 ;;; Creation: <2007-01-01 09:21:50 user> WRONG
8 ;;; File: ls-demo.lisp
9 ;;; Author: AJ Rossini <blindglobe@gmail.com>
10 ;;; Copyright: (c) 2007, AJ Rossini. BSD.
11 ;;; Purpose: demonstrations of how one might use CLS.
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 (in-package :cl-user)
18 ;;(asdf:oos 'asdf:load-op 'lift) ;; we need this, but I don't know why?
19 ;; hacked by simply doing this in the lispstat.asd UGLY hack.
20 (asdf:oos 'asdf:load-op 'lispstat)
22 ;;; non-rigorous check for exports.
23 ;;; This is generally not how I expect it to be used.
25 (in-package :cl-user)
26 (lisp-stat:binomial-quant 0.95 3 0.4) ;;; 3
27 (lisp-stat:binomial-quant 0 3 0.4) ;;; 0
28 (lisp-stat:normal-rand 20) ;;; 20 floating-point numbers :-)
30 ;;;; THIS is how I expect it to be used, either with work in ls-user,
31 ;;;; or a cloned package similar to ls-user.
33 (in-package :ls-user)
35 ;;;; Matrix algebra.
37 (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
38 ;; (#2A((1.7888543819998317 0.0 0.0)
39 ;; (1.6770509831248424 0.11180339887498929 0.0)
40 ;; (2.23606797749979 2.23606797749979 3.332000937312528e-8))
41 ;; 5.000000000000003)
45 (defvar my-chol-decomp-test (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5))))
46 my-chol-decomp-test
47 (nth 0 my-chol-decomp-test)
48 (nth 1 my-chol-decomp-test)
51 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
52 ;; (#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)
54 (lu-solve
55 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
56 #(2 3 4))
57 ;; #(-2.333333333333333 1.3333333333333335 0.6666666666666666)
59 (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
60 ;; #2A((2.0 -0.33333333333333326 -1.3333333333333335)
61 ;; (-1.0 -0.6666666666666666 1.3333333333333333)
62 ;; (0.0 0.6666666666666666 -0.3333333333333333))
64 (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
65 ;; (#2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
66 ;; (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
67 ;; (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
68 ;; #(9.699290438141343 0.8971681569301373 0.3447525123483081)
69 ;; #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
70 ;; (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
71 ;; (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
72 ;; T)
74 (qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
75 ;; (#2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
76 ;; (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
77 ;; (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
78 ;; #2A((-3.0 -5.333333333333334 -7.333333333333332)
79 ;; (0.0 -0.7453559924999292 -1.1925695879998877)
80 ;; (0.0 0.0 -1.3416407864998738)))
82 (rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
83 ;; 6.8157451e7
84 ;;; CURRENTLY FAILS!!
86 (eigen #2A((2 3 4) (1 2 4) (2 4 5)))
87 ;; (#(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
88 ;; (#(0.4999999999999998 0.4999999999999997 0.7071067811865475)
89 ;; #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
90 ;; #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
91 ;; NIL)
93 (spline #(1.0 1.2 1.3 1.8 2.1 2.5)
94 #(1.2 2.0 2.1 2.0 1.1 2.8) :xvals 6)
95 ;; ((1.0 1.3 1.6 1.9 2.2 2.5)
96 ;; (1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))
98 ;;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
99 (kernel-smooth #(1.0 1.2 1.3 1.8 2.1 2.5)
100 #(1.2 2.0 2.1 2.0 1.1 2.8) :xvals 5)
101 ;; ((1.0 1.375 1.75 2.125 2.5)
102 ;; (1.6603277642110226 1.9471748095239771 1.7938127405752287
103 ;; 1.5871511322219498 2.518194783156392))
105 (kernel-dens #(1.0 1.2 2.5 2.1 1.8 1.2) :xvals 5)
106 ;; ((1.0 1.375 1.75 2.125 2.5)
107 ;; (0.7224150453621405 0.5820045548233707 0.38216411702854214
108 ;; 0.4829822708587095 0.3485939156929503))
110 (fft #(1.0 1.2 2.5 2.1 1.8))
111 ;; #(#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))
113 (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))
114 ;; (#(1.0 1.2 1.2 1.8 2.1 2.5))
118 ;;;; Special functions
120 ;; Log-gamma function
122 (log-gamma 3.4) ;;1.0923280596789584
126 ;;;; Probability functions
128 ;; looking at these a bit more, perhaps a more CLOSy style is needed, i.e.
129 ;; (quantile :list-or-cons loc :type type (one of 'empirical 'normal 'cauchy, etc...))
130 ;; similar for the cdf, density, and rand.
131 ;; Probably worth figuring out how to add a new distribution
132 ;; efficiently, i.e. by keeping some kind of list.
134 ;; Normal distribution
136 (normal-quant 0.95) ;;1.6448536279366268
137 (normal-cdf 1.3) ;;0.9031995154143897
138 (normal-dens 1.3) ;;0.17136859204780736
139 (normal-rand 2) ;;(-0.40502015f0 -0.8091404f0)
141 (bivnorm-cdf 0.2 0.4 0.6) ;;0.4736873734160288
143 ;; Cauchy distribution
145 (cauchy-quant 0.95) ;;6.313751514675031
146 (cauchy-cdf 1.3) ;;0.7912855998398473
147 (cauchy-dens 1.3) ;;0.1183308127104695
148 (cauchy-rand 2) ;;(-1.06224644160405 -0.4524695943939537)
150 ;; Gamma distribution
152 (gamma-quant 0.95 4.3) ;;8.178692439291645
153 (gamma-cdf 1.3 4.3) ;;0.028895150986674906
154 (gamma-dens 1.3 4.3) ;;0.0731517686447374
155 (gamma-rand 2 4.3) ;;(2.454918912880936 4.081365384357454)
157 ;; Chi-square distribution
159 (chisq-quant 0.95 3) ;;7.814727903379012
160 (chisq-cdf 1 5) ;;0.03743422675631789
161 (chisq-dens 1 5) ;;0.08065690818083521
162 (chisq-rand 2 4) ;;(1.968535826180572 2.9988646156942997)
164 ;; Beta distribution
166 (beta-quant 0.95 3 2) ;;0.9023885371149876
167 (beta-cdf 0.4 2 2.4) ;;0.4247997418541529
168 (beta-dens 0.4 2 2.4) ;;1.5964741858913518
169 (beta-rand 2 2 2.4) ;;(0.8014897077282279 0.6516371997922659)
171 ;; t distribution
173 (t-quant 0.95 3) ;;2.35336343484194
174 (t-cdf 1 2.3) ;;0.794733624298342
175 (t-dens 1 2.3) ;;0.1978163816318102
176 (t-rand 2 2.3) ;;(-0.34303672776089306 -1.142505872436518)
178 ;; F distribution
180 (f-quant 0.95 3 5) ;;5.409451318117459
181 (f-cdf 1 3.2 5.4) ;;0.5347130905510765
182 (f-dens 1 3.2 5.4) ;;0.37551128864591415
183 (f-rand 2 3 2) ;;(0.7939093442091963 0.07442694152491144)
185 ;; Poisson distribution
187 (poisson-quant 0.95 3.2) ;;6
188 (poisson-cdf 1 3.2) ;;0.17120125672252395
189 (poisson-pmf 1 3.2) ;;0.13043905274097067
190 (poisson-rand 5 3.2) ;;(2 1 2 0 3)
192 ;; Binomial distribution
194 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
195 (binomial-quant 0 3 0.4) ;;; -2147483648
196 (binomial-cdf 1 3 0.4) ;;0.6479999999965776
197 (binomial-pmf 1 3 0.4) ;;0.4320000000226171
198 (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)
207 (defvar my-proto-instance nil)
208 (setf my-proto-instance (send *test-proto* :new))
209 (send *test-proto* :own-slots)
210 (send *test-proto* :has-slot 'proto-name)
211 (send *test-proto* :has-slot 'PROTO-NAME)
212 (send *test-proto* :has-slot 'make-data)
213 (send *test-proto* :has-slot 'MAKE-DATA)
214 (send *test-proto* :has-method 'make-data)
215 (send *test-proto* :has-method 'MAKE-DATA)
218 (defproto2 *test-proto3* (list) (list) (list) "test doc" t)
219 (defproto2 *test-proto4*)
220 *test-proto2*
221 (defmeth *test-proto* :make-data (&rest args) nil)
223 (defvar my-proto-instance nil)
224 (setf my-proto-instance (send *test-proto* :new))
225 (send *test-proto* :own-slots)
226 (send *test-proto* :has-slot 'proto-name)
227 (send *test-proto* :has-slot 'PROTO-NAME)
230 ;;;; Testing
232 (in-package :lisp-stat-unittests)
233 (testsuites)
234 (print-tests)
235 (run-tests)
236 (last-test-status)
237 ;;(failures)
239 (describe (run-tests :suite 'lisp-stat-testsupport))
240 (describe (run-tests :suite 'lisp-stat-testsupport2))
242 (testsuite-tests 'lisp-stat)
243 (run-tests :suite 'lisp-stat)
244 (describe (run-tests :suite 'lisp-stat))
246 (run-tests :suite 'lisp-stat-probdistn)
247 (describe (run-tests :suite 'lisp-stat-probdistn))
248 (run-tests :suite 'lisp-stat-spec-fns)
249 (describe (run-tests :suite 'lisp-stat-spec-fns))
251 (find-testsuite 'lisp-stat-lin-alg)
252 (testsuite-tests 'lisp-stat-lin-alg)
253 (run-tests :suite 'lisp-stat-lin-alg)
254 (describe (run-tests :suite 'lisp-stat-lin-alg))
256 ;;;; Data Analysis test
258 (in-package :ls-user)
260 ;; LispStat 1 approach to variables
262 (progn
263 (def iron (list 61 175 111 124 130 173 169 169 160 224 257 333 199))
264 iron
265 (def aluminum (list 13 21 24 23 64 38 33 61 39 71 112 88 54))
266 aluminum
267 (def absorbtion (list 4 18 14 18 26 26 21 30 28 36 65 62 40))
268 absorbtion
270 ;; LispStat 1 approach to data frames... (list of lists).
272 (DEF DIABETES
273 (QUOTE ((80 97 105 90 90 86 100 85 97 97 91 87 78 90 86 80 90 99 85 90 90 88 95 90 92 74 98 100 86 98 70 99 75 90 85 99 100 78 106 98 102 90 94 80 93 86 85 96 88 87 94 93 86 86 96 86 89 83 98 100 110 88 100 80 89 91 96 95 82 84 90 100 86 93 107 112 94 93 93 90 99 93 85 89 96 111 107 114 101 108 112 105 103 99 102 110 102 96 95 112 110 92 104 75 92 92 92 93 112 88 114 103 300 303 125 280 216 190 151 303 173 203 195 140 151 275 260 149 233 146 124 213 330 123 130 120 138 188 339 265 353 180 213 328 346)
274 (356 289 319 356 323 381 350 301 379 296 353 306 290 371 312 393 364 359 296 345 378 304 347 327 386 365 365 352 325 321 360 336 352 353 373 376 367 335 396 277 378 360 291 269 318 328 334 356 291 360 313 306 319 349 332 323 323 351 478 398 426 439 429 333 472 436 418 391 390 416 413 385 393 376 403 414 426 364 391 356 398 393 425 318 465 558 503 540 469 486 568 527 537 466 599 477 472 456 517 503 522 476 472 455 442 541 580 472 562 423 643 533 1468 1487 714 1470 1113 972 854 1364 832 967 920 613 857 1373 1133 849 1183 847 538 1001 1520 557 670 636 741 958 1354 1263 1428 923 1025 1246 1568)
275 (124 117 143 199 240 157 221 186 142 131 221 178 136 200 208 202 152 185 116 123 136 134 184 192 279 228 145 172 179 222 134 143 169 263 174 134 182 241 128 222 165 282 94 121 73 106 118 112 157 292 200 220 144 109 151 158 73 81 151 122 117 208 201 131 162 148 130 137 375 146 344 192 115 195 267 281 213 156 221 199 76 490 143 73 237 748 320 188 607 297 232 480 622 287 266 124 297 326 564 408 325 433 180 392 109 313 132 285 139 212 155 120 28 23 232 54 81 87 76 42 102 138 160 131 145 45 118 159 73 103 460 42 13 130 44 314 219 100 10 83 41 77 29 124 15)
276 (3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 3 3 2 2 3 2 2 3 3 3 3 2 3 3 3 3 3 2 3 3 3 3 3 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1))))
279 (DEF DLABS (QUOTE ("GLUFAST" "GLUTEST" "INSTEST" "CCLASS")))
280 (format t "loaded data.~%")
283 ;; Simple univariate variable-specific descriptions.
284 (fivnum absorbtion)
285 (median absorbtion)
286 (sort-data absorbtion)
287 (rank absorbtion)
288 (standard-deviation absorbtion)
289 (interquartile-range absorbtion)
291 (lisp-stat-matrix::bind-columns aluminum iron)
292 (bind-columns aluminum iron)
293 (apply #'bind-columns (list aluminum iron))
294 (lisp-stat-matrix::bind-columns #2a((1 2)(3 4)) #(5 6))
295 (bind-columns #2a((1 2)(3 4)) #(5 6))
298 (defvar fit1 nil)
299 (setf fit1 (regression-model absorbtion iron))
300 (send fit1 :display)
301 (send fit1 :residuals)
303 iron
304 (defvar fit1a nil)
305 (setf fit1a (regression-model absorbtion iron :print nil))
306 (send fit1a :doc)
307 (setf (send fit1a :doc) "this") ;; FIXME: this is a more naturualo
308 (send fit1a :x)
309 (send fit1a :y)
310 (send fit1a :compute)
311 (send fit1a :sweep-matrix)
312 (send fit1a :basis)
313 (send fit1a :residuals)
314 (send fit1a :display)
316 #+nil(progn
317 (array-dimension #2A ((1)) 0)
318 ;; more tests
321 ;;; FIXME: need to get multiple-linear regression working -- clearly
322 ;;; simple linear is working above!
323 (defvar m nil "holding variable.")
324 (def m (regression-model (list iron aluminum) absorbtion :print nil))
325 (send m :compute)
326 (send m :sweep-matrix)
327 (format t "~%~A~%" (send m :sweep-matrix))
328 (send m :display) ;; ERROR...
329 (def m (regression-model (bind-columns iron aluminum) absorbtion))
330 (send m :help)
331 (send m :help 'display)
332 (send m :plot-residuals)
335 (typep aluminum 'sequence)
336 (typep iron 'sequence)
337 (matrixp iron)
339 *variables*
341 (variables)
342 (undef 'iron)
343 (variables)
346 ;;; Example array calcs
348 #+nil(progn
349 (functionp #'and)
350 (= (array-dimensions #2A((2 3 3 ) (2 4 4)))
351 (array-dimensions #2A((2 3 3 ) (2 5 4))))
352 (reduce #'and (= (array-dimensions #2A((2 3) (2 4)))
353 (array-dimensions #2A((2 3 3 ) (2 5 4)))))
355 (defvar my-t-ar nil)
356 (setf my-t-ar #3A(((2 3) (2 2) (2 1))
357 ((2 3) (2 2) (2 1))))
358 (defvar my-t-ar2 nil)
359 (setf my-t-ar2 #2A((1 2 3 4)
360 (5 6 7 8)))
362 (array-dimensions my-t-ar)
363 (array-dimensions my-t-ar2)
365 (aref my-t-ar2 1 2) ;; GOOD
366 (aref my-t-ar2 (list 1 2)) ;; BAD
367 (apply #'aref my-t-ar2 (list 1 2)) ;; GOOD
368 ;; For extracting multiple elements
369 (mapcar #'(lambda (x) (apply #'aref my-t-ar2 x))
370 (list (list 0 0) (list 0 1)))
373 (aref my-t-ar 1 2 1)
374 (aref my-t-ar 1 2 1)
375 (aref my-t-ar 1 1 0)
377 (array-dimensions #3A(((2 3) (2 2) (2 1))
378 ((2 3) (2 2) (2 1))))
380 (reduce #'and (= #(2 3) #(2 4)))
381 (= #(2 3) #(2 3))
383 (let ((a #2A((2 3 3 ) (2 5 4)))
384 (b #2A((2 3 3 ) (2 5 4))))
385 (let ((a-rank (array-rank a))
386 (a-dim (array-dimensions a))
387 (a-b-elt-eq (loop for i in 0 to (aref a-dim 0)
388 for j in 0 to (aref a-dim 1)
389 collect (numerical= (apply #'aref a (list i j))
390 (apply #'aref b (list i j))))))
391 (every #'(lambda (x) x) a-b-elt-eq))))
393 (every #'(lambda (x) x) (list T T T))
394 (every #'(lambda (x) x) (list T T nil))
396 (and T T)
397 (mapcar #'(lambda (&rest args) (and args))
398 (list (= #(2 3) #(2 4))))
399 (= #(2 3) #(2 3))
401 ;;; examples of using CLEM
403 (in-package :clem-user)
405 (defvar m1 (make-instance 'double-float-matrix :rows 10 :cols 5))
408 (defvar m2 (make-instance 'number-matrix :rows 10 :cols 5))
412 ;;; Not defined but documented? Actually somewhere in clem/print.lisp
413 (setf *matrix-print-row-limit* 2)
414 (setf *matrix-print-col-limit* 2)
416 (print m2)
419 (mat-log m2)
420 (mat-abs m2)
421 (min m2)
422 (max m2)
424 (setf (mref m2 1 1) 5)
426 (setf (mref m2 0 0) 5)
430 ;;; prop list demo
433 (defvar tplist (list :this 'though :that 'there :thee 'tony))
434 (setf (symbol-plist tplist) (list :this 'though :that 'there :thee 'tony))
435 (get 'tplist :THIS)
436 tplist
437 (defvar tlist (list :this 'though :that 'there :thee 'tony))
438 (setf tlist (list :this 'though :that 'there :thee 'tony))
439 (listp tlist)
440 (getf tlist :THIS)
442 ;;; CL-SDL demos
445 (clc:clc-require :sdl-demos)
446 ;;(sdl-test:start) ; locks up SBCL.?
447 ;;; where <n> is 2-11, 16, :solar-system, :vertex-arrays,
448 (nehe:run-tutorial 2)