3 ;;; Time-stamp: <2009-03-11 06:12:57 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 (defparameter *indep-vars-2-matrix
*
44 (make-matrix (length iron
) 2
46 (mapcar #'(lambda (x y
)
47 (list (coerce x
'double-float
)
48 (coerce y
'double-float
)))
52 (defparameter *dep-var
*
53 (make-vector (length absorbtion
)
57 (mapcar #'(lambda (x) (coerce x
'double-float
))
60 (defparameter *dep-var-int
*
61 (make-vector (length absorbtion
)
63 :element-type
'integer
64 :initial-contents
(list absorbtion
)))
70 :initial-contents
#2A
((1d0 1d0
)
84 :initial-contents
'((1d0)
94 (m= *xv
+1a
* *xv
+1b
*) ; => T
96 (princ "Data Set up"))
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
130 (progn ;; FIXME: Regression modeling
132 ;; data setup in previous FIXME
133 (defparameter *m
* nil
135 ;; need to make vectors and matrices from the lists...
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
))
146 (send m
:own-methods
)
147 ;; (lsos::ls-objects-methods m) ; bogus?
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
))
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
) )
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).
180 ;; FIXME: Barf'd version of Data.Frames. See data/data-clos.lisp
181 ;; for better philosophy. For example, we could say that they
182 ;; probably deserve to be related to lists -- either lists of cases,
183 ;; or lists of variables. We probably do not want to mix them, but
184 ;; want to be able to convert between such structures.
186 (defparameter *my-case-data
*
190 (:case3 Y High
3.1 4))
191 (:var-names
(list "Response" "Level" "Pressure" "Size"))))
195 (elt *my-case-data
* 1)
196 (elt *my-case-data
* 0)
197 ;;(elt *my-case-data* 2) ;; error
198 (elt (elt *my-case-data
* 0) 1)
199 (elt (elt *my-case-data
* 0) 0)
200 (elt (elt (elt *my-case-data
* 0) 1) 0)
201 (elt (elt (elt *my-case-data
* 0) 1) 1)
202 (elt (elt *my-case-data
* 0) 2))
206 (progn ;; FIXME: read data from CSV file. To do.
209 ;; challenge is to ensure that we get mixed arrays when we want them,
210 ;; and single-type (simple) arrays in other cases.
213 (defparameter *csv-num
*
214 (cybertiggyr-dsv::load-escaped
215 #p
"/media/disk/Desktop/sandbox/CLS.git/Data/example-numeric.csv"
219 (nth 0 (nth 0 *csv-num
*))
221 (defparameter *csv-num
*
222 (cybertiggyr-dsv::load-escaped
223 #p
"/media/disk/Desktop/sandbox/CLS.git/Data/example-numeric2.dsv"
224 :field-separator
#\
:))
226 (nth 0 (nth 0 *csv-num
*))
229 ;; The handling of these types should be compariable to what we do for
230 ;; matrices, but without the numerical processing. i.e. mref, bind2,
231 ;; make-dataframe, and the class structure should be similar.
233 ;; With numerical data, there should be a straightforward mapping from
234 ;; the data.frame to a matrix. With categorical data (including
235 ;; dense categories such as doc-strings, as well as sparse categories
236 ;; such as binary data), we need to include metadata about ordering,
237 ;; coding, and such. So the structures should probably consider
239 ;; Using the CSV file:
241 (asdf:oos
'asdf
:compile-op
'csv
:force t
)
242 (asdf:oos
'asdf
:load-op
'parse-number
)
243 (asdf:oos
'asdf
:load-op
'csv
)
244 (fare-csv:read-csv-file
"Data/example-numeric.csv")
246 ;; but I think the cl-csv package is broken, need to use the dsv-style
249 ;; now we've got the DSV code in the codebase, auto-loaded I hope:
250 cybertiggyr-dsv
:*field-separator
*
251 (defparameter *example-numeric.csv
*
252 (cybertiggyr-dsv:load-escaped
"Data/example-numeric.csv"
253 :field-separator
#\
,))
254 *example-numeric.csv
*
256 ;; the following fails because we've got a bit of string conversion
257 ;; to do. 2 thoughts: #1 modify dsv package, but mucking with
258 ;; encapsulation. #2 add a coercion tool (better, but potentially
260 #+nil
(coerce (nth 3 (nth 3 *example-numeric.csv
*)) 'double-float
)
262 ;; cases, simple to not so
263 (defparameter *test-string1
* "1.2")
264 (defparameter *test-string2
* " 1.2")
265 (defparameter *test-string3
* " 1.2 ")
270 (progn ;; experiments with GSL and the Lisp interface.
271 (asdf:oos
'asdf
:load-op
'gsll
)
272 (asdf:oos
'asdf
:load-op
'gsll-tests
)
274 ;; the following should be equivalent
275 (setf *t1
* (LIST 6.18d0
6.647777777777779d0
6.18d0
))
276 (setf *t2
* (MULTIPLE-VALUE-LIST
278 (gsll:make-marray
'DOUBLE-FLOAT
279 :INITIAL-CONTENTS
'(-3.21d0
1.0d0
12.8d0
)))
281 (gsll:MAKE-MARRAY
'DOUBLE-FLOAT
282 :INITIAL-CONTENTS
'(3.0d0
1.0d0
2.0d0
))))
283 (LET ((MEAN (gsll:MEAN VEC
)))
284 (LIST (gsll:ABSOLUTE-DEVIATION VEC
)
285 (gsll:WEIGHTED-ABSOLUTE-DEVIATION VEC WEIGHTS
)
286 (gsll:ABSOLUTE-DEVIATION VEC MEAN
))))))
289 ;; from (gsll:examples 'gsll::numerical-integration) ...
290 (gsll:integration-qng gsll
::one-sine
0.0d0 PI
)
292 (gsll:defun-single axpb
(x) (+ (* 2 x
) 3)) ;; a<-2, b<-3
293 (gsll:integration-qng axpb
1d0
2d0
)
297 (defun-single axpb2
(x) (+ (* a x
) b
)))
298 (gsll:integration-qng axpb2
1d0
2d0
)
301 ;; (gsll:integration-qng
304 ;; (defun-single axpb2 (x) (+ (* a x) b)))
307 ;; right, but weird expansion...
308 (gsll:integration-qng
311 (defun axpb2 (x) (+ (* a x
) b
))
312 (gsll:def-single-function axpb2
)
316 ;; Linear least squares
318 (gsll:gsl-lookup
"gsl_linalg_LU_decomp") ; => gsll:lu-decomposition
319 (gsll:gsl-lookup
"gsl_linalg_LU_solve") ; => gsll:lu-solve
325 (progn ;; philosophy time
327 (setf my-model
(model :name
"ex1"
328 :data-slots
(list w x y z
)
329 :param-slots
(list alpha beta gamma
)
330 :math-form
(regression-model :formula
'(= w
(+ (* beta x
)
334 :centrality
'median
; 'mean
341 (setf my-dataset
(statistical-table :table data-frame-contents
342 :metadata
(list (:case-names
(list ))
344 (:documentation
"string of doc"))))
346 (setf my-analysis
(analysis
349 :parameter-map
(pairing (model-param-slots my-model
)
350 (data-var-names my-dataset
))))
352 ;; ontological implications -- the analysis is an abstract class of
353 ;; data, model, and mapping between the model and data. The fit is
354 ;; the instantiation of such. This provides a statistical object
355 ;; computation theory which can be realized as "executable
356 ;; statistics" or "computable statistics".
357 (setf my-analysis
(analyze my-fit
358 :estimation-method
'linear-least-squares-regression
))
360 ;; one of the tricks here is that one needs to provide the structure
361 ;; from which to consider estimation, and more importantly, the
362 ;; validity of the estimation.
365 (setf linear-least-squares-regression
366 (estimation-method-definition
367 :variable-defintions
((list
368 ;; from MachLearn: supervised,
370 :data-response-vars list-drv
; nil if unsup
373 :data-predictor-vars list-dpv
374 ;; nil in this case. these
375 ;; describe "out-of-box" specs
376 :hyper-vars list-hv
))
377 :form
'(regression-additive-error
378 :central-form
(linear-form drv pv dpv
)
379 :error-form
'normal-error
)
380 :resulting-decision
'(point-estimation interval-estimation
)
381 :philosophy
'frequentist
382 :documentation
"use least squares to fit a linear regression
385 (defparameter *statistical-philosophies
*
386 '(frequentist bayesian fiducial decision-analysis
)
387 "can be combined to build decision-making approaches and
390 (defparameter *decisions
*
391 '(estimation selection testing
)
392 "possible results from a...")
393 ;; is this really true? One can embedded hypothesis testing within
394 ;; estimation, as the hypothesis estimated to select. And
395 ;; categorical/continuous rear their ugly heads, but not really in
398 (defparameter *ontology-of-decision-procedures
*
402 (list :maximum-likelihood
407 (list :maximum-likelihood
413 :bioequivalence-inversion
)
418 :partially-parametric
))
419 "start of ontology"))
430 :initial-contents
'((1d0 2d0
3d0
4d0
5d0
6d0
7d0
8d0
))))
436 :initial-contents
'((1d0 1d0
)
446 ;; so something like (NOTE: matrices are transposed to begin with, hence the incongruety)
447 (defparameter *xtx-2
* (m* (transpose *xv
+1*) *xv
+1*))
448 ;; #<LA-SIMPLE-MATRIX-DOUBLE 2 x 2
452 (defparameter *xty-2
* (m* (transpose *xv
+1*) (transpose *y
*)))
453 ;; #<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
457 (defparameter *rcond-2
* 0.000001)
458 (defparameter *betahat-2
* (gelsy *xtx-2
* *xty-2
* *rcond-2
*))
459 ;; *xtx-2* => "details of complete orthogonal factorization"
460 ;; according to man page:
461 ;; #<LA-SIMPLE-MATRIX-DOUBLE 2 x 2
462 ;; -119.33147112141039d0 -29.095426104883202d0
463 ;; 0.7873402682880205d0 -1.20672274167718d0>
465 ;; *xty-2* => output becomes solution:
466 ;; #<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
467 ;; -0.16666666666668312d0
468 ;; 1.333333333333337d0>
470 *betahat-2
* ; which matches R, see below
472 (documentation 'gelsy
'function
)
475 ;; (#<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
476 ;; -0.16666666666668312 1.333333333333337>
479 ;; ## Test case in R:
480 ;; x <- c( 1.0, 3.0, 2.0, 4.0, 3.0, 5.0, 4.0, 6.0)
481 ;; y <- c( 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0)
483 ;; ## => Call: lm(formula = y ~ x)
485 ;; Coefficients: (Intercept) x
492 ;; lm(formula = y ~ x)
495 ;; Min 1Q Median 3Q Max
496 ;; -1.833e+00 -6.667e-01 -3.886e-16 6.667e-01 1.833e+00
499 ;; Estimate Std. Error t value Pr(>|t|)
500 ;; (Intercept) -0.1667 1.1587 -0.144 0.89034
501 ;; x 1.3333 0.3043 4.382 0.00466 **
503 ;; Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
505 ;; Residual standard error: 1.291 on 6 degrees of freedom
506 ;; Multiple R-squared: 0.7619, Adjusted R-squared: 0.7222
507 ;; F-statistic: 19.2 on 1 and 6 DF, p-value: 0.004659
511 ;; which suggests one might do (modulo ensuring correct
512 ;; orientations). When this is finalized, it should migrate to
517 (defparameter *n
* 20) ; # rows = # obsns
518 (defparameter *p
* 10) ; # cols = # vars
519 (defparameter *x-temp
* (rand *n
* *p
*))
520 (defparameter *b-temp
* (rand *p
* 1))
521 (defparameter *y-temp
* (m* *x-temp
* *b-temp
*))
523 (defparameter *rcond
* (* (coerce (expt 2 -
52) 'double-float
)
524 (max (nrows *x-temp
*) (ncols *y-temp
*))))
525 (defparameter *orig-x
* (copy *x-temp
*))
526 (defparameter *orig-b
* (copy *b-temp
*))
527 (defparameter *orig-y
* (copy *y-temp
*))
529 (defparameter *lm-result
* (lm *x-temp
* *y-temp
*))
530 (princ (first *lm-result
*))
531 (princ (second *lm-result
*))
532 (princ (third *lm-result
*))
533 (v= (third *lm-result
*)
534 (v- (first (first *lm-result
*))
535 (first (second *lm-result
*)))))