OpenMPCD
Gamma.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * Defines the `OpenMPCD::CUDA::Random::Distributions::Gamma` class.
4  */
5 
6 #ifndef OPENMPCD_CUDA_RANDOM_DISTRIBUTIONS_GAMMA_HPP
7 #define OPENMPCD_CUDA_RANDOM_DISTRIBUTIONS_GAMMA_HPP
8 
12 #include <OpenMPCD/Exceptions.hpp>
14 
15 #include <boost/static_assert.hpp>
16 #include <boost/type_traits/is_floating_point.hpp>
17 
18 
19 namespace OpenMPCD
20 {
21 namespace CUDA
22 {
23 namespace Random
24 {
25 namespace Distributions
26 {
27 
28 /**
29  * The gamma distribution.
30  *
31  * This is the distribution which has the probability density function
32  * \f[
33  * f \left(x ; k, \theta \right)
34  * =
35  * \frac
36  * {
37  * x^{k-1}
38  * \exp \left( - \frac{ x }{ \theta } \right)
39  * }
40  * {
41  * \Gamma \left( k \right)
42  * \theta^k
43  * }
44  * \f]
45  * where the parameters \f$ k \f$ and \f$ \theta \f$ are called `shape` and
46  * `scale`, respectively, and \f$ \Gamma \f$ is the gamma function.
47  *
48  * For further information, see e.g. @cite Devroye1986, chapter IX.3.
49  *
50  * @tparam T The underlying data type, which must be a floating-point type.
51  */
52 template<typename T>
53 class Gamma
54 {
55 public:
56  /**
57  * The constructor.
58  *
59  * @throw OpenMPCD::InvalidArgumentError
60  * If `OPENMPCD_DEBUG` is defined, throws if `shape < 0` or
61  * `scale < 0`.
62  *
63  * @param[in] shape The shape parameter \f$ k > 0 \f$.
64  * @param[in] scale The scale parameter \f$ \theta > 0 \f$.
65  */
67  Gamma(const T shape, const T scale)
68  : gamma_shape_ge_1(shape >= 1 ? shape : 1 + shape, scale),
69  inverse_k(shape >= 1 ? 0 : 1 / shape)
70  {
71  BOOST_STATIC_ASSERT(boost::is_floating_point<T>::value);
72 
75  }
76 
77 public:
78  /**
79  * Generates a random value sampled from the distribution.
80  *
81  * This function implements the algorithm in @cite Marsaglia2000, and
82  * additionally making use of the scaling property of the gamma
83  * distribution, i.e. the fact that if \f$ X \f$ is gamma-distributed with
84  * shape \f$ k \f$ and scale \f$ \theta \f$, then \f$ cX \f$ is
85  * gamma-distributed with shape \f$ k \f$ and scale \f$ c \theta \f$, given
86  * that \f$ c > 0 \f$ (see @cite Devroye1986, chapter IX.3).
87  *
88  * @tparam RNG The random number generator type.
89  *
90  * @param[in] rng The random number generator instance.
91  */
92  template<typename RNG>
94  T operator()(RNG& rng) const
95  {
96  if(inverse_k == 0)
97  return gamma_shape_ge_1(rng);
98 
100 
101  return gamma_shape_ge_1(rng) * pow(uniform0e1e(rng), inverse_k);
102  }
103 
104 private:
105  const Gamma_shape_ge_1<T> gamma_shape_ge_1;
106  ///< The underlying Gamma distribution with \f$ k \ge 1 \f$.
107 
108  const T inverse_k;
109  /**< The inverse \f$ k^{-1} \f$ of the shape parameter \f$ k \f$
110  if \f$ 0 < k < 1 \f$, and \f$ 0 \f$ otherwise, i.e.
111  if \f$ k \ge 1 \f$.*/
112 }; //class Gamma
113 
114 } //namespace Distributions
115 } //namespace Random
116 } //namespace CUDA
117 } //namespace OpenMPCD
118 
119 
120 #endif //OPENMPCD_CUDA_RANDOM_DISTRIBUTIONS_GAMMA_HPP
Exceptions.hpp
OpenMPCD::CUDA::DeviceCode::pow
OPENMPCD_CUDA_HOST_AND_DEVICE boost::enable_if< boost::is_integral< B >, double >::type pow(const B base, const double exponent)
The power function.
Definition: Utilities.hpp:50
Uniform0e1e.hpp
OpenMPCD::CUDA::Random::Distributions::Uniform0e1e
The uniform distribution in the open interval .
Definition: Uniform0e1e.hpp:30
OpenMPCD::CUDA::Random::Distributions::Gamma::Gamma
OPENMPCD_CUDA_DEVICE Gamma(const T shape, const T scale)
The constructor.
Definition: Gamma.hpp:67
OPENMPCD_CUDA_DEVICE
#define OPENMPCD_CUDA_DEVICE
Denotes a function to be callable from a CUDA Device.
Definition: Macros.hpp:25
OpenMPCD::CUDA::Random::Distributions::Gamma::operator()
OPENMPCD_CUDA_DEVICE T operator()(RNG &rng) const
Generates a random value sampled from the distribution.
Definition: Gamma.hpp:94
Gamma_shape_ge_1.hpp
OpenMPCD::RNG
boost::mt11213b RNG
The random number generator type.
Definition: Types.hpp:18
OPENMPCD_DEBUG_ASSERT.hpp
OPENMPCD_DEBUG_ASSERT_EXCEPTIONTYPE
#define OPENMPCD_DEBUG_ASSERT_EXCEPTIONTYPE(assertion, ExceptionType)
Definition: OPENMPCD_DEBUG_ASSERT.hpp:76
OpenMPCD::CUDA::Random::Distributions::Gamma
The gamma distribution.
Definition: Gamma.hpp:53
Macros.hpp
OpenMPCD::CUDA::Random::Distributions::Gamma_shape_ge_1
The gamma distribution, with shape parameter assumed to satisfy .
Definition: Gamma_shape_ge_1.hpp:54
OpenMPCD::InvalidArgumentException
Invalid argument exception.
Definition: Exceptions.hpp:128