28a26925e90d7b79377e5efbbf938dbb923e85cd
[alexxy/gromacs.git] / src / gromacs / statistics / statistics_test.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2008, The GROMACS development team.
6  * Copyright (c) 2011,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #include "gmxpre.h"
38
39 #include "statistics.h"
40
41 #include "config.h"
42
43 #include <stdio.h>
44
45 #include "gromacs/math/vec.h"
46 #include "gromacs/random/random.h"
47 #include "gromacs/utility/real.h"
48 #include "gromacs/utility/smalloc.h"
49
50 static void horizontal()
51 {
52     gmx_rng_t   rng;
53     gmx_stats_t straight;
54     int         i, ok, n = 1000;
55     real        y, a, b, da, db, aver, sigma, error, chi2, R, *xh, *yh;
56     FILE       *fp;
57
58     rng      = gmx_rng_init(13);
59     straight = gmx_stats_init();
60     for (i = 0; (i < n); i++)
61     {
62         y = gmx_rng_uniform_real(rng);
63         if ((ok = gmx_stats_add_point(straight, i, y, 0, 0)) != estatsOK)
64         {
65             fprintf(stderr, "%s\n", gmx_stats_message(ok));
66         }
67     }
68     /* Horizontal test */
69     if ((ok = gmx_stats_get_ase(straight, &aver, &sigma, &error)) != estatsOK)
70     {
71         fprintf(stderr, "%s\n", gmx_stats_message(ok));
72     }
73     fp = fopen("straight.xvg", "w");
74     if ((ok = gmx_stats_dump_xy(straight, fp)) != estatsOK)
75     {
76         fprintf(stderr, "%s\n", gmx_stats_message(ok));
77     }
78     fclose(fp);
79     printf("Horizontal line: average %g, sigma %g, error %g\n", aver, sigma, error);
80     if ((ok = gmx_stats_done(straight)) != estatsOK)
81     {
82         fprintf(stderr, "%s\n", gmx_stats_message(ok));
83     }
84 }
85
86 static void line()
87 {
88     gmx_rng_t   rng;
89     gmx_stats_t line;
90     int         i, dy, ok, n = 1000;
91     real        y, a, b, da, db, aver, sigma, error, chi2, R, rfit;
92     const real  a0 = 0.23, b0 = 2.7;
93     FILE       *fp;
94
95     for (dy = 0; (dy < 2); dy++)
96     {
97         rng      = gmx_rng_init(13);
98         line     = gmx_stats_init();
99         for (i = 0; (i < n); i++)
100         {
101             y = a0*i+b0+50*(gmx_rng_uniform_real(rng)-0.5);
102             if ((ok = gmx_stats_add_point(line, i, y, 0, dy*0.1)) != estatsOK)
103             {
104                 fprintf(stderr, "%s\n", gmx_stats_message(ok));
105             }
106         }
107         /* Line with slope test */
108         if ((ok = gmx_stats_get_ab(line, elsqWEIGHT_NONE, &a, &b, &da, &db, &chi2, &rfit)) != estatsOK)
109         {
110             fprintf(stderr, "%s\n", gmx_stats_message(ok));
111         }
112         if ((ok = gmx_stats_get_corr_coeff(line, &R)) != estatsOK)
113         {
114             fprintf(stderr, "%s\n", gmx_stats_message(ok));
115         }
116         if (dy == 0)
117         {
118             fp = fopen("line0.xvg", "w");
119         }
120         else
121         {
122             fp = fopen("line1.xvg", "w");
123         }
124         if ((ok = gmx_stats_dump_xy(line, fp)) != estatsOK)
125         {
126             fprintf(stderr, "%s\n", gmx_stats_message(ok));
127         }
128         fclose(fp);
129         printf("Line with eqn. y = %gx + %g with noise%s\n", a0, b0,
130                (dy == 0) ? "" : " and uncertainties");
131         printf("Found: a = %g +/- %g, b = %g +/- %g\n", a, da, b, db);
132         if ((ok = gmx_stats_done(line)) != estatsOK)
133         {
134             fprintf(stderr, "%s\n", gmx_stats_message(ok));
135         }
136         gmx_rng_destroy(rng);
137     }
138 }
139
140 static void histogram()
141 {
142     gmx_rng_t   rng;
143     gmx_stats_t camel;
144     int         i, ok, n = 1000, norm;
145     real        y, a, b, da, db, aver, sigma, error, chi2, R, *xh, *yh;
146     const real  a0 = 0.23, b0 = 2.7;
147     FILE       *fp;
148     char        fn[256];
149
150     for (norm = 0; (norm < 2); norm++)
151     {
152         rng      = gmx_rng_init(13);
153         camel    = gmx_stats_init();
154         for (i = 0; (i < n); i++)
155         {
156             y = sqr(gmx_rng_uniform_real(rng));
157             if ((ok = gmx_stats_add_point(camel, i, y+1, 0, 0)) != estatsOK)
158             {
159                 fprintf(stderr, "%s\n", gmx_stats_message(ok));
160             }
161             y = sqr(gmx_rng_uniform_real(rng));
162             if ((ok = gmx_stats_add_point(camel, i+0.5, y+2, 0, 0)) != estatsOK)
163             {
164                 fprintf(stderr, "%s\n", gmx_stats_message(ok));
165             }
166         }
167         /* Histogram test */
168         if ((ok = gmx_stats_make_histogram(camel, 0, 101, norm, &xh, &yh)) != estatsOK)
169         {
170             fprintf(stderr, "%s\n", gmx_stats_message(ok));
171         }
172         sprintf(fn, "histo%d-data.xvg", norm);
173         fp = fopen(fn, "w");
174         gmx_stats_dump_xy(camel, fp);
175         fclose(fp);
176         sprintf(fn, "histo%d.xvg", norm);
177         fp = fopen(fn, "w");
178         for (i = 0; (i < 101); i++)
179         {
180             fprintf(fp, "%12g  %12g\n", xh[i], yh[i]);
181         }
182         fclose(fp);
183         sfree(xh);
184         sfree(yh);
185     }
186 }
187
188 int main(int argc, char *argv[])
189 {
190     line();
191     horizontal();
192     histogram();
193
194     return 0;
195 }