int main(){ apop_opts.db_engine='s'; //SQLite only. apop_query("create table atab (a numeric)"); for (int i=0; i< 1e5; i++) apop_query("insert into atab values(ran())"); apop_query("create table powa as " "select a, pow(a, 2) as sq, pow(a, 0.5) as sqrt " "from atab"); //compare the std dev of a uniform as reported by the //database routine, the matrix routine, and math. double db_pop_stddev = apop_query_to_float("select stddev_pop(a) from powa"); apop_data *d = apop_query_to_data("select * from powa"); apop_data *cov = apop_data_covariance(d); double matrix_pop_stddev = sqrt(apop_data_get(cov)*(d->matrix->size1/(d->matrix->size1-1.))); assert(fabs(db_pop_stddev - matrix_pop_stddev) < 1e-4); double actual_stddev = sqrt(2*gsl_pow_3(.5)/3); assert(fabs(db_pop_stddev - actual_stddev) < 1e-3); float sq_mean = apop_query_to_float("select avg(sq) from powa"); float actual_sq_mean = 1./3; assert(fabs(sq_mean - actual_sq_mean) < 1e-3); float sqrt_mean = apop_query_to_float("select avg(sqrt) from powa"); float actual_sqrt_mean = 2./3; assert(fabs(sqrt_mean - actual_sqrt_mean) < 1e-3); }
apop_data *make_histo(char const *zila, int year) { apop_data *d = apop_data_calloc(65,1); for (int i=0; i< 64; i++) { char *col; asprintf(&col, "year_%i_num_intensity_%i", year, i); double val=apop_query_to_float("select %s from ppl where " "ADMName='%s'", col, zila); Apop_stopif(isnan(val), continue, 0, "couldn't find %s for %s", col, zila); apop_data_set(d, i+1, 0, val); free(col); } return d; }
int main(){ int rep_ct = 10000; gsl_rng *r = apop_rng_alloc(0); apop_db_open("data-census.db"); gsl_vector *base_data = apop_query_to_vector("select in_per_capita from income where sumlevel+0.0 =40"); double RI = apop_query_to_float("select in_per_capita from income where sumlevel+0.0 =40 and geo_id2+0.0=44"); gsl_vector *boot_sample = gsl_vector_alloc(base_data->size); gsl_vector *replications = gsl_vector_alloc(rep_ct); for (int i=0; i< rep_ct; i++){ one_boot(base_data, r, boot_sample); gsl_vector_set(replications, i, apop_mean(boot_sample)); } double stderror = sqrt(apop_var(replications)); double mean = apop_mean(replications); printf("mean: %g; standard error: %g; (RI-mean)/stderr: %g; p value: %g\n", mean, stderror, (RI-mean)/stderror, 2*gsl_cdf_gaussian_Q(fabs(RI-mean), stderror)); }
apop_table_exists("testcheckoutfill", 'd'); apop_table_exists("testcheckoutbase_copy", 'd'); apop_table_exists("tcb", 'd'); apop_query("create table testcheckoutbase (id, a, b); " " insert into testcheckoutbase values(0, 3, 0./0.);" " insert into testcheckoutbase values(1, 0./0., 3);" " insert into testcheckoutbase values(2, 3, 3);" "create table testcheckoutfill (draw, value, id, field); " " insert into testcheckoutfill values(0, 3, 1, 'a');" " insert into testcheckoutfill values(0, 3, 0, 'b');" " insert into testcheckoutfill values(1, 9, 1, 'a');" " insert into testcheckoutfill values(1, 6, 0, 'b');" ); char *strings[] = {"testcheckoutbase", //0 "testcheckoutfill", //1 "testcheckoutbase_copy", //2 "tcb" //3 }; set_key_text("id", NULL, "id"); check_out_impute( strings+0, strings+2, (int[]){0}, NULL, strings+1); assert(apop_query_to_float("select avg(a) from testcheckoutbase_copy")==3); assert(apop_query_to_float("select avg(b) from testcheckoutbase_copy")==3); check_out_impute(strings+0, strings+3, (int[]){1}, NULL, strings+1); assert(apop_query_to_float("select avg(a) from tcb")==5); assert(apop_query_to_float("select avg(b) from tcb")==4); apop_table_exists("testcheckoutbase", 'd'); apop_table_exists("testcheckoutfill", 'd'); apop_table_exists("testcheckoutbase_copy", 'd'); apop_table_exists("tcb", 'd'); }