binomial_distribution.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. {
  24. static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type");
  25. public:
  26. // types
  27. typedef _IntType result_type;
  28. class _LIBCPP_TEMPLATE_VIS param_type
  29. {
  30. result_type __t_;
  31. double __p_;
  32. double __pr_;
  33. double __odds_ratio_;
  34. result_type __r0_;
  35. public:
  36. typedef binomial_distribution distribution_type;
  37. _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __t = 1, double __p = 0.5);
  38. _LIBCPP_INLINE_VISIBILITY
  39. result_type t() const {return __t_;}
  40. _LIBCPP_INLINE_VISIBILITY
  41. double p() const {return __p_;}
  42. friend _LIBCPP_INLINE_VISIBILITY
  43. bool operator==(const param_type& __x, const param_type& __y)
  44. {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
  45. friend _LIBCPP_INLINE_VISIBILITY
  46. bool operator!=(const param_type& __x, const param_type& __y)
  47. {return !(__x == __y);}
  48. friend class binomial_distribution;
  49. };
  50. private:
  51. param_type __p_;
  52. public:
  53. // constructors and reset functions
  54. #ifndef _LIBCPP_CXX03_LANG
  55. _LIBCPP_INLINE_VISIBILITY
  56. binomial_distribution() : binomial_distribution(1) {}
  57. _LIBCPP_INLINE_VISIBILITY
  58. explicit binomial_distribution(result_type __t, double __p = 0.5)
  59. : __p_(param_type(__t, __p)) {}
  60. #else
  61. _LIBCPP_INLINE_VISIBILITY
  62. explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
  63. : __p_(param_type(__t, __p)) {}
  64. #endif
  65. _LIBCPP_INLINE_VISIBILITY
  66. explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
  67. _LIBCPP_INLINE_VISIBILITY
  68. void reset() {}
  69. // generating functions
  70. template<class _URNG>
  71. _LIBCPP_INLINE_VISIBILITY
  72. result_type operator()(_URNG& __g)
  73. {return (*this)(__g, __p_);}
  74. template<class _URNG>
  75. _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
  76. // property functions
  77. _LIBCPP_INLINE_VISIBILITY
  78. result_type t() const {return __p_.t();}
  79. _LIBCPP_INLINE_VISIBILITY
  80. double p() const {return __p_.p();}
  81. _LIBCPP_INLINE_VISIBILITY
  82. param_type param() const {return __p_;}
  83. _LIBCPP_INLINE_VISIBILITY
  84. void param(const param_type& __p) {__p_ = __p;}
  85. _LIBCPP_INLINE_VISIBILITY
  86. result_type min() const {return 0;}
  87. _LIBCPP_INLINE_VISIBILITY
  88. result_type max() const {return t();}
  89. friend _LIBCPP_INLINE_VISIBILITY
  90. bool operator==(const binomial_distribution& __x,
  91. const binomial_distribution& __y)
  92. {return __x.__p_ == __y.__p_;}
  93. friend _LIBCPP_INLINE_VISIBILITY
  94. bool operator!=(const binomial_distribution& __x,
  95. const binomial_distribution& __y)
  96. {return !(__x == __y);}
  97. };
  98. inline _LIBCPP_INLINE_VISIBILITY double __libcpp_lgamma(double __d) {
  99. #if defined(_LIBCPP_MSVCRT_LIKE)
  100. return lgamma(__d);
  101. #else
  102. int __sign;
  103. return lgamma_r(__d, &__sign);
  104. #endif
  105. }
  106. template<class _IntType>
  107. binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p)
  108. : __t_(__t), __p_(__p)
  109. {
  110. if (0 < __p_ && __p_ < 1)
  111. {
  112. __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
  113. __pr_ = _VSTD::exp(std::__libcpp_lgamma(__t_ + 1.) -
  114. std::__libcpp_lgamma(__r0_ + 1.) -
  115. std::__libcpp_lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) +
  116. (__t_ - __r0_) * _VSTD::log(1 - __p_));
  117. __odds_ratio_ = __p_ / (1 - __p_);
  118. }
  119. }
  120. // Reference: Kemp, C.D. (1986). `A modal method for generating binomial
  121. // variables', Commun. Statist. - Theor. Meth. 15(3), 805-813.
  122. template<class _IntType>
  123. template<class _URNG>
  124. _IntType
  125. binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
  126. {
  127. static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
  128. if (__pr.__t_ == 0 || __pr.__p_ == 0)
  129. return 0;
  130. if (__pr.__p_ == 1)
  131. return __pr.__t_;
  132. uniform_real_distribution<double> __gen;
  133. double __u = __gen(__g) - __pr.__pr_;
  134. if (__u < 0)
  135. return __pr.__r0_;
  136. double __pu = __pr.__pr_;
  137. double __pd = __pu;
  138. result_type __ru = __pr.__r0_;
  139. result_type __rd = __ru;
  140. while (true)
  141. {
  142. bool __break = true;
  143. if (__rd >= 1)
  144. {
  145. __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
  146. __u -= __pd;
  147. __break = false;
  148. if (__u < 0)
  149. return __rd - 1;
  150. }
  151. if ( __rd != 0 )
  152. --__rd;
  153. ++__ru;
  154. if (__ru <= __pr.__t_)
  155. {
  156. __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
  157. __u -= __pu;
  158. __break = false;
  159. if (__u < 0)
  160. return __ru;
  161. }
  162. if (__break)
  163. return 0;
  164. }
  165. }
  166. template <class _CharT, class _Traits, class _IntType>
  167. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  168. operator<<(basic_ostream<_CharT, _Traits>& __os,
  169. const binomial_distribution<_IntType>& __x)
  170. {
  171. __save_flags<_CharT, _Traits> __lx(__os);
  172. typedef basic_ostream<_CharT, _Traits> _OStream;
  173. __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
  174. _OStream::scientific);
  175. _CharT __sp = __os.widen(' ');
  176. __os.fill(__sp);
  177. return __os << __x.t() << __sp << __x.p();
  178. }
  179. template <class _CharT, class _Traits, class _IntType>
  180. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  181. operator>>(basic_istream<_CharT, _Traits>& __is,
  182. binomial_distribution<_IntType>& __x)
  183. {
  184. typedef binomial_distribution<_IntType> _Eng;
  185. typedef typename _Eng::result_type result_type;
  186. typedef typename _Eng::param_type param_type;
  187. __save_flags<_CharT, _Traits> __lx(__is);
  188. typedef basic_istream<_CharT, _Traits> _Istream;
  189. __is.flags(_Istream::dec | _Istream::skipws);
  190. result_type __t;
  191. double __p;
  192. __is >> __t >> __p;
  193. if (!__is.fail())
  194. __x.param(param_type(__t, __p));
  195. return __is;
  196. }
  197. _LIBCPP_END_NAMESPACE_STD
  198. _LIBCPP_POP_MACROS
  199. #endif // _LIBCPP___RANDOM_BINOMIAL_DISTRIBUTION_H