X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fmath%2Fts%2Finnovations.c;h=ba2120fb4e6bc735f41d300b5aef406be5c2828d;hb=81579d9e9f994fb2908f50af41c3eb033d216e58;hp=00d366cef7bbd9fe617209c949e9a655f1ff2407;hpb=39929aa077830c708adcc5dfd224fd973428d0bc;p=pspp-builds.git diff --git a/src/math/ts/innovations.c b/src/math/ts/innovations.c index 00d366ce..ba2120fb 100644 --- a/src/math/ts/innovations.c +++ b/src/math/ts/innovations.c @@ -1,22 +1,19 @@ -/* - src/math/ts/innovations.c - - Copyright (C) 2006 Free Software Foundation, Inc. Written by Jason H. Stover. - - This program is free software; you can redistribute it and/or modify it under - the terms of the GNU General Public License as published by the Free - Software Foundation; either version 2 of the License, or (at your option) - any later version. - - This program is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - more details. - - You should have received a copy of the GNU General Public License along with - this program; if not, write to the Free Software Foundation, Inc., 51 - Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA. - */ +/* PSPP - a program for statistical analysis. + Copyright (C) 2006, 2011 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + /* Find preliminary ARMA coefficients via the innovations algorithm. Also compute the sample mean and covariance matrix for each series. @@ -28,19 +25,25 @@ 0-387-97429-6. Sections 5.2, 8.3 and 8.4. */ +#include + +#include "math/ts/innovations.h" + #include #include -#include +#include #include -#include -#include -#include -#include + +#include "libpspp/compiler.h" +#include "libpspp/misc.h" +#include "math/coefficient.h" + +#include "gl/xalloc.h" static void get_mean (const gsl_matrix *data, struct innovations_estimate **est) - + { size_t n; size_t i; @@ -57,7 +60,7 @@ get_mean (const gsl_matrix *data, for (n = 0; n < data->size2; n++) { tmp = gsl_matrix_get (data, i, n); - if (!gsl_isnan (tmp)) + if (!isnan (tmp)) { est[n]->n_obs += 1.0; d = (tmp - est[n]->mean) / est[n]->n_obs; @@ -66,7 +69,7 @@ get_mean (const gsl_matrix *data, } } } -static void +static void update_cov (struct innovations_estimate **est, gsl_vector_const_view x, gsl_vector_const_view y, size_t lag) { @@ -78,9 +81,9 @@ update_cov (struct innovations_estimate **est, gsl_vector_const_view x, { xj = gsl_vector_get (&x.vector, j); yj = gsl_vector_get (&y.vector, j); - if (!gsl_isnan (xj)) + if (!isnan (xj)) { - if (!gsl_isnan (yj)) + if (!isnan (yj)) { xj -= est[j]->mean; yj -= est[j]->mean; @@ -90,7 +93,7 @@ update_cov (struct innovations_estimate **est, gsl_vector_const_view x, } } static int -get_covariance (const gsl_matrix *data, +get_covariance (const gsl_matrix *data, struct innovations_estimate **est, size_t max_lag) { size_t lag; @@ -116,7 +119,7 @@ get_covariance (const gsl_matrix *data, { for (lag = 0; lag <= max_lag && lag < data->size1 - i; lag++) { - update_cov (est, gsl_matrix_const_row (data, i), + update_cov (est, gsl_matrix_const_row (data, i), gsl_matrix_const_row (data, i + lag), lag); } } @@ -162,7 +165,7 @@ innovations_update_scale (struct innovations_estimate *est, double *theta, for (j = 0; j < i; j++) { k = i - j - 1; - result -= theta[k] * theta[k] * est->scale[j]; + result -= pow2 (theta[k]) * est->scale[j]; } est->scale[i] = result; } @@ -195,12 +198,12 @@ innovations_update_coeff (double **theta, struct innovations_estimate *est, for (j = 1; j <= i; j++) { k = i - j; - theta[i][k] = (est->cov[k+1] - + 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); - } + } } static void get_coef (const gsl_matrix *data, @@ -231,7 +234,7 @@ get_coef (const gsl_matrix *data, 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 + 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]}. @@ -239,9 +242,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]); } @@ -255,8 +255,8 @@ get_coef (const gsl_matrix *data, } static void -innovations_struct_init (struct innovations_estimate *est, - const struct design_matrix *dm, +innovations_struct_init (struct innovations_estimate *est, + const struct design_matrix *dm, size_t lag) { size_t j; @@ -281,8 +281,29 @@ innovations_struct_init (struct innovations_estimate *est, } est->max_lag = (double) lag; } - -struct innovations_estimate ** +/* + 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) { struct innovations_estimate **est; @@ -297,13 +318,14 @@ 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); - + return est; } -static void +static void pspp_innovations_free_one (struct innovations_estimate *est) { size_t i;