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)
69 if (alpha < static_cast<RealType>(0.6L))
85 d =
std::pow(alpha, alpha / (1 - alpha)) * (1 - alpha);
89 d = alpha -
static_cast<RealType
>(1) / 3;
105 template <
typename RealType>
115 void reset() { constant_.reset(alpha(), beta()); }
120 template <
typename RNGType>
124 return generate(rng, param_, constant_);
129 return generate(rng, param, constant);
132 template <
typename RNGType>
139 r = generate_t(rng, param, constant);
142 r = generate_w(rng, param, constant);
145 r = generate_n(rng, param, constant);
148 r = generate_e(rng, param, constant);
152 return param.
beta() * r;
155 template <
typename RNGType>
163 if (u > constant.
d) {
166 u = constant.
d + param.
alpha() * u;
174 template <
typename RNGType>
186 }
while (u + e < constant.
d + r);
191 template <
typename RNGType>
204 v = 1 + constant.
c * w;
208 e = 1 -
static_cast<result_type>(0.0331) * (w * w) * (w * w);
210 return constant.
d * v;
212 e = w * w / 2 + constant.
d * (1 - v +
std::log(v));
214 return constant.
d * v;
218 template <
typename RNGType>
231 template <std::
size_t K,
typename RealType,
typename RNGType>
233 RealType *r, RealType alpha, RealType beta,
236 const RealType
d = constant.
d;
237 const RealType
c = constant.
c;
239 RealType *
const u = s;
240 RealType *
const e = s + n;
241 RealType *
const x = s + n * 2;
245 mul(n, static_cast<RealType>(-1), e, e);
246 for (std::size_t i = 0; i != n; ++i) {
250 u[i] = d + alpha * u[i];
260 for (std::size_t i = 0; i != n; ++i)
267 template <std::
size_t K,
typename RealType,
typename RNGType>
269 RealType *r, RealType, RealType beta,
272 const RealType
d = constant.
d;
273 const RealType
c = constant.
c;
275 RealType *
const u = s;
276 RealType *
const e = s + n;
277 RealType *
const x = s + n * 2;
281 mul(n * 2, static_cast<RealType>(-1), s, s);
290 for (std::size_t i = 0; i != n; ++i)
297 template <std::
size_t K,
typename RealType,
typename RNGType>
299 RealType *r, RealType, RealType beta,
302 const RealType
d = constant.
d;
303 const RealType
c = constant.
c;
305 RealType *
const u = s;
306 RealType *
const e = s + n;
307 RealType *
const v = s + n * 2;
308 RealType *
const w = s + n * 3;
309 RealType *
const x = s + n * 4;
313 rng, n, w, static_cast<RealType>(0), static_cast<RealType>(1));
314 fma(n, c, w, static_cast<RealType>(1), v);
316 for (std::size_t i = 0; i != n; ++i) {
328 fma(n, -static_cast<RealType>(0.0331), e, static_cast<RealType>(1), e);
329 mul(n, d * beta, v, x);
332 for (std::size_t i = 0; i != n; ++i) {
336 e[i] = w[i] * w[i] / 2 + d * (1 - v[i] +
std::log(v[i]));
345 template <std::
size_t,
typename RealType,
typename RNGType>
347 RealType *r, RealType, RealType beta,
357 template <std::
size_t K,
typename RealType,
typename RNGType>
359 RealType *r, RealType alpha, RealType beta,
364 return gamma_distribution_impl_t<K>(
365 rng, n, r, alpha, beta, constant);
367 return gamma_distribution_impl_w<K>(
368 rng, n, r, alpha, beta, constant);
370 return gamma_distribution_impl_n<K>(
371 rng, n, r, alpha, beta, constant);
373 return gamma_distribution_impl_e<K>(
374 rng, n, r, alpha, beta, constant);
383 template <
typename RealType,
typename RNGType>
385 RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta)
387 static_assert(std::is_floating_point<RealType>::value,
388 "**gamma_distribution** USED WITH RealType OTHER THAN FLOATING POINT " 391 const std::size_t k = 1024;
394 std::size_t m = internal::gamma_distribution_impl<k>(
395 rng, k, r, alpha, beta, constant);
402 internal::gamma_distribution_impl<k>(rng, n, r, alpha, beta, constant);
407 for (std::size_t i = 0; i != n; ++i)
416 #endif // VSMC_RNG_GAMMA_DISTRIBUTION_HPP
void mul(std::size_t n, const float *a, const float *b, float *y)
#define VSMC_DEFINE_RNG_DISTRIBUTION_2(Name, name, p1, v1, p2, v2)
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 u01_distribution(RNGType &, std::size_t, RealType *)
Generate standard uniform random variates.
void reset(RealType alpha, RealType)
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)
result_type alpha() const
bool gamma_distribution_check_param(RealType alpha, RealType beta)
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)
Standard uniform distribution.
#define VSMC_DEFINE_RNG_DISTRIBUTION_RAND_2(Name, name, p1, p2)
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)