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>
35 #include <data/case.h>
36 #include <data/casefile.h>
37 #include <libpspp/alloc.h>
38 #include <libpspp/compiler.h>
39 #include <libpspp/message.h>
40 #include <math/coefficient.h>
41 #include <math/ts/innovations.h>
44 get_mean_variance (size_t n_vars, const struct casefile *cf,
45 struct innovations_estimate **est)
52 const union value *tmp;
54 for (n = 0; n < n_vars; n++)
58 est[n]->variance = 0.0;
60 for (r = casefile_get_reader (cf); casereader_read (r, &c);
63 for (n = 0; n < n_vars; n++)
65 tmp = case_data (&c, est[n]->variable->fv);
66 if (!mv_is_value_missing (&(est[n]->variable->miss), tmp))
68 d = (tmp->f - est[n]->mean) / est[n]->n_obs;
70 est[n]->variance += est[n]->n_obs * est[n]->n_obs * d * d;
75 for (n = 0; n < n_vars; n++)
77 /* Maximum likelihood estimate of the variance. */
78 est[n]->variance /= est[n]->n_obs;
83 Read the first MAX_LAG cases.
86 innovations_init_cases (struct casereader *r, struct ccase **c, size_t max_lag)
91 while (value && lag < max_lag)
94 value = casereader_read (r, c[lag]);
100 Read one case and update C, which contains the last MAX_LAG cases.
103 innovations_update_cases (struct casereader *r, struct ccase **c, size_t max_lag)
108 for (lag = 0; lag < max_lag - 1; lag++)
112 value = casereader_read (r, c[lag]);
116 get_covariance (size_t n_vars, const struct casefile *cf,
117 struct innovations_estimate **est, size_t max_lag)
119 struct casereader *r;
123 bool read_case = false;
126 const union value *tmp;
127 const union value *tmp2;
129 c = xnmalloc (max_lag, sizeof (*c));
131 for (lag = 0; lag < max_lag; lag++)
133 c[lag] = xmalloc (sizeof *c[lag]);
136 r = casefile_get_reader (cf);
137 read_case = innovations_init_cases (r, c, max_lag);
141 for (n = 0; n < n_vars; n++)
143 tmp2 = case_data (c[0], est[n]->variable->fv);
144 if (!mv_is_value_missing (&est[n]->variable->miss, tmp2))
146 x = tmp2->f - est[n]->mean;
147 for (lag = 1; lag <= max_lag; lag++)
149 tmp = case_data (c[lag], est[n]->variable->fv);
150 if (!mv_is_value_missing (&est[n]->variable->miss, tmp))
152 d = (tmp->f - est[n]->mean);
153 *(est[n]->cov + lag) += d * x;
158 read_case = innovations_update_cases (r, c, max_lag);
160 for (lag = 0; lag <= max_lag; lag++)
162 for (n = 0; n < n_vars; n++)
164 *(est[n]->cov + lag) /= (est[n]->n_obs - lag);
167 for (lag = 0; lag < max_lag; lag++)
174 innovations_convolve (double **theta, struct innovations_estimate *est,
180 for (k = 0; k < i; k++)
182 result += theta[i][i-k] * theta[j][i-j] * est->cov[k];
187 get_coef (size_t n_vars, const struct casefile *cf,
188 struct innovations_estimate **est, size_t max_lag)
197 for (n = 0; n < n_vars; n++)
199 for (i = 0; i < max_lag; i++)
202 for (j = 0; j < i; j++)
205 theta[i][k] = est[n]->cov[k] -
206 innovations_convolve (theta, est, i, j);
212 struct innovations_estimate **
213 pspp_innovations (const struct variable **vars,
216 const struct casefile *cf)
218 struct innovations_estimate **est;
222 est = xnmalloc (*n_vars, sizeof *est);
223 for (i = 0; i < *n_vars; i++)
225 if (vars[i]->type == NUMERIC)
227 est[i] = xmalloc (sizeof **est);
228 est[i]->variable = vars[i];
230 est[i]->variance = 0.0;
231 est[i]->cov = xnmalloc (lag, sizeof (est[i]->cov));
232 est[i]->coeff = xnmalloc (lag, sizeof (*est[i]->coeff));
233 for (j = 0; j < lag; j++)
235 est[i]->coeff[j] = xmalloc (sizeof (*(est[i]->coeff + j)));
241 /* msg (MW, _("Cannot compute autocovariance for a non-numeric variable %s"), */
242 /* var_to_string (vars[i])); */
246 get_mean_variance (*n_vars, cf, est);
247 get_covariance (*n_vars, cf, est, lag);
248 get_coef (*n_vars, cf, est, lag);