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, static_cast<RealType>(0.5)) &&
78 is_equal<RealType>(beta, static_cast<RealType>(0.5)))
80 else if (is_equal<RealType>(alpha, 1) && is_equal<RealType>(beta, 1))
82 else if (is_equal<RealType>(alpha, 1))
84 else if (is_equal<RealType>(beta, 1))
86 else if (alpha > 1 && beta > 1)
88 else if (alpha < 1 && beta < 1 && D <= 0)
90 else if (alpha < 1 && beta < 1)
92 else if (alpha < 1 && beta > 1)
94 else if (alpha > 1 && beta < 1)
107 b = std::min(alpha, beta);
124 p /=
p + alpha * (1 -
t);
156 template <
typename RealType>
166 void reset() { constant_.reset(alpha(), beta()); }
171 template <
typename RNGType>
175 return generate(rng, param_, constant_);
180 return generate(rng, param, constant);
183 template <
typename RNGType>
190 r = generate_as(rng, param, constant);
193 r = generate_11(rng, param, constant);
196 r = generate_1x(rng, param, constant);
199 r = generate_x1(rng, param, constant);
202 r = generate_c(rng, param, constant);
205 r = generate_j(rng, param, constant);
208 r = generate_a1(rng, param, constant);
211 r = generate_a2(rng, param, constant);
214 r = generate_a3(rng, param, constant);
221 template <
typename RNGType>
228 -const_pi_by2<result_type>() + const_pi<result_type>() * u);
234 template <
typename RNGType>
243 template <
typename RNGType>
252 template <
typename RNGType>
261 template <
typename RNGType>
266 const result_type ln_4 = 2 * const_ln_2<result_type>();
276 y = param.
beta() + x;
277 left = (constant.
p - constant.
a *
std::log(y)) +
278 (constant.
t * v - ln_4);
280 }
while (left < right);
285 template <
typename RNGType>
300 template <
typename RNGType>
310 if (u < constant.
p) {
311 x = constant.
t *
std::pow(u / constant.
p, constant.
a);
312 v = (1 - param.
beta()) *
std::log((1 - x) / (1 - constant.
t));
316 std::pow((1 - u) / (1 - constant.
p), constant.
b);
324 template <
typename RNGType>
334 if (u < constant.
p) {
335 x = constant.
t *
std::pow(u / constant.
p, constant.
a);
340 std::pow((1 - u) / (1 - constant.
p), constant.
b);
348 template <
typename RNGType>
358 if (u < constant.
p) {
359 x = constant.
t *
std::pow(u / constant.
p, constant.
a);
364 std::pow((1 - u) / (1 - constant.
p), constant.
b);
376 template <std::
size_t,
typename RealType,
typename RNGType>
378 RealType *r, RealType, RealType,
382 fma(n, const_pi<RealType>(), r, -const_pi_by2<RealType>(), r);
384 fma(n, static_cast<RealType>(0.5), r, static_cast<RealType>(0.5), r);
389 template <std::
size_t,
typename RealType,
typename RNGType>
391 RealType *r, RealType, RealType,
399 template <std::
size_t,
typename RealType,
typename RNGType>
401 RealType *r, RealType, RealType,
406 mul(n, constant.
b, r, r);
408 sub(n, static_cast<RealType>(1), r, r);
413 template <std::
size_t,
typename RealType,
typename RNGType>
415 RealType *r, RealType, RealType,
420 mul(n, constant.
a, r, r);
426 template <std::
size_t K,
typename RealType,
typename RNGType>
428 RealType *r, RealType alpha, RealType beta,
431 const RealType
a = constant.
a;
432 const RealType
b = constant.
b;
433 const RealType
t = constant.
t;
434 const RealType
p = constant.
p;
435 const RealType ln_4 = 2 * const_ln_2<RealType>();
437 RealType *
const u1 = s;
438 RealType *
const u2 = s + n;
439 RealType *
const v = s + n * 2;
440 RealType *
const x = s + n * 3;
441 RealType *
const y = s + n * 4;
443 sub(n, static_cast<RealType>(1), u1, v);
455 fma(n, -a, u1, p, u1);
456 fma(n, t, v, -ln_4, v);
460 for (std::size_t i = 0; i != n; ++i)
467 template <std::
size_t K,
typename RealType,
typename RNGType>
469 RealType *r, RealType, RealType,
472 const RealType
a = constant.
a;
473 const RealType
b = constant.
b;
475 RealType *
const x = s;
476 RealType *
const y = s + n;
477 RealType *
const u = s + n * 2;
485 for (std::size_t i = 0; i != n; ++i)
492 template <std::
size_t,
typename RealType,
typename RNGType>
499 template <std::
size_t,
typename RealType,
typename RNGType>
506 template <std::
size_t,
typename RealType,
typename RNGType>
513 template <std::
size_t K,
typename RealType,
typename RNGType>
515 RealType *r, RealType alpha, RealType beta,
520 return beta_distribution_impl_as<K>(
521 rng, n, r, alpha, beta, constant);
523 return beta_distribution_impl_11<K>(
524 rng, n, r, alpha, beta, constant);
526 return beta_distribution_impl_1x<K>(
527 rng, n, r, alpha, beta, constant);
529 return beta_distribution_impl_x1<K>(
530 rng, n, r, alpha, beta, constant);
532 return beta_distribution_impl_c<K>(
533 rng, n, r, alpha, beta, constant);
535 return beta_distribution_impl_j<K>(
536 rng, n, r, alpha, beta, constant);
538 return beta_distribution_impl_a1<K>(
539 rng, n, r, alpha, beta, constant);
541 return beta_distribution_impl_a2<K>(
542 rng, n, r, alpha, beta, constant);
544 return beta_distribution_impl_a3<K>(
545 rng, n, r, alpha, beta, constant);
554 template <
typename RealType,
typename RNGType>
556 RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta)
558 static_assert(std::is_floating_point<RealType>::value,
559 "**beta_distribution** USED WITH RealType OTHER THAN FLOATING POINT " 562 const std::size_t k = 1024;
565 std::size_t m = internal::beta_distribution_impl<k>(
566 rng, k, r, alpha, beta, constant);
573 internal::beta_distribution_impl<k>(rng, n, r, alpha, beta, constant);
578 for (std::size_t i = 0; i != n; ++i)
587 #endif // VSMC_RNG_BETA_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 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_distribution(RNGType &, std::size_t, RealType *)
Generate standard uniform random variates.
std::size_t beta_distribution_impl_as(RNGType &rng, std::size_t n, RealType *r, RealType, RealType, const BetaDistributionConstant< RealType > &)
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)
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)
Standard uniform distribution.
#define VSMC_DEFINE_RNG_DISTRIBUTION_RAND_2(Name, name, p1, p2)
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 > &)