Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / statistics / statistics.h
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) 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.
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
38 #ifndef GMX_STATISTICS_H
39 #define GMX_STATISTICS_H
40
41 #include <stdio.h>
42
43 #include "gromacs/utility/real.h"
44
45 /*! \libinternal \file
46  *
47  * \brief
48  * Declares simple statistics toolbox
49  *
50  * \authors David van der Spoel <david.vanderspoel@icm.uu.se>
51  *
52  * \inlibraryapi
53  */
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57
58 //! Abstract container type
59 typedef struct gmx_stats *gmx_stats_t;
60
61 //! Error codes returned by the routines
62 enum {
63     estatsOK, estatsNO_POINTS, estatsNO_MEMORY, estatsERROR,
64     estatsINVALID_INPUT, estatsNOT_IMPLEMENTED, estatsNR
65 };
66
67 //! Enum for statistical weights
68 enum {
69     elsqWEIGHT_NONE, elsqWEIGHT_X, elsqWEIGHT_Y,
70     elsqWEIGHT_XY, elsqWEIGHT_NR
71 };
72
73 //! Enum determining which coordinate to histogram
74 enum {
75     ehistoX, ehistoY, ehistoNR
76 };
77
78 /*! \brief
79  * Initiate a data structure
80  * \return the data structure
81  */
82 gmx_stats_t gmx_stats_init();
83
84 /*! \brief
85  * Destroy a data structure
86  * \param stats The data structure
87  * \return error code
88  */
89 int gmx_stats_done(gmx_stats_t stats);
90
91 /*! \brief
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
96  * \return error code
97  */
98 int gmx_stats_remove_outliers(gmx_stats_t stats, double level);
99
100 /*! \brief
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
107  * \return error code
108  */
109 int gmx_stats_add_point(gmx_stats_t stats, double x, double y,
110                         double dx, double dy);
111
112 /*! \brief
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.
115  *
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
122  * \return error code
123  */
124 int gmx_stats_add_points(gmx_stats_t stats, int n, real *x, real *y,
125                          real *dx, real *dy);
126
127 /*! \brief
128  * Delivers data points from the statistics.
129  *
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
135  *  been returned.
136  *  If level > 0 then the outliers outside level*sigma are reported
137  * only.
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)
144  * \return error code
145  */
146 int gmx_stats_get_point(gmx_stats_t stats, real *x, real *y,
147                         real *dx, real *dy, real level);
148
149 /*! \brief
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
160  * \return error code
161  */
162 int gmx_stats_get_ab(gmx_stats_t stats, int weight,
163                      real *a, real *b,
164                      real *da, real *db, real *chi2, real *Rfit);
165
166 /*! \brief
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
175  * \return error code
176  */
177 int gmx_stats_get_a(gmx_stats_t stats, int weight,
178                     real *a, real *da, real *chi2, real *Rfit);
179
180 /*! \brief
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.
184  * \return error code
185  */
186 int gmx_stats_get_corr_coeff(gmx_stats_t stats, real *R);
187
188 /*! \brief
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.
192  * \return error code
193  */
194 int gmx_stats_get_rmsd(gmx_stats_t stats, real *rmsd);
195
196 /*! \brief
197  * Get the number of points.
198  * \param[in]  stats The data structure
199  * \param[out] N     number of data points
200  * \return error code
201  */
202 int gmx_stats_get_npoints(gmx_stats_t stats, int *N);
203
204 /*! \brief
205  * Computes and returns the average value.
206  * \param[in]  stats The data structure
207  * \param[out] aver  Average value
208  * \return error code
209  */
210 int gmx_stats_get_average(gmx_stats_t stats, real *aver);
211
212 /*! \brief
213  * Computes and returns the standard deviation.
214  * \param[in]  stats The data structure
215  * \param[out] sigma  Standard deviation
216  * \return error code
217  */
218 int gmx_stats_get_sigma(gmx_stats_t stats, real *sigma);
219
220 /*! \brief
221  * Computes and returns the standard error.
222  * \param[in]  stats The data structure
223  * \param[out] error Standard error
224  * \return error code
225  */
226 int gmx_stats_get_error(gmx_stats_t stats, real *error);
227
228 /*! \brief
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
234  * \return error code
235  */
236 int gmx_stats_get_ase(gmx_stats_t stats, real *aver, real *sigma, real *error);
237
238 /*! \brief
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
242  * \return error code
243  */
244 int gmx_stats_dump_xy(gmx_stats_t stats, FILE *fp);
245
246 /*! \brief
247  * Make a histogram of the data present.
248  *
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
265  * \return error code
266  */
267 int gmx_stats_make_histogram(gmx_stats_t stats, real binwidth, int *nbins,
268                              int ehisto,
269                              int normalized, real **x, real **y);
270
271 /*! \brief
272  * Return message belonging to error code
273  * \param[in] estats error code
274  */
275 const char *gmx_stats_message(int estats);
276
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  ****************************************************/
281 /*! \brief
282  * Fit a straight line y=ax thru the n data points x, y, return the
283  * slope in *a.
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
288  * \return error code
289  */
290 int lsq_y_ax(int n, real x[], real y[], real *a);
291
292 /*! \brief
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
301  * \return error code
302  */
303 int lsq_y_ax_b(int n, real x[], real y[], real *a, real *b, real *r,
304                real *chi2);
305
306 /*! \copydoc lsq_y_ax_b
307  */
308 int lsq_y_ax_b_xdouble(int n, double x[], real y[],
309                        real *a, real *b, real *r, real *chi2);
310
311 /*! \brief
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
323  * \return error code
324  */
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);
328
329 #ifdef __cplusplus
330 }
331 #endif
332
333 #endif