This patch moves the statistics library to its own dir.
[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 /*! \libinternal \file
42  *
43  * \brief
44  * Declares simple statistics toolbox
45  *
46  * \authors David van der Spoel <david.vanderspoel@icm.uu.se>
47  *
48  * \inlibraryapi
49  */
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 #include <stdio.h>
54 #include "types/simple.h"
55
56 //! Abstract container type
57 typedef struct gmx_stats *gmx_stats_t;
58
59 //! Error codes returned by the routines
60 enum {
61     estatsOK, estatsNO_POINTS, estatsNO_MEMORY, estatsERROR,
62     estatsINVALID_INPUT, estatsNOT_IMPLEMENTED, estatsNR
63 };
64
65 //! Enum for statistical weights
66 enum {
67     elsqWEIGHT_NONE, elsqWEIGHT_X, elsqWEIGHT_Y,
68     elsqWEIGHT_XY, elsqWEIGHT_NR
69 };
70
71 //! Enum determining which coordinate to histogram
72 enum {
73     ehistoX, ehistoY, ehistoNR
74 };
75
76 /*! \brief
77  * Initiate a data structure
78  * \return the data structure
79  */
80 gmx_stats_t gmx_stats_init();
81
82 /*! \brief
83  * Destroy a data structure
84  * \param stats The data structure
85  * \return error code
86  */
87 int gmx_stats_done(gmx_stats_t stats);
88
89 /*! \brief
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
94  * \return error code
95  */
96 int gmx_stats_remove_outliers(gmx_stats_t stats, double level);
97
98 /*! \brief
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
105  * \return error code
106  */
107 int gmx_stats_add_point(gmx_stats_t stats, double x, double y,
108                         double dx, double dy);
109
110 /*! \brief
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.
113  *
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
120  * \return error code
121  */
122 int gmx_stats_add_points(gmx_stats_t stats, int n, real *x, real *y,
123                          real *dx, real *dy);
124
125 /*! \brief
126  * Delivers data points from the statistics.
127  *
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
133  *  been returned.
134  *  If level > 0 then the outliers outside level*sigma are reported
135  * only.
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)
142  * \return error code
143  */
144 int gmx_stats_get_point(gmx_stats_t stats, real *x, real *y,
145                         real *dx, real *dy, real level);
146
147 /*! \brief
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
158  * \return error code
159  */
160 int gmx_stats_get_ab(gmx_stats_t stats, int weight,
161                      real *a, real *b,
162                      real *da, real *db, real *chi2, real *Rfit);
163
164 /*! \brief
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
173  * \return error code
174  */
175 int gmx_stats_get_a(gmx_stats_t stats, int weight,
176                     real *a, real *da, real *chi2, real *Rfit);
177
178 /*! \brief
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.
182  * \return error code
183  */
184 int gmx_stats_get_corr_coeff(gmx_stats_t stats, real *R);
185
186 /*! \brief
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.
190  * \return error code
191  */
192 int gmx_stats_get_rmsd(gmx_stats_t stats, real *rmsd);
193
194 /*! \brief
195  * Get the number of points.
196  * \param[in]  stats The data structure
197  * \param[out] N     number of data points
198  * \return error code
199  */
200 int gmx_stats_get_npoints(gmx_stats_t stats, int *N);
201
202 /*! \brief
203  * Computes and returns the average value.
204  * \param[in]  stats The data structure
205  * \param[out] aver  Average value
206  * \return error code
207  */
208 int gmx_stats_get_average(gmx_stats_t stats, real *aver);
209
210 /*! \brief
211  * Computes and returns the standard deviation.
212  * \param[in]  stats The data structure
213  * \param[out] sigma  Standard deviation
214  * \return error code
215  */
216 int gmx_stats_get_sigma(gmx_stats_t stats, real *sigma);
217
218 /*! \brief
219  * Computes and returns the standard error.
220  * \param[in]  stats The data structure
221  * \param[out] error Standard error
222  * \return error code
223  */
224 int gmx_stats_get_error(gmx_stats_t stats, real *error);
225
226 /*! \brief
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
232  * \return error code
233  */
234 int gmx_stats_get_ase(gmx_stats_t stats, real *aver, real *sigma, real *error);
235
236 /*! \brief
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
240  * \return error code
241  */
242 int gmx_stats_dump_xy(gmx_stats_t stats, FILE *fp);
243
244 /*! \brief
245  * Make a histogram of the data present.
246  *
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
263  * \return error code
264  */
265 int gmx_stats_make_histogram(gmx_stats_t stats, real binwidth, int *nbins,
266                              int ehisto,
267                              int normalized, real **x, real **y);
268
269 /*! \brief
270  * Return message belonging to error code
271  * \param[in] estats error code
272  */
273 const char *gmx_stats_message(int estats);
274
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  ****************************************************/
279 /*! \brief
280  * Fit a straight line y=ax thru the n data points x, y, return the
281  * slope in *a.
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
286  * \return error code
287  */
288 int lsq_y_ax(int n, real x[], real y[], real *a);
289
290 /*! \brief
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
299  * \return error code
300  */
301 int lsq_y_ax_b(int n, real x[], real y[], real *a, real *b, real *r,
302                real *chi2);
303
304 /*! \copydoc lsq_y_ax_b
305  */
306 int lsq_y_ax_b_xdouble(int n, double x[], real y[],
307                        real *a, real *b, real *r, real *chi2);
308
309 /*! \brief
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
321  * \return error code
322  */
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);
326
327 #ifdef __cplusplus
328 }
329 #endif
330
331 #endif