2 src/math/ts/innovations.c
4 Copyright (C) 2006 Free Software Foundation, Inc. Written by Jason H. Stover.
6 This program is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2 of the License, or (at your option)
11 This program is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 51
18 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
21 Find preliminary ARMA coefficients via the innovations algorithm.
22 Also compute the sample mean and covariance matrix for each series.
26 P. J. Brockwell and R. A. Davis. Time Series: Theory and
27 Methods. Second edition. Springer. New York. 1991. ISBN
28 0-387-97429-6. Sections 5.2, 8.3 and 8.4.
31 #include <gsl/gsl_matrix.h>
32 #include <gsl/gsl_vector.h>
33 #include <gsl/gsl_math.h>
35 #include <libpspp/alloc.h>
36 #include <libpspp/compiler.h>
37 #include <math/coefficient.h>
38 #include <math/ts/innovations.h>
41 get_mean (const gsl_matrix *data,
42 struct innovations_estimate **est)
50 for (n = 0; n < data->size2; n++)
55 for (i = 0; i < data->size1; i++)
57 for (n = 0; n < data->size2; n++)
59 tmp = gsl_matrix_get (data, i, n);
63 d = (tmp - est[n]->mean) / est[n]->n_obs;
70 update_cov (struct innovations_estimate **est, gsl_vector_const_view x,
71 gsl_vector_const_view y, size_t lag)
77 for (j = 0; j < x.vector.size; j++)
79 xj = gsl_vector_get (&x.vector, j);
80 yj = gsl_vector_get (&y.vector, j);
87 *(est[j]->cov + lag) += xj * yj;
93 get_covariance (const gsl_matrix *data,
94 struct innovations_estimate **est, size_t max_lag)
101 assert (data != NULL);
102 assert (est != NULL);
104 for (j = 0; j < data->size2; j++)
106 for (lag = 0; lag <= max_lag; lag++)
108 *(est[j]->cov + lag) = 0.0;
112 The rows are in the outer loop because a gsl_matrix is stored in
115 for (i = 0; i < data->size1; i++)
117 for (lag = 0; lag < max_lag && lag < data->size1 - i; lag++)
119 update_cov (est, gsl_matrix_const_row (data, i),
120 gsl_matrix_const_row (data, i + lag), lag);
123 for (j = 0; j < data->size2; j++)
125 for (lag = 0; lag <= max_lag; lag++)
127 *(est[j]->cov + lag) /= est[j]->n_obs;
135 innovations_convolve (double **theta, struct innovations_estimate *est,
141 for (k = 0; k < j; k++)
143 result += theta[i-1][i-k-1] * theta[j][j-k-1] * est->scale[k];
148 innovations_update_scale (struct innovations_estimate *est, double *theta,
155 if (i < (size_t) est->max_lag)
157 result = est->cov[0];
158 for (j = 0; j < i; j++)
161 result -= theta[k] * theta[k] * est->scale[j];
163 est->scale[i] = result;
167 init_theta (double **theta, size_t max_lag)
172 for (i = 0; i < max_lag; i++)
174 for (j = 0; j <= i; j++)
181 innovations_update_coeff (double **theta, struct innovations_estimate *est,
188 for (i = 0; i < max_lag; i++)
190 for (j = 0; j <= i; j++)
193 theta[i][k] = (est->cov[k] -
194 innovations_convolve (theta, est, i, j))
197 innovations_update_scale (est, theta[i], i + 1);
201 get_coef (const gsl_matrix *data,
202 struct innovations_estimate **est, size_t max_lag)
208 theta = xnmalloc (max_lag, sizeof (*theta));
209 for (i = 0; i < max_lag; i++)
211 theta[i] = xnmalloc (max_lag, sizeof (**(theta + i)));
214 for (n = 0; n < data->size2; n++)
216 init_theta (theta, max_lag);
217 innovations_update_scale (est[n], theta[0], 0);
218 innovations_update_coeff (theta, est[n], max_lag);
219 /* Copy the final row of coefficients into EST->COEFF.*/
220 for (i = 0; i < max_lag; i++)
223 The order of storage here means that the best predicted value
224 for the time series is computed as follows:
226 Let X[m], X[m-1],... denote the original series.
227 Let X_hat[0] denote the best predicted value of X[0],
228 X_hat[1] denote the projection of X[1] onto the subspace
229 spanned by {X[0] - X_hat[0]}. Let X_hat[m] denote the
230 projection of X[m] onto the subspace spanned by {X[m-1] - X_hat[m-1],
231 X[m-2] - X_hat[m-2],...,X[0] - X_hat[0]}.
233 Then X_hat[m] = est->coeff[m-1] * (X[m-1] - X_hat[m-1])
234 + est->coeff[m-1] * (X[m-2] - X_hat[m-2])
236 + est->coeff[m-max_lag] * (X[m - max_lag] - X_hat[m - max_lag])
238 (That is what X_hat[m] SHOULD be, anyway. These routines need
241 pspp_coeff_set_estimate (est[n]->coeff[i], theta[max_lag - 1][i]);
245 for (i = 0; i < max_lag; i++)
253 innovations_struct_init (struct innovations_estimate *est,
254 const struct design_matrix *dm,
260 /* COV[0] stores the lag 0 covariance (i.e., the variance), COV[1]
261 holds the lag-1 covariance, etc.
263 est->cov = xnmalloc (lag + 1, sizeof (*est->cov));
264 est->scale = xnmalloc (lag + 1, sizeof (*est->scale));
265 est->coeff = xnmalloc (lag, sizeof (*est->coeff)); /* No intercept. */
268 The loop below is an unusual use of PSPP_COEFF_INIT(). In a
269 typical model, one column of a DESIGN_MATRIX has one
270 coefficient. But in a time-series model, one column has many
273 for (j = 0; j < lag; j++)
275 pspp_coeff_init (est->coeff + j, dm);
277 est->max_lag = (double) lag;
280 struct innovations_estimate **
281 pspp_innovations (const struct design_matrix *dm, size_t lag)
283 struct innovations_estimate **est;
286 est = xnmalloc (dm->m->size2, sizeof *est);
287 for (i = 0; i < dm->m->size2; i++)
289 est[i] = xmalloc (sizeof *est[i]);
290 /* est[i]->variable = vars[i]; */
291 innovations_struct_init (est[i], dm, lag);
294 get_mean (dm->m, est);
295 get_covariance (dm->m, est, lag);
296 get_coef (dm->m, est, lag);
302 pspp_innovations_free_one (struct innovations_estimate *est)
306 assert (est != NULL);
307 for (i = 0; i < (size_t) est->max_lag; i++)
309 pspp_coeff_free (est->coeff[i]);
316 void pspp_innovations_free (struct innovations_estimate **est, size_t n)
320 assert (est != NULL);
321 for (i = 0; i < n; i++)
323 pspp_innovations_free_one (est[i]);