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 *query_data(){ apop_db_open("data-census.db"); return apop_query_to_data(" select postcode as row_names, " " m_per_100_f, population/1e6 as population, median_age " " from geography, income,demos,postcodes " " where income.sumlevel= '040' " " and geography.geo_id = demos.geo_id " " and income.geo_name = postcodes.state " " and geography.geo_id = income.geo_id "); }
gsl_matrix *query(char *d, char *q, int no_plot){ apop_db_open(d); apop_data *result = apop_query_to_data("%s", q); apop_db_close(0); Apop_stopif(!result, exit(2), 0, "Your query returned a blank table. Quitting."); Apop_stopif(result->error, exit(2), 0, "Error running your query. Quitting."); if (no_plot){ apop_data_show(result); exit(0); } return result->matrix; }
int main(){ apop_db_open("data-climate.db"); apop_data *data = apop_query_to_data("select pcp from precip"); apop_data_pmf_compress(data); //creates a weights vector apop_vector_normalize(data->weights); apop_data_sort(data); apop_model *pmf = apop_estimate(data, apop_pmf); FILE *outfile = fopen("out.h", "w"); apop_model_print(pmf, outfile); apop_model *kernel = apop_model_set_parameters(apop_normal, 0., 0.1); apop_model *k = apop_model_copy(apop_kernel_density); Apop_settings_add_group(k, apop_kernel_density, .base_pmf=pmf, .kernel=kernel); plot(k, "out.k"); printf("plot 'out.h' with lines title 'data', 'out.k' with lines title 'smoothed'\n"); }
int main(){ char outfile[] = "scatter.gplot"; apop_db_open("data-metro.db"); apop_data *data = apop_query_to_data("select riders, year from riders where station like 'Silver%%' and riders>0"); apop_db_close(); //The regression destroys your data, so copy it first. apop_data *data_copy = apop_data_copy(data); //Run OLS, display results on terminal apop_model *est = apop_estimate(data, apop_OLS); apop_model_show(est); //Prep the file with a header, then call the function. FILE *f = fopen(outfile, "w"); fprintf(f,"set term postscript;\n set output \"scatter.eps\"\n set yrange [0:*]\n"); apop_plot_line_and_scatter(data_copy, est, .output_pipe=f); fclose(f); }
#include <apop.h> int main(void){ apop_text_to_db(.text_file="data", .tabname="d"); apop_data *data = apop_query_to_data("select * from d"); apop_model *est = apop_estimate(data, apop_ols); apop_model_show(est); Apop_settings_add_group(est, apop_pm, .index =1); apop_model *first_param_distribution = apop_parameter_model(data, est); Apop_row(est->parameters, 1, param); double area_under_p = apop_cdf(param, first_param_distribution); apop_data_set(param, 0, -1, .val=0); double area_under_zero = apop_cdf(param, first_param_distribution); printf("reject the null for x_1 with %g percent confidence.\n", 2*fabs(area_under_p-area_under_zero)); }