05c8159630d8aa8da3467148a72e6d7a2da95218
[pspp-builds.git] / src / math / 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 #include <src/math/coefficient.h>
24
25 enum
26 {
27   PSPP_LINREG_CONDITIONAL_INVERSE,
28   PSPP_LINREG_QR,
29   PSPP_LINREG_SWEEP,
30 };
31
32
33
34 /*
35   Options describing what special values should be computed.
36  */
37 struct pspp_linreg_opts_struct
38 {
39   int get_depvar_mean_std;
40   int *get_indep_mean_std;      /* Array of booleans
41                                    dictating which
42                                    independent variables need
43                                    their means and standard
44                                    deviations computed within
45                                    pspp_linreg. This array
46                                    MUST be of length
47                                    n_indeps. If element i is
48                                    1, pspp_linreg will
49                                    compute the mean and
50                                    variance of indpendent
51                                    variable i. If element i
52                                    is 0, it will not compute
53                                    the mean and standard
54                                    deviation, and assume the
55                                    values are stored.
56                                    cache->indep_mean[i] is
57                                    the mean and
58                                    cache->indep_std[i] is the
59                                    sample standard deviation. */
60 };
61 typedef struct pspp_linreg_opts_struct pspp_linreg_opts;
62
63
64 /*
65   Find the least-squares estimate of b for the linear model:
66
67   Y = Xb + Z
68
69   where Y is an n-by-1 column vector, X is an n-by-p matrix of
70   independent variables, b is a p-by-1 vector of regression coefficients,
71   and Z is an n-by-1 normally-distributed random vector with independent
72   identically distributed components with mean 0.
73
74   This estimate is found via the sweep operator or singular-value
75   decomposition with gsl.
76
77
78   References:
79
80   1. Matrix Computations, third edition. GH Golub and CF Van Loan.
81   The Johns Hopkins University Press. 1996. ISBN 0-8018-5414-8.
82
83   2. Numerical Analysis for Statisticians. K Lange. Springer. 1999.
84   ISBN 0-387-94979-8.
85
86   3. Numerical Linear Algebra for Applications in Statistics. JE Gentle.
87   Springer. 1998. ISBN 0-387-98542-5.
88 */
89
90
91 struct pspp_linreg_cache_struct
92 {
93   int n_obs;                    /* Number of observations. */
94   int n_indeps;                 /* Number of independent variables. */
95   int n_coeffs;                 /* The intercept is not considered a
96                                    coefficient here. */
97
98   /*
99     Pointer to the dependent variable.
100    */
101   const struct variable *depvar;
102
103   gsl_vector *residuals;
104   struct pspp_coeff **coeff;
105   double intercept;
106   int method;                   /* Method to use to estimate parameters. */
107   /*
108      Means and standard deviations of the variables.
109      If these pointers are null when pspp_linreg() is
110      called, pspp_linreg() will compute their values.
111
112      Entry i of indep_means is the mean of independent
113      variable i, whose observations are stored in the ith
114      column of the design matrix.
115    */
116   double depvar_mean;
117   double depvar_std;
118   gsl_vector *indep_means;
119   gsl_vector *indep_std;
120
121   /*
122      Sums of squares.
123    */
124   double ssm;                   /* Sums of squares for the overall model. */
125   gsl_vector *ss_indeps;        /* Sums of squares from each
126                                    independent variable. */
127   double sst;                   /* Sum of squares total. */
128   double sse;                   /* Sum of squares error. */
129   double mse;                   /* Mean squared error. This is just sse /
130                                    dfe, but since it is the best unbiased
131                                    estimate of the population variance, it
132                                    has its own entry here. */
133   gsl_vector *ssx;              /* Centered sums of squares for independent
134                                    variables, i.e. \sum (x[i] - mean(x))^2. */
135   double ssy;                   /* Centered sums of squares for dependent
136                                    variable.
137                                  */
138   /*
139      Covariance matrix of the parameter estimates.
140    */
141   gsl_matrix *cov;
142   /*
143      Degrees of freedom.
144    */
145   double dft;
146   double dfe;
147   double dfm;
148
149   /*
150      'Hat' or Hessian matrix, i.e. (X'X)^{-1}, where X is our
151      design matrix.
152    */
153   gsl_matrix *hat;
154
155   double (*predict) (const struct variable **, const union value **,
156                      const void *, int);
157   double (*residual) (const struct variable **,
158                       const union value **,
159                       const union value *, const void *, int);
160   /*
161      Returns pointers to the variables used in the model.
162    */
163   int (*get_vars) (const void *, const struct variable **);
164   struct variable *resid;
165   struct variable *pred;
166
167 };
168
169 typedef struct pspp_linreg_cache_struct pspp_linreg_cache;
170
171
172
173 /*
174   Allocate a pspp_linreg_cache and return a pointer
175   to it. n is the number of cases, p is the number of
176   independent variables.
177  */
178 pspp_linreg_cache *pspp_linreg_cache_alloc (size_t n, size_t p);
179
180 bool pspp_linreg_cache_free (void *);
181
182 /*
183   Fit the linear model via least squares. All pointers passed to pspp_linreg
184   are assumed to be allocated to the correct size and initialized to the
185   values as indicated by opts.
186  */
187 int
188 pspp_linreg (const gsl_vector *, const struct design_matrix *,
189              const pspp_linreg_opts *, pspp_linreg_cache *);
190
191 double
192 pspp_linreg_predict (const struct variable **, const union value **,
193                      const void *, int);
194 double
195 pspp_linreg_residual (const struct variable **, const union value **,
196                       const union value *, const void *, int);
197 /*
198   All variables used in the model.
199  */
200 int pspp_linreg_get_vars (const void *, const struct variable **);
201
202 struct pspp_coeff *pspp_linreg_get_coeff (const pspp_linreg_cache
203                                                        *,
204                                                        const struct variable
205                                                        *,
206                                                        const union value *);
207 /*
208   Return or set the standard deviation of the independent variable.
209  */
210 double pspp_linreg_get_indep_variable_sd (pspp_linreg_cache *, const struct variable *);
211 void pspp_linreg_set_indep_variable_sd (pspp_linreg_cache *, const struct variable *, double);
212 /*
213   Mean of the independent variable.
214  */
215 double pspp_linreg_get_indep_variable_mean (pspp_linreg_cache *, const struct variable *);
216 void pspp_linreg_set_indep_variable_mean (pspp_linreg_cache *, const struct variable *, double);
217 #endif