poisson_distribution.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* boost random/poisson_distribution.hpp header file
  2. *
  3. * Copyright Jens Maurer 2002
  4. * Copyright Steven Watanabe 2010
  5. * Distributed under the Boost Software License, Version 1.0. (See
  6. * accompanying file LICENSE_1_0.txt or copy at
  7. * http://www.boost.org/LICENSE_1_0.txt)
  8. *
  9. * See http://www.boost.org for most recent version including documentation.
  10. *
  11. * $Id$
  12. *
  13. */
  14. #ifndef BOOST_RANDOM_POISSON_DISTRIBUTION_HPP
  15. #define BOOST_RANDOM_POISSON_DISTRIBUTION_HPP
  16. #include <boost/config/no_tr1/cmath.hpp>
  17. #include <cstdlib>
  18. #include <iosfwd>
  19. #include <boost/assert.hpp>
  20. #include <boost/limits.hpp>
  21. #include <boost/random/uniform_01.hpp>
  22. #include <boost/random/detail/config.hpp>
  23. #include <boost/random/detail/disable_warnings.hpp>
  24. namespace boost {
  25. namespace random {
  26. namespace detail {
  27. template<class RealType>
  28. struct poisson_table {
  29. static RealType value[10];
  30. };
  31. template<class RealType>
  32. RealType poisson_table<RealType>::value[10] = {
  33. 0.0,
  34. 0.0,
  35. 0.69314718055994529,
  36. 1.7917594692280550,
  37. 3.1780538303479458,
  38. 4.7874917427820458,
  39. 6.5792512120101012,
  40. 8.5251613610654147,
  41. 10.604602902745251,
  42. 12.801827480081469
  43. };
  44. }
  45. /**
  46. * An instantiation of the class template @c poisson_distribution is a
  47. * model of \random_distribution. The poisson distribution has
  48. * \f$p(i) = \frac{e^{-\lambda}\lambda^i}{i!}\f$
  49. *
  50. * This implementation is based on the PTRD algorithm described
  51. *
  52. * @blockquote
  53. * "The transformed rejection method for generating Poisson random variables",
  54. * Wolfgang Hormann, Insurance: Mathematics and Economics
  55. * Volume 12, Issue 1, February 1993, Pages 39-45
  56. * @endblockquote
  57. */
  58. template<class IntType = int, class RealType = double>
  59. class poisson_distribution {
  60. public:
  61. typedef IntType result_type;
  62. typedef RealType input_type;
  63. class param_type {
  64. public:
  65. typedef poisson_distribution distribution_type;
  66. /**
  67. * Construct a param_type object with the parameter "mean"
  68. *
  69. * Requires: mean > 0
  70. */
  71. explicit param_type(RealType mean_arg = RealType(1))
  72. : _mean(mean_arg)
  73. {
  74. BOOST_ASSERT(_mean > 0);
  75. }
  76. /* Returns the "mean" parameter of the distribution. */
  77. RealType mean() const { return _mean; }
  78. #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
  79. /** Writes the parameters of the distribution to a @c std::ostream. */
  80. template<class CharT, class Traits>
  81. friend std::basic_ostream<CharT, Traits>&
  82. operator<<(std::basic_ostream<CharT, Traits>& os,
  83. const param_type& parm)
  84. {
  85. os << parm._mean;
  86. return os;
  87. }
  88. /** Reads the parameters of the distribution from a @c std::istream. */
  89. template<class CharT, class Traits>
  90. friend std::basic_istream<CharT, Traits>&
  91. operator>>(std::basic_istream<CharT, Traits>& is, param_type& parm)
  92. {
  93. is >> parm._mean;
  94. return is;
  95. }
  96. #endif
  97. /** Returns true if the parameters have the same values. */
  98. friend bool operator==(const param_type& lhs, const param_type& rhs)
  99. {
  100. return lhs._mean == rhs._mean;
  101. }
  102. /** Returns true if the parameters have different values. */
  103. friend bool operator!=(const param_type& lhs, const param_type& rhs)
  104. {
  105. return !(lhs == rhs);
  106. }
  107. private:
  108. RealType _mean;
  109. };
  110. /**
  111. * Constructs a @c poisson_distribution with the parameter @c mean.
  112. *
  113. * Requires: mean > 0
  114. */
  115. explicit poisson_distribution(RealType mean_arg = RealType(1))
  116. : _mean(mean_arg)
  117. {
  118. BOOST_ASSERT(_mean > 0);
  119. init();
  120. }
  121. /**
  122. * Construct an @c poisson_distribution object from the
  123. * parameters.
  124. */
  125. explicit poisson_distribution(const param_type& parm)
  126. : _mean(parm.mean())
  127. {
  128. init();
  129. }
  130. /**
  131. * Returns a random variate distributed according to the
  132. * poisson distribution.
  133. */
  134. template<class URNG>
  135. IntType operator()(URNG& urng) const
  136. {
  137. if(use_inversion()) {
  138. return invert(urng);
  139. } else {
  140. return generate(urng);
  141. }
  142. }
  143. /**
  144. * Returns a random variate distributed according to the
  145. * poisson distribution with parameters specified by param.
  146. */
  147. template<class URNG>
  148. IntType operator()(URNG& urng, const param_type& parm) const
  149. {
  150. return poisson_distribution(parm)(urng);
  151. }
  152. /** Returns the "mean" parameter of the distribution. */
  153. RealType mean() const { return _mean; }
  154. /** Returns the smallest value that the distribution can produce. */
  155. IntType min BOOST_PREVENT_MACRO_SUBSTITUTION() const { return 0; }
  156. /** Returns the largest value that the distribution can produce. */
  157. IntType max BOOST_PREVENT_MACRO_SUBSTITUTION() const
  158. { return (std::numeric_limits<IntType>::max)(); }
  159. /** Returns the parameters of the distribution. */
  160. param_type param() const { return param_type(_mean); }
  161. /** Sets parameters of the distribution. */
  162. void param(const param_type& parm)
  163. {
  164. _mean = parm.mean();
  165. init();
  166. }
  167. /**
  168. * Effects: Subsequent uses of the distribution do not depend
  169. * on values produced by any engine prior to invoking reset.
  170. */
  171. void reset() { }
  172. #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
  173. /** Writes the parameters of the distribution to a @c std::ostream. */
  174. template<class CharT, class Traits>
  175. friend std::basic_ostream<CharT,Traits>&
  176. operator<<(std::basic_ostream<CharT,Traits>& os,
  177. const poisson_distribution& pd)
  178. {
  179. os << pd.param();
  180. return os;
  181. }
  182. /** Reads the parameters of the distribution from a @c std::istream. */
  183. template<class CharT, class Traits>
  184. friend std::basic_istream<CharT,Traits>&
  185. operator>>(std::basic_istream<CharT,Traits>& is, poisson_distribution& pd)
  186. {
  187. pd.read(is);
  188. return is;
  189. }
  190. #endif
  191. /** Returns true if the two distributions will produce the same
  192. sequence of values, given equal generators. */
  193. friend bool operator==(const poisson_distribution& lhs,
  194. const poisson_distribution& rhs)
  195. {
  196. return lhs._mean == rhs._mean;
  197. }
  198. /** Returns true if the two distributions could produce different
  199. sequences of values, given equal generators. */
  200. friend bool operator!=(const poisson_distribution& lhs,
  201. const poisson_distribution& rhs)
  202. {
  203. return !(lhs == rhs);
  204. }
  205. private:
  206. /// @cond show_private
  207. template<class CharT, class Traits>
  208. void read(std::basic_istream<CharT, Traits>& is) {
  209. param_type parm;
  210. if(is >> parm) {
  211. param(parm);
  212. }
  213. }
  214. bool use_inversion() const
  215. {
  216. return _mean < 10;
  217. }
  218. static RealType log_factorial(IntType k)
  219. {
  220. BOOST_ASSERT(k >= 0);
  221. BOOST_ASSERT(k < 10);
  222. return detail::poisson_table<RealType>::value[k];
  223. }
  224. void init()
  225. {
  226. using std::sqrt;
  227. using std::exp;
  228. if(use_inversion()) {
  229. _u._exp_mean = exp(-_mean);
  230. } else {
  231. _u._ptrd.smu = sqrt(_mean);
  232. _u._ptrd.b = 0.931 + 2.53 * _u._ptrd.smu;
  233. _u._ptrd.a = -0.059 + 0.02483 * _u._ptrd.b;
  234. _u._ptrd.inv_alpha = 1.1239 + 1.1328 / (_u._ptrd.b - 3.4);
  235. _u._ptrd.v_r = 0.9277 - 3.6224 / (_u._ptrd.b - 2);
  236. }
  237. }
  238. template<class URNG>
  239. IntType generate(URNG& urng) const
  240. {
  241. using std::floor;
  242. using std::abs;
  243. using std::log;
  244. while(true) {
  245. RealType u;
  246. RealType v = uniform_01<RealType>()(urng);
  247. if(v <= 0.86 * _u._ptrd.v_r) {
  248. u = v / _u._ptrd.v_r - 0.43;
  249. return static_cast<IntType>(floor(
  250. (2*_u._ptrd.a/(0.5-abs(u)) + _u._ptrd.b)*u + _mean + 0.445));
  251. }
  252. if(v >= _u._ptrd.v_r) {
  253. u = uniform_01<RealType>()(urng) - 0.5;
  254. } else {
  255. u = v/_u._ptrd.v_r - 0.93;
  256. u = ((u < 0)? -0.5 : 0.5) - u;
  257. v = uniform_01<RealType>()(urng) * _u._ptrd.v_r;
  258. }
  259. RealType us = 0.5 - abs(u);
  260. if(us < 0.013 && v > us) {
  261. continue;
  262. }
  263. RealType k = floor((2*_u._ptrd.a/us + _u._ptrd.b)*u+_mean+0.445);
  264. v = v*_u._ptrd.inv_alpha/(_u._ptrd.a/(us*us) + _u._ptrd.b);
  265. RealType log_sqrt_2pi = 0.91893853320467267;
  266. if(k >= 10) {
  267. if(log(v*_u._ptrd.smu) <= (k + 0.5)*log(_mean/k)
  268. - _mean
  269. - log_sqrt_2pi
  270. + k
  271. - (1/12. - (1/360. - 1/(1260.*k*k))/(k*k))/k) {
  272. return static_cast<IntType>(k);
  273. }
  274. } else if(k >= 0) {
  275. if(log(v) <= k*log(_mean)
  276. - _mean
  277. - log_factorial(static_cast<IntType>(k))) {
  278. return static_cast<IntType>(k);
  279. }
  280. }
  281. }
  282. }
  283. template<class URNG>
  284. IntType invert(URNG& urng) const
  285. {
  286. RealType p = _u._exp_mean;
  287. IntType x = 0;
  288. RealType u = uniform_01<RealType>()(urng);
  289. while(u > p) {
  290. u = u - p;
  291. ++x;
  292. p = _mean * p / x;
  293. }
  294. return x;
  295. }
  296. RealType _mean;
  297. union {
  298. // for ptrd
  299. struct {
  300. RealType v_r;
  301. RealType a;
  302. RealType b;
  303. RealType smu;
  304. RealType inv_alpha;
  305. } _ptrd;
  306. // for inversion
  307. RealType _exp_mean;
  308. } _u;
  309. /// @endcond
  310. };
  311. } // namespace random
  312. using random::poisson_distribution;
  313. } // namespace boost
  314. #include <boost/random/detail/enable_warnings.hpp>
  315. #endif // BOOST_RANDOM_POISSON_DISTRIBUTION_HPP