center the data before computing the coefficients; remove variance member of innovati...
[pspp-builds.git] / src / math / ts / innovations.c
index 089665acb94bd9ee54b8914f7a472c72af10dd31..107d8ba47897e2eafde521b1ec02e1d7bfc4ad8b 100644 (file)
@@ -114,7 +114,7 @@ get_covariance (const gsl_matrix *data,
    */
   for (i = 0; i < data->size1; i++)
     {
-      for (lag = 0; lag < max_lag && lag < data->size1 - i; lag++)
+      for (lag = 0; lag <= max_lag && lag < data->size1 - i; lag++)
        {
          update_cov (est, gsl_matrix_const_row (data, i), 
                      gsl_matrix_const_row (data, i + lag), lag);
@@ -132,15 +132,19 @@ get_covariance (const gsl_matrix *data,
 }
 
 static double
-innovations_convolve (double **theta, struct innovations_estimate *est,
-                     int i, int j)
+innovations_convolve (double *x, double *y, struct innovations_estimate *est,
+                     int i)
 {
   int k;
   double result = 0.0;
 
-  for (k = 0; k < j; k++)
+  assert (x != NULL && y != NULL);
+  assert (est != NULL);
+  assert (est->scale != NULL);
+  assert (i > 0);
+  for (k = 0; k < i; k++)
     {
-      result += theta[i-1][i-k-1] * theta[j][j-k-1] * est->scale[k];
+      result += x[k] * y[k] * est->scale[i-k-1];
     }
   return result;
 }
@@ -187,12 +191,13 @@ innovations_update_coeff (double **theta, struct innovations_estimate *est,
 
   for (i = 0; i < max_lag; i++)
     {
-      for (j = 0; j <= i; j++)
+      theta[i][i] = est->cov[i+1] / est->scale[0];
+      for (j = 1; j <= i; j++)
        {
          k = i - j;
-         theta[i][k] = (est->cov[k] - 
-           innovations_convolve (theta, est, i, j))
-           / est->scale[k];
+         theta[i][k] = (est->cov[k+1] - 
+                        innovations_convolve (theta[i] + k + 1, theta[j - 1], est, j))
+           / est->scale[j];
        }
       innovations_update_scale (est, theta[i], i + 1);
     }  
@@ -234,9 +239,6 @@ get_coef (const gsl_matrix *data,
                          + 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]);
        }
@@ -276,7 +278,28 @@ innovations_struct_init (struct innovations_estimate *est,
     }
   est->max_lag = (double) lag;
 }
-      
+/*
+  The mean is subtracted from the original data before computing the
+  coefficients. The mean is NOT added back, so if you want to predict
+  a new value, you must add the mean to X_hat[m] to get the correct
+  value.
+ */
+static void
+subtract_mean (gsl_matrix *m, struct innovations_estimate **est)
+{      
+  size_t i;
+  size_t j;
+  double tmp;
+
+  for (i = 0; i < m->size1; i++)
+    {
+      for (j = 0; j < m->size2; j++)
+       {
+         tmp = gsl_matrix_get (m, i, j) - est[j]->mean;
+         gsl_matrix_set (m, i, j, tmp);
+       }
+    }
+}
 struct innovations_estimate ** 
 pspp_innovations (const struct design_matrix *dm, size_t lag)
 {
@@ -292,6 +315,7 @@ pspp_innovations (const struct design_matrix *dm, size_t lag)
     }
 
   get_mean (dm->m, est);
+  subtract_mean (dm->m, est);
   get_covariance (dm->m, est, lag);
   get_coef (dm->m, est, lag);