You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

beta_distribution.h 18 kB

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