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(alpha, static_cast<RealType>(0.5)) &&
78 is_equal(beta, static_cast<RealType>(0.5)))
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()); }
173 if (!
is_equal(constant_.
a, other.constant_.a))
175 if (!
is_equal(constant_.
b, other.constant_.b))
177 if (!
is_equal(constant_.
t, other.constant_.t))
179 if (!
is_equal(constant_.
p, other.constant_.p))
186 template <
typename CharT,
typename Traits>
187 void ostream(std::basic_ostream<CharT, Traits> &)
const 191 template <
typename CharT,
typename Traits>
192 void istream(std::basic_istream<CharT, Traits> &)
197 template <
typename RNGType>
201 return generate(rng, param_, constant_);
206 return generate(rng, param, constant);
209 template <
typename RNGType>
216 r = generate_as(rng, param, constant);
219 r = generate_11(rng, param, constant);
222 r = generate_1x(rng, param, constant);
225 r = generate_x1(rng, param, constant);
228 r = generate_c(rng, param, constant);
231 r = generate_j(rng, param, constant);
234 r = generate_a1(rng, param, constant);
237 r = generate_a2(rng, param, constant);
240 r = generate_a3(rng, param, constant);
247 template <
typename RNGType>
254 -const_pi_by2<result_type>() + const_pi<result_type>() * u);
260 template <
typename RNGType>
269 template <
typename RNGType>
278 template <
typename RNGType>
287 template <
typename RNGType>
292 const result_type ln_4 = 2 * const_ln_2<result_type>();
302 y = param.
beta() + x;
303 left = (constant.
p - constant.
a *
std::log(y)) +
304 (constant.
t * v - ln_4);
306 }
while (left < right);
311 template <
typename RNGType>
326 template <
typename RNGType>
336 if (u < constant.
p) {
337 x = constant.
t *
std::pow(u / constant.
p, constant.
a);
338 v = (1 - param.
beta()) *
std::log((1 - x) / (1 - constant.
t));
342 std::pow((1 - u) / (1 - constant.
p), constant.
b);
350 template <
typename RNGType>
360 if (u < constant.
p) {
361 x = constant.
t *
std::pow(u / constant.
p, constant.
a);
366 std::pow((1 - u) / (1 - constant.
p), constant.
b);
374 template <
typename RNGType>
384 if (u < constant.
p) {
385 x = constant.
t *
std::pow(u / constant.
p, constant.
a);
390 std::pow((1 - u) / (1 - constant.
p), constant.
b);
402 template <std::
size_t,
typename RealType,
typename RNGType>
404 RealType *r, RealType, RealType,
408 fma(n, const_pi<RealType>(), r, -const_pi_by2<RealType>(), r);
410 fma(n, static_cast<RealType>(0.5), r, static_cast<RealType>(0.5), r);
415 template <std::
size_t,
typename RealType,
typename RNGType>
417 RealType *r, RealType, RealType,
425 template <std::
size_t,
typename RealType,
typename RNGType>
427 RealType *r, RealType, RealType,
432 mul(n, constant.
b, r, r);
434 sub(n, static_cast<RealType>(1), r, r);
439 template <std::
size_t,
typename RealType,
typename RNGType>
441 RealType *r, RealType, RealType,
446 mul(n, constant.
a, r, r);
452 template <std::
size_t K,
typename RealType,
typename RNGType>
454 RealType *r, RealType alpha, RealType beta,
457 const RealType
a = constant.
a;
458 const RealType
b = constant.
b;
459 const RealType
t = constant.
t;
460 const RealType
p = constant.
p;
461 const RealType ln_4 = 2 * const_ln_2<RealType>();
463 RealType *
const u1 = s.data();
464 RealType *
const u2 = s.data() + n;
465 RealType *
const v = s.data() + n * 2;
466 RealType *
const x = s.data() + n * 3;
467 RealType *
const y = s.data() + n * 4;
469 sub(n, static_cast<RealType>(1), u1, v);
481 fma(n, -a, u1, p, u1);
482 fma(n, t, v, -ln_4, v);
486 for (std::size_t i = 0; i != n; ++i)
493 template <std::
size_t K,
typename RealType,
typename RNGType>
495 RealType *r, RealType, RealType,
498 const RealType
a = constant.
a;
499 const RealType
b = constant.
b;
501 RealType *
const x = s.data();
502 RealType *
const y = s.data() + n;
503 RealType *
const u = s.data() + n * 2;
511 for (std::size_t i = 0; i != n; ++i)
518 template <std::
size_t,
typename RealType,
typename RNGType>
525 template <std::
size_t,
typename RealType,
typename RNGType>
532 template <std::
size_t,
typename RealType,
typename RNGType>
539 template <std::
size_t K,
typename RealType,
typename RNGType>
541 RealType *r, RealType alpha, RealType beta,
546 return beta_distribution_impl_as<K>(
547 rng, n, r, alpha, beta, constant);
549 return beta_distribution_impl_11<K>(
550 rng, n, r, alpha, beta, constant);
552 return beta_distribution_impl_1x<K>(
553 rng, n, r, alpha, beta, constant);
555 return beta_distribution_impl_x1<K>(
556 rng, n, r, alpha, beta, constant);
558 return beta_distribution_impl_c<K>(
559 rng, n, r, alpha, beta, constant);
561 return beta_distribution_impl_j<K>(
562 rng, n, r, alpha, beta, constant);
564 return beta_distribution_impl_a1<K>(
565 rng, n, r, alpha, beta, constant);
567 return beta_distribution_impl_a2<K>(
568 rng, n, r, alpha, beta, constant);
570 return beta_distribution_impl_a3<K>(
571 rng, n, r, alpha, beta, constant);
580 template <
typename RealType,
typename RNGType>
582 RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta)
584 static_assert(std::is_floating_point<RealType>::value,
585 "**beta_distribution** USED WITH RealType OTHER THAN FLOATING POINT " 591 std::size_t m = internal::beta_distribution_impl<k>(
592 rng, k, r, alpha, beta, constant);
599 internal::beta_distribution_impl<k>(rng, n, r, alpha, beta, constant);
604 for (std::size_t i = 0; i != n; ++i)
613 #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)
std::basic_ostream< CharT, Traits > & ostream(std::basic_ostream< CharT, Traits > &os, const std::array< T, N > &ary)
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 (0, 1)
std::size_t beta_distribution_impl_as(RNGType &rng, std::size_t n, RealType *r, RealType, RealType, const BetaDistributionConstant< RealType > &)
Standard uniform distribution on (0, 1)
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)
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)
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)
std::basic_istream< CharT, Traits > & istream(std::basic_istream< CharT, Traits > &is, std::array< T, N > &ary)
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)
#define VSMC_DEFINE_RNG_DISTRIBUTION_RAND_2(Name, name, p1, p2)
std::array with proper alignment
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 > &)