lisp-matrix verson of regression (API change). Some fixes made, some to do.
[CommonLispStat.git] / src / stat-models / regression-clos.lisp
blobce73ed5a828df0479d3bca5fd37262873e8c6f59
1 ;;; -*- mode: lisp -*-
3 ;;; Time-stamp: <2009-01-31 14:20:14 tony>
4 ;;; Creation: <2008-10-03 02:07:10 tony>
5 ;;; Author: AJ Rossini <blindglobe@gmail.com>
6 ;;; Copyright: (c)2007, AJ Rossini. BSD, LLGPL, or GPLv2, depending
7 ;;; on how it arrives.
8 ;;; Purpose: redoing regression in a CLOS based framework. See
9 ;;; regression.lsp for basis of work.
11 ;;; What is this talk of 'release'? Klingons do not make software
12 ;;; 'releases'. Our software 'escapes', leaving a bloody trail of
13 ;;; designers and quality assurance people in its wake.
15 ;;; This organization and structure is new to the 21st Century
16 ;;; version.
18 (in-package :lisp-stat-regression-linear-clos)
20 ;;; Regresion Model CLOS, data structures
22 (defclass regression-model-clos (statistical-model)
23 ((x :initform nil :initarg :x :accessor x)
24 (y :initform nil :initarg :y :accessor y)
25 (included :initform nil :initarg :y :accessor y)
26 (total-sum-of-squares :initform nil :initarg :y :accessor y)
27 (residual-sum-of-squares :initform nil :initarg :y :accessor y)
28 (predictor-names :initform nil :initarg :y :accessor y)
29 (response-name :initform nil :initarg :y :accessor y)
30 (case-labels :initform nil :initarg :y :accessor y)
31 (needs-computing :initform T :initarg :compute? :accessor compute?))
32 (:documentation "Normal Linear Regression Model through CLOS.
33 Historical design based on what was done for LispStat, not modern."))
35 (defclass model-specification ()
36 ((spec-string :initform nil
37 :initarg :specification
38 :accessor :specification)
39 (spec-form :initform nil
40 :initarg :spec-form
41 :accessor :spec-form)
42 (model-class :initform nil))
43 (:documentation "container for mathematical structure"))
45 (defclass bayesian-model-specification (model-specification)
46 ((prior-model-class)
47 (spec-string :initform nil
48 :initarg :specification
49 :accessor :specification)
50 (spec-form :initform nil
51 :initarg :spec-form
52 :accessor :spec-form))
53 (:documentation "adds structure holding priors to the model"))
55 ;;; The following should be self-created based on introspection of
56 ;;; available:
57 ;;; ## inferential technologies (bayesian, frequentist, etc),
58 ;;; ## optimization criteria (likelihood, least-squares, min-entropy,
59 ;;; minimax, etc)
60 ;;; ## simplification macros, i.e. mapping directly to linear
61 ;;; regression and other applications. fast specialized
62 ;;; algorithms for edge cases and narrow conditions.
63 ;;; ##
65 (defparameter *model-class-list*
66 '((linear-regression frequentist)
67 (generalized-linear-regression parametric)
68 (linear-regression bayesian)
69 ()))
73 ;;; Regression model generics and methods
75 (defgeneric regression-model (model-spec data-pointer &key debug)
76 (:documentation "CLOSy framework for regression, using numerics from "))
78 (defmethod regression-model
79 ((regr-inst regression-model-clos)
80 (data-ptr data-pointer)
81 &key debug)
82 "Args: (regr-inst regressino-model-clos)
84 Returns a fitted regression model object. To examine the model further
85 assign the result to a variable and send it messages. Example (data
86 are in file absorbtion.lsp in the sample data directory/folder):
88 (def fit-m (fit (new 'regression-model-clos (list iron aluminum) absorbtion)))
89 (print fit-m)
90 (plot fit-m :feature 'residuals)
92 (let ((x (get-variable-matrix (x regr-inst) data-ptr))
93 (y (get-variable-vector (y regr-inst) data-ptr)))
97 ;;;;; More mischief from a different time
100 ;; regression-model is the old API, but regression as a generic will
101 ;; be the new API. We need to distinguish between APIs which enable
102 ;; the user to do clear activities, and APIs which enable developers
103 ;; to do clear extensions and development, and underlying
104 ;; infrastructure to keep everything straight and enabled.
106 ;; There are conflicting theories for how to structure the
107 ;; specification of mathematical models, along with the statistical
108 ;; inference, along with the data which is instantiating the model.
110 ;; i.e.: mathematical model for the relationships between components,
111 ;; between a component and a summarizing parameter, and between
112 ;; parameters.
114 ;; statistical inference describes the general approach for
115 ;; aggregating into a decision and has impliciations for the scale up
116 ;; from the model on a single instance to the generalization.
118 ;; The data represents the particular substantive context that is
119 ;; driving the model/inference combination, and about which we hope to
120 ;; generate knowledge.
122 ;; numerical analysis selects appropriate algorithms/implementations
123 ;; for combining the above 3.
125 ;; the end result is input on the decision being made (which could be
126 ;; specific (decision analysis/testing), risk-analysis (interval
127 ;; estimation) , most likely/appropriate selection (point estimation)
131 (defclass model ()
132 ((type structure)))
134 (defgeneric regression ;; assumes x/y from lisp-matrix -- start of a set of generics.
135 (model dataset)
136 "Args: (x y &key (intercept T) (print T) (weights nil)
137 included predictor-names response-name case-labels)
138 X - list of independent variables or X matrix
139 Y - dependent variable.
140 INTERCEPT - T to include (default), NIL for no intercept
141 PRINT - if not NIL print summary information
142 WEIGHTS - if supplied should be the same length as Y; error
143 variances are
144 assumed to be inversely proportional to WEIGHTS
145 PREDICTOR-NAMES, RESPONSE-NAME, CASE-LABELS
146 - sequences of strings or symbols.
147 INCLUDED - if supplied should be the same length as Y, with
148 elements nil to skip a in computing estimates (but not
149 in residual analysis).
150 Returns a regression model object. To examine the model further assign the
151 result to a variable and send it messages.
152 Example (data are in file absorbtion.lsp in the sample data directory):
153 (def m (regression-model (list iron aluminum) absorbtion))
154 (send m :help) (send m :plot-residuals)"
155 (let ((m (send regression-model-proto :new)))
156 (format t "~%")
157 (send m :doc doc)
158 (send m :x x)
159 (send m :y y)
160 (send m :intercept intercept)
161 (send m :weights weights)
162 (send m :included included)
163 (send m :predictor-names predictor-names)
164 (send m :response-name response-name)
165 (send m :case-labels case-labels)
166 (if debug
167 (progn
168 (format t "~%")
169 (format t "~S~%" (send m :doc))
170 (format t "X: ~S~%" (send m :x))
171 (format t "Y: ~S~%" (send m :y))))
172 (if print (send m :display))