File indexing completed on 2025-01-30 09:31:49
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #ifndef ABSL_RANDOM_BETA_DISTRIBUTION_H_
0016 #define ABSL_RANDOM_BETA_DISTRIBUTION_H_
0017
0018 #include <cassert>
0019 #include <cmath>
0020 #include <istream>
0021 #include <limits>
0022 #include <ostream>
0023 #include <type_traits>
0024
0025 #include "absl/meta/type_traits.h"
0026 #include "absl/random/internal/fast_uniform_bits.h"
0027 #include "absl/random/internal/fastmath.h"
0028 #include "absl/random/internal/generate_real.h"
0029 #include "absl/random/internal/iostream_state_saver.h"
0030
0031 namespace absl {
0032 ABSL_NAMESPACE_BEGIN
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 template <typename RealType = double>
0049 class beta_distribution {
0050 public:
0051 using result_type = RealType;
0052
0053 class param_type {
0054 public:
0055 using distribution_type = beta_distribution;
0056
0057 explicit param_type(result_type alpha, result_type beta)
0058 : alpha_(alpha), beta_(beta) {
0059 assert(alpha >= 0);
0060 assert(beta >= 0);
0061 assert(alpha <= (std::numeric_limits<result_type>::max)());
0062 assert(beta <= (std::numeric_limits<result_type>::max)());
0063 if (alpha == 0 || beta == 0) {
0064 method_ = DEGENERATE_SMALL;
0065 x_ = (alpha >= beta) ? 1 : 0;
0066 return;
0067 }
0068
0069 if (beta < alpha) {
0070 inverted_ = true;
0071 a_ = beta;
0072 b_ = alpha;
0073 } else {
0074 inverted_ = false;
0075 a_ = alpha;
0076 b_ = beta;
0077 }
0078 if (a_ <= 1 && b_ >= ThresholdForLargeA()) {
0079 method_ = DEGENERATE_SMALL;
0080 x_ = inverted_ ? result_type(1) : result_type(0);
0081 return;
0082 }
0083
0084
0085
0086 if ((b_ < 1.0 && a_ + b_ <= 1.2) || a_ <= ThresholdForSmallA()) {
0087
0088
0089 method_ = JOEHNK;
0090 a_ = result_type(1) / alpha_;
0091 b_ = result_type(1) / beta_;
0092 if (std::isinf(a_) || std::isinf(b_)) {
0093 method_ = DEGENERATE_SMALL;
0094 x_ = inverted_ ? result_type(1) : result_type(0);
0095 }
0096 return;
0097 }
0098 if (a_ >= ThresholdForLargeA()) {
0099 method_ = DEGENERATE_LARGE;
0100
0101
0102 result_type r = a_ / b_;
0103 x_ = (inverted_ ? result_type(1) : r) / (1 + r);
0104 return;
0105 }
0106 x_ = a_ + b_;
0107 log_x_ = std::log(x_);
0108 if (a_ <= 1) {
0109 method_ = CHENG_BA;
0110 y_ = result_type(1) / a_;
0111 gamma_ = a_ + a_;
0112 return;
0113 }
0114 method_ = CHENG_BB;
0115 result_type r = (a_ - 1) / (b_ - 1);
0116 y_ = std::sqrt((1 + r) / (b_ * r * 2 - r + 1));
0117 gamma_ = a_ + result_type(1) / y_;
0118 }
0119
0120 result_type alpha() const { return alpha_; }
0121 result_type beta() const { return beta_; }
0122
0123 friend bool operator==(const param_type& a, const param_type& b) {
0124 return a.alpha_ == b.alpha_ && a.beta_ == b.beta_;
0125 }
0126
0127 friend bool operator!=(const param_type& a, const param_type& b) {
0128 return !(a == b);
0129 }
0130
0131 private:
0132 friend class beta_distribution;
0133
0134 #ifdef _MSC_VER
0135
0136
0137 #define ABSL_RANDOM_INTERNAL_LOG_EXP_CONSTEXPR
0138 #else
0139 #define ABSL_RANDOM_INTERNAL_LOG_EXP_CONSTEXPR constexpr
0140 #endif
0141
0142
0143
0144 static ABSL_RANDOM_INTERNAL_LOG_EXP_CONSTEXPR result_type
0145 ThresholdForSmallA() {
0146 return result_type(1) /
0147 std::log((std::numeric_limits<result_type>::max)());
0148 }
0149
0150
0151 static ABSL_RANDOM_INTERNAL_LOG_EXP_CONSTEXPR result_type
0152 ThresholdForLargeA() {
0153 return std::exp(
0154 std::log((std::numeric_limits<result_type>::max)()) -
0155 std::log(std::log((std::numeric_limits<result_type>::max)())) -
0156 ThresholdPadding());
0157 }
0158
0159 #undef ABSL_RANDOM_INTERNAL_LOG_EXP_CONSTEXPR
0160
0161
0162
0163 static constexpr result_type ThresholdPadding() { return 0; }
0164
0165 enum Method {
0166 JOEHNK,
0167 CHENG_BA,
0168 CHENG_BB,
0169
0170
0171
0172
0173
0174
0175
0176
0177 DEGENERATE_SMALL,
0178 DEGENERATE_LARGE,
0179 };
0180
0181 result_type alpha_;
0182 result_type beta_;
0183
0184 result_type a_{};
0185 result_type b_{};
0186 result_type x_{};
0187 result_type log_x_{};
0188 result_type y_{};
0189 result_type gamma_{};
0190
0191 Method method_{};
0192
0193
0194
0195 bool inverted_{};
0196
0197 static_assert(std::is_floating_point<RealType>::value,
0198 "Class-template absl::beta_distribution<> must be "
0199 "parameterized using a floating-point type.");
0200 };
0201
0202 beta_distribution() : beta_distribution(1) {}
0203
0204 explicit beta_distribution(result_type alpha, result_type beta = 1)
0205 : param_(alpha, beta) {}
0206
0207 explicit beta_distribution(const param_type& p) : param_(p) {}
0208
0209 void reset() {}
0210
0211
0212 template <typename URBG>
0213 result_type operator()(URBG& g) {
0214 return (*this)(g, param_);
0215 }
0216
0217 template <typename URBG>
0218 result_type operator()(URBG& g,
0219 const param_type& p);
0220
0221 param_type param() const { return param_; }
0222 void param(const param_type& p) { param_ = p; }
0223
0224 result_type(min)() const { return 0; }
0225 result_type(max)() const { return 1; }
0226
0227 result_type alpha() const { return param_.alpha(); }
0228 result_type beta() const { return param_.beta(); }
0229
0230 friend bool operator==(const beta_distribution& a,
0231 const beta_distribution& b) {
0232 return a.param_ == b.param_;
0233 }
0234 friend bool operator!=(const beta_distribution& a,
0235 const beta_distribution& b) {
0236 return a.param_ != b.param_;
0237 }
0238
0239 private:
0240 template <typename URBG>
0241 result_type AlgorithmJoehnk(URBG& g,
0242 const param_type& p);
0243
0244 template <typename URBG>
0245 result_type AlgorithmCheng(URBG& g,
0246 const param_type& p);
0247
0248 template <typename URBG>
0249 result_type DegenerateCase(URBG& g,
0250 const param_type& p) {
0251 if (p.method_ == param_type::DEGENERATE_SMALL && p.alpha_ == p.beta_) {
0252
0253 random_internal::FastUniformBits<uint8_t> fast_u8;
0254 return static_cast<result_type>((fast_u8(g) & 0x10) !=
0255 0);
0256 }
0257 return p.x_;
0258 }
0259
0260 param_type param_;
0261 random_internal::FastUniformBits<uint64_t> fast_u64_;
0262 };
0263
0264 #if defined(__powerpc64__) || defined(__PPC64__) || defined(__powerpc__) || \
0265 defined(__ppc__) || defined(__PPC__)
0266
0267 template <>
0268 constexpr long double
0269 beta_distribution<long double>::param_type::ThresholdPadding() {
0270 return 10;
0271 }
0272 #endif
0273
0274 template <typename RealType>
0275 template <typename URBG>
0276 typename beta_distribution<RealType>::result_type
0277 beta_distribution<RealType>::AlgorithmJoehnk(
0278 URBG& g,
0279 const param_type& p) {
0280 using random_internal::GeneratePositiveTag;
0281 using random_internal::GenerateRealFromBits;
0282 using real_type =
0283 absl::conditional_t<std::is_same<RealType, float>::value, float, double>;
0284
0285
0286
0287
0288
0289 result_type u, v, x, y, z;
0290 for (;;) {
0291 u = GenerateRealFromBits<real_type, GeneratePositiveTag, false>(
0292 fast_u64_(g));
0293 v = GenerateRealFromBits<real_type, GeneratePositiveTag, false>(
0294 fast_u64_(g));
0295
0296
0297
0298 if (!std::is_same<float, result_type>::value) {
0299 x = std::pow(u, p.a_);
0300 y = std::pow(v, p.b_);
0301 z = x + y;
0302 if (z > 1) {
0303
0304 continue;
0305 }
0306 if (z > 0) {
0307
0308
0309 return x / z;
0310 }
0311 }
0312
0313
0314
0315
0316 x = std::log(u) * p.a_;
0317 y = std::log(v) * p.b_;
0318 if (!std::isfinite(x) || !std::isfinite(y)) {
0319 continue;
0320 }
0321
0322 z = x > y ? (x + std::log(1 + std::exp(y - x)))
0323 : (y + std::log(1 + std::exp(x - y)));
0324
0325 if (z > 0) {
0326 continue;
0327 }
0328 return std::exp(x - z);
0329 }
0330 }
0331
0332 template <typename RealType>
0333 template <typename URBG>
0334 typename beta_distribution<RealType>::result_type
0335 beta_distribution<RealType>::AlgorithmCheng(
0336 URBG& g,
0337 const param_type& p) {
0338 using random_internal::GeneratePositiveTag;
0339 using random_internal::GenerateRealFromBits;
0340 using real_type =
0341 absl::conditional_t<std::is_same<RealType, float>::value, float, double>;
0342
0343
0344
0345
0346 static constexpr result_type kLogFour =
0347 result_type(1.3862943611198906188344642429163531361);
0348 static constexpr result_type kS =
0349 result_type(2.6094379124341003746007593332261876);
0350
0351 const bool use_algorithm_ba = (p.method_ == param_type::CHENG_BA);
0352 result_type u1, u2, v, w, z, r, s, t, bw_inv, lhs;
0353 for (;;) {
0354 u1 = GenerateRealFromBits<real_type, GeneratePositiveTag, false>(
0355 fast_u64_(g));
0356 u2 = GenerateRealFromBits<real_type, GeneratePositiveTag, false>(
0357 fast_u64_(g));
0358 v = p.y_ * std::log(u1 / (1 - u1));
0359 w = p.a_ * std::exp(v);
0360 bw_inv = result_type(1) / (p.b_ + w);
0361 r = p.gamma_ * v - kLogFour;
0362 s = p.a_ + r - w;
0363 z = u1 * u1 * u2;
0364 if (!use_algorithm_ba && s + kS >= 5 * z) {
0365 break;
0366 }
0367 t = std::log(z);
0368 if (!use_algorithm_ba && s >= t) {
0369 break;
0370 }
0371 lhs = p.x_ * (p.log_x_ + std::log(bw_inv)) + r;
0372 if (lhs >= t) {
0373 break;
0374 }
0375 }
0376 return p.inverted_ ? (1 - w * bw_inv) : w * bw_inv;
0377 }
0378
0379 template <typename RealType>
0380 template <typename URBG>
0381 typename beta_distribution<RealType>::result_type
0382 beta_distribution<RealType>::operator()(URBG& g,
0383 const param_type& p) {
0384 switch (p.method_) {
0385 case param_type::JOEHNK:
0386 return AlgorithmJoehnk(g, p);
0387 case param_type::CHENG_BA:
0388 ABSL_FALLTHROUGH_INTENDED;
0389 case param_type::CHENG_BB:
0390 return AlgorithmCheng(g, p);
0391 default:
0392 return DegenerateCase(g, p);
0393 }
0394 }
0395
0396 template <typename CharT, typename Traits, typename RealType>
0397 std::basic_ostream<CharT, Traits>& operator<<(
0398 std::basic_ostream<CharT, Traits>& os,
0399 const beta_distribution<RealType>& x) {
0400 auto saver = random_internal::make_ostream_state_saver(os);
0401 os.precision(random_internal::stream_precision_helper<RealType>::kPrecision);
0402 os << x.alpha() << os.fill() << x.beta();
0403 return os;
0404 }
0405
0406 template <typename CharT, typename Traits, typename RealType>
0407 std::basic_istream<CharT, Traits>& operator>>(
0408 std::basic_istream<CharT, Traits>& is,
0409 beta_distribution<RealType>& x) {
0410 using result_type = typename beta_distribution<RealType>::result_type;
0411 using param_type = typename beta_distribution<RealType>::param_type;
0412 result_type alpha, beta;
0413
0414 auto saver = random_internal::make_istream_state_saver(is);
0415 alpha = random_internal::read_floating_point<result_type>(is);
0416 if (is.fail()) return is;
0417 beta = random_internal::read_floating_point<result_type>(is);
0418 if (!is.fail()) {
0419 x.param(param_type(alpha, beta));
0420 }
0421 return is;
0422 }
0423
0424 ABSL_NAMESPACE_END
0425 }
0426
0427 #endif