Merge branch release-2016
[gromacs.git] / src / gromacs / random / gammadistribution.h
blobac850cfeb87603918218b2c91ad968dc71ab08aa
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
36 /*! \file
37 * \brief The gamma distribution
39 * Portable version of the gamma distribution that generates the same sequence
40 * on all platforms.
42 * \note The gamma distribution is broken in some standard library headers
43 * (including those shipped with gcc-4.9), and it is not guaranteed to
44 * generate the same result on stdlibc++ and libc++. Use this one instead so
45 * our unit tests produce the same values on all platforms.
47 * \author Erik Lindahl <erik.lindahl@gmail.com>
48 * \inpublicapi
49 * \ingroup module_random
52 #ifndef GMX_RANDOM_GAMMADISTRIBUTION_H
53 #define GMX_RANDOM_GAMMADISTRIBUTION_H
55 #include <cmath>
57 #include <limits>
59 #include "gromacs/random/exponentialdistribution.h"
60 #include "gromacs/random/uniformrealdistribution.h"
61 #include "gromacs/utility/classhelpers.h"
64 * The workaround implementation for the broken std::gamma_distribution in the
65 * gcc-4.6 headers has been modified from the LLVM libcxx headers, distributed
66 * under the MIT license:
68 * Copyright (c) The LLVM compiler infrastructure
70 * Permission is hereby granted, free of charge, to any person obtaining a copy
71 * of this software and associated documentation files (the "Software"), to deal
72 * in the Software without restriction, including without limitation the rights
73 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
74 * copies of the Software, and to permit persons to whom the Software is
75 * furnished to do so, subject to the following conditions:
77 * The above copyright notice and this permission notice shall be included in
78 * all copies or substantial portions of the Software.
80 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
81 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
82 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
83 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
84 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
85 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
86 * THE SOFTWARE.
89 namespace gmx
92 /*! \brief Gamma distribution
94 * The C++ standard library does provide a gamma distribution, but when
95 * using libstdc++-4.4.7 with at least gcc-4.6 or icc-14.0 the headers
96 * produce errors. Even for newer compilers, libstdc++ and libc++ appear to
97 * use different algorithms to generate it, which means their values differ
98 * in contrast to the uniform and normal distributions where they are
99 * identical. To avoid both compiler bugs and make it easier to use
100 * GROMACS unit tests that depend on random numbers, we have our
101 * own implementation.
103 * Be warned that the gamma distribution works like the standard
104 * normal distribution and keeps drawing values from the random engine
105 * in a loop, so you want to make sure you use a random stream with a
106 * very large margin to make sure you do not run out of random numbers
107 * in an unlucky case (which will lead to an exception with the GROMACS
108 * default random engine).
110 * The gamma distribution is defined as
112 * \f[
113 * p(x|\alpha,\beta) = \frac{1}{\Gamma(\alpha)\beta^{alpha}} x^{\alpha - 1} e^{-\frac{x}{\beta}}, x\geq 0
114 * \f]
116 * \tparam RealType Floating-point type, real by default in GROMACS.
118 template<class RealType = real>
119 class GammaDistribution
121 public:
122 /*! \brief Type of values returned */
123 typedef RealType result_type;
125 /*! \brief Gamma distribution parameters */
126 class param_type
128 /*! \brief First parameter of gamma distribution */
129 result_type alpha_;
130 /*! \brief Second parameter of gamma distribution */
131 result_type beta_;
133 public:
134 /*! \brief Reference back to the distribution class */
135 typedef GammaDistribution distribution_type;
137 /*! \brief Construct parameter block
139 * \param alpha First parameter of gamma distribution
140 * \param beta Second parameter of gamma distribution
142 explicit param_type(result_type alpha = 1.0, result_type beta = 1.0)
143 : alpha_(alpha), beta_(beta) {}
145 /*! \brief Return first parameter */
146 result_type alpha() const { return alpha_; }
147 /*! \brief Return second parameter */
148 result_type beta() const { return beta_; }
150 /*! \brief True if two parameter sets will return the same distribution.
152 * \param x Instance to compare with.
154 bool
155 operator==(const param_type &x) const
157 return alpha_ == x.alpha_ && beta_ == x.beta_;
160 /*! \brief True if two parameter sets will return different distributions
162 * \param x Instance to compare with.
164 bool
165 operator!=(const param_type &x) const { return !operator==(x); }
168 public:
170 /*! \brief Construct new distribution with given floating-point parameters.
172 * \param alpha First parameter of gamma distribution
173 * \param beta Second parameter of gamma distribution
175 explicit GammaDistribution(result_type alpha = 1.0, result_type beta = 1.0)
176 : param_(param_type(alpha, beta)) {}
178 /*! \brief Construct new distribution from parameter class
180 * \param param Parameter class as defined inside gmx::GammaDistribution.
182 explicit GammaDistribution(const param_type &param) : param_(param) {}
184 /*! \brief Flush all internal saved values */
185 void
186 reset() {}
188 /*! \brief Return values from gamma distribution with internal parameters
190 * \tparam Rng Random engine class
192 * \param g Random engine
194 template<class Rng>
195 result_type
196 operator()(Rng &g) { return (*this)(g, param_); }
198 /*! \brief Return value from gamma distribution with given parameters
200 * \tparam Rng Random engine class
202 * \param g Random engine
203 * \param param Parameters to use
205 template<class Rng>
206 result_type
207 operator()(Rng &g, const param_type &param)
209 result_type alpha = param.alpha();
210 UniformRealDistribution<result_type> uniformDist(0, 1);
211 ExponentialDistribution<result_type> expDist;
213 result_type x;
215 if (alpha == 1.0)
217 x = expDist(g);
219 else if (alpha > 1.0)
221 const result_type b = alpha - 1.0;
222 const result_type c = 3.0 * alpha - result_type(0.75);
224 while (true)
226 const result_type u = uniformDist(g);
227 const result_type v = uniformDist(g);
228 const result_type w = u * (1 - u);
230 if (w != 0)
232 const result_type y = std::sqrt(c / w) *
233 (u - result_type(0.5));
234 x = b + y;
236 if (x >= 0)
238 const result_type z = 64 * w * w * w * v * v;
240 if (z <= 1.0 - 2.0 * y * y / x)
242 break;
244 if (std::log(z) <= 2.0 * (b * std::log(x / b) - y))
246 break;
252 else // __a < 1
254 while (true)
256 const result_type u = uniformDist(g);
257 const result_type es = expDist(g);
259 if (u <= 1.0 - alpha)
261 x = std::pow(u, 1.0 / alpha);
263 if (x <= es)
265 break;
268 else
270 const result_type e = -std::log((1.0 - u)/alpha);
271 x = std::pow(1.0 - alpha + alpha * e, 1.0 / alpha);
273 if (x <= e + es)
275 break;
280 return x * param.beta();
283 /*! \brief Return the first parameter of gamma distribution */
284 result_type
285 alpha() const { return param_.alpha(); }
287 /*! \brief Return the second parameter of gamma distribution */
288 result_type
289 beta() const { return param_.beta(); }
291 /*! \brief Return the full parameter class of gamma distribution */
292 param_type param() const { return param_; }
294 /*! \brief Smallest value that can be returned from gamma distribution */
295 result_type
296 min() const { return 0; }
298 /*! \brief Largest value that can be returned from gamma distribution */
299 result_type
300 max() const { return std::numeric_limits<result_type>::infinity(); }
302 /*! \brief True if two gamma distributions will produce the same values.
304 * \param x Instance to compare with.
306 bool
307 operator==(const GammaDistribution &x) const
308 { return param_ == x.param_; }
310 /*! \brief True if two gamma distributions will produce different values.
312 * \param x Instance to compare with.
314 bool
315 operator!=(const GammaDistribution &x) const
316 { return !operator==(x); }
318 private:
319 /*! \brief Internal value for parameters, can be overridden at generation time. */
320 param_type param_;
322 GMX_DISALLOW_COPY_AND_ASSIGN(GammaDistribution);
325 } // namespace gmx
327 #endif // GMX_RANDOM_GAMMADISTRIBUTION_H