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) 2010,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.
38 #ifndef GMX_STATISTICS_H
39 #define GMX_STATISTICS_H
43 #include "gromacs/utility/real.h"
45 /*! \libinternal \file
48 * Declares simple statistics toolbox
50 * \authors David van der Spoel <david.vanderspoel@icm.uu.se>
58 //! Abstract container type
59 typedef struct gmx_stats *gmx_stats_t;
61 //! Error codes returned by the routines
63 estatsOK, estatsNO_POINTS, estatsNO_MEMORY, estatsERROR,
64 estatsINVALID_INPUT, estatsNOT_IMPLEMENTED, estatsNR
67 //! Enum for statistical weights
69 elsqWEIGHT_NONE, elsqWEIGHT_X, elsqWEIGHT_Y,
70 elsqWEIGHT_XY, elsqWEIGHT_NR
73 //! Enum determining which coordinate to histogram
75 ehistoX, ehistoY, ehistoNR
79 * Initiate a data structure
80 * \return the data structure
82 gmx_stats_t gmx_stats_init();
85 * Destroy a data structure
86 * \param stats The data structure
89 int gmx_stats_done(gmx_stats_t stats);
92 * Remove outliers from a straight line, where level in units of
93 * sigma. Level needs to be larger than one obviously.
94 * \param[in] stats The data structure
95 * \param[in] level The sigma level
98 int gmx_stats_remove_outliers(gmx_stats_t stats, double level);
101 * Add a point to the data set
102 * \param[in] stats The data structure
103 * \param[in] x The x value
104 * \param[in] y The y value
105 * \param[in] dx The error in the x value
106 * \param[in] dy The error in the y value
109 int gmx_stats_add_point(gmx_stats_t stats, double x, double y,
110 double dx, double dy);
113 * Add a series of datapoints at once. The arrays dx and dy may
114 * be NULL in that case zero uncertainties will be assumed.
116 * \param[in] stats The data structure
117 * \param[in] n Number of points
118 * \param[in] x The array of x values
119 * \param[in] y The array of y values
120 * \param[in] dx The error in the x value
121 * \param[in] dy The error in the y value
124 int gmx_stats_add_points(gmx_stats_t stats, int n, real *x, real *y,
128 * Delivers data points from the statistics.
130 * Should be used in a while loop. Variables for either
131 * pointer may be NULL, in which case the routine can be used as an
132 * expensive point counter.
133 * Return the data points one by one. Return estatsOK while there are
134 * more points, and returns estatsNOPOINTS when the last point has
136 * If level > 0 then the outliers outside level*sigma are reported
138 * \param[in] stats The data structure
139 * \param[out] x The array of x values
140 * \param[out] y The array of y values
141 * \param[out] dx The error in the x value
142 * \param[out] dy The error in the y value
143 * \param[in] level sigma level (see above)
146 int gmx_stats_get_point(gmx_stats_t stats, real *x, real *y,
147 real *dx, real *dy, real level);
150 * Fit the data to y = ax + b, possibly weighted, if uncertainties
151 * have been input. da and db may be NULL.
152 * \param[in] stats The data structure
153 * \param[in] weight type of weighting
154 * \param[out] a slope
155 * \param[out] b intercept
156 * \param[out] da sigma in a
157 * \param[out] db sigma in b
158 * \param[out] chi2 normalized quality of fit
159 * \param[out] Rfit correlation coefficient
162 int gmx_stats_get_ab(gmx_stats_t stats, int weight,
164 real *da, real *db, real *chi2, real *Rfit);
167 * Fit the data to y = ax, possibly weighted, if uncertainties have
168 * have been input. da and db may be NULL.
169 * \param[in] stats The data structure
170 * \param[in] weight type of weighting
171 * \param[out] a slope
172 * \param[out] da sigma in a
173 * \param[out] chi2 normalized quality of fit
174 * \param[out] Rfit correlation coefficient
177 int gmx_stats_get_a(gmx_stats_t stats, int weight,
178 real *a, real *da, real *chi2, real *Rfit);
181 * Get the correlation coefficient.
182 * \param[in] stats The data structure
183 * \param[out] R the correlation coefficient between the data (x and y) as input to the structure.
186 int gmx_stats_get_corr_coeff(gmx_stats_t stats, real *R);
189 * Get the root mean square deviation.
190 * \param[in] stats The data structure
191 * \param[out] rmsd the root mean square deviation between x and y values.
194 int gmx_stats_get_rmsd(gmx_stats_t stats, real *rmsd);
197 * Get the number of points.
198 * \param[in] stats The data structure
199 * \param[out] N number of data points
202 int gmx_stats_get_npoints(gmx_stats_t stats, int *N);
205 * Computes and returns the average value.
206 * \param[in] stats The data structure
207 * \param[out] aver Average value
210 int gmx_stats_get_average(gmx_stats_t stats, real *aver);
213 * Computes and returns the standard deviation.
214 * \param[in] stats The data structure
215 * \param[out] sigma Standard deviation
218 int gmx_stats_get_sigma(gmx_stats_t stats, real *sigma);
221 * Computes and returns the standard error.
222 * \param[in] stats The data structure
223 * \param[out] error Standard error
226 int gmx_stats_get_error(gmx_stats_t stats, real *error);
229 * Pointers may be null, in which case no assignment will be done.
230 * \param[in] stats The data structure
231 * \param[out] aver Average value
232 * \param[out] sigma Standard deviation
233 * \param[out] error Standard error
236 int gmx_stats_get_ase(gmx_stats_t stats, real *aver, real *sigma, real *error);
239 * Dump the x, y, dx, dy data to a text file
240 * \param[in] stats The data structure
241 * \param[in] fp File pointer
244 int gmx_stats_dump_xy(gmx_stats_t stats, FILE *fp);
247 * Make a histogram of the data present.
249 * Uses either binwidth to
250 * determine the number of bins, or nbins to determine the binwidth,
251 * therefore one of these should be zero, but not the other. If *nbins = 0
252 * the number of bins will be returned in this variable. ehisto should be one of
253 * ehistoX or ehistoY. If
254 * normalized not equal to zero, the integral of the histogram will be
255 * normalized to one. The output is in two arrays, *x and *y, to which
256 * you should pass a pointer. Memory for the arrays will be allocated
257 * as needed. Function returns one of the estats codes.
258 * \param[in] stats The data structure
259 * \param[in] binwidth For the histogram
260 * \param[in] nbins Number of bins
261 * \param[in] ehisto Type (see enum above)
262 * \param[in] normalized see above
263 * \param[out] x see above
264 * \param[out] y see above
267 int gmx_stats_make_histogram(gmx_stats_t stats, real binwidth, int *nbins,
269 int normalized, real **x, real **y);
272 * Return message belonging to error code
273 * \param[in] estats error code
275 const char *gmx_stats_message(int estats);
277 /****************************************************
278 * Some statistics utilities for convenience: useful when a complete data
279 * set is available already from another source, e.g. an xvg file.
280 ****************************************************/
282 * Fit a straight line y=ax thru the n data points x, y, return the
284 * \param[in] n number of points
285 * \param[in] x data points x
286 * \param[in] y data point y
287 * \param[out] a slope
290 int lsq_y_ax(int n, real x[], real y[], real *a);
293 * Fit a straight line y=ax+b thru the n data points x, y.
294 * \param[in] n number of points
295 * \param[in] x data points x
296 * \param[in] y data point y
297 * \param[out] a slope
298 * \param[out] b intercept
299 * \param[out] r correlation coefficient
300 * \param[out] chi2 quality of fit
303 int lsq_y_ax_b(int n, real x[], real y[], real *a, real *b, real *r,
306 /*! \copydoc lsq_y_ax_b
308 int lsq_y_ax_b_xdouble(int n, double x[], real y[],
309 real *a, real *b, real *r, real *chi2);
312 * Fit a straight line y=ax+b thru the n data points x, y.
313 * \param[in] n number of points
314 * \param[in] x data points x
315 * \param[in] y data point y
316 * \param[in] dy uncertainty in data point y
317 * \param[out] a slope
318 * \param[out] b intercept
319 * \param[out] da error in slope
320 * \param[out] db error in intercept
321 * \param[out] r correlation coefficient
322 * \param[out] chi2 quality of fit
325 int lsq_y_ax_b_error(int n, real x[], real y[], real dy[],
326 real *a, real *b, real *da, real *db,
327 real *r, real *chi2);