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