b9a9dde134ea2b296f2554369a9d96575b414d27
[pspp-builds.git] / src / math / time-series / innovations.c
1 /*
2   src/math/time-series/arma/innovations.c
3   
4   Copyright (C) 2006 Free Software Foundation, Inc. Written by Jason H. Stover.
5   
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)
9   any later version.
10   
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
14   more details.
15   
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.
19  */
20 /*
21   Find preliminary ARMA coefficients via the innovations algorithm.
22   Also compute the sample mean and covariance matrix for each series.
23
24   Reference:
25
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.
29  */
30
31 #include <gsl/gsl_matrix.h>
32 #include <gsl/gsl_vector.h>
33 #include <math.h>
34 #include <stdlib.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 <gettext.h>
42 #define _(msgid) gettext (msgid)
43
44 static void
45 get_mean_variance (size_t n_vars, const struct casefile *cf,
46                    struct innovations_estimate **est)
47                    
48 {
49   struct casereader *r;
50   struct ccase *c;
51   struct ccase *c2;
52   size_t n;
53   double *x;
54   double d;
55   double tmp;
56   double variance;
57
58   for (n = 0; n < n_vars; n++)
59     {
60       est[n]->n_obs = 2.0;
61       est[n]->mean = 0.0;
62       est[n]->variance = 0.0;
63     }
64   for (r = casefile_get_reader (cf); casereader_read (r, &c);
65        case_destroy (&c))
66     {
67       for (n = 0; n < n_vars; n++)
68         {
69           if (!mv_is_value_missing (&v->miss, val))
70             {
71               tmp = case_data (&c, est[n]->variable->fv);
72               d = (tmp - est[n]->mean) / est[n]->n_obs;
73               est[n]->mean += d;
74               est[n]->variance += est[n]->n_obs * est[n]->n_obs * d * d;
75               est[n]->n_obs += 1.0;
76             }
77         }
78     }
79   for (n = 0; n < n_vars; n++)
80     {
81       /* Maximum likelihood estimate of the variance. */
82       est[n]->variance /= est[n]->n_obs;
83     }
84 }
85
86 /*
87   Read the first MAX_LAG cases.
88  */
89 static bool
90 innovations_init_cases (struct casereader *r, struct ccase **c, size_t max_lag)
91 {
92   bool value = true;
93   size_t lag = 0;
94
95   while (value)
96     {
97       lag++;
98       value = casereader_read (r, c + lag);
99     }
100   return value;
101 }
102
103 /*
104   Read one case and update C, which contains the last MAX_LAG cases.
105  */
106 static bool
107 innovations_update_cases (struct casereader *r, struct ccase **c, size_t max_lag)
108 {
109   size_t lag;
110   bool value = false;
111   
112   for (lag = 0; lag < max_lag - 1; lag++)
113     {
114       c[lag] = c[lag+1];
115     }
116   value = casereader_read (r, c + lag);
117   return value;
118 }
119 static void
120 get_covariance (size_t n_vars, const struct casefile *cf, 
121                 struct innovations **est, size_t max_lag)
122 {
123   struct casereader *r;
124   struct ccase **c;
125   struct ccase *cur_case;
126   size_t lag;
127   size_t n_vars;
128   bool read_case = false;
129   double d;
130   double tmp;
131
132   c = xnmalloc (max_lag, sizeof (*c));
133   
134   for (lag = 0; lag < max_lag; lag++)
135     {
136       c[lag] = xmalloc (sizeof *c[i]);
137     }
138
139   r = casefile_get_reader (cf);
140   read_case = innovations_init_cases (r, c, max_lag);
141
142   while (read_case)
143     {
144       for (n = 0; n < n_vars; n++)
145         {
146           cur_case = case_data (c[0], est[n]->variable->fv);
147           if (!mv_is_value_missing (&est[n]->variable->miss, cur_case))
148             {
149               cur_case -= est[n]->mean;
150               for (lag = 1; lag <= max_lag; lag++)
151                 {
152                   tmp = case_data (c[lag], est[n]->variable->fv);
153                   if (!mv_is_value_missing (&est[n]->variable->miss, tmp))
154                     {
155                       d = (tmp - est[n]->mean);
156                       *(est[n]->cov + lag) += d * cur_case;
157                     }
158                 }
159             }
160         }
161       read_case = innovations_update_cases (r, c, max_lag);
162     }
163   for (lag = 0; lag <= max_lag; lag++)
164     {
165       for (n = 0; n < n_vars; n++)
166         {
167           *(est[n]->cov + lag) /= (est[n]->n_obs - lag);
168         }
169     }
170   for (lag = 0; lag < max_lag; lag++)
171     {
172       free (c[lag]);
173     }
174   free (c);
175 }
176
177 struct innovations_estimate ** pspp_innovations (const struct variable **vars, size_t *n_vars,
178                                                  size_t lag, const struct casefile *cf)
179 {
180   struct innovations_estimate **est;
181   struct casereader *r;
182   struct ccase *c;
183   size_t i;
184
185   est = xnmalloc (*n_vars, sizeof *est);
186   for (i = 0; i < *n_vars; i++)
187     {
188       if (vars[i]->type == NUMERIC)
189         {
190           est[i] = xmalloc (sizeof **est);
191           est[i]->variable = vars[i];
192           est[i]->mean = 0.0;
193           est[i]->variance = 0.0;
194           est[i]->cov = gsl_matrix_calloc (lag, lag);
195           est[i]->coeff = xnmalloc (lag, sizeof (*est[i]->coeff));
196           for (j = 0; j < lag; j++)
197             {
198               est[i]->coeff + j = xmalloc (sizeof (*(est[i]->coeff + j)));
199             }
200         }
201       else
202         {
203           *n_vars--;
204           msg (MW, _("Cannot compute autocovariance for a non-numeric variable %s"),
205                      var_to_string (vars[i]));
206         }
207     }
208
209   /*
210     First data pass to get the mean and variance.
211    */
212   get_mean_variance (*n_vars, cf, est);
213   get_covariance (*n_vars, cf, est, lag);
214 }