32 #ifndef MCKL_RANDOM_BETA_DISTRIBUTION_HPP 33 #define MCKL_RANDOM_BETA_DISTRIBUTION_HPP 43 template <
typename RealType>
46 return alpha > 0 && beta > 0;
62 template <typename RealType>
70 const RealType K =
static_cast<RealType
>(0.852);
71 const RealType C =
static_cast<RealType
>(-0.956);
72 const RealType D = beta + K * alpha * alpha + C;
73 if (alpha == static_cast<RealType>(0.5) &&
74 beta ==
static_cast<RealType
>(0.5)) {
76 }
else if (alpha == 1 && beta == 1) {
78 }
else if (alpha == 1) {
80 }
else if (beta == 1) {
82 }
else if (alpha > 1 && beta > 1) {
84 }
else if (alpha < 1 && beta < 1 && D <= 0) {
86 }
else if (alpha < 1 && beta < 1) {
88 }
else if (alpha < 1 && beta > 1) {
90 }
else if (alpha > 1 && beta < 1) {
98 a_ = b_ = t_ = p_ = 0;
112 b_ = std::min(alpha, beta);
114 b_ =
std::sqrt((2 * alpha * beta - a_) / (a_ - 2));
130 p_ /= p_ + alpha * (1 - t_);
138 p_ /= p_ + alpha *
std::pow(1 - t_, beta);
146 p_ /= p_ + beta *
std::pow(1 - t_, alpha);
151 RealType
a()
const {
return a_; }
152 RealType
b()
const {
return b_; }
153 RealType
t()
const {
return t_; }
154 RealType
p()
const {
return p_; }
162 if (c1.a_ != c2.a_) {
165 if (c1.b_ != c2.b_) {
168 if (c1.t_ != c2.t_) {
171 if (c1.p_ != c2.p_) {
174 if (c1.algorithm_ != c2.algorithm_) {
191 template <std::
size_t,
typename RealType,
typename RNGType>
193 RealType *r, RealType, RealType,
197 muladd(n, const_pi<RealType>(), r, -const_pi_by2<RealType>(), r);
199 muladd(n, static_cast<RealType>(0.5), r, static_cast<RealType>(0.5), r);
204 template <std::
size_t,
typename RealType,
typename RNGType>
206 RealType *r, RealType, RealType,
214 template <std::
size_t,
typename RealType,
typename RNGType>
216 RealType *r, RealType, RealType,
221 mul(n, constant.
b(), r, r);
223 sub(n, const_one<RealType>(), r, r);
228 template <std::
size_t,
typename RealType,
typename RNGType>
230 RealType *r, RealType, RealType,
235 mul(n, constant.
a(), r, r);
241 template <std::
size_t K,
typename RealType,
typename RNGType>
243 RealType *r, RealType alpha, RealType beta,
246 const RealType a = constant.
a();
247 const RealType b = constant.
b();
248 const RealType t = constant.
t();
249 const RealType p = constant.
p();
250 const RealType ln_4 = 2 * const_ln_2<RealType>();
252 RealType *
const u1 = s.data();
253 RealType *
const u2 = s.data() + n;
254 RealType *
const v = s.data() + n * 2;
255 RealType *
const x = s.data() + n * 3;
256 RealType *
const y = s.data() + n * 4;
258 sub(n, const_one<RealType>(), u1, v);
271 muladd(n, t, v, -ln_4, v);
275 for (std::size_t i = 0; i != n; ++i) {
284 template <std::
size_t K,
typename RealType,
typename RNGType>
286 RealType *r, RealType, RealType,
290 const RealType a = constant.
a();
291 const RealType b = constant.
b();
292 RealType *
const x = s.data();
293 RealType *
const y = s.data() + n;
294 RealType *
const u = s.data() + n * 2;
302 for (std::size_t i = 0; i != n; ++i) {
311 template <std::
size_t,
typename RealType,
typename RNGType>
318 template <std::
size_t,
typename RealType,
typename RNGType>
325 template <std::
size_t,
typename RealType,
typename RNGType>
332 template <std::
size_t K,
typename RealType,
typename RNGType>
334 RealType *r, RealType alpha, RealType beta,
339 return beta_distribution_impl_as<K>(
340 rng, n, r, alpha, beta, constant);
342 return beta_distribution_impl_11<K>(
343 rng, n, r, alpha, beta, constant);
345 return beta_distribution_impl_1x<K>(
346 rng, n, r, alpha, beta, constant);
348 return beta_distribution_impl_x1<K>(
349 rng, n, r, alpha, beta, constant);
351 return beta_distribution_impl_c<K>(
352 rng, n, r, alpha, beta, constant);
354 return beta_distribution_impl_j<K>(
355 rng, n, r, alpha, beta, constant);
357 return beta_distribution_impl_a1<K>(
358 rng, n, r, alpha, beta, constant);
360 return beta_distribution_impl_a2<K>(
361 rng, n, r, alpha, beta, constant);
363 return beta_distribution_impl_a3<K>(
364 rng, n, r, alpha, beta, constant);
371 template <
typename RealType,
typename RNGType>
373 RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta)
378 std::size_t m = internal::beta_distribution_impl<k>(
379 rng, k, r, alpha, beta, constant);
387 internal::beta_distribution_impl<k>(rng, n, r, alpha, beta, constant);
392 for (std::size_t i = 0; i != n; ++i) {
398 template <
typename RealType,
typename RNGType>
407 template <
typename RealType>
430 return constant_ == other.constant_;
433 template <
typename CharT,
typename Traits>
434 void ostream(std::basic_ostream<CharT, Traits> &)
const 438 template <
typename CharT,
typename Traits>
439 void istream(std::basic_istream<CharT, Traits> &)
444 template <
typename RNGType>
447 if (param == param_) {
448 return generate(rng, param_, constant_);
454 return generate(rng, param, constant);
457 template <
typename RNGType>
464 r = generate_as(rng, param, constant);
467 r = generate_11(rng, param, constant);
470 r = generate_1x(rng, param, constant);
473 r = generate_x1(rng, param, constant);
476 r = generate_c(rng, param, constant);
479 r = generate_j(rng, param, constant);
482 r = generate_a1(rng, param, constant);
485 r = generate_a2(rng, param, constant);
488 r = generate_a3(rng, param, constant);
495 template <
typename RNGType>
502 -const_pi_by2<result_type>() + const_pi<result_type>() * u);
508 template <
typename RNGType>
517 template <
typename RNGType>
526 template <
typename RNGType>
535 template <
typename RNGType>
540 const result_type ln_4 = 2 * const_ln_2<result_type>();
550 y = param.
beta() + x;
551 left = (constant.
p() - constant.
a() *
std::log(y)) +
552 (constant.
t() * v - ln_4);
554 }
while (left < right);
559 template <
typename RNGType>
574 template <
typename RNGType>
584 if (u < constant.
p()) {
585 x = constant.
t() *
std::pow(u / constant.
p(), constant.
a());
586 v = (1 - param.
beta()) *
591 std::pow((1 - u) / (1 - constant.
p()), constant.
b());
600 template <
typename RNGType>
610 if (u < constant.
p()) {
611 x = constant.
t() *
std::pow(u / constant.
p(), constant.
a());
616 std::pow((1 - u) / (1 - constant.
p()), constant.
b());
625 template <
typename RNGType>
635 if (u < constant.
p()) {
636 x = constant.
t() *
std::pow(u / constant.
p(), constant.
a());
641 std::pow((1 - u) / (1 - constant.
p()), constant.
b());
655 #endif // MCKL_RANDOM_BETA_DISTRIBUTION_HPP
#define MCKL_DEFINE_RANDOM_DISTRIBUTION_2( Name, name, T, T1, p1, v1, T2, p2, v2)
void pow(std::size_t n, const float *a, const float *b, float *y)
std::size_t beta_distribution_impl_c(RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta, const BetaDistributionConstant< RealType > &constant)
void mul(std::size_t n, const float *a, const float *b, float *y)
BetaDistributionAlgorithm algorithm() const
BetaDistributionConstant(RealType alpha=1, RealType beta=1)
std::size_t beta_distribution_impl_a3(RNGType &, std::size_t, RealType *, RealType, RealType, const BetaDistributionConstant< RealType > &)
#define MCKL_PUSH_CLANG_WARNING(warning)
void sin(std::size_t n, const float *a, float *y)
void log(std::size_t n, const float *a, float *y)
std::size_t beta_distribution_impl_a1(RNGType &, std::size_t, RealType *, RealType, RealType, const BetaDistributionConstant< RealType > &)
void u01_oo_distribution(RNGType &rng, std::size_t n, RealType *r)
BetaDistributionAlgorithm
#define MCKL_DEFINE_RANDOM_DISTRIBUTION_RAND(Name, T)
RealType u01(UIntType u)
Convert uniform unsigned integers to floating points within [0, 1].
void beta_distribution(RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta)
std::basic_ostream< CharT, Traits > & ostream(std::basic_ostream< CharT, Traits > &os, const std::array< T, N > &ary)
void sub(std::size_t n, const float *a, const float *b, float *y)
std::size_t beta_distribution_impl_1x(RNGType &rng, std::size_t n, RealType *r, RealType, RealType, const BetaDistributionConstant< RealType > &constant)
Standard uniform distribution on (0, 1)
void sqr(std::size_t n, const float *a, float *y)
#define MCKL_PUSH_INTEL_WARNING(wid)
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_a2(RNGType &, std::size_t, RealType *, RealType, RealType, const BetaDistributionConstant< RealType > &)
result_type alpha() const
bool beta_distribution_check_param(RealType alpha, RealType beta)
friend bool operator==(const BetaDistribution< RealType > &c1, const BetaDistributionConstant< RealType > &c2)
std::size_t beta_distribution_impl_j(RNGType &rng, std::size_t n, RealType *r, RealType, RealType, const BetaDistributionConstant< RealType > &constant)
void sqrt(std::size_t n, const float *a, float *y)
std::size_t beta_distribution_impl(RNGType &rng, std::size_t n, RealType *r, RealType alpha, RealType beta, const BetaDistributionConstant< RealType > &constant)
void exp(std::size_t n, const float *a, float *y)
#define MCKL_POP_CLANG_WARNING
#define MCKL_ALIGNMENT
The default alignment for scalar type.
bool is_equal(const float &v1, const float &v2)
#define MCKL_DEFINE_RANDOM_DISTRIBUTION_ASSERT_REAL_TYPE(Name)
std::size_t beta_distribution_impl_x1(RNGType &rng, std::size_t n, RealType *r, RealType, RealType, const BetaDistributionConstant< RealType > &constant)
std::basic_istream< CharT, Traits > & istream(std::basic_istream< CharT, Traits > &is, std::array< T, N > &ary)
std::size_t beta_distribution_impl_as(RNGType &rng, std::size_t n, RealType *r, RealType, RealType, const BetaDistributionConstant< RealType > &)
void div(std::size_t n, const float *a, const float *b, float *y)
#define MCKL_POP_INTEL_WARNING
void add(std::size_t n, const float *a, const float *b, float *y)