~ubuntu-branches/debian/sid/apophenia/sid

« back to all changes in this revision

Viewing changes to tests/nist_tests.c

  • Committer: Package Import Robot
  • Author(s): Jerome Benoit
  • Date: 2014-09-11 12:36:28 UTC
  • Revision ID: package-import@ubuntu.com-20140911123628-mhyqci1xk9tjicph
Tags: upstream-0.999b+ds1
ImportĀ upstreamĀ versionĀ 0.999b+ds1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* These are stats tests from NIST. See http://www.itl.nist.gov/div898/strd/
 
2
Notice that I use various levels of tolerance, so this gives you an idea
 
3
of the relative accuracies of various operations. */
 
4
 
 
5
#ifdef Datadir
 
6
#define DATADIR Datadir
 
7
#else
 
8
#define DATADIR "."
 
9
#endif
 
10
 
 
11
#include <apop.h>
 
12
#include <unistd.h>
 
13
 
 
14
#define TOL 1e-15
 
15
#define TOL2 1e-5
 
16
#define TOL3 1e-9
 
17
 
 
18
#define Diff(L, R, eps) Apop_stopif(isnan(L-R) || fabs((L)-(R))>(eps), abort(), 0, "%g is too different from %g (abitrary limit=%g).", (double)(L), (double)(R), eps);
 
19
 
 
20
void pontius(){
 
21
    apop_text_to_db( DATADIR "/" "pontius.dat" ,"pont", .delimiters=" ");
 
22
    apop_data *d = apop_query_to_data("select y, x, pow(x,2) as p from pont");
 
23
    apop_model *est =  apop_estimate(d, apop_ols);
 
24
 
 
25
    Diff(apop_data_get(est->parameters, 0, -1), 0.673565789473684E-03, TOL3);
 
26
    Diff(apop_data_get(est->parameters, 1, -1), 0.732059160401003E-06, TOL);
 
27
    Diff(apop_data_get(est->parameters, 2, -1), -0.316081871345029E-14, TOL);
 
28
    apop_data *cov = apop_data_get_page(est->parameters, "<covariance>");
 
29
    Diff(apop_data_get(cov, 0, 0), pow(0.107938612033077E-03,2), TOL2);
 
30
    Diff(apop_data_get(cov, 1, 1), pow(0.157817399981659E-09,2), TOL2);
 
31
    Diff(apop_data_get(cov, 2, 2), pow(0.486652849992036E-16,2), TOL2);
 
32
    Diff(apop_data_get(est->info, .rowname="R squared"), 0.999999900178537, TOL);
 
33
    Diff(apop_data_get(est->info, .rowname="SSR"), 15.6040343244198, TOL3);
 
34
}
 
35
 
 
36
void wampler1(){
 
37
    apop_text_to_db( DATADIR "/" "wampler1.dat" ,"w1", .delimiters=" ");
 
38
    apop_data *d = apop_query_to_data("select y, x, pow(x,2) as p2, \
 
39
                                pow(x,3) as p3, pow(x,4) as p4, pow(x,5) as p5 from w1");
 
40
    apop_model *est = apop_estimate(d, apop_ols);
 
41
    for (int i=0; i<6; i++)
 
42
        Diff(apop_data_get(est->parameters, i, -1) ,1 , 1e-3);
 
43
    apop_data *cov = apop_data_get_page(est->parameters, "<covariance>");
 
44
    for (int i=0; i<6; i++)
 
45
        Diff(apop_data_get(cov, i, i), 0, TOL2);
 
46
    Diff(apop_data_get(est->info, .rowname="R squared"), 1, TOL);
 
47
}
 
48
 
 
49
void numacc4(){
 
50
    apop_data *d  = apop_text_to_data( DATADIR "/" "numacc4.dat" );
 
51
    gsl_vector *v = Apop_cv(d, 0);
 
52
    Diff(apop_vector_mean(v), 10000000.2, 1e-5);
 
53
    Diff(apop_vector_var(v)*(v->size -1)/v->size, 0.01, TOL3);
 
54
    //I don't do this yet:
 
55
    //Sample Autocorrelation Coefficient (lag 1) r(1):   -0.999     (exact)
 
56
}
 
57
 
 
58
int main(){
 
59
    pontius();
 
60
    wampler1();
 
61
    numacc4();
 
62
}