use struct design_matrix
[pspp-builds.git] / src / math / ts / innovations.c
index 3921ea1c7e4318bb09d29e999d39f3d23d11864f..75b88d6cb4d9bfbd0f9a73b4b734bd0d7464cbbd 100644 (file)
@@ -110,13 +110,11 @@ get_covariance (const gsl_matrix *data,
            }
        }
     }
-  for (lag = 1; lag <= max_lag; lag++)
+  for (j = 0; j < data->size2; j++)
     {
-      for (j = 0; j < data->size2; j++)
-       {
-         *(est[j]->cov + lag) /= (est[j]->n_obs - lag);
-       }
+      *(est[j]->cov + lag - 1) /= est[j]->n_obs;
     }
+
   return rc;
 }
 static double
@@ -140,14 +138,16 @@ innovations_update_scale (struct innovations_estimate *est, double *theta,
   size_t j;
   size_t k;
 
-
-  result = est->variance;
-  for (j = 0; j < i; j++)
+  if (i < (size_t) est->max_lag)
     {
-      k = i - j - 1;
-      result -= theta[k] * theta[k] * est->scale[j];
+      result = est->variance;
+      for (j = 0; j < i; j++)
+       {
+         k = i - j - 1;
+         result -= theta[k] * theta[k] * est->scale[j];
+       }
+      est->scale[i] = result;
     }
-  est->scale[i] = result;
 }
 static void
 init_theta (double **theta, size_t max_lag)
@@ -170,7 +170,6 @@ innovations_update_coeff (double **theta, struct innovations_estimate *est,
   size_t i;
   size_t j;
   size_t k;
-  double v;
 
   for (i = 0; i < max_lag; i++)
     {
@@ -195,7 +194,7 @@ get_coef (const gsl_matrix *data,
   theta = xnmalloc (max_lag, sizeof (*theta));
   for (i = 0; i < max_lag; i++)
     {
-      theta[i] = xnmalloc (i + 1, sizeof (**(theta + i)));
+      theta[i] = xnmalloc (max_lag, sizeof (**(theta + i)));
     }
 
   for (n = 0; n < data->size2; n++)
@@ -236,35 +235,42 @@ get_coef (const gsl_matrix *data,
   free (theta);
 }
 
+static void
+innovations_struct_init (struct innovations_estimate *est, size_t lag)
+{
+  size_t j;
+
+  est->mean = 0.0;
+  est->variance = 0.0;
+  est->cov = xnmalloc (lag, sizeof (*est->cov));
+  est->scale = xnmalloc (lag + 1, sizeof (*est->scale));
+  est->coeff = xnmalloc (lag, sizeof (*est->coeff));
+  est->max_lag = (double) lag;
+  /* COV does not the variance (i.e., the lag 0 covariance). So COV[0]
+     holds the lag 1 covariance, COV[i] holds the lag i+1 covariance. */
+  for (j = 0; j < lag; j++)
+    {
+      est->coeff[j] = xmalloc (sizeof (*(est->coeff[j])));
+    }
+}
+      
 struct innovations_estimate ** 
-pspp_innovations (const gsl_matrix *data, size_t lag)
+pspp_innovations (const struct design_matrix *dm, size_t lag)
 {
   struct innovations_estimate **est;
   size_t i;
-  size_t j;
 
-  est = xnmalloc (data->size2, sizeof *est);
-  for (i = 0; i < data->size2; i++)
+  est = xnmalloc (dm->m->size2, sizeof *est);
+  for (i = 0; i < dm->m->size2; i++)
     {
       est[i] = xmalloc (sizeof *est[i]);
 /*       est[i]->variable = vars[i]; */
-      est[i]->mean = 0.0;
-      est[i]->variance = 0.0;
-      /* COV does not the variance (i.e., the lag 0 covariance). So COV[0]
-         holds the lag 1 covariance, COV[i] holds the lag i+1 covariance. */
-      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));
-      est[i]->max_lag = (double) lag;
-      for (j = 0; j < lag; j++)
-       {
-         est[i]->coeff[j] = xmalloc (sizeof (*(est[i]->coeff + j)));
-       }
+      innovations_struct_init (est[i], lag);
     }
 
-  get_mean_variance (data, est);
-  get_covariance (data, est, lag);
-  get_coef (data, est, lag);
+  get_mean_variance (dm->m, est);
+  get_covariance (dm->m, est, lag);
+  get_coef (dm->m, est, lag);
   
   return est;
 }
@@ -275,12 +281,13 @@ pspp_innovations_free_one (struct innovations_estimate *est)
   size_t i;
 
   assert (est != NULL);
-  free (est->cov);
-  free (est->scale);
   for (i = 0; i < (size_t) est->max_lag; i++)
     {
       pspp_coeff_free (est->coeff[i]);
     }
+  free (est->scale);
+  free (est->cov);
+  free (est);
 }
 
 void pspp_innovations_free (struct innovations_estimate **est, size_t n)