3 ;;; Time-stamp: <2009-02-17 08:37:49 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 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....
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
34 :test-case
'lisp-stat-unittests
::create-proto
35 :suite
'lisp-stat-unittests
::lisp-stat-ut-proto
))
43 ;; Making data-frames (i.e. cases (rows) by variables (columns))
44 ;; takes a bit of getting used to. For this, it is important to
45 ;; realize that we can do the following:
46 ;; #1 - consider the possibility of having a row, and transposing
47 ;; it, so the list-of-lists is: ((1 2 3 4 5)) (1 row, 5 columns)
48 ;; #2 - naturally list-of-lists: ((1)(2)(3)(4)(5)) (5 rows, 1 column)
49 ;; see src/data/listoflist.lisp for code to process this particular
51 (defparameter *indep-vars-1-matrix
*
52 (transpose (make-matrix 1 (length iron
)
54 (list (mapcar #'(lambda (x) (coerce x
'double-float
))
56 "creating iron into double float, straightforward")
58 (documentation '*indep-vars-1-matrix
* 'variable
)
59 ;; *indep-vars-1-matrix*
62 (defparameter *indep-vars-1a-matrix
*
63 (make-matrix (length iron
) 1
65 (mapcar #'(lambda (x) (list (coerce x
'double-float
)))
67 ;; *indep-vars-1a-matrix*
69 ;; and mathematically, they seem equal:
70 (m= *indep-vars-1-matrix
* *indep-vars-1a-matrix
*) ; => T
71 ;; but of course not completely...
72 (eql *indep-vars-1-matrix
* *indep-vars-1a-matrix
*) ; => NIL
73 (eq *indep-vars-1-matrix
* *indep-vars-1a-matrix
*) ; => NIL
76 (print *indep-vars-1-matrix
*)
77 (print *indep-vars-1a-matrix
*)
79 (documentation 'lisp-matrix
:bind2
'function
) ; by which we mean:
80 (documentation 'bind2
'function
)
81 (bind2 *indep-vars-1-matrix
* *indep-vars-1a-matrix
* :by
:column
) ; 2 col
82 (bind2 *indep-vars-1-matrix
* *indep-vars-1a-matrix
* :by
:row
) ; 1 long col
85 (defparameter *indep-vars-2-matrix
*
86 (transpose (make-matrix 2 (length iron
)
89 (mapcar #'(lambda (x) (coerce x
'double-float
))
91 (mapcar #'(lambda (x) (coerce x
'double-float
))
93 ;; *indep-vars-2-matrix*
96 (defparameter *indep-vars-2-matrix
*
97 (make-matrix (length iron
) 2
99 (mapcar #'(lambda (x y
)
100 (list (coerce x
'double-float
)
101 (coerce y
'double-float
)))
103 ;; *indep-vars-2-matrix*
106 ;; The below FAILS due to coercion issues; it just isn't lispy, it's R'y.
108 (defparameter *dep-var
* (make-vector (length absorbtion
)
109 :initial-contents
(list absorbtion
)))
111 ;; BUT below, this should be the right type.
112 (defparameter *dep-var
*
113 (make-vector (length absorbtion
)
117 (mapcar #'(lambda (x) (coerce x
'double-float
))
122 (defparameter *dep-var-int
*
123 (make-vector (length absorbtion
)
125 :element-type
'integer
126 :initial-contents
(list absorbtion
)))
128 (typep *dep-var
* 'matrix-like
) ; => T
129 (typep *dep-var
* 'vector-like
) ; => T
131 (typep *indep-vars-1-matrix
* 'matrix-like
) ; => T
132 (typep *indep-vars-1-matrix
* 'vector-like
) ; => T
133 (typep *indep-vars-2-matrix
* 'matrix-like
) ; => T
134 (typep *indep-vars-2-matrix
* 'vector-like
) ; => F
137 ;; following fails, need to ensure that we work on list elts, not just
138 ;; elts within a list:
140 ;; (coerce iron 'real)
142 ;; the following is a general list-conversion coercion approach -- is
143 ;; there a more efficient way?
145 ;; (mapcar #'(lambda (x) (coerce x 'double-float)) iron)
147 (princ "Data Set up"))
151 ;; REVIEW: general Lisp use guidance
153 (fdefinition 'make-matrix
)
154 (documentation 'make-matrix
'function
)
156 #| Examples from CLHS
, a bit of guidance.
158 ;; This function assumes its callers have checked the types of the
159 ;; arguments, and authorizes the compiler to build in that assumption.
160 (defun discriminant (a b c
)
161 (declare (number a b c
))
162 "Compute the discriminant for a quadratic equation."
163 (- (* b b
) (* 4 a c
))) => DISCRIMINANT
164 (discriminant 1 2/3 -
2) => 76/9
166 ;; This function assumes its callers have not checked the types of the
167 ;; arguments, and performs explicit type checks before making any assumptions.
168 (defun careful-discriminant (a b c
)
169 "Compute the discriminant for a quadratic equation."
170 (check-type a number
)
171 (check-type b number
)
172 (check-type c number
)
173 (locally (declare (number a b c
))
174 (- (* b b
) (* 4 a c
)))) => CAREFUL-DISCRIMINANT
175 (careful-discriminant 1 2/3 -
2) => 76/9
181 (progn ;; FIXME: Regression modeling
183 ;; data setup in previous FIXME
184 (defparameter *m
* nil
186 ;; need to make vectors and matrices from the lists...
189 (def *m
* (regression-model (list->vector-like iron
)
190 (list->vector-like absorbtion
)))
192 (def m
(regression-model (list->vector-like iron
)
193 (list->vector-like absorbtion
) :print nil
))
197 (send m
:own-methods
)
198 ;; (lsos::ls-objects-methods m) ; bogus?
201 (def m
(regression-model (list->vector-like iron
)
202 (list->vector-like absorbtion
)))
204 (def m
(regression-model (listoflists->matrix-like
(list iron aluminum
))
205 (list->vector-like absorbtion
) :print nil
))
209 (send m
:sweep-matrix
)
210 (format t
"~%~A~%" (send m
:sweep-matrix
))
212 ;; need to get multiple-linear regression working (simple linear regr
213 ;; works)... to do this, we need to redo the whole numeric structure,
214 ;; I'm keeping these in as example of brokenness...
216 (send m
:basis
) ;; this should be positive?
217 (send m
:coef-estimates
) )
220 (progn ;; FIXME: Need to clean up data examples, licenses, attributions, etc.
221 ;; The following breaks because we should use a package to hold
222 ;; configuration details, and this would be the only package outside
223 ;; of packages.lisp, as it holds the overall defsystem structure.
224 (load-data "iris.lsp") ;; (the above partially fixed).
231 ;; FIXME: Data.Frames probably deserve to be related to lists --
232 ;; either lists of cases, or lists of variables. We probably do not
233 ;; want to mix them, but want to be able to convert between such
236 (defparameter *my-case-data
*
240 (:case3 Y High
3.1 4))
241 (:var-names
(list "Response" "Level" "Pressure" "Size"))))
245 (elt *my-case-data
* 1)
246 (elt *my-case-data
* 0)
247 ;;(elt *my-case-data* 2) ;; error
248 (elt (elt *my-case-data
* 0) 1)
249 (elt (elt *my-case-data
* 0) 0)
250 (elt (elt (elt *my-case-data
* 0) 1) 0)
251 (elt (elt (elt *my-case-data
* 0) 1) 1)
252 (elt (elt *my-case-data
* 0) 2))
256 (progn ;; FIXME: read data from CSV file. To do.
258 ;; challenge is to ensure that we get mixed arrays when we want them,
259 ;; and single-type (simple) arrays in other cases.
261 (defparameter *csv-num
* (read-csv "Data/example-num.csv" :type
'numeric
))
262 (defparameter *csv-mix
* (read-csv "Data/example-mixed.csv" :type
'data
))
264 ;; The handling of these types should be compariable to what we do for
265 ;; matrices, but without the numerical processing. i.e. mref, bind2,
266 ;; make-dataframe, and the class structure should be similar.
268 ;; With numerical data, there should be a straightforward mapping from
269 ;; the data.frame to a matrix. With categorical data (including
270 ;; dense categories such as doc-strings, as well as sparse categories
271 ;; such as binary data), we need to include metadata about ordering,
272 ;; coding, and such. So the structures should probably consider
274 ;; Using the CSV file:
276 (asdf:oos
'asdf
:compile-op
'csv
:force t
)
277 (asdf:oos
'asdf
:load-op
'parse-number
)
278 (asdf:oos
'asdf
:load-op
'csv
)
279 (fare-csv:read-csv-file
"Data/example-numeric.csv")
281 ;; but I think the cl-csv package is broken, need to use the dsv-style
284 ;; now we've got the DSV code in the codebase, auto-loaded I hope:
285 cybertiggyr-dsv
:*field-separator
*
286 (defparameter *example-numeric.csv
*
287 (cybertiggyr-dsv:load-escaped
"Data/example-numeric.csv"
288 :field-separator
#\
,))
289 *example-numeric.csv
*
291 ;; the following fails because we've got a bit of string conversion
292 ;; to do. 2 thoughts: #1 modify dsv package, but mucking with
293 ;; encapsulation. #2 add a coercion tool (better, but potentially
295 #+nil
(coerce (nth 3 (nth 3 *example-numeric.csv
*)) 'double-float
)
297 ;; cases, simple to not so
298 (defparameter *test-string1
* "1.2")
299 (defparameter *test-string2
* " 1.2")
300 (defparameter *test-string3
* " 1.2 ")
305 (progn ;; experiments with GSL and the Lisp interface.
306 (asdf:oos
'asdf
:load-op
'gsll
)
307 (asdf:oos
'asdf
:load-op
'gsll-tests
)
309 ;; the following should be equivalent
310 (setf *t1
* (LIST 6.18d0
6.647777777777779d0
6.18d0
))
311 (setf *t2
* (MULTIPLE-VALUE-LIST
313 (gsll:make-marray
'DOUBLE-FLOAT
314 :INITIAL-CONTENTS
'(-3.21d0
1.0d0
12.8d0
)))
316 (gsll:MAKE-MARRAY
'DOUBLE-FLOAT
317 :INITIAL-CONTENTS
'(3.0d0
1.0d0
2.0d0
))))
318 (LET ((MEAN (gsll:MEAN VEC
)))
319 (LIST (gsll:ABSOLUTE-DEVIATION VEC
)
320 (gsll:WEIGHTED-ABSOLUTE-DEVIATION VEC WEIGHTS
)
321 (gsll:ABSOLUTE-DEVIATION VEC MEAN
))))))
324 ;; from (gsll:examples 'gsll::numerical-integration) ...
325 (gsll:integration-qng gsll
::one-sine
0.0d0 PI
)
327 (gsll:defun-single axpb
(x) (+ (* 2 x
) 3)) ;; a<-2, b<-3
328 (gsll:integration-qng axpb
1d0
2d0
)
332 (defun-single axpb2
(x) (+ (* a x
) b
)))
333 (gsll:integration-qng axpb2
1d0
2d0
)
336 ;; (gsll:integration-qng
339 ;; (defun-single axpb2 (x) (+ (* a x) b)))
342 ;; right, but weird expansion...
343 (gsll:integration-qng
346 (defun axpb2 (x) (+ (* a x
) b
))
347 (gsll:def-single-function axpb2
)
351 ;; Linear least squares
353 (gsll:gsl-lookup
"gsl_linalg_LU_decomp") ; => gsll:lu-decomposition
354 (gsll:gsl-lookup
"gsl_linalg_LU_solve") ; => gsll:lu-solve
360 (progn ;; philosophy time
362 (setf my-model
(model :name
"ex1"
363 :data-slots
(list x y z
)
364 :param-slots
(list alpha beta gamma
)
365 :math-form
(regression-model :formula
'(= y
(+ (* beta x
)
369 (setf my-dataset
(statistical-table :table data-frame-contents
370 :metadata
(list (:case-names
(list ))
372 (:documentation
"string of doc"))))
374 (setf my-analysis
(analysis
377 :parameter-map
(pairing (model-param-slots my-model
)
378 (data-var-names my-dataset
))))
380 ;; ontological implications -- the analysis is an abstract class of
381 ;; data, model, and mapping between the model and data. The fit is
382 ;; the instantiation of such. This provides a statistical object
383 ;; computation theory which can be realized as "executable
384 ;; statistics" or "computable statistics".
385 (setf my-analysis
(analyze my-fit
386 :estimation-method
'linear-least-squares-regression
))
388 ;; one of the tricks here is that one needs to provide the structure
389 ;; from which to consider estimation, and more importantly, the
390 ;; validity of the estimation.
393 (setf linear-least-squares-regression
394 (estimation-method-definition
395 :variable-defintions
((list
396 ;; from MachLearn: supervised,
398 :data-response-vars list-drv
; nil if unsup
401 :data-predictor-vars list-dpv
402 ;; nil in this case. these
403 ;; describe "out-of-box" specs
404 :hyper-vars list-hv
))
405 :form
'(regression-additive-error
406 :central-form
(linear-form drv pv dpv
)
407 :error-form
'normal-error
)
408 :resulting-decision
'(point-estimation interval-estimation
)
409 :philosophy
'frequentist
410 :documentation
"use least squares to fit a linear regression
413 (defparameter *statistical-philosophies
*
414 '(frequentist bayesian fiducial decision-analysis
)
415 "can be combined to build decision-making approaches and
418 (defparameter *decisions
*
419 '(estimation selection testing
)
420 "possible results from a...")
421 ;; is this really true? One can embedded hypothesis testing within
422 ;; estimation, as the hypothesis estimated to select. And
423 ;; categorical/continuous rear their ugly heads, but not really in
426 (defparameter *ontology-of-decision-procedures
*
430 (list :maximum-likelihood
435 (list :maximum-likelihood
441 :bioequivalence-inversion
)
446 :partially-parametric
))
447 "start of ontology"))
458 :initial-contents
'((1d0 2d0
3d0
4d0
5d0
6d0
7d0
8d0
))))
464 :initial-contents
'((1d0 1d0
)
473 (defparameter *xv
+1a
*
476 :initial-contents
#2A
((1d0 1d0
)
485 (defparameter *xv
+1b
*
490 :initial-contents
'((1d0)
500 (m= *xv
+1a
* *xv
+1b
*) ; => T
502 ;; so something like (NOTE: matrices are transposed to begin with, hence the incongruety)
503 (defparameter *xtx-2
* (m* (transpose *xv
+1*) *xv
+1*))
504 ;; #<LA-SIMPLE-MATRIX-DOUBLE 2 x 2
508 (defparameter *xty-2
* (m* (transpose *xv
+1*) (transpose *y
*)))
509 ;; #<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
513 (defparameter *rcond-2
* 0.000001)
514 (defparameter *betahat-2
* (gelsy *xtx-2
* *xty-2
* *rcond-2
*))
515 ;; *xtx-2* => "details of complete orthogonal factorization"
516 ;; according to man page:
517 ;; #<LA-SIMPLE-MATRIX-DOUBLE 2 x 2
518 ;; -119.33147112141039d0 -29.095426104883202d0
519 ;; 0.7873402682880205d0 -1.20672274167718d0>
521 ;; *xty-2* => output becomes solution:
522 ;; #<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
523 ;; -0.16666666666668312d0
524 ;; 1.333333333333337d0>
526 *betahat-2
* ; which matches R, see below
528 (documentation 'gelsy
'function
)
531 ;; (#<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
532 ;; -0.16666666666668312 1.333333333333337>
535 ;; ## Test case in R:
536 ;; x <- c( 1.0, 3.0, 2.0, 4.0, 3.0, 5.0, 4.0, 6.0)
537 ;; y <- c( 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0)
539 ;; ## => Call: lm(formula = y ~ x)
541 ;; Coefficients: (Intercept) x
548 ;; lm(formula = y ~ x)
551 ;; Min 1Q Median 3Q Max
552 ;; -1.833e+00 -6.667e-01 -3.886e-16 6.667e-01 1.833e+00
555 ;; Estimate Std. Error t value Pr(>|t|)
556 ;; (Intercept) -0.1667 1.1587 -0.144 0.89034
557 ;; x 1.3333 0.3043 4.382 0.00466 **
559 ;; Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
561 ;; Residual standard error: 1.291 on 6 degrees of freedom
562 ;; Multiple R-squared: 0.7619, Adjusted R-squared: 0.7222
563 ;; F-statistic: 19.2 on 1 and 6 DF, p-value: 0.004659
567 ;; which suggests one might do (modulo ensuring correct
568 ;; orientations). When this is finalized, it should migrate to
573 (defparameter *n
* 20) ; # rows = # obsns
574 (defparameter *p
* 10) ; # cols = # vars
575 (defparameter *x-temp
* (rand *n
* *p
*))
576 (defparameter *b-temp
* (rand *p
* 1))
577 (defparameter *y-temp
* (m* *x-temp
* *b-temp
*))
579 (defparameter *rcond
* (* (coerce (expt 2 -
52) 'double-float
)
580 (max (nrows *x-temp
*) (ncols *y-temp
*))))
581 (defparameter *orig-x
* (copy *x-temp
*))
582 (defparameter *orig-b
* (copy *b-temp
*))
583 (defparameter *orig-y
* (copy *y-temp
*))
585 (defparameter *lm-result
* (lm *x-temp
* *y-temp
*))
586 (princ (first *lm-result
*))
587 (princ (second *lm-result
*))
588 (princ (third *lm-result
*))
589 (v= (third *lm-result
*)
590 (v- (first (first *lm-result
*))
591 (first (second *lm-result
*)))))