| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #ifndef ABSL_RANDOM_BETA_DISTRIBUTION_H_ |
| 16 | #define ABSL_RANDOM_BETA_DISTRIBUTION_H_ |
| 17 |
| 18 | #include <cassert> |
| 19 | #include <cmath> |
| 20 | #include <cstdint> |
| 21 | #include <istream> |
| 22 | #include <limits> |
| 23 | #include <ostream> |
| 24 | #include <type_traits> |
| 25 |
| 26 | #include "absl/base/attributes.h" |
| 27 | #include "absl/base/config.h" |
| 28 | #include "absl/meta/type_traits.h" |
| 29 | #include "absl/random/internal/fast_uniform_bits.h" |
| 30 | #include "absl/random/internal/generate_real.h" |
| 31 | #include "absl/random/internal/iostream_state_saver.h" |
| 32 |
| 33 | namespace absl { |
| 34 | ABSL_NAMESPACE_BEGIN |
| 35 |
| 36 | // absl::beta_distribution: |
| 37 | // Generate a floating-point variate conforming to a Beta distribution: |
| 38 | // pdf(x) \propto x^(alpha-1) * (1-x)^(beta-1), |
| 39 | // where the params alpha and beta are both strictly positive real values. |
| 40 | // |
| 41 | // The support is the open interval (0, 1), but the return value might be equal |
| 42 | // to 0 or 1, due to numerical errors when alpha and beta are very different. |
| 43 | // |
| 44 | // Usage note: One usage is that alpha and beta are counts of number of |
| 45 | // successes and failures. When the total number of trials are large, consider |
| 46 | // approximating a beta distribution with a Gaussian distribution with the same |
| 47 | // mean and variance. One could use the skewness, which depends only on the |
| 48 | // smaller of alpha and beta when the number of trials are sufficiently large, |
| 49 | // to quantify how far a beta distribution is from the normal distribution. |
| 50 | template <typename RealType = double> |
| 51 | class beta_distribution { |
| 52 | public: |
| 53 | using result_type = RealType; |
| 54 |
| 55 | class param_type { |
| 56 | public: |
| 57 | using distribution_type = beta_distribution; |
| 58 |
| 59 | explicit param_type(result_type alpha, result_type beta) |
| 60 | : alpha_(alpha), beta_(beta) { |
| 61 | assert(alpha >= 0); |
| 62 | assert(beta >= 0); |
| 63 | assert(alpha <= (std::numeric_limits<result_type>::max)()); |
| 64 | assert(beta <= (std::numeric_limits<result_type>::max)()); |
| 65 | if (alpha == 0 || beta == 0) { |
| 66 | method_ = DEGENERATE_SMALL; |
| 67 | x_ = (alpha >= beta) ? 1 : 0; |
| 68 | return; |
| 69 | } |
| 70 | // a_ = min(beta, alpha), b_ = max(beta, alpha). |
| 71 | if (beta < alpha) { |
| 72 | inverted_ = true; |
| 73 | a_ = beta; |
| 74 | b_ = alpha; |
| 75 | } else { |
| 76 | inverted_ = false; |
| 77 | a_ = alpha; |
| 78 | b_ = beta; |
| 79 | } |
| 80 | if (a_ <= 1 && b_ >= ThresholdForLargeA()) { |
| 81 | method_ = DEGENERATE_SMALL; |
| 82 | x_ = inverted_ ? result_type(1) : result_type(0); |
| 83 | return; |
| 84 | } |
| 85 | // For threshold values, see also: |
| 86 | // Evaluation of Beta Generation Algorithms, Ying-Chao Hung, et. al. |
| 87 | // February, 2009. |
| 88 | if ((b_ < 1.0 && a_ + b_ <= 1.2) || a_ <= ThresholdForSmallA()) { |
| 89 | // Choose Joehnk over Cheng when it's faster or when Cheng encounters |
| 90 | // numerical issues. |
| 91 | method_ = JOEHNK; |
| 92 | a_ = result_type(1) / alpha_; |
| 93 | b_ = result_type(1) / beta_; |
| 94 | if (std::isinf(a_) || std::isinf(b_)) { |
| 95 | method_ = DEGENERATE_SMALL; |
| 96 | x_ = inverted_ ? result_type(1) : result_type(0); |
| 97 | } |
| 98 | return; |
| 99 | } |
| 100 | if (a_ >= ThresholdForLargeA()) { |
| 101 | method_ = DEGENERATE_LARGE; |
| 102 | // Note: on PPC for long double, evaluating |
| 103 | // `std::numeric_limits::max() / ThresholdForLargeA` results in NaN. |
| 104 | result_type r = a_ / b_; |
| 105 | x_ = (inverted_ ? result_type(1) : r) / (1 + r); |
| 106 | return; |
| 107 | } |
| 108 | x_ = a_ + b_; |
| 109 | log_x_ = std::log(x_); |
| 110 | if (a_ <= 1) { |
| 111 | method_ = CHENG_BA; |
| 112 | y_ = result_type(1) / a_; |
| 113 | gamma_ = a_ + a_; |
| 114 | return; |
| 115 | } |
| 116 | method_ = CHENG_BB; |
| 117 | result_type r = (a_ - 1) / (b_ - 1); |
| 118 | y_ = std::sqrt((1 + r) / (b_ * r * 2 - r + 1)); |
| 119 | gamma_ = a_ + result_type(1) / y_; |
| 120 | } |
| 121 |
| 122 | result_type alpha() const { return alpha_; } |
| 123 | result_type beta() const { return beta_; } |
| 124 |
| 125 | friend bool operator==(const param_type& a, const param_type& b) { |
| 126 | return a.alpha_ == b.alpha_ && a.beta_ == b.beta_; |
| 127 | } |
| 128 |
| 129 | friend bool operator!=(const param_type& a, const param_type& b) { |
| 130 | return !(a == b); |
| 131 | } |
| 132 |
| 133 | private: |
| 134 | friend class beta_distribution; |
| 135 |
| 136 | #ifdef _MSC_VER |
| 137 | // MSVC does not have constexpr implementations for std::log and std::exp |
| 138 | // so they are computed at runtime. |
| 139 | #define ABSL_RANDOM_INTERNAL_LOG_EXP_CONSTEXPR |
| 140 | #else |
| 141 | #define ABSL_RANDOM_INTERNAL_LOG_EXP_CONSTEXPR constexpr |
| 142 | #endif |
| 143 |
| 144 | // The threshold for whether std::exp(1/a) is finite. |
| 145 | // Note that this value is quite large, and a smaller a_ is NOT abnormal. |
| 146 | static ABSL_RANDOM_INTERNAL_LOG_EXP_CONSTEXPR result_type |
| 147 | ThresholdForSmallA() { |
| 148 | return result_type(1) / |
| 149 | std::log((std::numeric_limits<result_type>::max)()); |
| 150 | } |
| 151 |
| 152 | // The threshold for whether a * std::log(a) is finite. |
| 153 | static ABSL_RANDOM_INTERNAL_LOG_EXP_CONSTEXPR result_type |
| 154 | ThresholdForLargeA() { |
| 155 | return std::exp( |
| 156 | std::log((std::numeric_limits<result_type>::max)()) - |
| 157 | std::log(std::log((std::numeric_limits<result_type>::max)())) - |
| 158 | ThresholdPadding()); |
| 159 | } |
| 160 |
| 161 | #undef ABSL_RANDOM_INTERNAL_LOG_EXP_CONSTEXPR |
| 162 |
| 163 | // Pad the threshold for large A for long double on PPC. This is done via a |
| 164 | // template specialization below. |
| 165 | static constexpr result_type ThresholdPadding() { return 0; } |
| 166 |
| 167 | enum Method { |
| 168 | JOEHNK, // Uses algorithm Joehnk |
| 169 | CHENG_BA, // Uses algorithm BA in Cheng |
| 170 | CHENG_BB, // Uses algorithm BB in Cheng |
| 171 |
| 172 | // Note: See also: |
| 173 | // Hung et al. Evaluation of beta generation algorithms. Communications |
| 174 | // in Statistics-Simulation and Computation 38.4 (2009): 750-770. |
| 175 | // especially: |
| 176 | // Zechner, Heinz, and Ernst Stadlober. Generating beta variates via |
| 177 | // patchwork rejection. Computing 50.1 (1993): 1-18. |
| 178 |
| 179 | DEGENERATE_SMALL, // a_ is abnormally small. |
| 180 | DEGENERATE_LARGE, // a_ is abnormally large. |
| 181 | }; |
| 182 |
| 183 | result_type alpha_; |
| 184 | result_type beta_; |
| 185 |
| 186 | result_type a_{}; // the smaller of {alpha, beta}, or 1.0/alpha_ in JOEHNK |
| 187 | result_type b_{}; // the larger of {alpha, beta}, or 1.0/beta_ in JOEHNK |
| 188 | result_type x_{}; // alpha + beta, or the result in degenerate cases |
| 189 | result_type log_x_{}; // log(x_) |
| 190 | result_type y_{}; // "beta" in Cheng |
| 191 | result_type gamma_{}; // "gamma" in Cheng |
| 192 |
| 193 | Method method_{}; |
| 194 |
| 195 | // Placing this last for optimal alignment. |
| 196 | // Whether alpha_ != a_, i.e. true iff alpha_ > beta_. |
| 197 | bool inverted_{}; |
| 198 |
| 199 | static_assert(std::is_floating_point<RealType>::value, |
| 200 | "Class-template absl::beta_distribution<> must be " |
| 201 | "parameterized using a floating-point type."); |
| 202 | }; |
| 203 |
| 204 | beta_distribution() : beta_distribution(1) {} |
| 205 |
| 206 | explicit beta_distribution(result_type alpha, result_type beta = 1) |
| 207 | : param_(alpha, beta) {} |
| 208 |
| 209 | explicit beta_distribution(const param_type& p) : param_(p) {} |
| 210 |
| 211 | void reset() {} |
| 212 |
| 213 | // Generating functions |
| 214 | template <typename URBG> |
| 215 | result_type operator()(URBG& g) { // NOLINT(runtime/references) |
| 216 | return (*this)(g, param_); |
| 217 | } |
| 218 |
| 219 | template <typename URBG> |
| 220 | result_type operator()(URBG& g, // NOLINT(runtime/references) |
| 221 | const param_type& p); |
| 222 |
| 223 | param_type param() const { return param_; } |
| 224 | void param(const param_type& p) { param_ = p; } |
| 225 |
| 226 | result_type(min)() const { return 0; } |
| 227 | result_type(max)() const { return 1; } |
| 228 |
| 229 | result_type alpha() const { return param_.alpha(); } |
| 230 | result_type beta() const { return param_.beta(); } |
| 231 |
| 232 | friend bool operator==(const beta_distribution& a, |
| 233 | const beta_distribution& b) { |
| 234 | return a.param_ == b.param_; |
| 235 | } |
| 236 | friend bool operator!=(const beta_distribution& a, |
| 237 | const beta_distribution& b) { |
| 238 | return a.param_ != b.param_; |
| 239 | } |
| 240 |
| 241 | private: |
| 242 | template <typename URBG> |
| 243 | result_type AlgorithmJoehnk(URBG& g, // NOLINT(runtime/references) |
| 244 | const param_type& p); |
| 245 |
| 246 | template <typename URBG> |
| 247 | result_type AlgorithmCheng(URBG& g, // NOLINT(runtime/references) |
| 248 | const param_type& p); |
| 249 |
| 250 | template <typename URBG> |
| 251 | result_type DegenerateCase(URBG& g, // NOLINT(runtime/references) |
| 252 | const param_type& p) { |
| 253 | if (p.method_ == param_type::DEGENERATE_SMALL && p.alpha_ == p.beta_) { |
| 254 | // Returns 0 or 1 with equal probability. |
| 255 | random_internal::FastUniformBits<uint8_t> fast_u8; |
| 256 | return static_cast<result_type>((fast_u8(g) & 0x10) != |
| 257 | 0); // pick any single bit. |
| 258 | } |
| 259 | return p.x_; |
| 260 | } |
| 261 |
| 262 | param_type param_; |
| 263 | random_internal::FastUniformBits<uint64_t> fast_u64_; |
| 264 | }; |
| 265 |
| 266 | #if defined(__powerpc64__) || defined(__PPC64__) || defined(__powerpc__) || \ |
| 267 | defined(__ppc__) || defined(__PPC__) |
| 268 | // PPC needs a more stringent boundary for long double. |
| 269 | template <> |
| 270 | constexpr long double |
| 271 | beta_distribution<long double>::param_type::ThresholdPadding() { |
| 272 | return 10; |
| 273 | } |
| 274 | #endif |
| 275 |
| 276 | template <typename RealType> |
| 277 | template <typename URBG> |
| 278 | typename beta_distribution<RealType>::result_type |
| 279 | beta_distribution<RealType>::AlgorithmJoehnk( |
| 280 | URBG& g, // NOLINT(runtime/references) |
| 281 | const param_type& p) { |
| 282 | using random_internal::GeneratePositiveTag; |
| 283 | using random_internal::GenerateRealFromBits; |
| 284 | using real_type = |
| 285 | absl::conditional_t<std::is_same<RealType, float>::value, float, double>; |
| 286 |
| 287 | // Based on Joehnk, M. D. Erzeugung von betaverteilten und gammaverteilten |
| 288 | // Zufallszahlen. Metrika 8.1 (1964): 5-15. |
| 289 | // This method is described in Knuth, Vol 2 (Third Edition), pp 134. |
| 290 |
| 291 | result_type u, v, x, y, z; |
| 292 | for (;;) { |
| 293 | u = GenerateRealFromBits<real_type, GeneratePositiveTag, false>( |
| 294 | fast_u64_(g)); |
| 295 | v = GenerateRealFromBits<real_type, GeneratePositiveTag, false>( |
| 296 | fast_u64_(g)); |
| 297 |
| 298 | // Direct method. std::pow is slow for float, so rely on the optimizer to |
| 299 | // remove the std::pow() path for that case. |
| 300 | if (!std::is_same<float, result_type>::value) { |
| 301 | x = std::pow(u, p.a_); |
| 302 | y = std::pow(v, p.b_); |
| 303 | z = x + y; |
| 304 | if (z > 1) { |
| 305 | // Reject if and only if `x + y > 1.0` |
| 306 | continue; |
| 307 | } |
| 308 | if (z > 0) { |
| 309 | // When both alpha and beta are small, x and y are both close to 0, so |
| 310 | // divide by (x+y) directly may result in nan. |
| 311 | return x / z; |
| 312 | } |
| 313 | } |
| 314 |
| 315 | // Log transform. |
| 316 | // x = log( pow(u, p.a_) ), y = log( pow(v, p.b_) ) |
| 317 | // since u, v <= 1.0, x, y < 0. |
| 318 | x = std::log(u) * p.a_; |
| 319 | y = std::log(v) * p.b_; |
| 320 | if (!std::isfinite(x) || !std::isfinite(y)) { |
| 321 | continue; |
| 322 | } |
| 323 | // z = log( pow(u, a) + pow(v, b) ) |
| 324 | z = x > y ? (x + std::log(1 + std::exp(y - x))) |
| 325 | : (y + std::log(1 + std::exp(x - y))); |
| 326 | // Reject iff log(x+y) > 0. |
| 327 | if (z > 0) { |
| 328 | continue; |
| 329 | } |
| 330 | return std::exp(x - z); |
| 331 | } |
| 332 | } |
| 333 |
| 334 | template <typename RealType> |
| 335 | template <typename URBG> |
| 336 | typename beta_distribution<RealType>::result_type |
| 337 | beta_distribution<RealType>::AlgorithmCheng( |
| 338 | URBG& g, // NOLINT(runtime/references) |
| 339 | const param_type& p) { |
| 340 | using random_internal::GeneratePositiveTag; |
| 341 | using random_internal::GenerateRealFromBits; |
| 342 | using real_type = |
| 343 | absl::conditional_t<std::is_same<RealType, float>::value, float, double>; |
| 344 |
| 345 | // Based on Cheng, Russell CH. Generating beta variates with nonintegral |
| 346 | // shape parameters. Communications of the ACM 21.4 (1978): 317-322. |
| 347 | // (https://dl.acm.org/citation.cfm?id=359482). |
| 348 | static constexpr result_type kLogFour = |
| 349 | result_type(1.3862943611198906188344642429163531361); // log(4) |
| 350 | static constexpr result_type kS = |
| 351 | result_type(2.6094379124341003746007593332261876); // 1+log(5) |
| 352 |
| 353 | const bool use_algorithm_ba = (p.method_ == param_type::CHENG_BA); |
| 354 | result_type u1, u2, v, w, z, r, s, t, bw_inv, lhs; |
| 355 | for (;;) { |
| 356 | u1 = GenerateRealFromBits<real_type, GeneratePositiveTag, false>( |
| 357 | fast_u64_(g)); |
| 358 | u2 = GenerateRealFromBits<real_type, GeneratePositiveTag, false>( |
| 359 | fast_u64_(g)); |
| 360 | v = p.y_ * std::log(u1 / (1 - u1)); |
| 361 | w = p.a_ * std::exp(v); |
| 362 | bw_inv = result_type(1) / (p.b_ + w); |
| 363 | r = p.gamma_ * v - kLogFour; |
| 364 | s = p.a_ + r - w; |
| 365 | z = u1 * u1 * u2; |
| 366 | if (!use_algorithm_ba && s + kS >= 5 * z) { |
| 367 | break; |
| 368 | } |
| 369 | t = std::log(z); |
| 370 | if (!use_algorithm_ba && s >= t) { |
| 371 | break; |
| 372 | } |
| 373 | lhs = p.x_ * (p.log_x_ + std::log(bw_inv)) + r; |
| 374 | if (lhs >= t) { |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | return p.inverted_ ? (1 - w * bw_inv) : w * bw_inv; |
| 379 | } |
| 380 |
| 381 | template <typename RealType> |
| 382 | template <typename URBG> |
| 383 | typename beta_distribution<RealType>::result_type |
| 384 | beta_distribution<RealType>::operator()(URBG& g, // NOLINT(runtime/references) |
| 385 | const param_type& p) { |
| 386 | switch (p.method_) { |
| 387 | case param_type::JOEHNK: |
| 388 | return AlgorithmJoehnk(g, p); |
| 389 | case param_type::CHENG_BA: |
| 390 | ABSL_FALLTHROUGH_INTENDED; |
| 391 | case param_type::CHENG_BB: |
| 392 | return AlgorithmCheng(g, p); |
| 393 | default: |
| 394 | return DegenerateCase(g, p); |
| 395 | } |
| 396 | } |
| 397 |
| 398 | template <typename CharT, typename Traits, typename RealType> |
| 399 | std::basic_ostream<CharT, Traits>& operator<<( |
| 400 | std::basic_ostream<CharT, Traits>& os, // NOLINT(runtime/references) |
| 401 | const beta_distribution<RealType>& x) { |
| 402 | auto saver = random_internal::make_ostream_state_saver(os); |
| 403 | os.precision(random_internal::stream_precision_helper<RealType>::kPrecision); |
| 404 | os << x.alpha() << os.fill() << x.beta(); |
| 405 | return os; |
| 406 | } |
| 407 |
| 408 | template <typename CharT, typename Traits, typename RealType> |
| 409 | std::basic_istream<CharT, Traits>& operator>>( |
| 410 | std::basic_istream<CharT, Traits>& is, // NOLINT(runtime/references) |
| 411 | beta_distribution<RealType>& x) { // NOLINT(runtime/references) |
| 412 | using result_type = typename beta_distribution<RealType>::result_type; |
| 413 | using param_type = typename beta_distribution<RealType>::param_type; |
| 414 | result_type alpha, beta; |
| 415 |
| 416 | auto saver = random_internal::make_istream_state_saver(is); |
| 417 | alpha = random_internal::read_floating_point<result_type>(is); |
| 418 | if (is.fail()) return is; |
| 419 | beta = random_internal::read_floating_point<result_type>(is); |
| 420 | if (!is.fail()) { |
| 421 | x.param(param_type(alpha, beta)); |
| 422 | } |
| 423 | return is; |
| 424 | } |
| 425 |
| 426 | ABSL_NAMESPACE_END |
| 427 | } // namespace absl |
| 428 |
| 429 | #endif // ABSL_RANDOM_BETA_DISTRIBUTION_H_ |
| 430 |