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
41 /*! \libinternal \file
44 * Declares simple statistics toolbox
46 * \authors David van der Spoel <david.vanderspoel@icm.uu.se>
54 #include "types/simple.h"
56 //! Abstract container type
57 typedef struct gmx_stats *gmx_stats_t;
59 //! Error codes returned by the routines
61 estatsOK, estatsNO_POINTS, estatsNO_MEMORY, estatsERROR,
62 estatsINVALID_INPUT, estatsNOT_IMPLEMENTED, estatsNR
65 //! Enum for statistical weights
67 elsqWEIGHT_NONE, elsqWEIGHT_X, elsqWEIGHT_Y,
68 elsqWEIGHT_XY, elsqWEIGHT_NR
71 //! Enum determining which coordinate to histogram
73 ehistoX, ehistoY, ehistoNR
77 * Initiate a data structure
78 * \return the data structure
80 gmx_stats_t gmx_stats_init();
83 * Destroy a data structure
84 * \param stats The data structure
87 int gmx_stats_done(gmx_stats_t stats);
90 * Remove outliers from a straight line, where level in units of
91 * sigma. Level needs to be larger than one obviously.
92 * \param[in] stats The data structure
93 * \param[in] level The sigma level
96 int gmx_stats_remove_outliers(gmx_stats_t stats, double level);
99 * Add a point to the data set
100 * \param[in] stats The data structure
101 * \param[in] x The x value
102 * \param[in] y The y value
103 * \param[in] dx The error in the x value
104 * \param[in] dy The error in the y value
107 int gmx_stats_add_point(gmx_stats_t stats, double x, double y,
108 double dx, double dy);
111 * Add a series of datapoints at once. The arrays dx and dy may
112 * be NULL in that case zero uncertainties will be assumed.
114 * \param[in] stats The data structure
115 * \param[in] n Number of points
116 * \param[in] x The array of x values
117 * \param[in] y The array of y values
118 * \param[in] dx The error in the x value
119 * \param[in] dy The error in the y value
122 int gmx_stats_add_points(gmx_stats_t stats, int n, real *x, real *y,
126 * Delivers data points from the statistics.
128 * Should be used in a while loop. Variables for either
129 * pointer may be NULL, in which case the routine can be used as an
130 * expensive point counter.
131 * Return the data points one by one. Return estatsOK while there are
132 * more points, and returns estatsNOPOINTS when the last point has
134 * If level > 0 then the outliers outside level*sigma are reported
136 * \param[in] stats The data structure
137 * \param[out] x The array of x values
138 * \param[out] y The array of y values
139 * \param[out] dx The error in the x value
140 * \param[out] dy The error in the y value
141 * \param[in] level sigma level (see above)
144 int gmx_stats_get_point(gmx_stats_t stats, real *x, real *y,
145 real *dx, real *dy, real level);
148 * Fit the data to y = ax + b, possibly weighted, if uncertainties
149 * have been input. da and db may be NULL.
150 * \param[in] stats The data structure
151 * \param[in] weight type of weighting
152 * \param[out] a slope
153 * \param[out] b intercept
154 * \param[out] da sigma in a
155 * \param[out] db sigma in b
156 * \param[out] chi2 normalized quality of fit
157 * \param[out] Rfit correlation coefficient
160 int gmx_stats_get_ab(gmx_stats_t stats, int weight,
162 real *da, real *db, real *chi2, real *Rfit);
165 * Fit the data to y = ax, possibly weighted, if uncertainties have
166 * have been input. da and db may be NULL.
167 * \param[in] stats The data structure
168 * \param[in] weight type of weighting
169 * \param[out] a slope
170 * \param[out] da sigma in a
171 * \param[out] chi2 normalized quality of fit
172 * \param[out] Rfit correlation coefficient
175 int gmx_stats_get_a(gmx_stats_t stats, int weight,
176 real *a, real *da, real *chi2, real *Rfit);
179 * Get the correlation coefficient.
180 * \param[in] stats The data structure
181 * \param[out] R the correlation coefficient between the data (x and y) as input to the structure.
184 int gmx_stats_get_corr_coeff(gmx_stats_t stats, real *R);
187 * Get the root mean square deviation.
188 * \param[in] stats The data structure
189 * \param[out] rmsd the root mean square deviation between x and y values.
192 int gmx_stats_get_rmsd(gmx_stats_t stats, real *rmsd);
195 * Get the number of points.
196 * \param[in] stats The data structure
197 * \param[out] N number of data points
200 int gmx_stats_get_npoints(gmx_stats_t stats, int *N);
203 * Computes and returns the average value.
204 * \param[in] stats The data structure
205 * \param[out] aver Average value
208 int gmx_stats_get_average(gmx_stats_t stats, real *aver);
211 * Computes and returns the standard deviation.
212 * \param[in] stats The data structure
213 * \param[out] sigma Standard deviation
216 int gmx_stats_get_sigma(gmx_stats_t stats, real *sigma);
219 * Computes and returns the standard error.
220 * \param[in] stats The data structure
221 * \param[out] error Standard error
224 int gmx_stats_get_error(gmx_stats_t stats, real *error);
227 * Pointers may be null, in which case no assignment will be done.
228 * \param[in] stats The data structure
229 * \param[out] aver Average value
230 * \param[out] sigma Standard deviation
231 * \param[out] error Standard error
234 int gmx_stats_get_ase(gmx_stats_t stats, real *aver, real *sigma, real *error);
237 * Dump the x, y, dx, dy data to a text file
238 * \param[in] stats The data structure
239 * \param[in] fp File pointer
242 int gmx_stats_dump_xy(gmx_stats_t stats, FILE *fp);
245 * Make a histogram of the data present.
247 * Uses either binwidth to
248 * determine the number of bins, or nbins to determine the binwidth,
249 * therefore one of these should be zero, but not the other. If *nbins = 0
250 * the number of bins will be returned in this variable. ehisto should be one of
251 * ehistoX or ehistoY. If
252 * normalized not equal to zero, the integral of the histogram will be
253 * normalized to one. The output is in two arrays, *x and *y, to which
254 * you should pass a pointer. Memory for the arrays will be allocated
255 * as needed. Function returns one of the estats codes.
256 * \param[in] stats The data structure
257 * \param[in] binwidth For the histogram
258 * \param[in] nbins Number of bins
259 * \param[in] ehisto Type (see enum above)
260 * \param[in] normalized see above
261 * \param[out] x see above
262 * \param[out] y see above
265 int gmx_stats_make_histogram(gmx_stats_t stats, real binwidth, int *nbins,
267 int normalized, real **x, real **y);
270 * Return message belonging to error code
271 * \param[in] estats error code
273 const char *gmx_stats_message(int estats);
275 /****************************************************
276 * Some statistics utilities for convenience: useful when a complete data
277 * set is available already from another source, e.g. an xvg file.
278 ****************************************************/
280 * Fit a straight line y=ax thru the n data points x, y, return the
282 * \param[in] n number of points
283 * \param[in] x data points x
284 * \param[in] y data point y
285 * \param[out] a slope
288 int lsq_y_ax(int n, real x[], real y[], real *a);
291 * Fit a straight line y=ax+b thru the n data points x, y.
292 * \param[in] n number of points
293 * \param[in] x data points x
294 * \param[in] y data point y
295 * \param[out] a slope
296 * \param[out] b intercept
297 * \param[out] r correlation coefficient
298 * \param[out] chi2 quality of fit
301 int lsq_y_ax_b(int n, real x[], real y[], real *a, real *b, real *r,
304 /*! \copydoc lsq_y_ax_b
306 int lsq_y_ax_b_xdouble(int n, double x[], real y[],
307 real *a, real *b, real *r, real *chi2);
310 * Fit a straight line y=ax+b thru the n data points x, y.
311 * \param[in] n number of points
312 * \param[in] x data points x
313 * \param[in] y data point y
314 * \param[in] dy uncertainty in data point y
315 * \param[out] a slope
316 * \param[out] b intercept
317 * \param[out] da error in slope
318 * \param[out] db error in intercept
319 * \param[out] r correlation coefficient
320 * \param[out] chi2 quality of fit
323 int lsq_y_ax_b_error(int n, real x[], real y[], real dy[],
324 real *a, real *b, real *da, real *db,
325 real *r, real *chi2);