3 ;;; Time-stamp: <2009-04-15 16:34:53 tony>
4 ;;; Creation: <2008-09-08 08:06:30 tony>
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
9 ;;; progns... This file contains the current challenges to
10 ;;; solve, including a description of the setup and the work
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.
20 ;;(asdf:oos 'asdf:load-op 'lisp-matrix)
21 ;;(asdf:oos 'asdf:compile-op 'lispstat)
22 ;;(asdf:oos 'asdf:load-op 'lispstat)
24 (in-package :lisp-stat-unittests
)
26 ;; tests = 80, failures = 8, errors = 15
27 (run-tests :suite
'lisp-stat-ut
)
28 (describe (run-tests :suite
'lisp-stat-ut
))
30 ;; FIXME: Example: currently not relevant, yet
31 ;; (describe (lift::run-test :test-case 'lisp-stat-unittests::create-proto
32 ;; :suite 'lisp-stat-unittests::lisp-stat-ut-proto))
34 (describe 'lisp-stat-ut
)
38 (progn ;; FIXME: Regression modeling (some data future-ish)
41 (regression-model (list->vector-like iron
) ;; BROKEN
42 (list->vector-like absorbtion
))
53 (covariance-matrix *m-fit
*)
56 (regression-model (listoflist->matrix-like
(list iron aluminum
))
57 (list->vector-like absorbtion
) :print nil
))
58 (defparameter *m3-fit
*
64 (progn ;; FIXME: Need to clean up data examples, licenses, attributions, etc.
65 ;; The following breaks because we should use a package to hold
66 ;; configuration details, and this would be the only package outside
67 ;; of packages.lisp, as it holds the overall defsystem structure.
68 (load-data "iris.lsp") ;; (the above partially fixed).
75 (describe (lift::run-tests
:suite
'lisp-stat-ut-dataframe
))
76 (lift::run-tests
:suite
'lisp-stat-ut-dataframe
)
80 :test-case
'lisp-stat-unittests
::create-proto
81 :suite
'lisp-stat-unittests
::lisp-stat-ut-proto
))
83 (defparameter *my-df-1
*
84 (make-instance 'dataframe-array
85 :storage
#2A
((1 2 3 4 5)
87 :doc
"This is an interesting dataframe-array"
88 :case-labels
(list "x" "y")
89 :var-labels
(list "a" "b" "c" "d" "e")))
91 (setf (dfref *my-df-1
* 0 0) -
1d0
)
95 (make-dataframe #2A
((1 2 3 4 5)
98 (make-dataframe (rand 4 3))
102 (make-instance 'dataframe-array
109 (make-instance 'dataframe-array
116 (make-instance 'dataframe-array
117 :storage
#2A
((1d0 2d0
)
123 (defparameter *my-df-1
*
124 (make-dataframe #2A
((1 2 3 4 5)
126 :caselabels
(list "x" "y")
127 :varlabels
(list "a" "b" "c" "d" "e")
128 :doc
"This is an interesting dataframe-array"))
130 (caselabels *my-df-1
*)
131 (varlabels *my-df-1
*)
134 (defparameter *my-df-2
*
135 (make-instance 'dataframe-array
137 (make-array-from-listoflists
138 (cybertiggyr-dsv::load-escaped
139 "/media/disk/Desktop/sandbox/CLS.git/Data/example-mixed.csv"))
140 :doc
"This is an interesting dataframe-array"))
141 #|
:case-labels
(list "x" "y")
142 :var-labels
(list "a" "b" "c" "d" "e")
150 (describe 'make-matrix
)
152 (defparameter *indep-vars-2-matrix
*
153 (make-matrix (length iron
) 2
155 (mapcar #'(lambda (x y
)
156 (list (coerce x
'double-float
)
157 (coerce y
'double-float
)))
161 (defparameter *dep-var
*
162 (make-vector (length absorbtion
)
166 (mapcar #'(lambda (x) (coerce x
'double-float
))
169 (make-dataframe *dep-var
*)
170 (make-dataframe (transpose *dep-var
*))
172 (defparameter *dep-var-int
*
173 (make-vector (length absorbtion
)
175 :element-type
'integer
176 :initial-contents
(list absorbtion
)))
179 (defparameter *xv
+1a
*
182 :initial-contents
#2A
((1d0 1d0
)
191 (defparameter *xv
+1b
*
196 :initial-contents
'((1d0)
206 (m= *xv
+1a
* *xv
+1b
*) ; => T
208 (princ "Data Set up"))
214 ;; REVIEW: general Lisp use guidance
216 (fdefinition 'make-matrix
)
217 (documentation 'make-matrix
'function
)
219 #| Examples from CLHS
, a bit of guidance.
221 ;; This function assumes its callers have checked the types of the
222 ;; arguments, and authorizes the compiler to build in that assumption.
223 (defun discriminant (a b c
)
224 (declare (number a b c
))
225 "Compute the discriminant for a quadratic equation."
226 (- (* b b
) (* 4 a c
))) => DISCRIMINANT
227 (discriminant 1 2/3 -
2) => 76/9
229 ;; This function assumes its callers have not checked the types of the
230 ;; arguments, and performs explicit type checks before making any assumptions.
231 (defun careful-discriminant (a b c
)
232 "Compute the discriminant for a quadratic equation."
233 (check-type a number
)
234 (check-type b number
)
235 (check-type c number
)
236 (locally (declare (number a b c
))
237 (- (* b b
) (* 4 a c
)))) => CAREFUL-DISCRIMINANT
238 (careful-discriminant 1 2/3 -
2) => 76/9
246 (progn ;; FIXME: read data from CSV file. To do.
249 ;; challenge is to ensure that we get mixed arrays when we want them,
250 ;; and single-type (simple) arrays in other cases.
253 (defparameter *csv-num
*
254 (cybertiggyr-dsv::load-escaped
255 #p
"/media/disk/Desktop/sandbox/CLS.git/Data/example-numeric.csv"
259 (nth 0 (nth 0 *csv-num
*))
261 (defparameter *csv-num
*
262 (cybertiggyr-dsv::load-escaped
263 #p
"/media/disk/Desktop/sandbox/CLS.git/Data/example-numeric2.dsv"
264 :field-separator
#\
:))
266 (nth 0 (nth 0 *csv-num
*))
269 ;; The handling of these types should be compariable to what we do for
270 ;; matrices, but without the numerical processing. i.e. mref, bind2,
271 ;; make-dataframe, and the class structure should be similar.
273 ;; With numerical data, there should be a straightforward mapping from
274 ;; the data.frame to a matrix. With categorical data (including
275 ;; dense categories such as doc-strings, as well as sparse categories
276 ;; such as binary data), we need to include metadata about ordering,
277 ;; coding, and such. So the structures should probably consider
279 ;; Using the CSV file:
281 (defun parse-number (s)
282 (let* ((*read-eval
* nil
)
283 (n (read-from-string s
)))
289 (parse-number " 34 ")
291 (+ (parse-number "3.4") 3)
292 (parse-number "3.4 ")
293 (parse-number " 3.4")
294 (+ (parse-number " 3.4 ") 3)
298 ;; (coerce "2.3" 'number) => ERROR
299 ;; (coerce "2" 'float) => ERROR
301 (defparameter *csv-num
*
302 (cybertiggyr-dsv::load-escaped
303 #p
"/media/disk/Desktop/sandbox/CLS.git/Data/example-numeric.csv"
305 :filter
#'parse-number
308 (nth 0 (nth 0 *csv-num
*))
310 (defparameter *csv-num
*
311 (cybertiggyr-dsv::load-escaped
312 #p
"/media/disk/Desktop/sandbox/CLS.git/Data/example-numeric2.dsv"
314 :filter
#'parse-number
))
316 (nth 0 (nth 0 *csv-num
*))
318 ;; now we've got the DSV code in the codebase, auto-loaded I hope:
319 cybertiggyr-dsv
:*field-separator
*
320 (defparameter *example-numeric.csv
*
321 (cybertiggyr-dsv:load-escaped
"Data/example-numeric.csv"
322 :field-separator
#\
,))
323 *example-numeric.csv
*
325 ;; the following fails because we've got a bit of string conversion
326 ;; to do. 2 thoughts: #1 modify dsv package, but mucking with
327 ;; encapsulation. #2 add a coercion tool (better, but potentially
329 #+nil
(coerce (nth 3 (nth 3 *example-numeric.csv
*)) 'double-float
)
331 ;; cases, simple to not so
332 (defparameter *test-string1
* "1.2")
333 (defparameter *test-string2
* " 1.2")
334 (defparameter *test-string3
* " 1.2 ")
339 (progn ;; experiments with GSL and the Lisp interface.
340 (asdf:oos
'asdf
:load-op
'gsll
)
341 (asdf:oos
'asdf
:load-op
'gsll-tests
)
343 ;; the following should be equivalent
344 (setf *t1
* (LIST 6.18d0
6.647777777777779d0
6.18d0
))
345 (setf *t2
* (MULTIPLE-VALUE-LIST
347 (gsll:make-marray
'DOUBLE-FLOAT
348 :INITIAL-CONTENTS
'(-3.21d0
1.0d0
12.8d0
)))
350 (gsll:MAKE-MARRAY
'DOUBLE-FLOAT
351 :INITIAL-CONTENTS
'(3.0d0
1.0d0
2.0d0
))))
352 (LET ((MEAN (gsll:MEAN VEC
)))
353 (LIST (gsll:ABSOLUTE-DEVIATION VEC
)
354 (gsll:WEIGHTED-ABSOLUTE-DEVIATION VEC WEIGHTS
)
355 (gsll:ABSOLUTE-DEVIATION VEC MEAN
))))))
358 ;; from (gsll:examples 'gsll::numerical-integration) ...
359 (gsll:integration-qng gsll
::one-sine
0.0d0 PI
)
361 (gsll:defun-single axpb
(x) (+ (* 2 x
) 3)) ;; a<-2, b<-3
362 (gsll:integration-qng axpb
1d0
2d0
)
366 (defun-single axpb2
(x) (+ (* a x
) b
)))
367 (gsll:integration-qng axpb2
1d0
2d0
)
370 ;; (gsll:integration-qng
373 ;; (defun-single axpb2 (x) (+ (* a x) b)))
376 ;; right, but weird expansion...
377 (gsll:integration-qng
380 (defun axpb2 (x) (+ (* a x
) b
))
381 (gsll:def-single-function axpb2
)
385 ;; Linear least squares
387 (gsll:gsl-lookup
"gsl_linalg_LU_decomp") ; => gsll:lu-decomposition
388 (gsll:gsl-lookup
"gsl_linalg_LU_solve") ; => gsll:lu-solve
394 (progn ;; philosophy time
396 (setf my-model
(model :name
"ex1"
397 :data-slots
(list w x y z
)
398 :param-slots
(list alpha beta gamma
)
399 :math-form
(regression-model :formula
'(= w
(+ (* beta x
)
403 :centrality
'median
; 'mean
410 (setf my-dataset
(statistical-table :table data-frame-contents
411 :metadata
(list (:case-names
(list ))
413 (:documentation
"string of doc"))))
415 (setf my-analysis
(analysis
418 :parameter-map
(pairing (model-param-slots my-model
)
419 (data-var-names my-dataset
))))
421 ;; ontological implications -- the analysis is an abstract class of
422 ;; data, model, and mapping between the model and data. The fit is
423 ;; the instantiation of such. This provides a statistical object
424 ;; computation theory which can be realized as "executable
425 ;; statistics" or "computable statistics".
426 (setf my-analysis
(analyze my-fit
427 :estimation-method
'linear-least-squares-regression
))
429 ;; one of the tricks here is that one needs to provide the structure
430 ;; from which to consider estimation, and more importantly, the
431 ;; validity of the estimation.
434 (setf linear-least-squares-regression
435 (estimation-method-definition
436 :variable-defintions
((list
437 ;; from MachLearn: supervised,
439 :data-response-vars list-drv
; nil if unsup
442 :data-predictor-vars list-dpv
443 ;; nil in this case. these
444 ;; describe "out-of-box" specs
445 :hyper-vars list-hv
))
446 :form
'(regression-additive-error
447 :central-form
(linear-form drv pv dpv
)
448 :error-form
'normal-error
)
449 :resulting-decision
'(point-estimation interval-estimation
)
450 :philosophy
'frequentist
451 :documentation
"use least squares to fit a linear regression
454 (defparameter *statistical-philosophies
*
455 '(frequentist bayesian fiducial decision-analysis
)
456 "can be combined to build decision-making approaches and
459 (defparameter *decisions
*
460 '(estimation selection testing
)
461 "possible results from a...")
462 ;; is this really true? One can embedded hypothesis testing within
463 ;; estimation, as the hypothesis estimated to select. And
464 ;; categorical/continuous rear their ugly heads, but not really in
467 (defparameter *ontology-of-decision-procedures
*
471 (list :maximum-likelihood
476 (list :maximum-likelihood
482 :bioequivalence-inversion
)
487 :partially-parametric
))
488 "start of ontology"))
499 :initial-contents
'((1d0 2d0
3d0
4d0
5d0
6d0
7d0
8d0
))))
505 :initial-contents
'((1d0 1d0
)
515 ;; so something like (NOTE: matrices are transposed to begin with, hence the incongruety)
516 (defparameter *xtx-2
* (m* (transpose *xv
+1*) *xv
+1*))
517 ;; #<LA-SIMPLE-MATRIX-DOUBLE 2 x 2
521 (defparameter *xty-2
* (m* (transpose *xv
+1*) (transpose *y
*)))
522 ;; #<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
526 (defparameter *rcond-2
* 0.000001)
527 (defparameter *betahat-2
* (gelsy *xtx-2
* *xty-2
* *rcond-2
*))
528 ;; *xtx-2* => "details of complete orthogonal factorization"
529 ;; according to man page:
530 ;; #<LA-SIMPLE-MATRIX-DOUBLE 2 x 2
531 ;; -119.33147112141039d0 -29.095426104883202d0
532 ;; 0.7873402682880205d0 -1.20672274167718d0>
534 ;; *xty-2* => output becomes solution:
535 ;; #<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
536 ;; -0.16666666666668312d0
537 ;; 1.333333333333337d0>
539 *betahat-2
* ; which matches R, see below
541 (documentation 'gelsy
'function
)
544 ;; (#<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
545 ;; -0.16666666666668312 1.333333333333337>
548 ;; ## Test case in R:
549 ;; x <- c( 1.0, 3.0, 2.0, 4.0, 3.0, 5.0, 4.0, 6.0)
550 ;; y <- c( 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0)
552 ;; ## => Call: lm(formula = y ~ x)
554 ;; Coefficients: (Intercept) x
561 ;; lm(formula = y ~ x)
564 ;; Min 1Q Median 3Q Max
565 ;; -1.833e+00 -6.667e-01 -3.886e-16 6.667e-01 1.833e+00
568 ;; Estimate Std. Error t value Pr(>|t|)
569 ;; (Intercept) -0.1667 1.1587 -0.144 0.89034
570 ;; x 1.3333 0.3043 4.382 0.00466 **
572 ;; Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
574 ;; Residual standard error: 1.291 on 6 degrees of freedom
575 ;; Multiple R-squared: 0.7619, Adjusted R-squared: 0.7222
576 ;; F-statistic: 19.2 on 1 and 6 DF, p-value: 0.004659
580 ;; which suggests one might do (modulo ensuring correct
581 ;; orientations). When this is finalized, it should migrate to
586 (defparameter *n
* 20) ; # rows = # obsns
587 (defparameter *p
* 10) ; # cols = # vars
588 (defparameter *x-temp
* (rand *n
* *p
*))
589 (defparameter *b-temp
* (rand *p
* 1))
590 (defparameter *y-temp
* (m* *x-temp
* *b-temp
*))
592 (defparameter *rcond
* (* (coerce (expt 2 -
52) 'double-float
)
593 (max (nrows *x-temp
*) (ncols *y-temp
*))))
594 (defparameter *orig-x
* (copy *x-temp
*))
595 (defparameter *orig-b
* (copy *b-temp
*))
596 (defparameter *orig-y
* (copy *y-temp
*))
598 (defparameter *lm-result
* (lm *x-temp
* *y-temp
*))
599 (princ (first *lm-result
*))
600 (princ (second *lm-result
*))
601 (princ (third *lm-result
*))
602 (v= (third *lm-result
*)
603 (v- (first (first *lm-result
*))
604 (first (second *lm-result
*))))
609 ;; Some issues exist in the LAPACK vs. LINPACK variants, hence R
610 ;; uses LINPACK primarily, rather than LAPACK. See comments in R
611 ;; source for issues.
614 ;; Goal is to start from X, Y and then realize that if
615 ;; Y = X \beta, then, i.e. 8x1 = 8xp px1 + 8x1
616 ;; XtX \hat\beta = Xt Y
617 ;; so that we can solve the equation W \beta = Z where W and Z
618 ;; are known, to estimate \beta.
620 ;; the above is known to be numerically instable -- some processing
621 ;; of X is preferred and should be done prior. And most of the
622 ;; transformation-based work does precisely that.
624 ;; recall: Var[Y] = E[(Y - E[Y])(Y-E[Y])t]
625 ;; = E[Y Yt] - 2 \mu \mut + \mu \mut
626 ;; = E[Y Yt] - \mu \mut
628 ;; Var Y = E[Y^2] - \mu^2
631 ;; For initial estimates of covariance of \hat\beta:
633 ;; \hat\beta = (Xt X)^-1 Xt Y
634 ;; with E[ \hat\beta ]
635 ;; = E[ (Xt X)^-1 Xt Y ]
636 ;; = E[(Xt X)^-1 Xt (X\beta)]
639 ;; So Var[\hat\beta] = ...
641 ;; and this gives SE(\beta_i) = (* (sqrt (mref Var i i)) adjustment)
647 (let ((*default-implementation
* :foreign-array
))
653 (rcond (* (coerce (expt 2 -
52) 'double-float
)
654 (max (nrows a
) (ncols a
))))
658 (list x
(gelsy a b rcond
))
659 ;; no applicable conversion?
660 ;; (m- (#<FA-SIMPLE-VECTOR-DOUBLE (10 x 1))
661 ;; (#<FA-SIMPLE-VECTOR-DOUBLE (10 x 1)) )
662 (v- x
(first (gelsy a b rcond
))))))
665 (princ *temp-result
*)
668 (let ((*default-implementation
* :lisp-array
))
674 (rcond (* (coerce (expt 2 -
52) 'double-float
)
675 (max (nrows a
) (ncols a
))))
679 (list x
(gelsy a b rcond
))
680 (m- x
(first (gelsy a b rcond
)))
682 (princ *temp-result
*)
688 :type
:row
;; default, not usually needed!
689 :initial-contents
'((1d0 3d0
2d0
4d0
3d0
5d0
4d0
6d0
))))
695 :initial-contents
'((1d0 2d0
3d0
4d0
5d0
6d0
7d0
8d0
))))
697 ;; so something like (NOTE: matrices are transposed to begin with, hence the incongruety)
698 (defparameter *xtx-1
* (m* *xv
* (transpose *xv
*)))
699 (defparameter *xty-1
* (m* *xv
* (transpose *y
*)))
700 (defparameter *rcond-in
* (* (coerce (expt 2 -
52) 'double-float
)
704 (defparameter *betahat
* (gelsy *xtx-1
* *xty-1
* *rcond-in
*))
706 ;; (#<LA-SIMPLE-VECTOR-DOUBLE (1 x 1)
707 ;; 1.293103448275862>
710 ;; ## Test case in R:
711 ;; x <- c( 1.0, 3.0, 2.0, 4.0, 3.0, 5.0, 4.0, 6.0)
712 ;; y <- c( 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0)
716 ;; lm(formula = y ~ x - 1)
729 (asdf:oos
'asdf
:load-op
'cl-plplot
)
735 (type-of #2A
((1 2 3 4 5)
738 (type-of (rand 10 20))
740 (typep #2A
((1 2 3 4 5)
744 (typep (rand 10 20) 'matrix-like
)
746 (typep #2A
((1 2 3 4 5)
750 (typep (rand 10 20) 'array
)