beta_distribution.h 14 KB

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