1 /* boost random/poisson_distribution.hpp header file
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)
9 * See http://www.boost.org for most recent version including documentation.
11 * $Id: poisson_distribution.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
15 #ifndef BOOST_RANDOM_POISSON_DISTRIBUTION_HPP
16 #define BOOST_RANDOM_POISSON_DISTRIBUTION_HPP
18 #include <boost/config/no_tr1/cmath.hpp>
21 #include <boost/assert.hpp>
22 #include <boost/limits.hpp>
23 #include <boost/random/uniform_01.hpp>
24 #include <boost/random/detail/config.hpp>
26 #include <boost/random/detail/disable_warnings.hpp>
33 template<class RealType>
34 struct poisson_table {
35 static RealType value[10];
38 template<class RealType>
39 RealType poisson_table<RealType>::value[10] = {
55 * An instantiation of the class template @c poisson_distribution is a
56 * model of \random_distribution. The poisson distribution has
57 * \f$p(i) = \frac{e^{-\lambda}\lambda^i}{i!}\f$
59 * This implementation is based on the PTRD algorithm described
62 * "The transformed rejection method for generating Poisson random variables",
63 * Wolfgang Hormann, Insurance: Mathematics and Economics
64 * Volume 12, Issue 1, February 1993, Pages 39-45
67 template<class IntType = int, class RealType = double>
68 class poisson_distribution {
70 typedef IntType result_type;
71 typedef RealType input_type;
75 typedef poisson_distribution distribution_type;
77 * Construct a param_type object with the parameter "mean"
81 explicit param_type(RealType mean_arg = RealType(1))
84 BOOST_ASSERT(_mean > 0);
86 /* Returns the "mean" parameter of the distribution. */
87 RealType mean() const { return _mean; }
88 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
89 /** Writes the parameters of the distribution to a @c std::ostream. */
90 template<class CharT, class Traits>
91 friend std::basic_ostream<CharT, Traits>&
92 operator<<(std::basic_ostream<CharT, Traits>& os,
93 const param_type& parm)
99 /** Reads the parameters of the distribution from a @c std::istream. */
100 template<class CharT, class Traits>
101 friend std::basic_istream<CharT, Traits>&
102 operator>>(std::basic_istream<CharT, Traits>& is, param_type& parm)
108 /** Returns true if the parameters have the same values. */
109 friend bool operator==(const param_type& lhs, const param_type& rhs)
111 return lhs._mean == rhs._mean;
113 /** Returns true if the parameters have different values. */
114 friend bool operator!=(const param_type& lhs, const param_type& rhs)
116 return !(lhs == rhs);
123 * Constructs a @c poisson_distribution with the parameter @c mean.
127 explicit poisson_distribution(RealType mean_arg = RealType(1))
130 BOOST_ASSERT(_mean > 0);
135 * Construct an @c poisson_distribution object from the
138 explicit poisson_distribution(const param_type& parm)
145 * Returns a random variate distributed according to the
146 * poisson distribution.
149 IntType operator()(URNG& urng) const
151 if(use_inversion()) {
154 return generate(urng);
159 * Returns a random variate distributed according to the
160 * poisson distribution with parameters specified by param.
163 IntType operator()(URNG& urng, const param_type& parm) const
165 return poisson_distribution(parm)(urng);
168 /** Returns the "mean" parameter of the distribution. */
169 RealType mean() const { return _mean; }
171 /** Returns the smallest value that the distribution can produce. */
172 IntType min BOOST_PREVENT_MACRO_SUBSTITUTION() const { return 0; }
173 /** Returns the largest value that the distribution can produce. */
174 IntType max BOOST_PREVENT_MACRO_SUBSTITUTION() const
175 { return (std::numeric_limits<IntType>::max)(); }
177 /** Returns the parameters of the distribution. */
178 param_type param() const { return param_type(_mean); }
179 /** Sets parameters of the distribution. */
180 void param(const param_type& parm)
187 * Effects: Subsequent uses of the distribution do not depend
188 * on values produced by any engine prior to invoking reset.
192 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
193 /** Writes the parameters of the distribution to a @c std::ostream. */
194 template<class CharT, class Traits>
195 friend std::basic_ostream<CharT,Traits>&
196 operator<<(std::basic_ostream<CharT,Traits>& os,
197 const poisson_distribution& pd)
203 /** Reads the parameters of the distribution from a @c std::istream. */
204 template<class CharT, class Traits>
205 friend std::basic_istream<CharT,Traits>&
206 operator>>(std::basic_istream<CharT,Traits>& is, poisson_distribution& pd)
213 /** Returns true if the two distributions will produce the same
214 sequence of values, given equal generators. */
215 friend bool operator==(const poisson_distribution& lhs,
216 const poisson_distribution& rhs)
218 return lhs._mean == rhs._mean;
220 /** Returns true if the two distributions could produce different
221 sequences of values, given equal generators. */
222 friend bool operator!=(const poisson_distribution& lhs,
223 const poisson_distribution& rhs)
225 return !(lhs == rhs);
230 /// @cond show_private
232 template<class CharT, class Traits>
233 void read(std::basic_istream<CharT, Traits>& is) {
240 bool use_inversion() const
245 static RealType log_factorial(IntType k)
247 BOOST_ASSERT(k >= 0);
248 BOOST_ASSERT(k < 10);
249 return detail::poisson_table<RealType>::value[k];
257 if(use_inversion()) {
258 _exp_mean = exp(-_mean);
260 _ptrd.smu = sqrt(_mean);
261 _ptrd.b = 0.931 + 2.53 * _ptrd.smu;
262 _ptrd.a = -0.059 + 0.02483 * _ptrd.b;
263 _ptrd.inv_alpha = 1.1239 + 1.1328 / (_ptrd.b - 3.4);
264 _ptrd.v_r = 0.9277 - 3.6224 / (_ptrd.b - 2);
269 IntType generate(URNG& urng) const
277 RealType v = uniform_01<RealType>()(urng);
278 if(v <= 0.86 * _ptrd.v_r) {
279 u = v / _ptrd.v_r - 0.43;
280 return static_cast<IntType>(floor(
281 (2*_ptrd.a/(0.5-abs(u)) + _ptrd.b)*u + _mean + 0.445));
285 u = uniform_01<RealType>()(urng) - 0.5;
287 u = v/_ptrd.v_r - 0.93;
288 u = ((u < 0)? -0.5 : 0.5) - u;
289 v = uniform_01<RealType>()(urng) * _ptrd.v_r;
292 RealType us = 0.5 - abs(u);
293 if(us < 0.013 && v > us) {
297 RealType k = floor((2*_ptrd.a/us + _ptrd.b)*u+_mean+0.445);
298 v = v*_ptrd.inv_alpha/(_ptrd.a/(us*us) + _ptrd.b);
300 RealType log_sqrt_2pi = 0.91893853320467267;
303 if(log(v*_ptrd.smu) <= (k + 0.5)*log(_mean/k)
307 - (1/12. - (1/360. - 1/(1260.*k*k))/(k*k))/k) {
308 return static_cast<IntType>(k);
311 if(log(v) <= k*log(_mean)
313 - log_factorial(static_cast<IntType>(k))) {
314 return static_cast<IntType>(k);
321 IntType invert(URNG& urng) const
323 RealType p = _exp_mean;
325 RealType u = uniform_01<RealType>()(urng);
352 } // namespace random
354 using random::poisson_distribution;
358 #include <boost/random/detail/enable_warnings.hpp>
360 #endif // BOOST_RANDOM_POISSON_DISTRIBUTION_HPP