From c62b6143aae0d791683892a7d3b2588bf2c9c511 Mon Sep 17 00:00:00 2001
From: AJ Rossini
Date: Fri, 3 Apr 2009 10:24:46 +0200
Subject: [PATCH] merged what little useful structure still existed in the clos
droppings

src/statmodels/regressionclos.lisp  174 
src/statmodels/regression.lisp  87 ++++++++++++++++++
2 files changed, 87 insertions(+), 174 deletions()
delete mode 100644 src/statmodels/regressionclos.lisp
diff git a/src/statmodels/regressionclos.lisp b/src/statmodels/regressionclos.lisp
deleted file mode 100644
index ce73ed5..0000000
 a/src/statmodels/regressionclos.lisp
+++ /dev/null
@@ 1,174 +0,0 @@
;;; * mode: lisp *

;;; Timestamp: <20090131 14:20:14 tony>
;;; Creation: <20081003 02:07:10 tony>
;;; Author: AJ Rossini
;;; Copyright: (c)2007, AJ Rossini. BSD, LLGPL, or GPLv2, depending
;;; on how it arrives.
;;; Purpose: redoing regression in a CLOS based framework. See
;;; regression.lsp for basis of work.

;;; 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.

;;; This organization and structure is new to the 21st Century
;;; version.

(inpackage :lispstatregressionlinearclos)

;;; Regresion Model CLOS, data structures

(defclass regressionmodelclos (statisticalmodel)
 ((x :initform nil :initarg :x :accessor x)
 (y :initform nil :initarg :y :accessor y)
 (included :initform nil :initarg :y :accessor y)
 (totalsumofsquares :initform nil :initarg :y :accessor y)
 (residualsumofsquares :initform nil :initarg :y :accessor y)
 (predictornames :initform nil :initarg :y :accessor y)
 (responsename :initform nil :initarg :y :accessor y)
 (caselabels :initform nil :initarg :y :accessor y)
 (needscomputing :initform T :initarg :compute? :accessor compute?))
 (:documentation "Normal Linear Regression Model through CLOS.
 Historical design based on what was done for LispStat, not modern."))

(defclass modelspecification ()
 ((specstring :initform nil
 :initarg :specification
 :accessor :specification)
 (specform :initform nil
 :initarg :specform
 :accessor :specform)
 (modelclass :initform nil))
 (:documentation "container for mathematical structure"))

(defclass bayesianmodelspecification (modelspecification)
 ((priormodelclass)
 (specstring :initform nil
 :initarg :specification
 :accessor :specification)
 (specform :initform nil
 :initarg :specform
 :accessor :specform))
 (:documentation "adds structure holding priors to the model"))

;;; The following should be selfcreated based on introspection of
;;; available:
;;; ## inferential technologies (bayesian, frequentist, etc),
;;; ## optimization criteria (likelihood, leastsquares, minentropy,
;;; minimax, etc)
;;; ## simplification macros, i.e. mapping directly to linear
;;; regression and other applications. fast specialized
;;; algorithms for edge cases and narrow conditions.
;;; ##

(defparameter *modelclasslist*
 '((linearregression frequentist)
 (generalizedlinearregression parametric)
 (linearregression bayesian)
 ()))



;;; Regression model generics and methods

(defgeneric regressionmodel (modelspec datapointer &key debug)
 (:documentation "CLOSy framework for regression, using numerics from "))

(defmethod regressionmodel
 ((regrinst regressionmodelclos)
 (dataptr datapointer)
 &key debug)
 "Args: (regrinst regressinomodelclos)

Returns a fitted regression model object. To examine the model further
assign the result to a variable and send it messages. Example (data
are in file absorbtion.lsp in the sample data directory/folder):

 (def fitm (fit (new 'regressionmodelclos (list iron aluminum) absorbtion)))
 (print fitm)
 (plot fitm :feature 'residuals)
"
 (let ((x (getvariablematrix (x regrinst) dataptr))
 (y (getvariablevector (y regrinst) dataptr)))



;;;;; More mischief from a different time


;; regressionmodel 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), riskanalysis (interval
;; estimation) , most likely/appropriate selection (point estimation)
;;

#
 (defclass model ()
 ((type structure)))

 (defgeneric regression ;; assumes x/y from lispmatrix  start of a set of generics.
 (model dataset)
 "Args: (x y &key (intercept T) (print T) (weights nil)
 included predictornames responsename caselabels)
X  list of independent variables or X matrix
Y  dependent variable.
INTERCEPT  T to include (default), NIL for no intercept
PRINT  if not NIL print summary information
WEIGHTS  if supplied should be the same length as Y; error
 variances are
 assumed to be inversely proportional to WEIGHTS
PREDICTORNAMES, RESPONSENAME, CASELABELS
  sequences of strings or symbols.
INCLUDED  if supplied should be the same length as Y, with
 elements nil to skip a in computing estimates (but not
 in residual analysis).
Returns a regression model object. To examine the model further assign the
result to a variable and send it messages.
Example (data are in file absorbtion.lsp in the sample data directory):
 (def m (regressionmodel (list iron aluminum) absorbtion))
 (send m :help) (send m :plotresiduals)"
 (let ((m (send regressionmodelproto :new)))
 (format t "~%")
 (send m :doc doc)
 (send m :x x)
 (send m :y y)
 (send m :intercept intercept)
 (send m :weights weights)
 (send m :included included)
 (send m :predictornames predictornames)
 (send m :responsename responsename)
 (send m :caselabels caselabels)
 (if debug
 (progn
 (format t "~%")
 (format t "~S~%" (send m :doc))
 (format t "X: ~S~%" (send m :x))
 (format t "Y: ~S~%" (send m :y))))
 (if print (send m :display))
 m))
#
diff git a/src/statmodels/regression.lisp b/src/statmodels/regression.lisp
index 772ee69..eb453ef 100644
 a/src/statmodels/regression.lisp
+++ b/src/statmodels/regression.lisp
@@ 34,6 +34,8 @@
;; challenge of reproducibility in the setting of prototype "online"
;; computation.
+
+
(defvar regressionmodelproto nil
"Prototype for all regression model instances.")
@@ 51,6 +53,91 @@
"Normal Linear Regression Model")
+(defclass regressionmodelstore (statisticalmodel)
+ ((x :initform nil :initarg :x :accessor x)
+ (y :initform nil :initarg :y :accessor y)
+ (included :initform nil :initarg :y :accessor y)
+ (totalsumofsquares :initform nil :initarg :y :accessor y)
+ (residualsumofsquares :initform nil :initarg :y :accessor y)
+ (predictornames :initform nil :initarg :y :accessor y)
+ (responsename :initform nil :initarg :y :accessor y)
+ (caselabels :initform nil :initarg :y :accessor y)
+ (needscomputing :initform T :initarg :compute? :accessor compute?))
+ (:documentation "Normal Linear Regression Model through CLOS.
+ Historical design based on what was done for LispStat, not modern."))
+
+(defclass modelspecification ()
+ ((specstring :initform nil
+ :initarg :specification
+ :accessor :specification)
+ (specform :initform nil
+ :initarg :specform
+ :accessor :specform)
+ (modelclass :initform nil))
+ (:documentation "container for mathematical structure"))
+
+(defclass bayesianmodelspecification (modelspecification)
+ ((priormodelclass)
+ (specstring :initform nil
+ :initarg :specification
+ :accessor :specification)
+ (specform :initform nil
+ :initarg :specform
+ :accessor :specform))
+ (:documentation "adds structure holding priors to the model"))
+
+;;; The following should be selfcreated based on introspection of
+;;; available:
+;;; ## inferential technologies (bayesian, frequentist, etc),
+;;; ## optimization criteria (likelihood, leastsquares, minentropy,
+;;; minimax, etc)
+;;; ## simplification macros, i.e. mapping directly to linear
+;;; regression and other applications. fast specialized
+;;; algorithms for edge cases and narrow conditions.
+;;; ##
+
+(defparameter *modelclasslist*
+ '((linearregression frequentist)
+ (generalizedlinearregression parametric)
+ (linearregression bayesian)
+ ()))
+
+;;;;; More mischief from a different time
+
+
+;; regressionmodel 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), riskanalysis (interval
+;; estimation) , most likely/appropriate selection (point estimation)
+;;
+
+
+
+;;;;;;;; Helper functions
(defun xtxinv (x)

2.11.4.GIT