X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fmath%2Flinreg.c;h=c03af4672955f4dd20514291faf8cf06bebb49e5;hb=2f1db0c762ca29e54b29d9a28bce14c54cf090db;hp=a7fa15657344bd1cf52aad870acffd80c9d661f6;hpb=b4fdd51a0bf62800c53c8a805f31ea735d931029;p=pspp-builds.git diff --git a/src/math/linreg.c b/src/math/linreg.c index a7fa1565..c03af467 100644 --- a/src/math/linreg.c +++ b/src/math/linreg.c @@ -24,7 +24,6 @@ #include #include #include -#include #include #include #include @@ -96,10 +95,10 @@ linreg_mean_std (gsl_vector_const_view v, double *mp, double *sp, double *ssp) The return value is the number of distinct variables found. */ int -pspp_linreg_get_vars (const void *c_, struct variable **v) +pspp_linreg_get_vars (const void *c_, const struct variable **v) { const pspp_linreg_cache *c = c_; - struct variable *tmp; + const struct variable *tmp; int i; int j; int result = 0; @@ -384,8 +383,8 @@ pspp_linreg (const gsl_vector * Y, const struct design_matrix *dm, gsl_matrix_set (design, j, i, tmp); } } - sw = gsl_matrix_calloc (cache->n_indeps + 1, cache->n_indeps + 1); - xtx = gsl_matrix_submatrix (sw, 0, 0, cache->n_indeps, cache->n_indeps); + sw = gsl_matrix_calloc (cache->n_coeffs + 1, cache->n_coeffs + 1); + xtx = gsl_matrix_submatrix (sw, 0, 0, cache->n_coeffs, cache->n_coeffs); for (i = 0; i < xtx.matrix.size1; i++) { @@ -400,8 +399,8 @@ pspp_linreg (const gsl_vector * Y, const struct design_matrix *dm, } } - gsl_matrix_set (sw, cache->n_indeps, cache->n_indeps, cache->sst); - xty = gsl_matrix_column (sw, cache->n_indeps); + gsl_matrix_set (sw, cache->n_coeffs, cache->n_coeffs, cache->sst); + xty = gsl_matrix_column (sw, cache->n_coeffs); /* This loop starts at 1, with i=0 outside the loop, so we can get the model sum of squares due to the first independent variable. @@ -411,7 +410,7 @@ pspp_linreg (const gsl_vector * Y, const struct design_matrix *dm, gsl_vector_set (&(xty.vector), 0, tmp); tmp *= tmp / gsl_vector_get (cache->ssx, 0); gsl_vector_set (cache->ss_indeps, 0, tmp); - for (i = 1; i < cache->n_indeps; i++) + for (i = 1; i < cache->n_coeffs; i++) { xi = gsl_matrix_column (design, i); gsl_blas_ddot (&(xi.vector), Y, &tmp); @@ -642,7 +641,7 @@ double pspp_linreg_get_indep_variable_mean (pspp_linreg_cache *c, const struct v coef = pspp_linreg_get_coeff (c, v, NULL); return pspp_coeff_get_mean (coef); } - return GSL_NAN; + return 0.0; } void pspp_linreg_set_indep_variable_mean (pspp_linreg_cache *c, const struct variable *v, @@ -661,24 +660,28 @@ void pspp_linreg_set_indep_variable_mean (pspp_linreg_cache *c, const struct var only variables in the model are in the covariance matrix. */ static struct design_matrix * -rearrange_covariance_matrix (const struct design_matrix *cov, pspp_linreg_cache *c) +rearrange_covariance_matrix (const struct covariance_matrix *cm, pspp_linreg_cache *c) { - struct variable **model_vars; + const struct variable **model_vars; + struct design_matrix *cov; struct design_matrix *result; size_t *permutation; size_t i; size_t j; size_t k; + size_t n_coeffs = 0; + assert (cm != NULL); + cov = covariance_to_design (cm); assert (cov != NULL); assert (c != NULL); assert (cov->m->size1 > 0); assert (cov->m->size2 == cov->m->size1); - permutation = xnmalloc (cov->m->size2, sizeof (*permutation)); model_vars = xnmalloc (1 + c->n_indeps, sizeof (*model_vars)); /* Put the model variables in the right order in MODEL_VARS. + Count the number of coefficients. */ for (i = 0; i < c->n_indeps; i++) { @@ -686,19 +689,22 @@ rearrange_covariance_matrix (const struct design_matrix *cov, pspp_linreg_cache } model_vars[i] = c->depvar; result = covariance_matrix_create (1 + c->n_indeps, model_vars); + permutation = xnmalloc (design_matrix_get_n_cols (result), sizeof (*permutation)); + for (j = 0; j < cov->m->size2; j++) { - for (k = 0; k < result->m->size2; k++) + k = 0; + while (k < result->m->size2) { if (design_matrix_col_to_var (cov, j) == design_matrix_col_to_var (result, k)) { permutation[k] = j; - break; } + k++; } } - for (j = 0; j < result->m->size2; j++) - for (i = 0; i < result->m->size1; i++) + for (i = 0; i < result->m->size1; i++) + for (j = 0; j < result->m->size2; j++) { gsl_matrix_set (result->m, i, j, gsl_matrix_get (cov->m, permutation[i], permutation[j])); } @@ -718,8 +724,8 @@ rearrange_covariance_matrix (const struct design_matrix *cov, pspp_linreg_cache having to alter it. The problem is that this means the caller must set CACHE->N_COEFFS. */ -int -pspp_linreg_with_cov (const struct design_matrix *full_cov, +void +pspp_linreg_with_cov (const struct covariance_matrix *full_cov, pspp_linreg_cache * cache) { struct design_matrix *cov; @@ -731,6 +737,11 @@ pspp_linreg_with_cov (const struct design_matrix *full_cov, cache_init (cache); reg_sweep (cov->m); post_sweep_computations (cache, cov, cov->m); - covariance_matrix_destroy (cov); + design_matrix_destroy (cov); } +double pspp_linreg_mse (const pspp_linreg_cache *c) +{ + assert (c != NULL); + return (c->sse / c->dfe); +}