f256ef9d3fca6ea7f208022d258a7a605f16568c
[pspp-builds.git] / src / math / linreg / linreg.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2005 Free Software Foundation, Inc. Written by Jason H. Stover.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #ifndef LINREG_H
18 #define LINREG_H
19 #include <stdbool.h>
20 #include <gsl/gsl_math.h>
21 #include <gsl/gsl_vector.h>
22 #include <gsl/gsl_matrix.h>
23
24 struct variable;
25 struct pspp_coeff;
26 union value;
27
28 enum
29 {
30   PSPP_LINREG_SWEEP,
31   PSPP_LINREG_SVD
32 };
33
34
35
36 /*
37   Options describing what special values should be computed.
38  */
39 struct pspp_linreg_opts_struct
40 {
41   int get_depvar_mean_std;
42   int *get_indep_mean_std;      /* Array of booleans
43                                    dictating which
44                                    independent variables need
45                                    their means and standard
46                                    deviations computed within
47                                    pspp_linreg. This array
48                                    MUST be of length
49                                    n_indeps. If element i is
50                                    1, pspp_linreg will
51                                    compute the mean and
52                                    variance of indpendent
53                                    variable i. If element i
54                                    is 0, it will not compute
55                                    the mean and standard
56                                    deviation, and assume the
57                                    values are stored.
58                                    cache->indep_mean[i] is
59                                    the mean and
60                                    cache->indep_std[i] is the
61                                    sample standard deviation. */
62 };
63 typedef struct pspp_linreg_opts_struct pspp_linreg_opts;
64
65
66 /*
67   Find the least-squares estimate of b for the linear model:
68
69   Y = Xb + Z
70
71   where Y is an n-by-1 column vector, X is an n-by-p matrix of
72   independent variables, b is a p-by-1 vector of regression coefficients,
73   and Z is an n-by-1 normally-distributed random vector with independent
74   identically distributed components with mean 0.
75
76   This estimate is found via the sweep operator or singular-value
77   decomposition with gsl.
78
79
80   References:
81
82   1. Matrix Computations, third edition. GH Golub and CF Van Loan.
83   The Johns Hopkins University Press. 1996. ISBN 0-8018-5414-8.
84
85   2. Numerical Analysis for Statisticians. K Lange. Springer. 1999.
86   ISBN 0-387-94979-8.
87
88   3. Numerical Linear Algebra for Applications in Statistics. JE Gentle.
89   Springer. 1998. ISBN 0-387-98542-5.
90 */
91
92
93 struct pspp_linreg_cache_struct
94 {
95   int n_obs;                    /* Number of observations. */
96   int n_indeps;                 /* Number of independent variables. */
97   int n_coeffs;
98
99   /*
100      The variable struct is ignored during estimation. It is here so
101      the calling procedure can find the variable used in the model.
102    */
103   const struct variable *depvar;
104
105   gsl_vector *residuals;
106   struct pspp_coeff **coeff;
107   int method;                   /* Method to use to estimate parameters. */
108   /*
109      Means and standard deviations of the variables.
110      If these pointers are null when pspp_linreg() is
111      called, pspp_linreg() will compute their values.
112
113      Entry i of indep_means is the mean of independent
114      variable i, whose observations are stored in the ith
115      column of the design matrix.
116    */
117   double depvar_mean;
118   double depvar_std;
119   gsl_vector *indep_means;
120   gsl_vector *indep_std;
121
122   /*
123      Sums of squares.
124    */
125   double ssm;                   /* Sums of squares for the overall model. */
126   gsl_vector *ss_indeps;        /* Sums of squares from each
127                                    independent variable. */
128   double sst;                   /* Sum of squares total. */
129   double sse;                   /* Sum of squares error. */
130   double mse;                   /* Mean squared error. This is just sse /
131                                    dfe, but since it is the best unbiased
132                                    estimate of the population variance, it
133                                    has its own entry here. */
134   gsl_vector *ssx;              /* Centered sums of squares for independent
135                                    variables, i.e. \sum (x[i] - mean(x))^2. */
136   double ssy;                   /* Centered sums of squares for dependent
137                                    variable.
138                                  */
139   /*
140      Covariance matrix of the parameter estimates.
141    */
142   gsl_matrix *cov;
143   /*
144      Degrees of freedom.
145    */
146   double dft;
147   double dfe;
148   double dfm;
149
150   /*
151      'Hat' or Hessian matrix, i.e. (X'X)^{-1}, where X is our
152      design matrix.
153    */
154   gsl_matrix *hat;
155
156   double (*predict) (const struct variable **, const union value **,
157                      const void *, int);
158   double (*residual) (const struct variable **,
159                       const union value **,
160                       const union value *, const void *, int);
161   /*
162      Returns pointers to the variables used in the model.
163    */
164   int (*get_vars) (const void *, const struct variable **);
165   struct variable *resid;
166   struct variable *pred;
167
168 };
169
170 typedef struct pspp_linreg_cache_struct pspp_linreg_cache;
171
172
173
174 /*
175   Allocate a pspp_linreg_cache and return a pointer
176   to it. n is the number of cases, p is the number of
177   independent variables.
178  */
179 pspp_linreg_cache *pspp_linreg_cache_alloc (size_t n, size_t p);
180
181 bool pspp_linreg_cache_free (void *);
182
183 /*
184   Fit the linear model via least squares. All pointers passed to pspp_linreg
185   are assumed to be allocated to the correct size and initialized to the
186   values as indicated by opts.
187  */
188 int
189 pspp_linreg (const gsl_vector * Y, const gsl_matrix * X,
190              const pspp_linreg_opts * opts, pspp_linreg_cache * cache);
191
192 double
193 pspp_linreg_predict (const struct variable **, const union value **,
194                      const void *, int);
195 double
196 pspp_linreg_residual (const struct variable **, const union value **,
197                       const union value *, const void *, int);
198 /*
199   All variables used in the model.
200  */
201 int pspp_linreg_get_vars (const void *, const struct variable **);
202 #endif