Here are a few pieces of sample code for testing your installation or to give you a sense of what code with Apophenia's tools looks like.
Two data streams
The sample program here is intended to show how one would integrate Apophenia into an existing program. For example, say that you are running a simulation of two different treatments, or say that two sensors are posting data at regular intervals. The goal is to gather the data in an organized form, and then ask questions of the resulting data set. Below, a thousand draws are made from the two processes and put into a database. Then, the data is pulled out, some simple statistics are compiled, and the data is written to a text file for inspection outside of the program. This program will compile cleanly with the sample Makefile.
#include <apop.h>
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_query("create table samples(iteration, process, value); begin;");
for (int i=0; i<1000; i++){
double p1 = process_one(r);
double 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;");
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");
}
Run a regression
See A quick overview for an example of loading a data set and running a simple regression.
A sequence of t-tests
In The 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;
void offset_rng(double *v){*v = gsl_rng_uniform(apop_rng_get_thread()) + 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(){
for (int i=0; i< 10; i++){
row_offset = gsl_rng_uniform(r)*2 -1;
}
int df = d->matrix->size2-1;
printf("means:\n"); apop_data_show(means);
printf("\nt stats:\n"); apop_data_show(tstats);
printf("\nconfidences:\n"); apop_data_show(confidences);
for (int i=0; i< 10; i++){
assert(fabs(gsl_cdf_tdist_Pinv(
apop_data_get(confidences, i, -1), 99)
}
}
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= apop_query_to_text(
"select name " "from sqlite_master where type=='table'");
for(int 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]);
}
Marginal distribution
A demonstration of fixing parameters to create a marginal distribution, via apop_model_fix_params
#include <apop.h>
int main(){
size_t ct = 5e4;
apop_data *params = apop_data_falloc((2,2,2), 8, 1, 0.5,
2, 0.5, 1);
pvm->dsize = 2;
gsl_vector_set_all(pvm->parameters->vector, GSL_NAN);
printf("original params: ");
printf("estimated params: ");
}