Draft of Rice talk, initially, but it's wrong.
[CommonLispStat.git] / TODO.lisp
blob6ad425f9d903aa9614ce146024cb24c3be1fed6c
1 ;;; -*- mode: lisp -*-
3 ;;; Time-stamp: <2009-03-11 09:27:31 tony>
4 ;;; Creation: <2008-09-08 08:06:30 tony>
5 ;;; File: TODO.lisp
6 ;;; Author: AJ Rossini <blindglobe@gmail.com>
7 ;;; Copyright: (c) 2007-2008, AJ Rossini <blindglobe@gmail.com>. BSD.
8 ;;; Purpose: Stuff that needs to be made working sits inside the progns...
10 ;;; What is this talk of 'release'? Klingons do not make software
11 ;;; 'releases'. Our software 'escapes', leaving a bloody trail of
12 ;;; designers and quality assurance people in its wake.
14 ;;; This file contains the current challenges to solve, including a
15 ;;; description of the setup and the work to solve....
17 ;;; SET UP
19 (in-package :cl-user)
20 ;;(asdf:oos 'asdf:compile-op 'lispstat)
21 ;;(asdf:oos 'asdf:load-op 'lispstat)
23 (in-package :lisp-stat-unittests)
25 ;; tests = 54, failures = 7, errors = 3
27 (describe (run-tests :suite 'lisp-stat-ut))
28 (run-tests :suite 'lisp-stat-ut)
31 ;; FIXME: Example: currently not relevant, yet
32 (describe
33 (lift::run-test
34 :test-case 'lisp-stat-unittests::create-proto
35 :suite 'lisp-stat-unittests::lisp-stat-ut-proto))
38 (in-package :ls-user)
41 (progn ;; Data setup
43 (defparameter *indep-vars-2-matrix*
44 (make-matrix (length iron) 2
45 :initial-contents
46 (mapcar #'(lambda (x y)
47 (list (coerce x 'double-float)
48 (coerce y 'double-float)))
49 iron aluminum)))
52 (defparameter *dep-var*
53 (make-vector (length absorbtion)
54 :type :row
55 :initial-contents
56 (list
57 (mapcar #'(lambda (x) (coerce x 'double-float))
58 absorbtion))))
60 (defparameter *dep-var-int*
61 (make-vector (length absorbtion)
62 :type :row
63 :element-type 'integer
64 :initial-contents (list absorbtion)))
67 (defparameter *xv+1a*
68 (make-matrix
69 8 2
70 :initial-contents #2A((1d0 1d0)
71 (1d0 3d0)
72 (1d0 2d0)
73 (1d0 4d0)
74 (1d0 3d0)
75 (1d0 5d0)
76 (1d0 4d0)
77 (1d0 6d0))))
79 (defparameter *xv+1b*
80 (bind2
81 (ones 8 1)
82 (make-matrix
83 8 1
84 :initial-contents '((1d0)
85 (3d0)
86 (2d0)
87 (4d0)
88 (3d0)
89 (5d0)
90 (4d0)
91 (6d0)))
92 :by :column))
94 (m= *xv+1a* *xv+1b*) ; => T
96 (princ "Data Set up"))
99 (progn
100 ;; REVIEW: general Lisp use guidance
102 (fdefinition 'make-matrix)
103 (documentation 'make-matrix 'function)
105 #| Examples from CLHS, a bit of guidance.
107 ;; This function assumes its callers have checked the types of the
108 ;; arguments, and authorizes the compiler to build in that assumption.
109 (defun discriminant (a b c)
110 (declare (number a b c))
111 "Compute the discriminant for a quadratic equation."
112 (- (* b b) (* 4 a c))) => DISCRIMINANT
113 (discriminant 1 2/3 -2) => 76/9
115 ;; This function assumes its callers have not checked the types of the
116 ;; arguments, and performs explicit type checks before making any assumptions.
117 (defun careful-discriminant (a b c)
118 "Compute the discriminant for a quadratic equation."
119 (check-type a number)
120 (check-type b number)
121 (check-type c number)
122 (locally (declare (number a b c))
123 (- (* b b) (* 4 a c)))) => CAREFUL-DISCRIMINANT
124 (careful-discriminant 1 2/3 -2) => 76/9
129 #+nil
130 (progn ;; FIXME: Regression modeling
132 ;; data setup in previous FIXME
133 (defparameter *m* nil
134 "holding variable.")
135 ;; need to make vectors and matrices from the lists...
137 ;; BROKEN
138 (def *m* (regression-model (list->vector-like iron)
139 (list->vector-like absorbtion)))
141 (def m (regression-model (list->vector-like iron)
142 (list->vector-like absorbtion) :print nil))
143 ;;Good
144 (send m :print)
145 (send m :own-slots)
146 (send m :own-methods)
147 ;; (lsos::ls-objects-methods m) ; bogus?
148 (send m :show)
150 (def m (regression-model (list->vector-like iron)
151 (list->vector-like absorbtion)))
153 (def m (regression-model (listoflists->matrix-like (list iron aluminum))
154 (list->vector-like absorbtion) :print nil))
157 (send m :compute)
158 (send m :sweep-matrix)
159 (format t "~%~A~%" (send m :sweep-matrix))
161 ;; need to get multiple-linear regression working (simple linear regr
162 ;; works)... to do this, we need to redo the whole numeric structure,
163 ;; I'm keeping these in as example of brokenness...
165 (send m :basis) ;; this should be positive?
166 (send m :coef-estimates) )
168 #+nil
169 (progn ;; FIXME: Need to clean up data examples, licenses, attributions, etc.
170 ;; The following breaks because we should use a package to hold
171 ;; configuration details, and this would be the only package outside
172 ;; of packages.lisp, as it holds the overall defsystem structure.
173 (load-data "iris.lsp") ;; (the above partially fixed).
174 (variables)
175 diabetes )
180 (progn ;; FIXME: read data from CSV file. To do.
183 ;; challenge is to ensure that we get mixed arrays when we want them,
184 ;; and single-type (simple) arrays in other cases.
187 (defparameter *csv-num*
188 (cybertiggyr-dsv::load-escaped
189 #p"/media/disk/Desktop/sandbox/CLS.git/Data/example-numeric.csv"
190 :field-separator #\,
191 :trace T))
193 (nth 0 (nth 0 *csv-num*))
195 (defparameter *csv-num*
196 (cybertiggyr-dsv::load-escaped
197 #p"/media/disk/Desktop/sandbox/CLS.git/Data/example-numeric2.dsv"
198 :field-separator #\:))
200 (nth 0 (nth 0 *csv-num*))
203 ;; The handling of these types should be compariable to what we do for
204 ;; matrices, but without the numerical processing. i.e. mref, bind2,
205 ;; make-dataframe, and the class structure should be similar.
207 ;; With numerical data, there should be a straightforward mapping from
208 ;; the data.frame to a matrix. With categorical data (including
209 ;; dense categories such as doc-strings, as well as sparse categories
210 ;; such as binary data), we need to include metadata about ordering,
211 ;; coding, and such. So the structures should probably consider
213 ;; Using the CSV file:
215 (defun parse-number (s)
216 (let* ((*read-eval* nil)
217 (n (read-from-string s)))
218 (if (numberp n) n)))
220 (parse-number "34")
221 (parse-number "34 ")
222 (parse-number " 34")
223 (parse-number " 34 ")
225 (+ (parse-number "3.4") 3)
226 (parse-number "3.4 ")
227 (parse-number " 3.4")
228 (+ (parse-number " 3.4 ") 3)
230 (parse-number "a")
232 ;; (coerce "2.3" 'number) => ERROR
233 ;; (coerce "2" 'float) => ERROR
237 (defparameter *csv-num*
238 (cybertiggyr-dsv::load-escaped
239 #p"/media/disk/Desktop/sandbox/CLS.git/Data/example-numeric.csv"
240 :field-separator #\,
241 :filter #'parse-number
242 :trace T))
244 (nth 0 (nth 0 *csv-num*))
246 (defparameter *csv-num*
247 (cybertiggyr-dsv::load-escaped
248 #p"/media/disk/Desktop/sandbox/CLS.git/Data/example-numeric2.dsv"
249 :field-separator #\:
250 :filter #'parse-number))
252 (nth 0 (nth 0 *csv-num*))
254 ;; now we've got the DSV code in the codebase, auto-loaded I hope:
255 cybertiggyr-dsv:*field-separator*
256 (defparameter *example-numeric.csv*
257 (cybertiggyr-dsv:load-escaped "Data/example-numeric.csv"
258 :field-separator #\,))
259 *example-numeric.csv*
261 ;; the following fails because we've got a bit of string conversion
262 ;; to do. 2 thoughts: #1 modify dsv package, but mucking with
263 ;; encapsulation. #2 add a coercion tool (better, but potentially
264 ;; inefficient).
265 #+nil(coerce (nth 3 (nth 3 *example-numeric.csv*)) 'double-float)
267 ;; cases, simple to not so
268 (defparameter *test-string1* "1.2")
269 (defparameter *test-string2* " 1.2")
270 (defparameter *test-string3* " 1.2 ")
274 #+nil
275 (progn ;; experiments with GSL and the Lisp interface.
276 (asdf:oos 'asdf:load-op 'gsll)
277 (asdf:oos 'asdf:load-op 'gsll-tests)
279 ;; the following should be equivalent
280 (setf *t1* (LIST 6.18d0 6.647777777777779d0 6.18d0))
281 (setf *t2* (MULTIPLE-VALUE-LIST
282 (LET ((VEC
283 (gsll:make-marray 'DOUBLE-FLOAT
284 :INITIAL-CONTENTS '(-3.21d0 1.0d0 12.8d0)))
285 (WEIGHTS
286 (gsll:MAKE-MARRAY 'DOUBLE-FLOAT
287 :INITIAL-CONTENTS '(3.0d0 1.0d0 2.0d0))))
288 (LET ((MEAN (gsll:MEAN VEC)))
289 (LIST (gsll:ABSOLUTE-DEVIATION VEC)
290 (gsll:WEIGHTED-ABSOLUTE-DEVIATION VEC WEIGHTS)
291 (gsll:ABSOLUTE-DEVIATION VEC MEAN))))))
292 (eql *t1* *t2*)
294 ;; from (gsll:examples 'gsll::numerical-integration) ...
295 (gsll:integration-qng gsll::one-sine 0.0d0 PI)
297 (gsll:defun-single axpb (x) (+ (* 2 x) 3)) ;; a<-2, b<-3
298 (gsll:integration-qng axpb 1d0 2d0)
300 (let ((a 2)
301 (b 3))
302 (defun-single axpb2 (x) (+ (* a x) b)))
303 (gsll:integration-qng axpb2 1d0 2d0)
305 ;; BAD
306 ;; (gsll:integration-qng
307 ;; (let ((a 2)
308 ;; (b 3))
309 ;; (defun-single axpb2 (x) (+ (* a x) b)))
310 ;; 1d0 2d0)
312 ;; right, but weird expansion...
313 (gsll:integration-qng
314 (let ((a 2)
315 (b 3))
316 (defun axpb2 (x) (+ (* a x) b))
317 (gsll:def-single-function axpb2)
318 axpb2)
319 1d0 2d0)
321 ;; Linear least squares
323 (gsll:gsl-lookup "gsl_linalg_LU_decomp") ; => gsll:lu-decomposition
324 (gsll:gsl-lookup "gsl_linalg_LU_solve") ; => gsll:lu-solve
329 #+nil
330 (progn ;; philosophy time
332 (setf my-model (model :name "ex1"
333 :data-slots (list w x y z)
334 :param-slots (list alpha beta gamma)
335 :math-form (regression-model :formula '(= w (+ (* beta x)
336 (* alpha y)
337 (* gamma z)
338 normal-error))
339 :centrality 'median ; 'mean
342 #| or:
343 #R"W ~ x+ y + z "
346 (setf my-dataset (statistical-table :table data-frame-contents
347 :metadata (list (:case-names (list ))
348 (:var-names (list ))
349 (:documentation "string of doc"))))
351 (setf my-analysis (analysis
352 :model my-model
353 :data my-dataset
354 :parameter-map (pairing (model-param-slots my-model)
355 (data-var-names my-dataset))))
357 ;; ontological implications -- the analysis is an abstract class of
358 ;; data, model, and mapping between the model and data. The fit is
359 ;; the instantiation of such. This provides a statistical object
360 ;; computation theory which can be realized as "executable
361 ;; statistics" or "computable statistics".
362 (setf my-analysis (analyze my-fit
363 :estimation-method 'linear-least-squares-regression))
365 ;; one of the tricks here is that one needs to provide the structure
366 ;; from which to consider estimation, and more importantly, the
367 ;; validity of the estimation.
370 (setf linear-least-squares-regression
371 (estimation-method-definition
372 :variable-defintions ((list
373 ;; from MachLearn: supervised,
374 ;; unsupervised
375 :data-response-vars list-drv ; nil if unsup
377 :param-vars list-pv
378 :data-predictor-vars list-dpv
379 ;; nil in this case. these
380 ;; describe "out-of-box" specs
381 :hyper-vars list-hv))
382 :form '(regression-additive-error
383 :central-form (linear-form drv pv dpv)
384 :error-form 'normal-error)
385 :resulting-decision '(point-estimation interval-estimation)
386 :philosophy 'frequentist
387 :documentation "use least squares to fit a linear regression
388 model to data."))
390 (defparameter *statistical-philosophies*
391 '(frequentist bayesian fiducial decision-analysis)
392 "can be combined to build decision-making approaches and
393 characterizations")
395 (defparameter *decisions*
396 '(estimation selection testing)
397 "possible results from a...")
398 ;; is this really true? One can embedded hypothesis testing within
399 ;; estimation, as the hypothesis estimated to select. And
400 ;; categorical/continuous rear their ugly heads, but not really in
401 ;; an essential way.
403 (defparameter *ontology-of-decision-procedures*
404 (list :decisions
405 (list :estimation
406 (list :point
407 (list :maximum-likelihood
408 :minimum-entropy
409 :least-squares
410 :method-of-moments)
411 :interval
412 (list :maximum-likelihood
414 :testing
415 (list :fisherian
416 :neyman-pearson
417 (list :traditional
418 :bioequivalence-inversion)
419 :selection
420 (list :ranking
421 :top-k-of-n-select))
422 :parametric
423 :partially-parametric))
424 "start of ontology"))
427 ;;;; LM
429 (progn
431 (defparameter *y*
432 (make-vector
434 :type :row
435 :initial-contents '((1d0 2d0 3d0 4d0 5d0 6d0 7d0 8d0))))
438 (defparameter *xv+1*
439 (make-matrix
441 :initial-contents '((1d0 1d0)
442 (1d0 3d0)
443 (1d0 2d0)
444 (1d0 4d0)
445 (1d0 3d0)
446 (1d0 5d0)
447 (1d0 4d0)
448 (1d0 6d0))))
451 ;; so something like (NOTE: matrices are transposed to begin with, hence the incongruety)
452 (defparameter *xtx-2* (m* (transpose *xv+1*) *xv+1*))
453 ;; #<LA-SIMPLE-MATRIX-DOUBLE 2 x 2
454 ;; 8.0d0 28.0d0
455 ;; 28.0d0 116.0d0>
457 (defparameter *xty-2* (m* (transpose *xv+1*) (transpose *y*)))
458 ;; #<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
459 ;; 36.0d0
460 ;; 150.0d0>
462 (defparameter *rcond-2* 0.000001)
463 (defparameter *betahat-2* (gelsy *xtx-2* *xty-2* *rcond-2*))
464 ;; *xtx-2* => "details of complete orthogonal factorization"
465 ;; according to man page:
466 ;; #<LA-SIMPLE-MATRIX-DOUBLE 2 x 2
467 ;; -119.33147112141039d0 -29.095426104883202d0
468 ;; 0.7873402682880205d0 -1.20672274167718d0>
470 ;; *xty-2* => output becomes solution:
471 ;; #<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
472 ;; -0.16666666666668312d0
473 ;; 1.333333333333337d0>
475 *betahat-2* ; which matches R, see below
477 (documentation 'gelsy 'function)
480 ;; (#<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
481 ;; -0.16666666666668312 1.333333333333337>
482 ;; 2)
484 ;; ## Test case in R:
485 ;; x <- c( 1.0, 3.0, 2.0, 4.0, 3.0, 5.0, 4.0, 6.0)
486 ;; y <- c( 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0)
487 ;; lm(y~x)
488 ;; ## => Call: lm(formula = y ~ x)
490 ;; Coefficients: (Intercept) x
491 ;; -0.1667 1.3333
493 ;; summary(lm(y~x))
494 ;; ## =>
496 ;; Call:
497 ;; lm(formula = y ~ x)
499 ;; Residuals:
500 ;; Min 1Q Median 3Q Max
501 ;; -1.833e+00 -6.667e-01 -3.886e-16 6.667e-01 1.833e+00
503 ;; Coefficients:
504 ;; Estimate Std. Error t value Pr(>|t|)
505 ;; (Intercept) -0.1667 1.1587 -0.144 0.89034
506 ;; x 1.3333 0.3043 4.382 0.00466 **
507 ;; ---
508 ;; Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
510 ;; Residual standard error: 1.291 on 6 degrees of freedom
511 ;; Multiple R-squared: 0.7619, Adjusted R-squared: 0.7222
512 ;; F-statistic: 19.2 on 1 and 6 DF, p-value: 0.004659
516 ;; which suggests one might do (modulo ensuring correct
517 ;; orientations). When this is finalized, it should migrate to
518 ;; CLS.
522 (defparameter *n* 20) ; # rows = # obsns
523 (defparameter *p* 10) ; # cols = # vars
524 (defparameter *x-temp* (rand *n* *p*))
525 (defparameter *b-temp* (rand *p* 1))
526 (defparameter *y-temp* (m* *x-temp* *b-temp*))
527 ;; so Y=Xb + \eps
528 (defparameter *rcond* (* (coerce (expt 2 -52) 'double-float)
529 (max (nrows *x-temp*) (ncols *y-temp*))))
530 (defparameter *orig-x* (copy *x-temp*))
531 (defparameter *orig-b* (copy *b-temp*))
532 (defparameter *orig-y* (copy *y-temp*))
534 (defparameter *lm-result* (lm *x-temp* *y-temp*))
535 (princ (first *lm-result*))
536 (princ (second *lm-result*))
537 (princ (third *lm-result*))
538 (v= (third *lm-result*)
539 (v- (first (first *lm-result*))
540 (first (second *lm-result*)))))