X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fmath%2Fts%2Finnovations.c;h=4fc48c575b81e461123ce4ee93fd6690a59011da;hb=d94b00b8d019bb2fda05366a2e86505fff13dbe3;hp=43d86ec1677c8c9bb381f59b23109da8f09940f5;hpb=29e712969d0d5bc1ee20458309dee7df2c38f1ae;p=pspp-builds.git diff --git a/src/math/ts/innovations.c b/src/math/ts/innovations.c index 43d86ec1..4fc48c57 100644 --- a/src/math/ts/innovations.c +++ b/src/math/ts/innovations.c @@ -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. @@ -30,183 +30,273 @@ #include #include -#include +#include #include -#include -#include #include #include -#include #include #include static void -get_mean_variance (size_t n_vars, const struct casefile *cf, +get_mean_variance (const gsl_matrix *data, struct innovations_estimate **est) { - struct casereader *r; - struct ccase c; size_t n; + size_t i; double d; - const union value *tmp; + double tmp; - for (n = 0; n < n_vars; n++) + for (n = 0; n < data->size2; n++) { est[n]->n_obs = 2.0; est[n]->mean = 0.0; est[n]->variance = 0.0; } - for (r = casefile_get_reader (cf); casereader_read (r, &c); - case_destroy (&c)) + for (i = 0; i < data->size1; i++) { - for (n = 0; n < n_vars; n++) + for (n = 0; n < data->size2; n++) { - tmp = case_data (&c, est[n]->variable->fv); - if (!mv_is_value_missing (&(est[n]->variable->miss), tmp)) + tmp = gsl_matrix_get (data, i, n); + if (!gsl_isnan (tmp)) { - d = (tmp->f - est[n]->mean) / est[n]->n_obs; + d = (tmp - 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; } } } - for (n = 0; n < n_vars; n++) + for (n = 0; n < data->size2; n++) { /* Maximum likelihood estimate of the variance. */ est[n]->variance /= est[n]->n_obs; } } -/* - Read the first MAX_LAG cases. - */ -static bool -innovations_init_cases (struct casereader *r, struct ccase **c, size_t max_lag) -{ - bool value = true; - size_t lag = 0; - - while (value && lag < max_lag) - { - lag++; - value = casereader_read (r, c[lag]); - } - return value; -} - -/* - Read one case and update C, which contains the last MAX_LAG cases. - */ -static bool -innovations_update_cases (struct casereader *r, struct ccase **c, size_t max_lag) -{ - size_t lag; - bool value = false; - - for (lag = 0; lag < max_lag - 1; lag++) - { - c[lag] = c[lag+1]; - } - value = casereader_read (r, c[lag]); - return value; -} -static void -get_covariance (size_t n_vars, const struct casefile *cf, +static int +get_covariance (const gsl_matrix *data, struct innovations_estimate **est, size_t max_lag) { - struct casereader *r; - struct ccase **c; size_t lag; - size_t n; - bool read_case = false; - double d; + size_t j; + size_t i; double x; - const union value *tmp; - const union value *tmp2; + double y; + int rc = 1; - c = xnmalloc (max_lag, sizeof (*c)); + assert (data != NULL); + assert (est != NULL); - for (lag = 0; lag < max_lag; lag++) - { - c[lag] = xmalloc (sizeof *c[lag]); - } - - r = casefile_get_reader (cf); - read_case = innovations_init_cases (r, c, max_lag); - - while (read_case) + for (i = 0; i < data->size1; i++) { - for (n = 0; n < n_vars; n++) + for (j = 0; j < data->size2; j++) { - tmp2 = case_data (c[0], est[n]->variable->fv); - if (!mv_is_value_missing (&est[n]->variable->miss, tmp2)) + x = gsl_matrix_get (data, i, j); + + if (!gsl_isnan (x)) { - x = tmp2->f - est[n]->mean; - for (lag = 1; lag <= max_lag; lag++) + x -= est[j]->mean; + for (lag = 1; lag <= max_lag && lag < (data->size1 - i); lag++) { - tmp = case_data (c[lag], est[n]->variable->fv); - if (!mv_is_value_missing (&est[n]->variable->miss, tmp)) + y = gsl_matrix_get (data, i + lag, j); + if (!gsl_isnan (y)) { - d = (tmp->f - est[n]->mean); - *(est[n]->cov + lag) += d * x; + y -= est[j]->mean; + *(est[j]->cov + lag - 1) += y * x; + est[j]->n_obs += 1.0; } } } } - read_case = innovations_update_cases (r, c, max_lag); } - for (lag = 0; lag <= max_lag; lag++) + for (lag = 1; lag <= max_lag; lag++) { - for (n = 0; n < n_vars; n++) + for (j = 0; j < data->size2; j++) { - *(est[n]->cov + lag) /= (est[n]->n_obs - lag); + *(est[j]->cov + lag) /= (est[j]->n_obs - lag); } } - for (lag = 0; lag < max_lag; lag++) + return rc; +} +static double +innovations_convolve (double **theta, struct innovations_estimate *est, + int i, int j) +{ + int k; + double result = 0.0; + + for (k = 0; k < j; k++) { - free (c[lag]); + result += theta[i-1][i-k-1] * theta[j][j-k-1] * est->scale[k]; } - free (c); + 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; -struct innovations_estimate ** pspp_innovations (const struct variable **vars, size_t *n_vars, - size_t lag, const struct casefile *cf) + + 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; +} +static void +init_theta (double **theta, size_t max_lag) { - struct innovations_estimate **est; size_t i; size_t j; - est = xnmalloc (*n_vars, sizeof *est); - for (i = 0; i < *n_vars; i++) + for (i = 0; i < max_lag; i++) { - if (vars[i]->type == NUMERIC) + for (j = 0; j <= i; j++) { - est[i] = xmalloc (sizeof **est); - 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]->coeff = xnmalloc (lag, sizeof (*est[i]->coeff)); - for (j = 0; j < lag; j++) - { - est[i]->coeff[j] = xmalloc (sizeof (*(est[i]->coeff + j))); - } + theta[i][j] = 0.0; + } + } +} +static void +innovations_update_coeff (double **theta, struct innovations_estimate *est, + size_t max_lag) +{ + size_t i; + size_t j; + size_t k; + + for (i = 0; i < max_lag; i++) + { + for (j = 0; j <= i; j++) + { + k = i - j; + theta[i][k] = (est->cov[k] - + innovations_convolve (theta, est, i, j)) + / est->scale[k]; } - else + innovations_update_scale (est, theta[i], i + 1); + } +} +static void +get_coef (const gsl_matrix *data, + struct innovations_estimate **est, size_t max_lag) +{ + size_t i; + size_t n; + double **theta; + + theta = xnmalloc (max_lag, sizeof (*theta)); + for (i = 0; i < max_lag; i++) + { + theta[i] = xnmalloc (max_lag, sizeof (**(theta + i))); + } + + for (n = 0; n < data->size2; n++) + { + init_theta (theta, max_lag); + innovations_update_scale (est[n], theta[0], 0); + innovations_update_coeff (theta, est[n], max_lag); + /* Copy the final row of coefficients into EST->COEFF.*/ + for (i = 0; i < max_lag; i++) { - *n_vars--; -/* msg (MW, _("Cannot compute autocovariance for a non-numeric variable %s"), */ -/* var_to_string (vars[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]); } } - /* - First data pass to get the mean and variance. - */ - get_mean_variance (*n_vars, cf, est); - get_covariance (*n_vars, cf, est, lag); + for (i = 0; i < max_lag; i++) + { + free (theta[i]); + } + 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, 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; + + est = xnmalloc (data->size2, sizeof *est); + for (i = 0; i < data->size2; i++) + { + est[i] = xmalloc (sizeof *est[i]); +/* est[i]->variable = vars[i]; */ + innovations_struct_init (est[i], lag); + } + + get_mean_variance (data, est); + get_covariance (data, est, lag); + get_coef (data, est, lag); return est; } + +static void +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]); + } +} + +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); +}