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.6))
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()); }
122 if (!
is_equal(constant_.
d, other.constant_.d))
124 if (!
is_equal(constant_.
c, other.constant_.c))
131 template <
typename CharT,
typename Traits>
132 void ostream(std::basic_ostream<CharT, Traits> &)
const 136 template <
typename CharT,
typename Traits>
137 void istream(std::basic_istream<CharT, Traits> &)
142 template <
typename RNGType>
146 return generate(rng, param_, constant_);
151 return generate(rng, param, constant);
154 template <
typename RNGType>
161 r = generate_t(rng, param, constant);
164 r = generate_w(rng, param, constant);
167 r = generate_n(rng, param, constant);
170 r = generate_e(rng, param, constant);
174 return param.
beta() * r;
177 template <
typename RNGType>
185 if (u > constant.
d) {
188 u = constant.
d + param.
alpha() * u;
196 template <
typename RNGType>
208 }
while (u + e < constant.
d + r);
213 template <
typename RNGType>
226 v = 1 + constant.
c * w;
230 e = 1 -
static_cast<result_type>(0.0331) * (w * w) * (w * w);
232 return constant.
d * v;
234 e = w * w / 2 + constant.
d * (1 - v +
std::log(v));
236 return constant.
d * v;
240 template <
typename RNGType>
253 template <std::
size_t K,
typename RealType,
typename RNGType>
255 RealType *r, RealType alpha, RealType beta,
258 const RealType
d = constant.
d;
259 const RealType
c = constant.
c;
261 RealType *
const u = s.data();
262 RealType *
const e = s.data() + n;
263 RealType *
const x = s.data() + n * 2;
267 mul(n, static_cast<RealType>(-1), e, e);
268 for (std::size_t i = 0; i != n; ++i) {
272 u[i] = d + alpha * u[i];
282 for (std::size_t i = 0; i != n; ++i)
289 template <std::
size_t K,
typename RealType,
typename RNGType>
291 RealType *r, RealType, RealType beta,
294 const RealType
d = constant.
d;
295 const RealType
c = constant.
c;
297 RealType *
const u = s.data();
298 RealType *
const e = s.data() + n;
299 RealType *
const x = s.data() + n * 2;
302 log(n * 2, s.data(), s.data());
303 mul(n * 2, static_cast<RealType>(-1), s.data(), s.data());
312 for (std::size_t i = 0; i != n; ++i)
319 template <std::
size_t K,
typename RealType,
typename RNGType>
321 RealType *r, RealType, RealType beta,
324 const RealType
d = constant.
d;
325 const RealType
c = constant.
c;
327 RealType *
const u = s.data();
328 RealType *
const e = s.data() + n;
329 RealType *
const v = s.data() + n * 2;
330 RealType *
const w = s.data() + n * 3;
331 RealType *
const x = s.data() + n * 4;
335 rng, n, w, static_cast<RealType>(0), static_cast<RealType>(1));
336 fma(n, c, w, static_cast<RealType>(1), v);
338 for (std::size_t i = 0; i != n; ++i) {
350 fma(n, -static_cast<RealType>(0.0331), e, static_cast<RealType>(1), e);
351 mul(n, d * beta, v, x);
354 for (std::size_t i = 0; i != n; ++i) {
358 e[i] = w[i] * w[i] / 2 + d * (1 - v[i] +
std::log(v[i]));
367 template <std::
size_t,
typename RealType,
typename RNGType>
369 RealType *r, RealType, RealType beta,
379 template <std::
size_t K,
typename RealType,
typename RNGType>
381 RealType *r, RealType alpha, RealType beta,
386 return gamma_distribution_impl_t<K>(
387 rng, n, r, alpha, beta, constant);
389 return gamma_distribution_impl_w<K>(
390 rng, n, r, alpha, beta, constant);
392 return gamma_distribution_impl_n<K>(
393 rng, n, r, alpha, beta, constant);
395 return gamma_distribution_impl_e<K>(
396 rng, n, r, alpha, beta, constant);
405 template <
typename RealType,
typename RNGType>
407 RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta)
409 static_assert(std::is_floating_point<RealType>::value,
410 "**gamma_distribution** USED WITH RealType OTHER THAN FLOATING POINT " 416 std::size_t m = internal::gamma_distribution_impl<k>(
417 rng, k, r, alpha, beta, constant);
424 internal::gamma_distribution_impl<k>(rng, n, r, alpha, beta, constant);
429 for (std::size_t i = 0; i != n; ++i)
438 #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::basic_ostream< CharT, Traits > & ostream(std::basic_ostream< CharT, Traits > &os, const std::array< T, N > &ary)
std::size_t gamma_distribution_impl_n(RNGType &rng, std::size_t n, RealType *r, RealType, RealType beta, const GammaDistributionConstant< RealType > &constant)
GammaDistributionAlgorithm algorithm
void u01_oo_distribution(RNGType &rng, std::size_t n, RealType *r)
Generate standard uniform random variates on (0, 1)
GammaDistributionConstant(RealType alpha=1, RealType beta=1)
void reset(RealType alpha, RealType)
Standard uniform distribution on (0, 1)
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)
RealType u01(UIntType u) noexcept
Convert uniform unsigned integers to floating points within [0, 1].
bool is_equal(const T &a, const T &b, std::true_type)
result_type alpha() const
bool gamma_distribution_check_param(RealType alpha, RealType beta)
GammaDistribution< RealType > distribution_type
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 .
std::basic_istream< CharT, Traits > & istream(std::basic_istream< CharT, Traits > &is, std::array< T, N > &ary)
void add(std::size_t n, const float *a, const float *b, float *y)
#define VSMC_DEFINE_RNG_DISTRIBUTION_RAND_2(Name, name, p1, p2)
std::array with proper alignment
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)