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,2015, 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 /*! \libinternal \file
39 * Declares simple statistics toolbox
41 * \authors David van der Spoel <david.vanderspoel@icm.uu.se>
44 #ifndef GMX_STATISTICS_H
45 #define GMX_STATISTICS_H
49 #include "gromacs/utility/real.h"
51 //! Abstract container type
52 typedef struct gmx_stats
*gmx_stats_t
;
54 //! Error codes returned by the routines
56 estatsOK
, estatsNO_POINTS
, estatsNO_MEMORY
, estatsERROR
,
57 estatsINVALID_INPUT
, estatsNOT_IMPLEMENTED
, estatsNR
60 //! Enum for statistical weights
62 elsqWEIGHT_NONE
, elsqWEIGHT_X
, elsqWEIGHT_Y
,
63 elsqWEIGHT_XY
, elsqWEIGHT_NR
66 //! Enum determining which coordinate to histogram
68 ehistoX
, ehistoY
, ehistoNR
72 * Initiate a data structure
73 * \return the data structure
75 gmx_stats_t
gmx_stats_init();
78 * Destroy a data structure
79 * \param stats The data structure
81 void gmx_stats_free(gmx_stats_t stats
);
84 * Remove outliers from a straight line, where level in units of
85 * sigma. Level needs to be larger than one obviously.
86 * \param[in] stats The data structure
87 * \param[in] level The sigma level
90 int gmx_stats_remove_outliers(gmx_stats_t stats
, double level
);
93 * Add a point to the data set
94 * \param[in] stats The data structure
95 * \param[in] x The x value
96 * \param[in] y The y value
97 * \param[in] dx The error in the x value
98 * \param[in] dy The error in the y value
101 int gmx_stats_add_point(gmx_stats_t stats
, double x
, double y
,
102 double dx
, double dy
);
105 * Add a series of datapoints at once. The arrays dx and dy may
106 * be NULL in that case zero uncertainties will be assumed.
108 * \param[in] stats The data structure
109 * \param[in] n Number of points
110 * \param[in] x The array of x values
111 * \param[in] y The array of y values
112 * \param[in] dx The error in the x value
113 * \param[in] dy The error in the y value
116 int gmx_stats_add_points(gmx_stats_t stats
, int n
, real
*x
, real
*y
,
120 * Delivers data points from the statistics.
122 * Should be used in a while loop. Variables for either
123 * pointer may be NULL, in which case the routine can be used as an
124 * expensive point counter.
125 * Return the data points one by one. Return estatsOK while there are
126 * more points, and returns estatsNOPOINTS when the last point has
128 * If level > 0 then the outliers outside level*sigma are reported
130 * \param[in] stats The data structure
131 * \param[out] x The array of x values
132 * \param[out] y The array of y values
133 * \param[out] dx The error in the x value
134 * \param[out] dy The error in the y value
135 * \param[in] level sigma level (see above)
138 int gmx_stats_get_point(gmx_stats_t stats
, real
*x
, real
*y
,
139 real
*dx
, real
*dy
, real level
);
142 * Fit the data to y = ax + b, possibly weighted, if uncertainties
143 * have been input. da and db may be NULL.
144 * \param[in] stats The data structure
145 * \param[in] weight type of weighting
146 * \param[out] a slope
147 * \param[out] b intercept
148 * \param[out] da sigma in a
149 * \param[out] db sigma in b
150 * \param[out] chi2 normalized quality of fit
151 * \param[out] Rfit correlation coefficient
154 int gmx_stats_get_ab(gmx_stats_t stats
, int weight
,
156 real
*da
, real
*db
, real
*chi2
, real
*Rfit
);
159 * Fit the data to y = ax, possibly weighted, if uncertainties have
160 * have been input. da and db may be NULL.
161 * \param[in] stats The data structure
162 * \param[in] weight type of weighting
163 * \param[out] a slope
164 * \param[out] da sigma in a
165 * \param[out] chi2 normalized quality of fit
166 * \param[out] Rfit correlation coefficient
169 int gmx_stats_get_a(gmx_stats_t stats
, int weight
,
170 real
*a
, real
*da
, real
*chi2
, real
*Rfit
);
173 * Get the correlation coefficient.
174 * \param[in] stats The data structure
175 * \param[out] R the correlation coefficient between the data (x and y) as input to the structure.
178 int gmx_stats_get_corr_coeff(gmx_stats_t stats
, real
*R
);
181 * Get the root mean square deviation.
182 * \param[in] stats The data structure
183 * \param[out] rmsd the root mean square deviation between x and y values.
186 int gmx_stats_get_rmsd(gmx_stats_t stats
, real
*rmsd
);
189 * Get the number of points.
190 * \param[in] stats The data structure
191 * \param[out] N number of data points
194 int gmx_stats_get_npoints(gmx_stats_t stats
, int *N
);
197 * Computes and returns the average value.
198 * \param[in] stats The data structure
199 * \param[out] aver Average value
202 int gmx_stats_get_average(gmx_stats_t stats
, real
*aver
);
205 * Computes and returns the standard deviation.
206 * \param[in] stats The data structure
207 * \param[out] sigma Standard deviation
210 int gmx_stats_get_sigma(gmx_stats_t stats
, real
*sigma
);
213 * Computes and returns the standard error.
214 * \param[in] stats The data structure
215 * \param[out] error Standard error
218 int gmx_stats_get_error(gmx_stats_t stats
, real
*error
);
221 * Pointers may be null, in which case no assignment will be done.
222 * \param[in] stats The data structure
223 * \param[out] aver Average value
224 * \param[out] sigma Standard deviation
225 * \param[out] error Standard error
228 int gmx_stats_get_ase(gmx_stats_t stats
, real
*aver
, real
*sigma
, real
*error
);
231 * Dump the x, y, dx, dy data to a text file
232 * \param[in] stats The data structure
233 * \param[in] fp File pointer
236 int gmx_stats_dump_xy(gmx_stats_t stats
, FILE *fp
);
239 * Make a histogram of the data present.
241 * Uses either binwidth to
242 * determine the number of bins, or nbins to determine the binwidth,
243 * therefore one of these should be zero, but not the other. If *nbins = 0
244 * the number of bins will be returned in this variable. ehisto should be one of
245 * ehistoX or ehistoY. If
246 * normalized not equal to zero, the integral of the histogram will be
247 * normalized to one. The output is in two arrays, *x and *y, to which
248 * you should pass a pointer. Memory for the arrays will be allocated
249 * as needed. Function returns one of the estats codes.
250 * \param[in] stats The data structure
251 * \param[in] binwidth For the histogram
252 * \param[in] nbins Number of bins
253 * \param[in] ehisto Type (see enum above)
254 * \param[in] normalized see above
255 * \param[out] x see above
256 * \param[out] y see above
259 int gmx_stats_make_histogram(gmx_stats_t stats
, real binwidth
, int *nbins
,
261 int normalized
, real
**x
, real
**y
);
264 * Return message belonging to error code
265 * \param[in] estats error code
267 const char *gmx_stats_message(int estats
);
269 /****************************************************
270 * Some statistics utilities for convenience: useful when a complete data
271 * set is available already from another source, e.g. an xvg file.
272 ****************************************************/
274 * Fit a straight line y=ax thru the n data points x, y, return the
276 * \param[in] n number of points
277 * \param[in] x data points x
278 * \param[in] y data point y
279 * \param[out] a slope
282 int lsq_y_ax(int n
, real x
[], real y
[], real
*a
);
285 * Fit a straight line y=ax+b thru the n data points x, y.
286 * \param[in] n number of points
287 * \param[in] x data points x
288 * \param[in] y data point y
289 * \param[out] a slope
290 * \param[out] b intercept
291 * \param[out] r correlation coefficient
292 * \param[out] chi2 quality of fit
295 int lsq_y_ax_b(int n
, real x
[], real y
[], real
*a
, real
*b
, real
*r
,
298 /*! \copydoc lsq_y_ax_b
300 int lsq_y_ax_b_xdouble(int n
, double x
[], real y
[],
301 real
*a
, real
*b
, real
*r
, real
*chi2
);
304 * Fit a straight line y=ax+b thru the n data points x, y.
305 * \param[in] n number of points
306 * \param[in] x data points x
307 * \param[in] y data point y
308 * \param[in] dy uncertainty in data point y
309 * \param[out] a slope
310 * \param[out] b intercept
311 * \param[out] da error in slope
312 * \param[out] db error in intercept
313 * \param[out] r correlation coefficient
314 * \param[out] chi2 quality of fit
317 int lsq_y_ax_b_error(int n
, real x
[], real y
[], real dy
[],
318 real
*a
, real
*b
, real
*da
, real
*db
,
319 real
*r
, real
*chi2
);