static apop_data *colmeans(apop_data *in){ Get_vmsizes(in); //maxsize apop_data *sums = apop_data_summarize(in); Apop_col_tv(sums, "mean", means); apop_data *out = apop_matrix_to_data(apop_vector_to_matrix(means, 'r')); apop_name_stack(out->names, in->names, 'c', 'c'); apop_data *cov = apop_data_add_page(out, apop_data_covariance(in), "<Covariance>"); gsl_matrix_scale(cov->matrix, 1/sqrt(maxsize)); return out; }
/* This sample produces a dummy times table, gets a summary, and prunes the summary table. If you are not a test script, uncomment the last line to display the pruned table. */ void test_prune_cols(){ int i, j; apop_data *d = apop_data_alloc(0, 10, 4); for (i=0; i< 10; i++) for (j=0; j< 4; j++) apop_data_set(d, i, j, i*j); apop_data *summary = apop_data_summarize(d); apop_data_prune_columns(summary, "mean", "median"); assert(apop_name_find(summary->names, "mean", 'c')!=-2); assert(apop_name_find(summary->names, "median", 'c')!=-2); assert(apop_name_find(summary->names, "max", 'c')==-2); //not found assert(apop_name_find(summary->names, "variance", 'c')==-2); //not found assert(apop_data_get(summary, .row=0, .colname="mean")==0); assert(apop_data_get(summary, .row=1, .colname="median")==4); assert(apop_data_get(summary, .row=2, .colname="median")==8); //apop_data_show(summary); }
//We need to estimate the df via MLE. //However, the right value of the wishart covariance grid is CM/df. //So, for a value of df that we're trying, scale CM appropriately. gsl_matrix_scale(m->parameters->matrix, 1./m->parameters->vector->data[0]); double out = wishart_ll(in, m); gsl_matrix_scale(m->parameters->matrix, m->parameters->vector->data[0]); return out; } static void wishart_estimate(apop_data *d, apop_model *m){ Nullcheck_m(m, ); //apop_data_set(m->parameters, 0, -1, d->matrix->size1); //Start with cov matrix via mean of inputs; df=NaN apop_data_set(m->parameters, 0, -1, GSL_NAN); apop_data *summ=apop_data_summarize(d); Apop_col_t(summ, "mean", means); gsl_vector *t = m->parameters->vector; //mask this while unpacking m->parameters->vector=NULL; apop_data_unpack(means, m->parameters); m->parameters->vector=t; //Estimate a model with fixed cov matrix and blank (NaN) df. apop_model *modified_wish = apop_model_copy(m); modified_wish->log_likelihood = fixed_wishart_ll; apop_model *fixed_wish = apop_model_fix_params(modified_wish); apop_model *est_via_fix = apop_estimate(d, fixed_wish); //copy df from fixed version to the real thing; clean up. t->data[0] = apop_data_get(est_via_fix->parameters, 0, -1); gsl_matrix_scale(m->parameters->matrix, 1./t->data[0]);