8cf2db947d17a58135bd5672c1bc57e88eac4082
[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,2015,2019,2021, 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 /*! \libinternal \file
38  * \brief
39  * Declares simple statistics toolbox
40  *
41  * \authors David van der Spoel <david.vanderspoel@icm.uu.se>
42  * \inlibraryapi
43  */
44 #ifndef GMX_STATISTICS_H
45 #define GMX_STATISTICS_H
46
47 #include <cstdio>
48
49 #include "gromacs/utility/real.h"
50
51 //! Abstract container type
52 typedef struct gmx_stats* gmx_stats_t;
53
54 //! Error codes returned by the routines
55 enum class StatisticsStatus : int
56 {
57     Ok,
58     NoPoints,
59     Count
60 };
61
62 //! Enum for statistical weights
63 enum
64 {
65     elsqWEIGHT_NONE,
66     elsqWEIGHT_X,
67     elsqWEIGHT_Y,
68     elsqWEIGHT_XY,
69     elsqWEIGHT_NR
70 };
71
72 /*! \brief
73  * Initiate a data structure
74  * \return the data structure
75  */
76 gmx_stats_t gmx_stats_init();
77
78 /*! \brief
79  * Destroy a data structure
80  * \param stats The data structure
81  */
82 void gmx_stats_free(gmx_stats_t stats);
83
84 /*! \brief
85  * Add a point to the data set
86  * \param[in] stats The data structure
87  * \param[in] x   The x value
88  * \param[in] y   The y value
89  * \param[in] dx  The error in the x value
90  * \param[in] dy  The error in the y value
91  * \return error code
92  */
93 StatisticsStatus gmx_stats_add_point(gmx_stats_t stats, double x, double y, double dx, double dy);
94
95 /*! \brief
96  * Fit the data to y = ax + b, possibly weighted, if uncertainties
97  * have been input. da and db may be NULL.
98  * \param[in] stats The data structure
99  * \param[in] weight type of weighting
100  * \param[out] a slope
101  * \param[out] b intercept
102  * \param[out] da sigma in a
103  * \param[out] db sigma in b
104  * \param[out] chi2 normalized quality of fit
105  * \param[out] Rfit correlation coefficient
106  * \return error code
107  */
108 StatisticsStatus
109 gmx_stats_get_ab(gmx_stats_t stats, int weight, real* a, real* b, real* da, real* db, real* chi2, real* Rfit);
110
111 /*! \brief
112  * Computes and returns the average value.
113  * \param[in]  stats The data structure
114  * \param[out] aver  Average value
115  * \return error code
116  */
117 StatisticsStatus gmx_stats_get_average(gmx_stats_t stats, real* aver);
118
119 /*! \brief
120  * Pointers may be null, in which case no assignment will be done.
121  * \param[in]  stats The data structure
122  * \param[out] aver  Average value
123  * \param[out] sigma  Standard deviation
124  * \param[out] error Standard error
125  * \return error code
126  */
127 StatisticsStatus gmx_stats_get_ase(gmx_stats_t stats, real* aver, real* sigma, real* error);
128
129 /*! \brief Assert that statistics are OK
130  *
131  * \param[in] estats error code
132  */
133 void gmx_stats_message([[maybe_unused]] StatisticsStatus estats);
134
135 /****************************************************
136  * Some statistics utilities for convenience: useful when a complete data
137  * set is available already from another source, e.g. an xvg file.
138  ****************************************************/
139
140 /*! \brief
141  * Fit a straight line y=ax+b thru the n data points x, y.
142  * \param[in] n number of points
143  * \param[in] x data points x
144  * \param[in] y data point y
145  * \param[out] a slope
146  * \param[out] b intercept
147  * \param[out] r correlation coefficient
148  * \param[out] chi2 quality of fit
149  * \return error code
150  */
151 StatisticsStatus lsq_y_ax_b(int n, real x[], real y[], real* a, real* b, real* r, real* chi2);
152
153 /*! \copydoc lsq_y_ax_b
154  */
155 StatisticsStatus lsq_y_ax_b_xdouble(int n, double x[], real y[], real* a, real* b, real* r, real* chi2);
156
157 /*! \brief
158  * Fit a straight line y=ax+b thru the n data points x, y.
159  * \param[in] n number of points
160  * \param[in] x data points x
161  * \param[in] y data point y
162  * \param[in] dy uncertainty in data point y
163  * \param[out] a slope
164  * \param[out] b intercept
165  * \param[out] da error in slope
166  * \param[out] db error in intercept
167  * \param[out] r correlation coefficient
168  * \param[out] chi2 quality of fit
169  * \return error code
170  */
171 StatisticsStatus
172 lsq_y_ax_b_error(int n, real x[], real y[], real dy[], real* a, real* b, real* da, real* db, real* r, real* chi2);
173
174 #endif