X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fmath%2Fcovariance-matrix.c;h=c419ce02e8b2357a868526aff9147f172b1125d7;hb=991f3d8dbedb0c1ceb87207229f606f3d1d17e5d;hp=fb67a64cf1a2205b9dfd5bc9a60d2dcb463f9cb2;hpb=c3ac5a8af9c449072c7e872ca70a78c1755ae309;p=pspp-builds.git diff --git a/src/math/covariance-matrix.c b/src/math/covariance-matrix.c index fb67a64c..c419ce02 100644 --- a/src/math/covariance-matrix.c +++ b/src/math/covariance-matrix.c @@ -64,11 +64,14 @@ struct covariance_matrix int n_pass; int missing_handling; enum mv_class missing_value; - void (*accumulate) (struct covariance_matrix *, const struct ccase *); + void (*accumulate) (struct covariance_matrix *, const struct ccase *, + const struct interaction_variable **, size_t); void (*update_moments) (struct covariance_matrix *, size_t, double); }; -static struct hsh_table *covariance_hsh_create (size_t); + + +static struct hsh_table *covariance_hsh_create (size_t *); static hsh_hash_func covariance_accumulator_hash; static unsigned int hash_numeric_alpha (const struct variable *, const struct variable *, @@ -93,9 +96,13 @@ static struct covariance_accumulator *get_new_covariance_accumulator (const value *); static void covariance_accumulate_listwise (struct covariance_matrix *, - const struct ccase *); + const struct ccase *, + const struct interaction_variable **, + size_t); static void covariance_accumulate_pairwise (struct covariance_matrix *, - const struct ccase *); + const struct ccase *, + const struct interaction_variable **, + size_t); struct covariance_matrix * covariance_matrix_init (size_t n_variables, @@ -107,7 +114,8 @@ covariance_matrix_init (size_t n_variables, result = xmalloc (sizeof (*result)); result->cov = NULL; - result->ca = covariance_hsh_create (n_variables); + result->n_variables = n_variables; + result->ca = covariance_hsh_create (&result->n_variables); result->m = NULL; result->m1 = NULL; result->missing_handling = missing_handling; @@ -133,7 +141,7 @@ covariance_matrix_init (size_t n_variables, } } result->v_variables = v_variables; - result->n_variables = n_variables; + result->n_pass = n_pass; return result; @@ -227,7 +235,7 @@ column_iterate (struct design_matrix *cov, const struct variable *v, col += i; y = -1.0 * cat_get_category_count (i, v) / ssize; tmp_val = cat_subscript_to_value (i, v); - if (compare_values (tmp_val, val1, v)) + if (!compare_values_short (tmp_val, val1, v)) { y += -1.0; } @@ -262,7 +270,7 @@ covariance_pass_two (struct design_matrix *cov, double mean1, double mean2, row += i; x = -1.0 * cat_get_category_count (i, v1) / ssize; tmp_val = cat_subscript_to_value (i, v1); - if (compare_values (tmp_val, val1, v1)) + if (!compare_values_short (tmp_val, val1, v1)) { x += 1.0; } @@ -342,15 +350,10 @@ covariance_accumulator_hash (const void *h, const void *aux) } if (var_is_alpha (v_max) && var_is_alpha (v_min)) { - unsigned int tmp; - char *x = - xnmalloc (1 + var_get_width (v_max) + var_get_width (v_min), - sizeof (*x)); - strncpy (x, val_max->s, var_get_width (v_max)); - strncat (x, val_min->s, var_get_width (v_min)); - tmp = *n_vars * (*n_vars + 1 + idx_max) + idx_min + hsh_hash_string (x); - free (x); - return tmp; + unsigned tmp = hsh_hash_bytes (val_max, var_get_width (v_max)); + tmp ^= hsh_hash_bytes (val_min, var_get_width (v_min)); + tmp += *n_vars * (*n_vars + 1 + idx_max) + idx_min; + return (size_t) tmp; } return -1u; } @@ -362,11 +365,11 @@ covariance_accumulator_hash (const void *h, const void *aux) in the data. */ static struct hsh_table * -covariance_hsh_create (size_t n_vars) +covariance_hsh_create (size_t *n_vars) { - return hsh_create (n_vars * n_vars, covariance_accumulator_compare, + return hsh_create (*n_vars * *n_vars, covariance_accumulator_compare, covariance_accumulator_hash, covariance_accumulator_free, - &n_vars); + n_vars); } static void @@ -399,23 +402,23 @@ match_nodes (const struct covariance_accumulator *c, } if (var_is_numeric (v1) && var_is_alpha (v2)) { - if (compare_values (val2, c->val2, v2)) + if (!compare_values_short (val2, c->val2, v2)) { return 0; } } if (var_is_alpha (v1) && var_is_numeric (v2)) { - if (compare_values (val1, c->val1, v1)) + if (!compare_values_short (val1, c->val1, v1)) { return 0; } } if (var_is_alpha (v1) && var_is_alpha (v2)) { - if (compare_values (val1, c->val1, v1)) + if (!compare_values_short (val1, c->val1, v1)) { - if (compare_values (val2, c->val2, v2)) + if (!compare_values_short (val2, c->val2, v2)) { return 0; } @@ -490,13 +493,13 @@ update_product (const struct variable *v1, const struct variable *v2, return 0.0; } static double -update_sum (const struct variable *var, const union value *val) +update_sum (const struct variable *var, const union value *val, double weight) { assert (var != NULL); assert (val != NULL); if (var_is_alpha (var)) { - return 1.0; + return weight; } return val->f; } @@ -525,22 +528,30 @@ get_covariance_variables (const struct covariance_matrix *cov) return cov->v_variables; } + static void update_hash_entry (struct hsh_table *c, const struct variable *v1, const struct variable *v2, - const union value *val1, const union value *val2) + const union value *val1, const union value *val2, + const struct interaction_value *i_val1, + const struct interaction_value *i_val2) { struct covariance_accumulator *ca; struct covariance_accumulator *new_entry; + double iv_f1; + double iv_f2; - + iv_f1 = interaction_value_get_nonzero_entry (i_val1); + iv_f2 = interaction_value_get_nonzero_entry (i_val2); ca = get_new_covariance_accumulator (v1, v2, val1, val2); ca->dot_product = update_product (ca->v1, ca->v2, ca->val1, ca->val2); - ca->sum1 = update_sum (ca->v1, ca->val1); - ca->sum2 = update_sum (ca->v2, ca->val2); + ca->dot_product *= iv_f1 * iv_f2; + ca->sum1 = update_sum (ca->v1, ca->val1, iv_f1); + ca->sum2 = update_sum (ca->v2, ca->val2, iv_f2); ca->ssize = 1.0; new_entry = hsh_insert (c, ca); + if (new_entry != NULL) { new_entry->dot_product += ca->dot_product; @@ -568,13 +579,17 @@ update_hash_entry (struct hsh_table *c, */ static void covariance_accumulate_pairwise (struct covariance_matrix *cov, - const struct ccase *ccase) + const struct ccase *ccase, + const struct interaction_variable **i_var, + size_t n_intr) { size_t i; size_t j; const union value *val1; const union value *val2; const struct variable **v_variables; + struct interaction_value *i_val1 = NULL; + struct interaction_value *i_val2 = NULL; assert (cov != NULL); assert (ccase != NULL); @@ -584,24 +599,40 @@ covariance_accumulate_pairwise (struct covariance_matrix *cov, for (i = 0; i < cov->n_variables; ++i) { - val1 = case_data (ccase, v_variables[i]); + if (is_interaction (v_variables[i], i_var, n_intr)) + { + i_val1 = interaction_case_data (ccase, v_variables[i], i_var, n_intr); + val1 = interaction_value_get (i_val1); + } + else + { + val1 = case_data (ccase, v_variables[i]); + } if (!var_is_value_missing (v_variables[i], val1, cov->missing_value)) { cat_value_update (v_variables[i], val1); - if (var_is_alpha (v_variables[i])) + if (var_is_numeric (v_variables[i])) cov->update_moments (cov, i, val1->f); for (j = i; j < cov->n_variables; j++) { - val2 = case_data (ccase, v_variables[j]); + if (is_interaction (v_variables[j], i_var, n_intr)) + { + i_val2 = interaction_case_data (ccase, v_variables[j], i_var, n_intr); + val2 = interaction_value_get (i_val2); + } + else + { + val2 = case_data (ccase, v_variables[j]); + } if (!var_is_value_missing (v_variables[j], val2, cov->missing_value)) { update_hash_entry (cov->ca, v_variables[i], v_variables[j], - val1, val2); + val1, val2, i_val1, i_val2); if (j != i) update_hash_entry (cov->ca, v_variables[j], - v_variables[i], val2, val1); + v_variables[i], val2, val1, i_val2, i_val1); } } } @@ -625,13 +656,17 @@ covariance_accumulate_pairwise (struct covariance_matrix *cov, */ static void covariance_accumulate_listwise (struct covariance_matrix *cov, - const struct ccase *ccase) + const struct ccase *ccase, + const struct interaction_variable **i_var, + size_t n_intr) { size_t i; size_t j; const union value *val1; const union value *val2; const struct variable **v_variables; + struct interaction_value *i_val1 = NULL; + struct interaction_value *i_val2 = NULL; assert (cov != NULL); assert (ccase != NULL); @@ -641,19 +676,35 @@ covariance_accumulate_listwise (struct covariance_matrix *cov, for (i = 0; i < cov->n_variables; ++i) { - val1 = case_data (ccase, v_variables[i]); + if (is_interaction (v_variables[i], i_var, n_intr)) + { + i_val1 = interaction_case_data (ccase, v_variables[i], i_var, n_intr); + val1 = interaction_value_get (i_val1); + } + else + { + val1 = case_data (ccase, v_variables[i]); + } cat_value_update (v_variables[i], val1); - if (var_is_alpha (v_variables[i])) + if (var_is_numeric (v_variables[i])) cov->update_moments (cov, i, val1->f); for (j = i; j < cov->n_variables; j++) { - val2 = case_data (ccase, v_variables[j]); + if (is_interaction (v_variables[j], i_var, n_intr)) + { + i_val2 = interaction_case_data (ccase, v_variables[j], i_var, n_intr); + val2 = interaction_value_get (i_val2); + } + else + { + val2 = case_data (ccase, v_variables[j]); + } update_hash_entry (cov->ca, v_variables[i], v_variables[j], - val1, val2); + val1, val2, i_val1, i_val2); if (j != i) update_hash_entry (cov->ca, v_variables[j], v_variables[i], - val2, val1); + val2, val1, i_val2, i_val1); } } } @@ -662,13 +713,13 @@ covariance_accumulate_listwise (struct covariance_matrix *cov, Call this function during the data pass. Each case will be added to a hash containing all values of the covariance matrix. After the data have been passed, call covariance_matrix_compute to put the - values in the struct covariance_matrix. + values in the struct covariance_matrix. */ void covariance_matrix_accumulate (struct covariance_matrix *cov, - const struct ccase *ccase) + const struct ccase *ccase, void **aux, size_t n_intr) { - cov->accumulate (cov, ccase); + cov->accumulate (cov, ccase, (const struct interaction_variable **) aux, n_intr); } static void @@ -689,7 +740,7 @@ covariance_matrix_insert (struct design_matrix *cov, { i = 0; tmp_val = cat_subscript_to_value (i, v1); - while (!compare_values (tmp_val, val1, v1)) + while (compare_values_short (tmp_val, val1, v1)) { i++; tmp_val = cat_subscript_to_value (i, v1); @@ -704,7 +755,7 @@ covariance_matrix_insert (struct design_matrix *cov, col = design_matrix_var_to_column (cov, v2); i = 0; tmp_val = cat_subscript_to_value (i, v1); - while (!compare_values (tmp_val, val1, v1)) + while (compare_values_short (tmp_val, val1, v1)) { i++; tmp_val = cat_subscript_to_value (i, v1); @@ -723,6 +774,7 @@ covariance_matrix_insert (struct design_matrix *cov, covariance_matrix_insert (cov, v2, v1, val2, val1, product); } } + gsl_matrix_set (cov->m, row, col, product); }