5 #include "gmx_random.h"
6 #include "gmx_statistics.h"
8 static void horizontal()
13 real y
,a
,b
,da
,db
,aver
,sigma
,error
,chi2
,R
,*xh
,*yh
;
16 rng
= gmx_rng_init(13);
17 straight
= gmx_stats_init();
18 for(i
=0; (i
<n
); i
++) {
19 y
= gmx_rng_uniform_real(rng
);
20 if ((ok
= gmx_stats_add_point(straight
,i
,y
,0,0)) != estatsOK
)
21 fprintf(stderr
,"%s\n",gmx_stats_message(ok
));
24 if ((ok
= gmx_stats_get_ase(straight
,&aver
,&sigma
,&error
)) != estatsOK
)
25 fprintf(stderr
,"%s\n",gmx_stats_message(ok
));
26 fp
= fopen("straight.xvg","w");
27 if ((ok
= gmx_stats_dump_xy(straight
,fp
)) != estatsOK
)
28 fprintf(stderr
,"%s\n",gmx_stats_message(ok
));
30 printf("Horizontal line: average %g, sigma %g, error %g\n",aver
,sigma
,error
);
31 if ((ok
= gmx_stats_done(straight
)) != estatsOK
)
32 fprintf(stderr
,"%s\n",gmx_stats_message(ok
));
40 real y
,a
,b
,da
,db
,aver
,sigma
,error
,chi2
,R
,rfit
;
41 const real a0
=0.23,b0
=2.7;
44 for(dy
=0; (dy
<2); dy
++) {
45 rng
= gmx_rng_init(13);
46 line
= gmx_stats_init();
47 for(i
=0; (i
<n
); i
++) {
48 y
= a0
*i
+b0
+50*(gmx_rng_uniform_real(rng
)-0.5);
49 if ((ok
= gmx_stats_add_point(line
,i
,y
,0,dy
*0.1)) != estatsOK
)
50 fprintf(stderr
,"%s\n",gmx_stats_message(ok
));
52 /* Line with slope test */
53 if ((ok
= gmx_stats_get_ab(line
,elsqWEIGHT_NONE
,&a
,&b
,&da
,&db
,&chi2
,&rfit
)) != estatsOK
)
54 fprintf(stderr
,"%s\n",gmx_stats_message(ok
));
55 if ((ok
= gmx_stats_get_corr_coeff(line
,&R
)) != estatsOK
)
56 fprintf(stderr
,"%s\n",gmx_stats_message(ok
));
58 fp
= fopen("line0.xvg","w");
60 fp
= fopen("line1.xvg","w");
61 if ((ok
= gmx_stats_dump_xy(line
,fp
)) != estatsOK
)
62 fprintf(stderr
,"%s\n",gmx_stats_message(ok
));
64 printf("Line with eqn. y = %gx + %g with noise%s\n",a0
,b0
,
65 (dy
== 0) ? "" : " and uncertainties");
66 printf("Found: a = %g +/- %g, b = %g +/- %g\n",a
,da
,b
,db
);
67 if ((ok
= gmx_stats_done(line
)) != estatsOK
)
68 fprintf(stderr
,"%s\n",gmx_stats_message(ok
));
73 static void histogram()
78 real y
,a
,b
,da
,db
,aver
,sigma
,error
,chi2
,R
,*xh
,*yh
;
79 const real a0
=0.23,b0
=2.7;
83 for(norm
=0; (norm
<2); norm
++) {
84 rng
= gmx_rng_init(13);
85 camel
= gmx_stats_init();
86 for(i
=0; (i
<n
); i
++) {
87 y
= sqr(gmx_rng_uniform_real(rng
));
88 if ((ok
= gmx_stats_add_point(camel
,i
,y
+1,0,0)) != estatsOK
)
89 fprintf(stderr
,"%s\n",gmx_stats_message(ok
));
90 y
= sqr(gmx_rng_uniform_real(rng
));
91 if ((ok
= gmx_stats_add_point(camel
,i
+0.5,y
+2,0,0)) != estatsOK
)
92 fprintf(stderr
,"%s\n",gmx_stats_message(ok
));
95 if ((ok
= gmx_stats_make_histogram(camel
,0,101,norm
,&xh
,&yh
)) != estatsOK
)
96 fprintf(stderr
,"%s\n",gmx_stats_message(ok
));
97 sprintf(fn
,"histo%d-data.xvg",norm
);
99 gmx_stats_dump_xy(camel
,fp
);
101 sprintf(fn
,"histo%d.xvg",norm
);
103 for(i
=0; (i
<101); i
++)
104 fprintf(fp
,"%12g %12g\n",xh
[i
],yh
[i
]);
111 int main(int argc
,char *argv
[])