2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016,2019, 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.
37 * \brief The gamma distribution
39 * Portable version of the gamma distribution that generates the same sequence
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>
49 * \ingroup module_random
52 #ifndef GMX_RANDOM_GAMMADISTRIBUTION_H
53 #define GMX_RANDOM_GAMMADISTRIBUTION_H
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
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
113 * p(x|\alpha,\beta) = \frac{1}{\Gamma(\alpha)\beta^{alpha}} x^{\alpha - 1}
114 * e^{-\frac{x}{\beta}}, x\geq 0 \f]
116 * \tparam RealType Floating-point type, real by default in GROMACS.
118 template<class RealType = real>
119 class GammaDistribution
122 /*! \brief Type of values returned */
123 typedef RealType result_type;
125 /*! \brief Gamma distribution parameters */
128 /*! \brief First parameter of gamma distribution */
130 /*! \brief Second parameter of gamma distribution */
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) :
148 /*! \brief Return first parameter */
149 result_type alpha() const { return alpha_; }
150 /*! \brief Return second parameter */
151 result_type beta() const { return beta_; }
153 /*! \brief True if two parameter sets will return the same distribution.
155 * \param x Instance to compare with.
157 bool operator==(const param_type& x) const
159 return alpha_ == x.alpha_ && beta_ == x.beta_;
162 /*! \brief True if two parameter sets will return different distributions
164 * \param x Instance to compare with.
166 bool operator!=(const param_type& x) const { return !operator==(x); }
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))
180 /*! \brief Construct new distribution from parameter class
182 * \param param Parameter class as defined inside gmx::GammaDistribution.
184 explicit GammaDistribution(const param_type& param) : param_(param) {}
186 /*! \brief Flush all internal saved values */
189 /*! \brief Return values from gamma distribution with internal parameters
191 * \tparam Rng Random engine class
193 * \param g Random engine
196 result_type operator()(Rng& g)
198 return (*this)(g, param_);
201 /*! \brief Return value from gamma distribution with given parameters
203 * \tparam Rng Random engine class
205 * \param g Random engine
206 * \param param Parameters to use
209 result_type operator()(Rng& g, const param_type& param)
211 result_type alpha = param.alpha();
212 UniformRealDistribution<result_type> uniformDist(0, 1);
213 ExponentialDistribution<result_type> expDist;
221 else if (alpha > 1.0)
223 const result_type b = alpha - 1.0;
224 const result_type c = 3.0 * alpha - result_type(0.75);
228 const result_type u = uniformDist(g);
229 const result_type v = uniformDist(g);
230 const result_type w = u * (1 - u);
234 const result_type y = std::sqrt(c / w) * (u - result_type(0.5));
239 const result_type z = 64 * w * w * w * v * v;
241 if (z <= 1.0 - 2.0 * y * y / x)
245 if (std::log(z) <= 2.0 * (b * std::log(x / b) - y))
257 const result_type u = uniformDist(g);
258 const result_type es = expDist(g);
260 if (u <= 1.0 - alpha)
262 x = std::pow(u, 1.0 / alpha);
271 const result_type e = -std::log((1.0 - u) / alpha);
272 x = std::pow(1.0 - alpha + alpha * e, 1.0 / alpha);
281 return x * param.beta();
284 /*! \brief Return the first parameter of gamma distribution */
285 result_type alpha() const { return param_.alpha(); }
287 /*! \brief Return the second parameter of gamma distribution */
288 result_type beta() const { return param_.beta(); }
290 /*! \brief Return the full parameter class of gamma distribution */
291 param_type param() const { return param_; }
293 /*! \brief Smallest value that can be returned from gamma distribution */
294 result_type min() const { return 0; }
296 /*! \brief Largest value that can be returned from gamma distribution */
297 result_type max() const { return std::numeric_limits<result_type>::infinity(); }
299 /*! \brief True if two gamma distributions will produce the same values.
301 * \param x Instance to compare with.
303 bool operator==(const GammaDistribution& x) const { return param_ == x.param_; }
305 /*! \brief True if two gamma distributions will produce different values.
307 * \param x Instance to compare with.
309 bool operator!=(const GammaDistribution& x) const { return !operator==(x); }
312 /*! \brief Internal value for parameters, can be overridden at generation time. */
315 GMX_DISALLOW_COPY_AND_ASSIGN(GammaDistribution);
320 #endif // GMX_RANDOM_GAMMADISTRIBUTION_H