![]() |
|
Here are a few pieces of sample code, gathered from elsewhere in the documentation, for testing your installation or to give you a sense of what code with Apophenia's tools looks like. If you'd like more context or explanation, please click through to the page from which the example was taken.
In the documentation for the apop_ols model, a program to read in data and run a regression. You'll need to go to that page for the sample data and further discussion.
#include <apop.h> int main(void){ apop_data *data; apop_model *est; apop_text_to_db("data","d"); data = apop_query_to_data("select * from d"); est = apop_estimate(data, apop_ols); apop_model_show(est); return 0; }
In the apop_text_to_db page, a variant, demonstrating the use of optional, named arguments:
#include <apop.h> int main(void){ apop_data *data; apop_model *est; apop_db_open(NULL); apop_text_to_db("data", "d",.has_row_names= 0,.has_col_names=1); data = apop_query_to_data("select * from d"); est = apop_estimate(data, apop_ols); printf("The OLS coefficients:\n"); apop_model_print(est); }
Now, from the Python section of the outline page, the same thing via Python:
from apop import * apop_text_to_db("data", "d", 0, 1, None) data = apop_query_to_data("select * from d") est = apop_estimate(data, avar.apop_ols) print est #And one last example of using a method of the apop_data object: print est.parameters.transpose()
The same page illustrates some other calls of Apophenia functions via Python, such as the apop_test_fisher_exact function.
from apop import * data = [[15, 13], [18, 19]] adata = apop_pylist_to_data(data) print "display the data" print adata.matrix result = apop_test_fisher_exact(adata) print result print "Or, just one value:" print result.get("p value", -1)
From the Setting up page, an example of gathering data from two processes, saving the input to a database, then doing a later analysis:
#include <apop.h> //Your processes are probably a bit more complex. double process_one(gsl_rng *r){ return gsl_rng_uniform(r) * gsl_rng_uniform(r) ; } double process_two(gsl_rng *r){ return gsl_rng_uniform(r); } int main(){ apop_data *m; double p1, p2; int i; gsl_rng * r = apop_rng_alloc(); //create the database and the data table. apop_db_open("runs.db"); apop_table_exists("samples",1); //If the table already exists, delete it. apop_query("create table samples(iteration, process, value); begin;"); //populate the data table with runs. for (i=0; i<1000; i++){ p1 = process_one(r); p2 = process_two(r); apop_query("insert into samples values(%i, %i, %g);", i, 1, p1); apop_query("insert into samples values(%i, %i, %g);", i, 2, p2); } apop_query("commit;"); //the begin-commit wrapper saves writes to the drive. //pull the data from the database, converting it into a table along the way. m = apop_db_to_crosstab("samples", "iteration","process", "value"); Apop_col(m, 0, v1); //get vector views of the two table columns. Apop_col(m, 1, v2); //Output a table of means/variances, and t-test results. printf("\t mean\t\t var\n"); printf("process 1: %f\t%f\n", apop_mean(v1), apop_var(v1)); printf("process 2: %f\t%f\n\n", apop_mean(v2), apop_var(v2)); printf("t test\n"); apop_data_show(apop_t_test(v1, v2)); apop_data_print(m, "the_data.txt"); //does not overwrite; appends. return 0; }
A demonstration of apop_plot_line_and_scatter . You'll need a database from the {Modeling with Data} sample code, at http://modelingwithdata.org/appendices.html.
#include <apop.h> int main(){ apop_data *data, *data_copy; apop_model *est; FILE *f; char outfile[] = "scatter.gplot"; apop_db_open("data-metro.db"); data = apop_query_to_data("select riders, year from riders where station like 'Silver%%'"); apop_db_close(); //The regression destroys your data, so copy it first. data_copy = apop_data_copy(data); //Run OLS, display results on terminal est = apop_estimate(data, apop_OLS); apop_model_show(est); //Prep the file with a header, then call the function. 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); return 0; }
In the outline section on map/apply, a new
-test on every row, with all operations acting on entire rows rather than individual data points:
#include <apop.h> double row_offset; gsl_rng *r; void offset_rng(double *v){*v = gsl_rng_uniform(r) + row_offset;} double find_tstat(gsl_vector *in){ return apop_mean(in)/sqrt(apop_var(in));} double conf(double in, void *df){ return gsl_cdf_tdist_P(in, *(int *)df);} int main(){ apop_data *d = apop_data_alloc(0, 10, 100); r = apop_rng_alloc(3242); for (int i=0; i< 10; i++){ row_offset = gsl_rng_uniform(r)*2 -1; Apop_row(d, i, onerow); apop_vector_apply(onerow, offset_rng); } size_t df = d->matrix->size2-1; apop_data *means = apop_map(d, .fn_v = apop_vector_mean, .part ='r'); apop_data *tstats = apop_map(d, .fn_v = find_tstat, .part ='r'); apop_data *confidences = apop_map(tstats, .fn_dp = conf, .param = &df); printf("means:\t\t"); apop_data_show(means); printf("t stats:\t"); apop_data_show(tstats); printf("confidences:\t"); apop_data_show(confidences); }
In the documentation for apop_query_to_text, a program to list all the tables in an Sqlite database.
#include <apop.h> void print_table_list(char *db_file){ apop_data *tab_list; int i; apop_db_open(db_file); tab_list= apop_query_to_text("select name from sqlite_master where type==\"table\";"); for(i=0; i< tab_list->textsize[0]; i++) printf("%s\n", tab_list->text[i][0]); } int main(int argc, char **argv){ if (argc == 1){ printf("Give me a database name, and I will print out the list of tables contained therein.\n"); return 0; } print_table_list(argv[1]); }
Finally, a demonstration of fixing parameters to create a marginal distribution, via apop_model_fix_params
#include <apop.h> int main(){ gsl_rng *r = apop_rng_alloc(10); size_t i, ct = 1e4; apop_data *d = apop_data_alloc(0,ct,2); double draws[2]; apop_data *params = apop_data_alloc(2,2,2); apop_data_fill(params, 8, 1, 0.5, 2, 0.5, 1); apop_model *pvm = apop_model_copy(apop_multivariate_normal); pvm->parameters = apop_data_copy(params); for(i=0; i< ct; i++){ apop_draw(draws, r, pvm); apop_data_set(d, i, 0, draws[0]); apop_data_set(d, i, 1, draws[1]); } gsl_vector_set_all(pvm->parameters->vector, GSL_NAN); apop_model *mep1 = apop_model_fix_params(pvm); apop_model *e1 = apop_estimate(d, *mep1); printf("original params: "); apop_vector_show(params->vector); printf("estimated params: "); apop_vector_show(e1->parameters->vector); return 0; }