32 #ifndef VSMC_RNG_GAMMA_DISTRIBUTION_HPP 33 #define VSMC_RNG_GAMMA_DISTRIBUTION_HPP 45 template <
typename RealType>
48 return alpha > 0 && beta > 0;
58 template <
typename RealType>
67 void reset(RealType alpha, RealType)
85 d =
std::pow(alpha, alpha / (1 - alpha)) * (1 - alpha);
89 d = alpha -
static_cast<RealType
>(1) / 3;
105 template <
typename RealType>
117 return std::numeric_limits<result_type>::max
VSMC_MNE();
120 void reset() { constant_.reset(alpha(), beta()); }
125 template <
typename RNGType>
129 return generate(rng, param_, constant_);
134 return generate(rng, param, constant);
137 template <
typename RNGType>
144 r = generate_t(rng, param, constant);
147 r = generate_w(rng, param, constant);
150 r = generate_n(rng, param, constant);
153 r = generate_e(rng, param, constant);
157 return param.
beta() * r;
160 template <
typename RNGType>
168 if (u > constant.
d) {
171 u = constant.
d + param.
alpha() * u;
179 template <
typename RNGType>
191 }
while (u + e < constant.
d + r);
196 template <
typename RNGType>
209 v = 1 + constant.
c * w;
213 e = 1 -
static_cast<result_type>(0.0331) * (w * w) * (w * w);
215 return constant.
d * v;
217 e = w * w / 2 + constant.
d * (1 - v +
std::log(v));
219 return constant.
d * v;
223 template <
typename RNGType>
236 template <std::
size_t K,
typename RealType,
typename RNGType>
238 RealType *r, RealType alpha, RealType beta,
241 const RealType
d = constant.
d;
242 const RealType
c = constant.
c;
244 RealType *
const u = s;
245 RealType *
const e = s + n;
246 RealType *
const x = s + n * 2;
250 mul(n, static_cast<RealType>(-1), e, e);
251 for (std::size_t i = 0; i != n; ++i) {
255 u[i] = d + alpha * u[i];
265 for (std::size_t i = 0; i != n; ++i)
272 template <std::
size_t K,
typename RealType,
typename RNGType>
274 RealType *r, RealType, RealType beta,
277 const RealType
d = constant.
d;
278 const RealType
c = constant.
c;
280 RealType *
const u = s;
281 RealType *
const e = s + n;
282 RealType *
const x = s + n * 2;
286 mul(n * 2, static_cast<RealType>(-1), s, s);
295 for (std::size_t i = 0; i != n; ++i)
302 template <std::
size_t K,
typename RealType,
typename RNGType>
304 RealType *r, RealType, RealType beta,
307 const RealType
d = constant.
d;
308 const RealType
c = constant.
c;
310 RealType *
const u = s;
311 RealType *
const e = s + n;
312 RealType *
const v = s + n * 2;
313 RealType *
const w = s + n * 3;
314 RealType *
const x = s + n * 4;
318 rng, n, w, static_cast<RealType>(0), static_cast<RealType>(1));
319 fma(n, c, w, static_cast<RealType>(1), v);
321 for (std::size_t i = 0; i != n; ++i) {
333 fma(n, -static_cast<RealType>(0.0331), e, static_cast<RealType>(1), e);
334 mul(n, d * beta, v, x);
337 for (std::size_t i = 0; i != n; ++i) {
341 e[i] = w[i] * w[i] / 2 + d * (1 - v[i] +
std::log(v[i]));
350 template <std::
size_t,
typename RealType,
typename RNGType>
352 RealType *r, RealType, RealType beta,
362 template <std::
size_t K,
typename RealType,
typename RNGType>
364 RealType *r, RealType alpha, RealType beta,
369 return gamma_distribution_impl_t<K>(
370 rng, n, r, alpha, beta, constant);
372 return gamma_distribution_impl_w<K>(
373 rng, n, r, alpha, beta, constant);
375 return gamma_distribution_impl_n<K>(
376 rng, n, r, alpha, beta, constant);
378 return gamma_distribution_impl_e<K>(
379 rng, n, r, alpha, beta, constant);
388 template <
typename RealType,
typename RNGType>
390 RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta)
392 const std::size_t k = 1000;
395 std::size_t m = internal::gamma_distribution_impl<k>(
396 rng, k, r, alpha, beta, constant);
403 internal::gamma_distribution_impl<k>(rng, n, r, alpha, beta, constant);
408 for (std::size_t i = 0; i != n; ++i)
413 template <
typename RealType,
typename RNGType>
415 std::size_t n, RealType *r)
422 #endif // VSMC_RNG_GAMMA_DISTRIBUTION_HPP
Standard uniform distribution with open/closed variants.
void mul(std::size_t n, const float *a, const float *b, float *y)
void sqrt(std::size_t n, const float *a, float *y)
std::size_t gamma_distribution_impl_n(RNGType &rng, std::size_t n, RealType *r, RealType, RealType beta, const GammaDistributionConstant< RealType > &constant)
GammaDistributionAlgorithm algorithm
GammaDistributionConstant(RealType alpha=1, RealType beta=1)
void rng_rand(RNGType &rng, BernoulliDistribution< IntType > &dist, std::size_t n, IntType *r)
void reset(RealType alpha, RealType)
#define VSMC_DEFINE_RNG_DISTRIBUTION_OPERATORS
void normal_distribution(RNGType &, std::size_t, RealType *, RealType, RealType)
Generating normal random variates.
std::size_t gamma_distribution_impl_t(RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta, const GammaDistributionConstant< RealType > &constant)
void pow(std::size_t n, const float *a, const float *b, float *y)
#define VSMC_DEFINE_RNG_DISTRIBUTION_2(Name, name, T, T1, p1, v1, T2, p2, v2)
result_type alpha() const
bool gamma_distribution_check_param(RealType alpha, RealType beta)
void u01_co_distribution(RNGType &rng, std::size_t n, RealType *r)
Generate standard uniform random variates on closed-open interval.
void exp(std::size_t n, const float *a, float *y)
std::size_t gamma_distribution_impl(RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta, const GammaDistributionConstant< RealType > &constant)
std::size_t gamma_distribution_impl_e(RNGType &rng, std::size_t n, RealType *r, RealType, RealType beta, const GammaDistributionConstant< RealType > &)
std::size_t gamma_distribution_impl_w(RNGType &rng, std::size_t n, RealType *r, RealType, RealType beta, const GammaDistributionConstant< RealType > &constant)
void fma(std::size_t n, const T *a, const T *b, const T *c, T *y)
For , compute .
void add(std::size_t n, const float *a, const float *b, float *y)
void gamma_distribution(RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta)
Generating gamma random variates.
void log(std::size_t n, const float *a, float *y)
void sqr(std::size_t n, const float *a, float *y)
GammaDistributionAlgorithm
void abs(std::size_t n, const float *a, float *y)