save coefficients in innovations estimate structure
[pspp-builds.git] / src / math / ts / innovations.c
index dba4484950928648ca28f1aa24df4c64c8926bc5..a1a544a1a1a5717102dc3186389a1fe16691b3c1 100644 (file)
@@ -1,5 +1,5 @@
 /*
-  src/math/time-series/arma/innovations.c
+  src/math/ts/innovations.c
   
   Copyright (C) 2006 Free Software Foundation, Inc. Written by Jason H. Stover.
   
@@ -38,9 +38,7 @@
 #include <libpspp/compiler.h>
 #include <libpspp/message.h>
 #include <math/coefficient.h>
-#include <math/innovations.h>
-#include <gettext.h>
-#define _(msgid) gettext (msgid)
+#include <math/ts/innovations.h>
 
 static void
 get_mean_variance (size_t n_vars, const struct casefile *cf,
@@ -48,13 +46,10 @@ get_mean_variance (size_t n_vars, const struct casefile *cf,
                   
 {
   struct casereader *r;
-  struct ccase *c;
-  struct ccase *c2;
+  struct ccase c;
   size_t n;
-  double *x;
   double d;
-  double tmp;
-  double variance;
+  const union value *tmp;
 
   for (n = 0; n < n_vars; n++)
     {
@@ -67,10 +62,10 @@ get_mean_variance (size_t n_vars, const struct casefile *cf,
     {
       for (n = 0; n < n_vars; n++)
        {
-         if (!mv_is_value_missing (&v->miss, val))
+         tmp = case_data (&c, est[n]->variable->fv);
+         if (!mv_is_value_missing (&(est[n]->variable->miss), tmp))
            {
-             tmp = case_data (&c, est[n]->variable->fv);
-             d = (tmp - est[n]->mean) / est[n]->n_obs;
+             d = (tmp->f - est[n]->mean) / est[n]->n_obs;
              est[n]->mean += d;
              est[n]->variance += est[n]->n_obs * est[n]->n_obs * d * d;
              est[n]->n_obs += 1.0;
@@ -93,10 +88,10 @@ innovations_init_cases (struct casereader *r, struct ccase **c, size_t max_lag)
   bool value = true;
   size_t lag = 0;
 
-  while (value)
+  while (value && lag < max_lag)
     {
       lag++;
-      value = casereader_read (r, c + lag);
+      value = casereader_read (r, c[lag]);
     }
   return value;
 }
@@ -114,27 +109,28 @@ innovations_update_cases (struct casereader *r, struct ccase **c, size_t max_lag
     {
       c[lag] = c[lag+1];
     }
-  value = casereader_read (r, c + lag);
+  value = casereader_read (r, c[lag]);
   return value;
 }
 static void
 get_covariance (size_t n_vars, const struct casefile *cf, 
-               struct innovations **est, size_t max_lag)
+               struct innovations_estimate **est, size_t max_lag)
 {
   struct casereader *r;
   struct ccase **c;
-  struct ccase *cur_case;
   size_t lag;
-  size_t n_vars;
+  size_t n;
   bool read_case = false;
   double d;
-  double tmp;
+  double x;
+  const union value *tmp;
+  const union value *tmp2;
 
   c = xnmalloc (max_lag, sizeof (*c));
   
   for (lag = 0; lag < max_lag; lag++)
     {
-      c[lag] = xmalloc (sizeof *c[i]);
+      c[lag] = xmalloc (sizeof *c[lag]);
     }
 
   r = casefile_get_reader (cf);
@@ -144,17 +140,17 @@ get_covariance (size_t n_vars, const struct casefile *cf,
     {
       for (n = 0; n < n_vars; n++)
        {
-         cur_case = case_data (c[0], est[n]->variable->fv);
-         if (!mv_is_value_missing (&est[n]->variable->miss, cur_case))
+         tmp2 = case_data (c[0], est[n]->variable->fv);
+         if (!mv_is_value_missing (&est[n]->variable->miss, tmp2))
            {
-             cur_case -= est[n]->mean;
+             x = tmp2->f - est[n]->mean;
              for (lag = 1; lag <= max_lag; lag++)
                {
                  tmp = case_data (c[lag], est[n]->variable->fv);
                  if (!mv_is_value_missing (&est[n]->variable->miss, tmp))
                    {
-                     d = (tmp - est[n]->mean);
-                     *(est[n]->cov + lag) += d * cur_case;
+                     d = (tmp->f - est[n]->mean);
+                     *(est[n]->cov + lag) += d * x;
                    }
                }
            }
@@ -174,14 +170,116 @@ get_covariance (size_t n_vars, const struct casefile *cf,
     }
   free (c);
 }
+static double
+innovations_convolve (double **theta, struct innovations_estimate *est,
+                     int i, int j)
+{
+  int k;
+  double result = 0.0;
+
+  for (k = 0; k < i; k++)
+    {
+      result += theta[i-1][i-k-1] * theta[j-1][j-k-1] * est->scale[k];
+    }
+  return result;
+}
+static void
+innovations_update_scale (struct innovations_estimate *est, double *theta,
+                         size_t i)
+{
+  double result = 0.0;
+  size_t j;
+  size_t k;
+
+
+  result = est->cov[0];
+  for (j = 0; j < i; j++)
+    {
+      k = i - j;
+      result -= theta[k] * theta[k] * est->scale[j];
+    }
+  est->scale[i] = result;
+}
 
-struct innovations_estimate ** pspp_innovations (const struct variable **vars, size_t *n_vars,
-                                                size_t lag, const struct casefile *cf)
+static void
+get_coef (size_t n_vars, const struct casefile *cf, 
+               struct innovations_estimate **est, size_t max_lag)
+{
+  size_t j;
+  size_t i;
+  size_t k;
+  size_t n;
+  double v;
+  double **theta;
+
+  theta = xnmalloc (max_lag, sizeof (*theta));
+  for (i = 0; i < max_lag; i++)
+    {
+      theta[i] = xnmalloc (i+1, sizeof (theta[i]));
+
+    }
+  for (n = 0; n < n_vars; n++)
+    {
+      for (i = 0; i < max_lag; i++)
+       {
+         for (j = 0; j < i; j++)
+           {
+             theta[i][j] = 0.0;
+           }
+       }
+      innovations_update_scale (est[n], theta[0], 0);
+      for (i = 0; i < max_lag; i++)
+       {
+         v = est[n]->cov[i];
+         for (j = 0; j < i; j++)
+           {
+             k = i - j;
+             theta[i-1][k-1] = est[n]->cov[k] - 
+               innovations_convolve (theta, est[n], i, j);
+           }
+         innovations_update_scale (est[n], theta[i], i);
+       }
+      /* Copy the final row of coefficients into EST->COEFF.*/
+      for (i = 0; i < max_lag; i++)
+       {
+         /*
+           The order of storage here means that the best predicted value
+           for the time series is computed as follows:
+
+           Let X[m], X[m-1],... denote the original series.
+           Let X_hat[0] denote the best predicted value of X[0],
+           X_hat[1] denote the projection of X[1] onto the subspace
+           spanned by {X[0] - X_hat[0]}. Let X_hat[m] denote the 
+           projection of X[m] onto the subspace spanned by {X[m-1] - X_hat[m-1],
+           X[m-2] - X_hat[m-2],...,X[0] - X_hat[0]}.
+
+           Then X_hat[m] = est->coeff[m-1] * (X[m-1] - X_hat[m-1])
+                         + est->coeff[m-1] * (X[m-2] - X_hat[m-2])
+                         ...
+                         + est->coeff[m-max_lag] * (X[m - max_lag] - X_hat[m - max_lag])
+
+           (That is what X_hat[m] SHOULD be, anyway. These routines need
+           to be tested.)
+          */
+         pspp_coeff_set_estimate (est[n]->coeff[i], theta[max_lag - 1][i]);
+       }
+    }
+  for (i = 0; i < max_lag; i++)
+    {
+      free (theta[i]);
+    }
+  free (theta);
+}
+
+struct innovations_estimate ** 
+pspp_innovations (const struct variable **vars, 
+                 size_t *n_vars,
+                 size_t lag, 
+                 const struct casefile *cf)
 {
   struct innovations_estimate **est;
-  struct casereader *r;
-  struct ccase *c;
   size_t i;
+  size_t j;
 
   est = xnmalloc (*n_vars, sizeof *est);
   for (i = 0; i < *n_vars; i++)
@@ -192,24 +290,25 @@ struct innovations_estimate ** pspp_innovations (const struct variable **vars, s
          est[i]->variable = vars[i];
          est[i]->mean = 0.0;
          est[i]->variance = 0.0;
-         est[i]->cov = xnmalloc (lag, sizeof (est[i]->cov));
+         est[i]->cov = xnmalloc (lag, sizeof (*est[i]->cov));
+         est[i]->scale = xnmalloc (lag, sizeof (*est[i]->scale));
          est[i]->coeff = xnmalloc (lag, sizeof (*est[i]->coeff));
          for (j = 0; j < lag; j++)
            {
-             est[i]->coeff + j = xmalloc (sizeof (*(est[i]->coeff + j)));
+             est[i]->coeff[j] = xmalloc (sizeof (*(est[i]->coeff + j)));
            }
        }
       else
        {
          *n_vars--;
-         msg (MW, _("Cannot compute autocovariance for a non-numeric variable %s"),
-                    var_to_string (vars[i]));
+/*       msg (MW, _("Cannot compute autocovariance for a non-numeric variable %s"), */
+/*                  var_to_string (vars[i])); */
        }
     }
 
-  /*
-    First data pass to get the mean and variance.
-   */
   get_mean_variance (*n_vars, cf, est);
   get_covariance (*n_vars, cf, est, lag);
+  get_coef (*n_vars, cf, est, lag);
+  
+  return est;
 }