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