2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 #include "statistics.h"
43 #include "gromacs/math/vec.h"
44 #include "gromacs/random/random.h"
45 #include "gromacs/utility/real.h"
46 #include "gromacs/utility/smalloc.h"
48 static void horizontal()
53 real y
, a
, b
, da
, db
, aver
, sigma
, error
, chi2
, R
, *xh
, *yh
;
56 rng
= gmx_rng_init(13);
57 straight
= gmx_stats_init();
58 for (i
= 0; (i
< n
); i
++)
60 y
= gmx_rng_uniform_real(rng
);
61 if ((ok
= gmx_stats_add_point(straight
, i
, y
, 0, 0)) != estatsOK
)
63 fprintf(stderr
, "%s\n", gmx_stats_message(ok
));
67 if ((ok
= gmx_stats_get_ase(straight
, &aver
, &sigma
, &error
)) != estatsOK
)
69 fprintf(stderr
, "%s\n", gmx_stats_message(ok
));
71 fp
= fopen("straight.xvg", "w");
72 if ((ok
= gmx_stats_dump_xy(straight
, fp
)) != estatsOK
)
74 fprintf(stderr
, "%s\n", gmx_stats_message(ok
));
77 printf("Horizontal line: average %g, sigma %g, error %g\n", aver
, sigma
, error
);
78 if ((ok
= gmx_stats_done(straight
)) != estatsOK
)
80 fprintf(stderr
, "%s\n", gmx_stats_message(ok
));
88 int i
, dy
, ok
, n
= 1000;
89 real y
, a
, b
, da
, db
, aver
, sigma
, error
, chi2
, R
, rfit
;
90 const real a0
= 0.23, b0
= 2.7;
93 for (dy
= 0; (dy
< 2); dy
++)
95 rng
= gmx_rng_init(13);
96 line
= gmx_stats_init();
97 for (i
= 0; (i
< n
); i
++)
99 y
= a0
*i
+b0
+50*(gmx_rng_uniform_real(rng
)-0.5);
100 if ((ok
= gmx_stats_add_point(line
, i
, y
, 0, dy
*0.1)) != estatsOK
)
102 fprintf(stderr
, "%s\n", gmx_stats_message(ok
));
105 /* Line with slope test */
106 if ((ok
= gmx_stats_get_ab(line
, elsqWEIGHT_NONE
, &a
, &b
, &da
, &db
, &chi2
, &rfit
)) != estatsOK
)
108 fprintf(stderr
, "%s\n", gmx_stats_message(ok
));
110 if ((ok
= gmx_stats_get_corr_coeff(line
, &R
)) != estatsOK
)
112 fprintf(stderr
, "%s\n", gmx_stats_message(ok
));
116 fp
= fopen("line0.xvg", "w");
120 fp
= fopen("line1.xvg", "w");
122 if ((ok
= gmx_stats_dump_xy(line
, fp
)) != estatsOK
)
124 fprintf(stderr
, "%s\n", gmx_stats_message(ok
));
127 printf("Line with eqn. y = %gx + %g with noise%s\n", a0
, b0
,
128 (dy
== 0) ? "" : " and uncertainties");
129 printf("Found: a = %g +/- %g, b = %g +/- %g\n", a
, da
, b
, db
);
130 if ((ok
= gmx_stats_done(line
)) != estatsOK
)
132 fprintf(stderr
, "%s\n", gmx_stats_message(ok
));
134 gmx_rng_destroy(rng
);
138 static void histogram()
142 int i
, ok
, n
= 1000, norm
;
143 real y
, a
, b
, da
, db
, aver
, sigma
, error
, chi2
, R
, *xh
, *yh
;
144 const real a0
= 0.23, b0
= 2.7;
148 for (norm
= 0; (norm
< 2); norm
++)
150 rng
= gmx_rng_init(13);
151 camel
= gmx_stats_init();
152 for (i
= 0; (i
< n
); i
++)
154 y
= sqr(gmx_rng_uniform_real(rng
));
155 if ((ok
= gmx_stats_add_point(camel
, i
, y
+1, 0, 0)) != estatsOK
)
157 fprintf(stderr
, "%s\n", gmx_stats_message(ok
));
159 y
= sqr(gmx_rng_uniform_real(rng
));
160 if ((ok
= gmx_stats_add_point(camel
, i
+0.5, y
+2, 0, 0)) != estatsOK
)
162 fprintf(stderr
, "%s\n", gmx_stats_message(ok
));
166 if ((ok
= gmx_stats_make_histogram(camel
, 0, 101, norm
, &xh
, &yh
)) != estatsOK
)
168 fprintf(stderr
, "%s\n", gmx_stats_message(ok
));
170 sprintf(fn
, "histo%d-data.xvg", norm
);
172 gmx_stats_dump_xy(camel
, fp
);
174 sprintf(fn
, "histo%d.xvg", norm
);
176 for (i
= 0; (i
< 101); i
++)
178 fprintf(fp
, "%12g %12g\n", xh
[i
], yh
[i
]);
186 int main(int argc
, char *argv
[])