From 59ec94061ee7495c02218327bae05c9e7bd7b425 Mon Sep 17 00:00:00 2001 From: AJ Rossini Date: Sun, 12 Apr 2009 13:12:36 +0200 Subject: [PATCH] refactoring model code --- src/stat-models/model-fit.lisp | 103 -------------- src/stat-models/model.lisp | 296 ++++++++++++++++++++++++++++++++--------- 2 files changed, 231 insertions(+), 168 deletions(-) delete mode 100644 src/stat-models/model-fit.lisp diff --git a/src/stat-models/model-fit.lisp b/src/stat-models/model-fit.lisp deleted file mode 100644 index 8ec3880..0000000 --- a/src/stat-models/model-fit.lisp +++ /dev/null @@ -1,103 +0,0 @@ -;;; -*- mode: lisp -*- -;;; Copyright (c) 2005--2007, by A.J. Rossini -;;; See COPYRIGHT file for any additional restrictions (BSD license). -;;; Since 1991, ANSI was finally finished. Edited for ANSI Common Lisp. - -;;; File: model-fit.lisp -;;; Author: AJ Rossini -;;; Copyright: (c)2007, AJ Rossini. BSD, LLGPL, or GPLv2, depending -;;; on how it arrives. -;;; Purpose: models as a data summarization tools. -;;; Time-stamp: <2006-05-19 12:33:41 rossini> -;;; Creation: <2006-05-17 21:34:07 rossini> - -;;; What is this talk of 'release'? Klingons do not make software -;;; 'releases'. Our software 'escapes', leaving a bloody trail of -;;; designers and quality assurance people in its wake. - -;;; The model class is used to map a partial or complete model -;;; specification (partial specification made complete by assumptions -;;; inherent in the type of model used, i.e. linear regression assumes -;;; mean-zero and homoskedasticity) and the model-fit class is used to -;;; complete the model specification into how the model instance and -;;; type will then get computed. The approach tken is that we -;;; definitiely want to be able to see how we can explicitly try to -;;; characterize the work that we are trying to infere about the data. - -(in-package :cl-user) - -(defpackage :lisp-stat-model-fit - (:documentation "Model fitting theory.") - (:nicknames :ls-model-fit) - (:use :common-lisp - :lisp-stat-config - :lisp-stat-object-system - :lisp-stat-types - :lisp-stat-compound-data -#| - :lisp-stat-matrix - :lisp-stat-linalg -|# - ) - (:shadowing-import-from :lisp-stat-object-system - slot-value call-method call-next-method) - (:export fit)) - -(in-package :lisp-stat-model) - -(defclass fit () - ((criteria-functin-name :initform nil - :initarg :name - :accessor name - :reader model-name) - (criteria-function :initform nil - :initarg :formula - :accessor function - :reader fit-criteria) - (parameter-vars :initform nil - :initarg :parameter-vars - :accessor param-vars - :reader model-formula) - (data-vars :initform nil - :initarg :data-vars - :accessor data-vars - :reader model-formula) - (fixed-vars :initform nil - :initarg :fixed-vars - :accessor fixed-vars - :reader model-formula) - - (:documentation "Mathematical Model Fit approach")) - -(defclass optimization (model-fit)) - - -(defclass least-squares (optimization)) -(defclass weighted-least-squares (least-squares)) - -(defclass maximum-likelihood (optimization)) -(defclass minimax (optimization)) -(defclass maximin (optimization)) - -(defclass minimum-entropy (optimization)) -(defclass lq-norm (optimization)) - -(defclass root-finding (model-fit)) - -(defclass method-of-moments (root-finding)) -(defclass marginal-models (method-of-moments)) -(defclass gee (marginal-models)) -(defclass gee2 (marginal-models)) - - - -;;; How would this be used? - -(setf my-regr-model-1 - (new 'least-squares '(- y (+ (* beta1 x1) (* beta2 x2))))) - -;; and there should be an approach which could provide a mapping to this, i.e. - -(regression-model (list y) (list x1 x2)) - -;; could map to the above via macros. diff --git a/src/stat-models/model.lisp b/src/stat-models/model.lisp index df52c40..4e6bfab 100644 --- a/src/stat-models/model.lisp +++ b/src/stat-models/model.lisp @@ -1,74 +1,61 @@ ;;; -*- mode: lisp -*- -;;; Copyright (c) 2005--2007, by A.J. Rossini -;;; See COPYRIGHT file for any additional restrictions (BSD license). -;;; Since 1991, ANSI was finally finished. Edited for ANSI Common Lisp. ;;; File: model.lisp +;;; Time-stamp: <2009-04-12 12:09:16 tony> +;;; Creation: <2006-05-17 21:34:07 rossini> ;;; Author: AJ Rossini -;;; Copyright: (c)2007, AJ Rossini. BSD, LLGPL, or GPLv2, depending +;;; Copyright: (c)2007-- , AJ Rossini. BSD, LLGPL, or GPLv2, depending ;;; on how it arrives. -;;; Purpose: models as a data summarization tools. -;;; Time-stamp: <2006-05-19 12:33:41 rossini> -;;; Creation: <2006-05-17 21:34:07 rossini> +;;; Purpose: models as a data summarization tools. Work towards an +;;; object system with a comprehensive theory of data +;;; summarization through models. The basic idea is that +;;; models are used to summarize different aspects of a +;;; data generating process, possibly realized by a +;;; dataset. ;;; What is this talk of 'release'? Klingons do not make software ;;; 'releases'. Our software 'escapes', leaving a bloody trail of ;;; designers and quality assurance people in its wake. -;;; Work towards an object system with a comprehensive theory of data -;;; summarization through models. The basic idea is that models are -;;; used to summarize different aspects of a data generating process, -;;; possibly realized by a dataset. - -(in-package :cl-user) - -(defpackage :lisp-stat-model - (:documentation "Model management and other mathematical technologies.") - (:nicknames :ls-data) - (:use :common-lisp - :lisp-stat-config - :lisp-stat-object-system - :lisp-stat-types - :lisp-stat-compound-data -#| - :lisp-stat-matrix - :lisp-stat-linalg -|# - ) - (:shadowing-import-from :lisp-stat-object-system - slot-value call-method call-next-method) - (:export model)) - (in-package :lisp-stat-model) (defclass model () - ((name :initform nil - :initarg :name - :accessor name - :reader model-name) - (form :initform nil - :initarg :formula - :accessor form - :reader model-formula) - (parameter-vars :initform nil - :initarg :parameter-vars - :accessor param-vars - :reader model-formula) - (data-vars :initform nil - :initarg :data-vars - :accessor data-vars - :reader model-formula) - (fixed-vars :initform nil - :initarg :fixed-vars - :accessor fixed-vars - :reader model-formula) + ((name + :initform nil + :initarg :name + :accessor name + :reader model-name + :type string) + (form + :initform nil + :initarg :formula + :accessor form + :reader model-formula + :type list) + ;; The following might not be all part of the model?!? + (parameter-vars + :initform nil + :initarg :parameter-vars + :accessor param-vars + :reader model-formula + :type list) + (data-vars + :initform nil + :initarg :data-vars + :accessor data-vars + :reader model-formula) + (fixed-vars + :initform nil + :initarg :fixed-vars + :accessor fixed-vars + :reader model-formula) + (solution :initform nil :initarg :criteriaFunction :accessor critFcn-vars :reader model-formula) - (done-solution? :initform nil :reader done-setup?) - (prototypes-initialized? :initform nil :reader prototypes-initialized?) + (current-values :initform nil :accessor current-values) (log-file :initform nil :initarg :log-file :reader log-file) @@ -81,7 +68,7 @@ :reader expected-problem-p)) (:documentation "Mathematical Model")) -(defclass result (model) +(defclass result () ((param-values ) (param-uncertainity ) (param-characterization )) @@ -89,9 +76,9 @@ ;; The following are types of models -- in particular, we can consider ;; that these models -(defclass statistical-model (model) ) +(defclass statistical-model (model result) ) -(defclass ode-model (model )) +(defclass ode-model (model result )) (defclass linear-regression-model (statistical-mode)) @@ -101,7 +88,7 @@ - +#| ;;; garbage follows @@ -120,6 +107,7 @@ varExtract tableExtract relationExtract +|# ;; mappingType @@ -139,10 +127,6 @@ relationExtract (vars-data :initarg nil :arg data) (critFcn )) -(de - - - (defclass meanModel (model) ... ) ;; a macro to map onto Model (defclass meanVarModel (model) ... ) @@ -167,10 +151,11 @@ relationExtract :dataname :modelname ) +#| -(with-mapping map :model mymod :data mydata + (with-mapping map :model mymod :data mydata (bootstrap mymod mydata)) - +|# ;; solution should inherit from model and data (and be recomputable) ;; func args override embedded args @@ -180,6 +165,187 @@ relationExtract ;; unless stoch approx used. ;; + + + + + + + +;;; -*- mode: lisp -*- +;;; Copyright (c) 2005--2007, by A.J. Rossini +;;; See COPYRIGHT file for any additional restrictions (BSD license). +;;; Since 1991, ANSI was finally finished. Edited for ANSI Common Lisp. + +;;; File: model-fit.lisp +;;; Author: AJ Rossini +;;; Copyright: (c)2007, AJ Rossini. BSD, LLGPL, or GPLv2, depending +;;; on how it arrives. +;;; Purpose: models as a data summarization tools. +;;; Time-stamp: <2006-05-19 12:33:41 rossini> +;;; Creation: <2006-05-17 21:34:07 rossini> + +;;; What is this talk of 'release'? Klingons do not make software +;;; 'releases'. Our software 'escapes', leaving a bloody trail of +;;; designers and quality assurance people in its wake. + +;;; The model class is used to map a partial or complete model +;;; specification (partial specification made complete by assumptions +;;; inherent in the type of model used, i.e. linear regression assumes +;;; mean-zero and homoskedasticity) and the model-fit class is used to +;;; complete the model specification into how the model instance and +;;; type will then get computed. The approach tken is that we +;;; definitiely want to be able to see how we can explicitly try to +;;; characterize the work that we are trying to infere about the data. + +(in-package :cl-user) + +(defpackage :lisp-stat-model-fit + (:documentation "Model fitting theory.") + (:nicknames :ls-model-fit) + (:use :common-lisp + :lisp-stat-config + :lisp-stat-object-system + :lisp-stat-types + :lisp-stat-compound-data +#| + :lisp-stat-matrix + :lisp-stat-linalg +|# + ) + (:shadowing-import-from :lisp-stat-object-system + slot-value call-method call-next-method) + (:export fit)) + +(in-package :lisp-stat-model) + +(defclass fit () + ((criteria-functin-name :initform nil + :initarg :name + :accessor name + :reader model-name) + (criteria-function :initform nil + :initarg :formula + :accessor function + :reader fit-criteria) + (parameter-vars :initform nil + :initarg :parameter-vars + :accessor param-vars + :reader model-formula) + (data-vars :initform nil + :initarg :data-vars + :accessor data-vars + :reader model-formula) + (fixed-vars :initform nil + :initarg :fixed-vars + :accessor fixed-vars + :reader model-formula) + + (:documentation "Mathematical Model Fit approach")) + +(defclass optimization (model-fit)) + + +(defclass least-squares (optimization)) +(defclass weighted-least-squares (least-squares)) + +(defclass maximum-likelihood (optimization)) +(defclass minimax (optimization)) +(defclass maximin (optimization)) + +(defclass minimum-entropy (optimization)) +(defclass lq-norm (optimization)) + +(defclass root-finding (model-fit)) + +(defclass method-of-moments (root-finding)) +(defclass marginal-models (method-of-moments)) +(defclass gee (marginal-models)) +(defclass gee2 (marginal-models)) + + + +;;; How would this be used? + +(setf my-regr-model-1 + (new 'least-squares '(- y (+ (* beta1 x1) (* beta2 x2))))) + +;; and there should be an approach which could provide a mapping to this, i.e. + +(regression-model (list y) (list x1 x2)) + +;; could map to the above via macros. + + +;;;;; More misc protos... + + +(defclass model-specification () + ((spec-string :initform nil + :initarg :specification + :accessor :specification) + (spec-form :initform nil + :initarg :spec-form + :accessor :spec-form) + (model-class :initform nil)) + (:documentation "container for mathematical structure")) + +(defclass bayesian-model-specification (model-specification) + ((prior-model-class) + (spec-string :initform nil + :initarg :specification + :accessor :specification) + (spec-form :initform nil + :initarg :spec-form + :accessor :spec-form)) + (:documentation "adds structure holding priors to the model")) + +;;; The following should be self-created based on introspection of +;;; available: +;;; ## inferential technologies (bayesian, frequentist, etc), +;;; ## optimization criteria (likelihood, least-squares, min-entropy, +;;; minimax, etc) +;;; ## simplification macros, i.e. mapping directly to linear +;;; regression and other applications. fast specialized +;;; algorithms for edge cases and narrow conditions. +;;; ## + +(defparameter *model-class-list* + '((linear-regression frequentist) + (generalized-linear-regression parametric) + (linear-regression bayesian) + ())) + +;;;;; More mischief from a different time + + +;; regression-model is the old API, but regression as a generic will +;; be the new API. We need to distinguish between APIs which enable +;; the user to do clear activities, and APIs which enable developers +;; to do clear extensions and development, and underlying +;; infrastructure to keep everything straight and enabled. + +;; There are conflicting theories for how to structure the +;; specification of mathematical models, along with the statistical +;; inference, along with the data which is instantiating the model. +;; +;; i.e.: mathematical model for the relationships between components, +;; between a component and a summarizing parameter, and between +;; parameters. +;; +;; statistical inference describes the general approach for +;; aggregating into a decision and has impliciations for the scale up +;; from the model on a single instance to the generalization. +;; +;; The data represents the particular substantive context that is +;; driving the model/inference combination, and about which we hope to +;; generate knowledge. +;; +;; numerical analysis selects appropriate algorithms/implementations +;; for combining the above 3. +;; +;; the end result is input on the decision being made (which could be +;; specific (decision analysis/testing), risk-analysis (interval +;; estimation) , most likely/appropriate selection (point estimation) +;; -(defclass - \ No newline at end of file -- 2.11.4.GIT