poisson_distribution.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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_POISSON_DISTRIBUTION_H
  9. #define _LIBCPP___RANDOM_POISSON_DISTRIBUTION_H
  10. #include <__config>
  11. #include <__random/clamp_to_integral.h>
  12. #include <__random/exponential_distribution.h>
  13. #include <__random/is_valid.h>
  14. #include <__random/normal_distribution.h>
  15. #include <__random/uniform_real_distribution.h>
  16. #include <cmath>
  17. #include <iosfwd>
  18. #include <limits>
  19. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  20. # pragma GCC system_header
  21. #endif
  22. _LIBCPP_PUSH_MACROS
  23. #include <__undef_macros>
  24. _LIBCPP_BEGIN_NAMESPACE_STD
  25. template<class _IntType = int>
  26. class _LIBCPP_TEMPLATE_VIS poisson_distribution
  27. {
  28. static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type");
  29. public:
  30. // types
  31. typedef _IntType result_type;
  32. class _LIBCPP_TEMPLATE_VIS param_type
  33. {
  34. double __mean_;
  35. double __s_;
  36. double __d_;
  37. double __l_;
  38. double __omega_;
  39. double __c0_;
  40. double __c1_;
  41. double __c2_;
  42. double __c3_;
  43. double __c_;
  44. public:
  45. typedef poisson_distribution distribution_type;
  46. _LIBCPP_HIDE_FROM_ABI explicit param_type(double __mean = 1.0);
  47. _LIBCPP_INLINE_VISIBILITY
  48. double mean() const {return __mean_;}
  49. friend _LIBCPP_INLINE_VISIBILITY
  50. bool operator==(const param_type& __x, const param_type& __y)
  51. {return __x.__mean_ == __y.__mean_;}
  52. friend _LIBCPP_INLINE_VISIBILITY
  53. bool operator!=(const param_type& __x, const param_type& __y)
  54. {return !(__x == __y);}
  55. friend class poisson_distribution;
  56. };
  57. private:
  58. param_type __p_;
  59. public:
  60. // constructors and reset functions
  61. #ifndef _LIBCPP_CXX03_LANG
  62. _LIBCPP_INLINE_VISIBILITY
  63. poisson_distribution() : poisson_distribution(1.0) {}
  64. _LIBCPP_INLINE_VISIBILITY
  65. explicit poisson_distribution(double __mean)
  66. : __p_(__mean) {}
  67. #else
  68. _LIBCPP_INLINE_VISIBILITY
  69. explicit poisson_distribution(double __mean = 1.0)
  70. : __p_(__mean) {}
  71. #endif
  72. _LIBCPP_INLINE_VISIBILITY
  73. explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
  74. _LIBCPP_INLINE_VISIBILITY
  75. void reset() {}
  76. // generating functions
  77. template<class _URNG>
  78. _LIBCPP_INLINE_VISIBILITY
  79. result_type operator()(_URNG& __g)
  80. {return (*this)(__g, __p_);}
  81. template<class _URNG>
  82. _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
  83. // property functions
  84. _LIBCPP_INLINE_VISIBILITY
  85. double mean() const {return __p_.mean();}
  86. _LIBCPP_INLINE_VISIBILITY
  87. param_type param() const {return __p_;}
  88. _LIBCPP_INLINE_VISIBILITY
  89. void param(const param_type& __p) {__p_ = __p;}
  90. _LIBCPP_INLINE_VISIBILITY
  91. result_type min() const {return 0;}
  92. _LIBCPP_INLINE_VISIBILITY
  93. result_type max() const {return numeric_limits<result_type>::max();}
  94. friend _LIBCPP_INLINE_VISIBILITY
  95. bool operator==(const poisson_distribution& __x,
  96. const poisson_distribution& __y)
  97. {return __x.__p_ == __y.__p_;}
  98. friend _LIBCPP_INLINE_VISIBILITY
  99. bool operator!=(const poisson_distribution& __x,
  100. const poisson_distribution& __y)
  101. {return !(__x == __y);}
  102. };
  103. template<class _IntType>
  104. poisson_distribution<_IntType>::param_type::param_type(double __mean)
  105. // According to the standard `inf` is a valid input, but it causes the
  106. // distribution to hang, so we replace it with the maximum representable
  107. // mean.
  108. : __mean_(isinf(__mean) ? numeric_limits<double>::max() : __mean)
  109. {
  110. if (__mean_ < 10)
  111. {
  112. __s_ = 0;
  113. __d_ = 0;
  114. __l_ = _VSTD::exp(-__mean_);
  115. __omega_ = 0;
  116. __c3_ = 0;
  117. __c2_ = 0;
  118. __c1_ = 0;
  119. __c0_ = 0;
  120. __c_ = 0;
  121. }
  122. else
  123. {
  124. __s_ = _VSTD::sqrt(__mean_);
  125. __d_ = 6 * __mean_ * __mean_;
  126. __l_ = _VSTD::trunc(__mean_ - 1.1484);
  127. __omega_ = .3989423 / __s_;
  128. double __b1 = .4166667E-1 / __mean_;
  129. double __b2 = .3 * __b1 * __b1;
  130. __c3_ = .1428571 * __b1 * __b2;
  131. __c2_ = __b2 - 15. * __c3_;
  132. __c1_ = __b1 - 6. * __b2 + 45. * __c3_;
  133. __c0_ = 1. - __b1 + 3. * __b2 - 15. * __c3_;
  134. __c_ = .1069 / __mean_;
  135. }
  136. }
  137. template <class _IntType>
  138. template<class _URNG>
  139. _IntType
  140. poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
  141. {
  142. static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
  143. double __tx;
  144. uniform_real_distribution<double> __urd;
  145. if (__pr.__mean_ < 10)
  146. {
  147. __tx = 0;
  148. for (double __p = __urd(__urng); __p > __pr.__l_; ++__tx)
  149. __p *= __urd(__urng);
  150. }
  151. else
  152. {
  153. double __difmuk;
  154. double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
  155. double __u;
  156. if (__g > 0)
  157. {
  158. __tx = _VSTD::trunc(__g);
  159. if (__tx >= __pr.__l_)
  160. return _VSTD::__clamp_to_integral<result_type>(__tx);
  161. __difmuk = __pr.__mean_ - __tx;
  162. __u = __urd(__urng);
  163. if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
  164. return _VSTD::__clamp_to_integral<result_type>(__tx);
  165. }
  166. exponential_distribution<double> __edist;
  167. for (bool __using_exp_dist = false; true; __using_exp_dist = true)
  168. {
  169. double __e;
  170. if (__using_exp_dist || __g <= 0)
  171. {
  172. double __t;
  173. do
  174. {
  175. __e = __edist(__urng);
  176. __u = __urd(__urng);
  177. __u += __u - 1;
  178. __t = 1.8 + (__u < 0 ? -__e : __e);
  179. } while (__t <= -.6744);
  180. __tx = _VSTD::trunc(__pr.__mean_ + __pr.__s_ * __t);
  181. __difmuk = __pr.__mean_ - __tx;
  182. __using_exp_dist = true;
  183. }
  184. double __px;
  185. double __py;
  186. if (__tx < 10 && __tx >= 0)
  187. {
  188. const double __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
  189. 40320, 362880};
  190. __px = -__pr.__mean_;
  191. __py = _VSTD::pow(__pr.__mean_, (double)__tx) / __fac[static_cast<int>(__tx)];
  192. }
  193. else
  194. {
  195. double __del = .8333333E-1 / __tx;
  196. __del -= 4.8 * __del * __del * __del;
  197. double __v = __difmuk / __tx;
  198. if (_VSTD::abs(__v) > 0.25)
  199. __px = __tx * _VSTD::log(1 + __v) - __difmuk - __del;
  200. else
  201. __px = __tx * __v * __v * (((((((.1250060 * __v + -.1384794) *
  202. __v + .1421878) * __v + -.1661269) * __v + .2000118) *
  203. __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
  204. __py = .3989423 / _VSTD::sqrt(__tx);
  205. }
  206. double __r = (0.5 - __difmuk) / __pr.__s_;
  207. double __r2 = __r * __r;
  208. double __fx = -0.5 * __r2;
  209. double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
  210. __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
  211. if (__using_exp_dist)
  212. {
  213. if (__pr.__c_ * _VSTD::abs(__u) <= __py * _VSTD::exp(__px + __e) -
  214. __fy * _VSTD::exp(__fx + __e))
  215. break;
  216. }
  217. else
  218. {
  219. if (__fy - __u * __fy <= __py * _VSTD::exp(__px - __fx))
  220. break;
  221. }
  222. }
  223. }
  224. return _VSTD::__clamp_to_integral<result_type>(__tx);
  225. }
  226. template <class _CharT, class _Traits, class _IntType>
  227. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  228. operator<<(basic_ostream<_CharT, _Traits>& __os,
  229. const poisson_distribution<_IntType>& __x)
  230. {
  231. __save_flags<_CharT, _Traits> __lx(__os);
  232. typedef basic_ostream<_CharT, _Traits> _OStream;
  233. __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
  234. _OStream::scientific);
  235. return __os << __x.mean();
  236. }
  237. template <class _CharT, class _Traits, class _IntType>
  238. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  239. operator>>(basic_istream<_CharT, _Traits>& __is,
  240. poisson_distribution<_IntType>& __x)
  241. {
  242. typedef poisson_distribution<_IntType> _Eng;
  243. typedef typename _Eng::param_type param_type;
  244. __save_flags<_CharT, _Traits> __lx(__is);
  245. typedef basic_istream<_CharT, _Traits> _Istream;
  246. __is.flags(_Istream::dec | _Istream::skipws);
  247. double __mean;
  248. __is >> __mean;
  249. if (!__is.fail())
  250. __x.param(param_type(__mean));
  251. return __is;
  252. }
  253. _LIBCPP_END_NAMESPACE_STD
  254. _LIBCPP_POP_MACROS
  255. #endif // _LIBCPP___RANDOM_POISSON_DISTRIBUTION_H