binomial_distribution.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___RANDOM_BINOMIAL_DISTRIBUTION_H
  9. #define _LIBCPP___RANDOM_BINOMIAL_DISTRIBUTION_H
  10. #include <__config>
  11. #include <__random/is_valid.h>
  12. #include <__random/uniform_real_distribution.h>
  13. #include <cmath>
  14. #include <iosfwd>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. _LIBCPP_PUSH_MACROS
  19. #include <__undef_macros>
  20. _LIBCPP_BEGIN_NAMESPACE_STD
  21. template <class _IntType = int>
  22. class _LIBCPP_TEMPLATE_VIS binomial_distribution {
  23. static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type");
  24. public:
  25. // types
  26. typedef _IntType result_type;
  27. class _LIBCPP_TEMPLATE_VIS param_type {
  28. result_type __t_;
  29. double __p_;
  30. double __pr_;
  31. double __odds_ratio_;
  32. result_type __r0_;
  33. public:
  34. typedef binomial_distribution distribution_type;
  35. _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __t = 1, double __p = 0.5);
  36. _LIBCPP_HIDE_FROM_ABI result_type t() const { return __t_; }
  37. _LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }
  38. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
  39. return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;
  40. }
  41. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
  42. friend class binomial_distribution;
  43. };
  44. private:
  45. param_type __p_;
  46. public:
  47. // constructors and reset functions
  48. #ifndef _LIBCPP_CXX03_LANG
  49. _LIBCPP_HIDE_FROM_ABI binomial_distribution() : binomial_distribution(1) {}
  50. _LIBCPP_HIDE_FROM_ABI explicit binomial_distribution(result_type __t, double __p = 0.5)
  51. : __p_(param_type(__t, __p)) {}
  52. #else
  53. _LIBCPP_HIDE_FROM_ABI explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
  54. : __p_(param_type(__t, __p)) {}
  55. #endif
  56. _LIBCPP_HIDE_FROM_ABI explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
  57. _LIBCPP_HIDE_FROM_ABI void reset() {}
  58. // generating functions
  59. template <class _URNG>
  60. _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
  61. return (*this)(__g, __p_);
  62. }
  63. template <class _URNG>
  64. _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
  65. // property functions
  66. _LIBCPP_HIDE_FROM_ABI result_type t() const { return __p_.t(); }
  67. _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }
  68. _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
  69. _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
  70. _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
  71. _LIBCPP_HIDE_FROM_ABI result_type max() const { return t(); }
  72. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const binomial_distribution& __x, const binomial_distribution& __y) {
  73. return __x.__p_ == __y.__p_;
  74. }
  75. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const binomial_distribution& __x, const binomial_distribution& __y) {
  76. return !(__x == __y);
  77. }
  78. };
  79. inline _LIBCPP_HIDE_FROM_ABI double __libcpp_lgamma(double __d) {
  80. #if defined(_LIBCPP_MSVCRT_LIKE)
  81. return lgamma(__d);
  82. #else
  83. int __sign;
  84. return lgamma_r(__d, &__sign);
  85. #endif
  86. }
  87. template <class _IntType>
  88. binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p) : __t_(__t), __p_(__p) {
  89. if (0 < __p_ && __p_ < 1) {
  90. __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
  91. __pr_ = std::exp(
  92. std::__libcpp_lgamma(__t_ + 1.) - std::__libcpp_lgamma(__r0_ + 1.) - std::__libcpp_lgamma(__t_ - __r0_ + 1.) +
  93. __r0_ * std::log(__p_) + (__t_ - __r0_) * std::log(1 - __p_));
  94. __odds_ratio_ = __p_ / (1 - __p_);
  95. }
  96. }
  97. // Reference: Kemp, C.D. (1986). `A modal method for generating binomial
  98. // variables', Commun. Statist. - Theor. Meth. 15(3), 805-813.
  99. template <class _IntType>
  100. template <class _URNG>
  101. _IntType binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr) {
  102. static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
  103. if (__pr.__t_ == 0 || __pr.__p_ == 0)
  104. return 0;
  105. if (__pr.__p_ == 1)
  106. return __pr.__t_;
  107. uniform_real_distribution<double> __gen;
  108. double __u = __gen(__g) - __pr.__pr_;
  109. if (__u < 0)
  110. return __pr.__r0_;
  111. double __pu = __pr.__pr_;
  112. double __pd = __pu;
  113. result_type __ru = __pr.__r0_;
  114. result_type __rd = __ru;
  115. while (true) {
  116. bool __break = true;
  117. if (__rd >= 1) {
  118. __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
  119. __u -= __pd;
  120. __break = false;
  121. if (__u < 0)
  122. return __rd - 1;
  123. }
  124. if (__rd != 0)
  125. --__rd;
  126. ++__ru;
  127. if (__ru <= __pr.__t_) {
  128. __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
  129. __u -= __pu;
  130. __break = false;
  131. if (__u < 0)
  132. return __ru;
  133. }
  134. if (__break)
  135. return 0;
  136. }
  137. }
  138. template <class _CharT, class _Traits, class _IntType>
  139. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  140. operator<<(basic_ostream<_CharT, _Traits>& __os, const binomial_distribution<_IntType>& __x) {
  141. __save_flags<_CharT, _Traits> __lx(__os);
  142. typedef basic_ostream<_CharT, _Traits> _OStream;
  143. __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
  144. _CharT __sp = __os.widen(' ');
  145. __os.fill(__sp);
  146. return __os << __x.t() << __sp << __x.p();
  147. }
  148. template <class _CharT, class _Traits, class _IntType>
  149. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  150. operator>>(basic_istream<_CharT, _Traits>& __is, binomial_distribution<_IntType>& __x) {
  151. typedef binomial_distribution<_IntType> _Eng;
  152. typedef typename _Eng::result_type result_type;
  153. typedef typename _Eng::param_type param_type;
  154. __save_flags<_CharT, _Traits> __lx(__is);
  155. typedef basic_istream<_CharT, _Traits> _Istream;
  156. __is.flags(_Istream::dec | _Istream::skipws);
  157. result_type __t;
  158. double __p;
  159. __is >> __t >> __p;
  160. if (!__is.fail())
  161. __x.param(param_type(__t, __p));
  162. return __is;
  163. }
  164. _LIBCPP_END_NAMESPACE_STD
  165. _LIBCPP_POP_MACROS
  166. #endif // _LIBCPP___RANDOM_BINOMIAL_DISTRIBUTION_H