subscript fixes
[pspp-builds.git] / src / math / ts / innovations.c
index 0b1ccd63aa6bbcd5aeca8710deb51ceec7ef769c..4e3dfbf8f323eb6f98eb9b490ca6d3c3d802cc14 100644 (file)
@@ -97,24 +97,24 @@ get_covariance (const gsl_matrix *data,
          if (!gsl_isnan (x))
            {
              x -= est[j]->mean;
-             for (lag = 1; lag <= max_lag && lag < data->size1 - max_lag; lag++)
+             for (lag = 1; lag <= max_lag && lag < (data->size1 - i); lag++)
                {
                  y = gsl_matrix_get (data, i + lag, j);
                  if (!gsl_isnan (y))
                    {
                      y -= est[j]->mean;
-                     *(est[j]->cov + lag) += y * x;
+                     *(est[j]->cov + lag - 1) += y * x;
                      est[j]->n_obs += 1.0;
                    }
                }
            }
        }
     }
-  for (lag = 0; lag <= max_lag && lag < data->size1 - max_lag; lag++)
+  for (lag = 1; lag <= max_lag; lag++)
     {
       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 - lag);
        }
     }
   return rc;
@@ -140,14 +140,16 @@ innovations_update_scale (struct innovations_estimate *est, double *theta,
   size_t j;
   size_t k;
 
-
-  result = est->cov[0];
-  for (j = 0; j < i; j++)
+  if (i < (size_t) est->max_lag)
     {
-      k = i - j;
-      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,18 +172,17 @@ 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++)
     {
-      v = est->cov[i];
-      for (j = 0; j < i; j++)
+      for (j = 0; j <= i; j++)
        {
          k = i - j;
-         theta[i-1][k-1] = est->cov[k] - 
-           innovations_convolve (theta, est, i, j);
+         theta[i][k] = (est->cov[k] - 
+           innovations_convolve (theta, est, i, j))
+           / est->scale[k];
        }
-      innovations_update_scale (est, theta[i], i);
+      innovations_update_scale (est, theta[i], i + 1);
     }  
 }
 static void
@@ -195,7 +196,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,27 +237,37 @@ 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)
 {
   struct innovations_estimate **est;
   size_t i;
-  size_t j;
 
   est = xnmalloc (data->size2, sizeof *est);
   for (i = 0; i < data->size2; i++)
     {
-      est[i] = xmalloc (sizeof **est);
+      est[i] = xmalloc (sizeof *est[i]);
 /*       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]->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)));
-       }
+      innovations_struct_init (est[i], lag);
     }
 
   get_mean_variance (data, est);
@@ -265,3 +276,30 @@ pspp_innovations (const gsl_matrix *data, size_t lag)
   
   return est;
 }
+
+static void 
+pspp_innovations_free_one (struct innovations_estimate *est)
+{
+  size_t i;
+
+  assert (est != NULL);
+  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)
+{
+  size_t i;
+
+  assert (est != NULL);
+  for (i = 0; i < n; i++)
+    {
+      pspp_innovations_free_one (est[i]);
+    }
+  free (est);
+}