32 #ifndef VSMC_RNG_BETA_DISTRIBUTION_HPP 33 #define VSMC_RNG_BETA_DISTRIBUTION_HPP 45 template <
typename RealType>
48 return alpha > 0 && beta > 0;
63 template <
typename RealType>
72 void reset(RealType alpha, RealType beta)
74 const RealType K =
static_cast<RealType
>(0.852);
75 const RealType C =
static_cast<RealType
>(-0.956);
76 const RealType D = beta + K * alpha * alpha + C;
77 if (is_equal<RealType>(alpha, 0.5) && is_equal<RealType>(beta, 0.5))
79 else if (is_equal<RealType>(alpha, 1) && is_equal<RealType>(beta, 1))
81 else if (is_equal<RealType>(alpha, 1))
83 else if (is_equal<RealType>(beta, 1))
85 else if (alpha > 1 && beta > 1)
87 else if (alpha < 1 && beta < 1 && D <= 0)
89 else if (alpha < 1 && beta < 1)
91 else if (alpha < 1 && beta > 1)
93 else if (alpha > 1 && beta < 1)
106 b = std::min(alpha, beta);
123 p /=
p + alpha * (1 -
t);
155 template <
typename RealType>
165 void reset() { constant_.reset(alpha(), beta()); }
170 template <
typename RNGType>
174 return generate(rng, param_, constant_);
179 return generate(rng, param, constant);
182 template <
typename RNGType>
189 r = generate_as(rng, param, constant);
192 r = generate_11(rng, param, constant);
195 r = generate_1x(rng, param, constant);
198 r = generate_x1(rng, param, constant);
201 r = generate_c(rng, param, constant);
204 r = generate_j(rng, param, constant);
207 r = generate_a1(rng, param, constant);
210 r = generate_a2(rng, param, constant);
213 r = generate_a3(rng, param, constant);
220 template <
typename RNGType>
227 -const_pi_by2<result_type>() + const_pi<result_type>() * u);
233 template <
typename RNGType>
242 template <
typename RNGType>
251 template <
typename RNGType>
260 template <
typename RNGType>
265 const result_type ln_4 = 2 * const_ln_2<result_type>();
275 y = param.
beta() + x;
276 left = (constant.
p - constant.
a *
std::log(y)) +
277 (constant.
t * v - ln_4);
279 }
while (left < right);
284 template <
typename RNGType>
299 template <
typename RNGType>
309 if (u < constant.
p) {
310 x = constant.
t *
std::pow(u / constant.
p, constant.
a);
311 v = (1 - param.
beta()) *
std::log((1 - x) / (1 - constant.
t));
315 std::pow((1 - u) / (1 - constant.
p), constant.
b);
323 template <
typename RNGType>
333 if (u < constant.
p) {
334 x = constant.
t *
std::pow(u / constant.
p, constant.
a);
339 std::pow((1 - u) / (1 - constant.
p), constant.
b);
347 template <
typename RNGType>
357 if (u < constant.
p) {
358 x = constant.
t *
std::pow(u / constant.
p, constant.
a);
363 std::pow((1 - u) / (1 - constant.
p), constant.
b);
375 template <std::
size_t,
typename RealType,
typename RNGType>
377 RealType *r, RealType, RealType,
381 fma(n, const_pi<RealType>(), r, -const_pi_by2<RealType>(), r);
383 fma(n, static_cast<RealType>(0.5), r, static_cast<RealType>(0.5), r);
388 template <std::
size_t,
typename RealType,
typename RNGType>
390 RealType *r, RealType, RealType,
398 template <std::
size_t,
typename RealType,
typename RNGType>
400 RealType *r, RealType, RealType,
405 mul(n, constant.
b, r, r);
407 sub(n, static_cast<RealType>(1), r, r);
412 template <std::
size_t,
typename RealType,
typename RNGType>
414 RealType *r, RealType, RealType,
419 mul(n, constant.
a, r, r);
425 template <std::
size_t K,
typename RealType,
typename RNGType>
427 RealType *r, RealType alpha, RealType beta,
430 const RealType
a = constant.
a;
431 const RealType
b = constant.
b;
432 const RealType
t = constant.
t;
433 const RealType
p = constant.
p;
434 const RealType ln_4 = 2 * const_ln_2<RealType>();
436 RealType *
const u1 = s;
437 RealType *
const u2 = s + n;
438 RealType *
const v = s + n * 2;
439 RealType *
const x = s + n * 3;
440 RealType *
const y = s + n * 4;
442 sub(n, static_cast<RealType>(1), u1, v);
454 fma(n, -a, u1, p, u1);
455 fma(n, t, v, -ln_4, v);
459 for (std::size_t i = 0; i != n; ++i)
466 template <std::
size_t K,
typename RealType,
typename RNGType>
468 RealType *r, RealType, RealType,
471 const RealType
a = constant.
a;
472 const RealType
b = constant.
b;
474 RealType *
const x = s;
475 RealType *
const y = s + n;
476 RealType *
const u = s + n * 2;
484 for (std::size_t i = 0; i != n; ++i)
491 template <std::
size_t,
typename RealType,
typename RNGType>
498 template <std::
size_t,
typename RealType,
typename RNGType>
505 template <std::
size_t,
typename RealType,
typename RNGType>
512 template <std::
size_t K,
typename RealType,
typename RNGType>
514 RealType *r, RealType alpha, RealType beta,
519 return beta_distribution_impl_as<K>(
520 rng, n, r, alpha, beta, constant);
522 return beta_distribution_impl_11<K>(
523 rng, n, r, alpha, beta, constant);
525 return beta_distribution_impl_1x<K>(
526 rng, n, r, alpha, beta, constant);
528 return beta_distribution_impl_x1<K>(
529 rng, n, r, alpha, beta, constant);
531 return beta_distribution_impl_c<K>(
532 rng, n, r, alpha, beta, constant);
534 return beta_distribution_impl_j<K>(
535 rng, n, r, alpha, beta, constant);
537 return beta_distribution_impl_a1<K>(
538 rng, n, r, alpha, beta, constant);
540 return beta_distribution_impl_a2<K>(
541 rng, n, r, alpha, beta, constant);
543 return beta_distribution_impl_a3<K>(
544 rng, n, r, alpha, beta, constant);
553 template <
typename RealType,
typename RNGType>
555 RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta)
557 const std::size_t k = 1000;
560 std::size_t m = internal::beta_distribution_impl<k>(
561 rng, k, r, alpha, beta, constant);
568 internal::beta_distribution_impl<k>(rng, n, r, alpha, beta, constant);
573 for (std::size_t i = 0; i != n; ++i)
578 template <
typename RealType,
typename RNGType>
587 #endif // VSMC_RNG_BETA_DISTRIBUTION_HPP
Standard uniform distribution with open/closed variants.
void mul(std::size_t n, const float *a, const float *b, float *y)
void reset(RealType alpha, RealType beta)
std::size_t beta_distribution_impl_c(RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta, const BetaDistributionConstant< RealType > &constant)
void sqrt(std::size_t n, const float *a, float *y)
BetaDistributionAlgorithm
std::size_t beta_distribution_impl_11(RNGType &rng, std::size_t n, RealType *r, RealType, RealType, const BetaDistributionConstant< RealType > &)
std::size_t beta_distribution_impl_j(RNGType &rng, std::size_t n, RealType *r, RealType, RealType, const BetaDistributionConstant< RealType > &constant)
std::size_t beta_distribution_impl(RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta, const BetaDistributionConstant< RealType > &constant)
void u01_oo_distribution(RNGType &rng, std::size_t n, RealType *r)
Generate standard uniform random variates on open-open interval.
void rng_rand(RNGType &rng, BernoulliDistribution< IntType > &dist, std::size_t n, IntType *r)
std::size_t beta_distribution_impl_as(RNGType &rng, std::size_t n, RealType *r, RealType, RealType, const BetaDistributionConstant< RealType > &)
#define VSMC_DEFINE_RNG_DISTRIBUTION_OPERATORS
std::size_t beta_distribution_impl_a2(RNGType &, std::size_t, RealType *, RealType, RealType, const BetaDistributionConstant< RealType > &)
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)
void u01_co_distribution(RNGType &rng, std::size_t n, RealType *r)
Generate standard uniform random variates on closed-open interval.
BetaDistributionConstant(RealType alpha=1, RealType beta=1)
result_type alpha() const
void exp(std::size_t n, const float *a, float *y)
std::size_t beta_distribution_impl_x1(RNGType &rng, std::size_t n, RealType *r, RealType, RealType, const BetaDistributionConstant< RealType > &constant)
std::size_t beta_distribution_impl_a1(RNGType &, std::size_t, RealType *, RealType, RealType, const BetaDistributionConstant< RealType > &)
void div(std::size_t n, const float *a, const float *b, float *y)
void sub(std::size_t n, const float *a, const float *b, float *y)
void fma(std::size_t n, const T *a, const T *b, const T *c, T *y)
For , compute .
void sin(std::size_t n, const float *a, float *y)
void beta_distribution(RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta)
Generating beta random variates.
void add(std::size_t n, const float *a, const float *b, float *y)
bool beta_distribution_check_param(RealType alpha, RealType beta)
void log(std::size_t n, const float *a, float *y)
void sqr(std::size_t n, const float *a, float *y)
BetaDistributionAlgorithm algorithm
std::size_t beta_distribution_impl_1x(RNGType &rng, std::size_t n, RealType *r, RealType, RealType, const BetaDistributionConstant< RealType > &constant)
std::size_t beta_distribution_impl_a3(RNGType &, std::size_t, RealType *, RealType, RealType, const BetaDistributionConstant< RealType > &)