066530d25b7791ab290e19481a10e4096918ba17
[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   size_t n;
52   double *x;
53   double d;
54   double tmp;
55   double variance;
56
57   x = xnmalloc (n_vars, sizeof *j);
58   
59   for (n = 0; n < n_vars; n++)
60     {
61       x[n] = 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) / x[n];
74               est[n]->mean += d;
75               est[n]->variance += x[n] * x[n] * d * d;
76               x[n] += 1.0;
77             }
78         }
79     }
80   for (n = 0; n < n_vars; n++)
81     {
82       est[n]->variance /= x[n];
83     }
84   free (x);
85 }
86
87 struct innovations_estimate ** pspp_innovations (const struct variable **vars, size_t *n_vars,
88                                                  size_t max_lag, const struct casefile *cf)
89 {
90   struct innovations_estimate **est;
91   struct casereader *r;
92   struct ccase *c;
93   size_t i;
94
95   est = xnmalloc (*n_vars, sizeof *est);
96   for (i = 0; i < *n_vars; i++)
97     {
98       if (vars[i]->type == NUMERIC)
99         {
100           est[i] = xmalloc (sizeof **est);
101           est[i]->variable = vars[i];
102           est[i]->mean = 0.0;
103           est[i]->variance = 0.0;
104           est[i]->cov = gsl_matrix_calloc (max_lag, max_lag);
105           est[i]->coeff = xnmalloc (max_lag, sizeof (*est[i]->coeff));
106           for (j = 0; j < max_lag; j++)
107             {
108               est[i]->coeff + j = xmalloc (sizeof (*(est[i]->coeff + j)));
109             }
110         }
111       else
112         {
113           *n_vars--;
114           msg (MW, _("Cannot compute autocovariance for a non-numeric variable %s"),
115                      var_to_string (vars[i]));
116         }
117     }
118
119   /*
120     First data pass to get the mean and variance.
121    */
122   get_mean_variance (*n_vars, cf, est);
123 }