add docs and make a bit more CL'spy, using sequence.
[CommonLispStat.git] / model.lisp
blob85e90c8868be7f86470bf4181ade48937b16825c
1 ;;; -*- mode: lisp -*-
2 ;;; Copyright (c) 2005--2007, by A.J. Rossini <blindglobe@gmail.com>
3 ;;; See COPYRIGHT file for any additional restrictions (BSD license).
4 ;;; Since 1991, ANSI was finally finished. Edited for ANSI Common Lisp.
6 ;;; File: model.lisp
7 ;;; Author: AJ Rossini <blindglobe@gmail.com>
8 ;;; Copyright: (c)2007, AJ Rossini. BSD, LLGPL, or GPLv2, depending
9 ;;; on how it arrives.
10 ;;; Purpose: models as a data summarization tools.
11 ;;; Time-stamp: <2006-05-19 12:33:41 rossini>
12 ;;; Creation: <2006-05-17 21:34:07 rossini>
14 ;;; What is this talk of 'release'? Klingons do not make software
15 ;;; 'releases'. Our software 'escapes', leaving a bloody trail of
16 ;;; designers and quality assurance people in its wake.
18 ;;; Work towards an object system with a comprehensive theory of data
19 ;;; summarization through models. The basic idea is that models are
20 ;;; used to summarize different aspects of a data generating process,
21 ;;; possibly realized by a dataset.
23 (in-package :cl-user)
25 (defpackage :lisp-stat-model
26 (:documentation "Model management and other mathematical technologies.")
27 (:nicknames :ls-data)
28 (:use :common-lisp
29 :lisp-stat-config
30 :lisp-stat-object-system
31 :lisp-stat-types
32 :lisp-stat-compound-data
33 :lisp-stat-matrix
34 :lisp-stat-linalg)
35 (:shadowing-import-from :lisp-stat-object-system
36 slot-value call-method call-next-method)
37 (:export model))
39 (in-package :lisp-stat-model)
41 (defclass model ()
42 ((name :initform nil
43 :initarg :name
44 :accessor name
45 :reader model-name)
46 (form :initform nil
47 :initarg :formula
48 :accessor form
49 :reader model-formula)
50 (parameter-vars :initform nil
51 :initarg :parameter-vars
52 :accessor param-vars
53 :reader model-formula)
54 (data-vars :initform nil
55 :initarg :data-vars
56 :accessor data-vars
57 :reader model-formula)
58 (fixed-vars :initform nil
59 :initarg :fixed-vars
60 :accessor fixed-vars
61 :reader model-formula)
62 (solution :initform nil
63 :initarg :criteriaFunction
64 :accessor critFcn-vars
65 :reader model-formula)
67 (done-solution? :initform nil :reader done-setup?)
68 (prototypes-initialized? :initform nil :reader prototypes-initialized?)
69 (current-values :initform nil :accessor current-values)
71 (log-file :initform nil :initarg :log-file :reader log-file)
72 (test-data :initform nil :accessor test-data)
73 (expected-failure-p :initform nil :initarg :expected-failure-p
74 :reader expected-failure-p)
75 (expected-error-p :initform nil :initarg :expected-error-p
76 :reader expected-error-p)
77 (expected-problem-p :initform nil :initarg :expected-problem-p
78 :reader expected-problem-p))
79 (:documentation "Mathematical Model"))
81 (defclass result (model)
82 ((param-values )
83 (param-uncertainity )
84 (param-characterization ))
86 ;; The following are types of models -- in particular, we can consider
87 ;; that these models
89 (defclass statistical-model (model) )
91 (defclass ode-model (model ))
93 (defclass linear-regression-model (statistical-mode))
95 (defclass generalized-linear-regression-model (statistical-model))
97 (defclass nonlinear-linear-regression-model (statistical-model))
102 ;;; garbage follows
105 ;; modelType
107 root
108 opt (min / max)
110 diffeqn
112 diffeqn + solve
114 ;; dataType
116 varExtract
117 tableExtract
118 relationExtract
121 ;; mappingType
123 (setf myFirstModel
124 (defineModel normalLikelihood '((* (/ 1 (sqrt (* 2 (pi) sigma)))
125 (exp (/ (- x mu)
126 sigma))))
127 :fixed '()
128 :param '(mu sigma)
129 :data '(x)
130 :critFcnSoln '(:max (one-of bfgs nelder-mead conjugate-gradient))))
132 (defclass model ()
133 ((name )
134 (vars-fixed :initarg nil :arg fixed)
135 (vars-param :initarg nil :arg param)
136 (vars-data :initarg nil :arg data)
137 (critFcn ))
144 (defclass meanModel (model) ... ) ;; a macro to map onto Model
145 (defclass meanVarModel (model) ... )
146 (defclass regressionModel (meanModel) )
147 (defclass mixedModel (regressionModel) ... )
148 (defclass bayesianModel (model) ... )
149 (defclass diffintgleqnModel (model) ) ;;(ODE, PDF, integral equations)
153 (setf modX
154 (solveModel :model myFirstModel
155 :data myVar
156 :mapping '((x . myVar))))
158 ;; result structure
160 (:solution
161 :params
162 :params-characterisation
163 :paradigm '(bayesian frequentist)
164 :dataname
165 :modelname )
168 (with-mapping map :model mymod :data mydata
169 (bootstrap mymod mydata))
172 ;; solution should inherit from model and data (and be recomputable)
173 ;; func args override embedded args
175 ;; (solveModel firstSoln) "=" firstSoln
177 ;; unless stoch approx used.
181 (defclass