Chris@16
|
1 /* boost random/binomial_distribution.hpp header file
|
Chris@16
|
2 *
|
Chris@16
|
3 * Copyright Steven Watanabe 2010
|
Chris@16
|
4 * Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
5 * accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 *
|
Chris@16
|
8 * See http://www.boost.org for most recent version including documentation.
|
Chris@16
|
9 *
|
Chris@101
|
10 * $Id$
|
Chris@16
|
11 */
|
Chris@16
|
12
|
Chris@16
|
13 #ifndef BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP_INCLUDED
|
Chris@16
|
14 #define BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP_INCLUDED
|
Chris@16
|
15
|
Chris@16
|
16 #include <boost/config/no_tr1/cmath.hpp>
|
Chris@16
|
17 #include <cstdlib>
|
Chris@16
|
18 #include <iosfwd>
|
Chris@16
|
19
|
Chris@16
|
20 #include <boost/random/detail/config.hpp>
|
Chris@16
|
21 #include <boost/random/uniform_01.hpp>
|
Chris@16
|
22
|
Chris@16
|
23 #include <boost/random/detail/disable_warnings.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 namespace boost {
|
Chris@16
|
26 namespace random {
|
Chris@16
|
27
|
Chris@16
|
28 namespace detail {
|
Chris@16
|
29
|
Chris@16
|
30 template<class RealType>
|
Chris@16
|
31 struct binomial_table {
|
Chris@16
|
32 static const RealType table[10];
|
Chris@16
|
33 };
|
Chris@16
|
34
|
Chris@16
|
35 template<class RealType>
|
Chris@16
|
36 const RealType binomial_table<RealType>::table[10] = {
|
Chris@16
|
37 0.08106146679532726,
|
Chris@16
|
38 0.04134069595540929,
|
Chris@16
|
39 0.02767792568499834,
|
Chris@16
|
40 0.02079067210376509,
|
Chris@16
|
41 0.01664469118982119,
|
Chris@16
|
42 0.01387612882307075,
|
Chris@16
|
43 0.01189670994589177,
|
Chris@16
|
44 0.01041126526197209,
|
Chris@16
|
45 0.009255462182712733,
|
Chris@16
|
46 0.008330563433362871
|
Chris@16
|
47 };
|
Chris@16
|
48
|
Chris@16
|
49 }
|
Chris@16
|
50
|
Chris@16
|
51 /**
|
Chris@16
|
52 * The binomial distribution is an integer valued distribution with
|
Chris@16
|
53 * two parameters, @c t and @c p. The values of the distribution
|
Chris@16
|
54 * are within the range [0,t].
|
Chris@16
|
55 *
|
Chris@16
|
56 * The distribution function is
|
Chris@16
|
57 * \f$\displaystyle P(k) = {t \choose k}p^k(1-p)^{t-k}\f$.
|
Chris@16
|
58 *
|
Chris@16
|
59 * The algorithm used is the BTRD algorithm described in
|
Chris@16
|
60 *
|
Chris@16
|
61 * @blockquote
|
Chris@16
|
62 * "The generation of binomial random variates", Wolfgang Hormann,
|
Chris@16
|
63 * Journal of Statistical Computation and Simulation, Volume 46,
|
Chris@16
|
64 * Issue 1 & 2 April 1993 , pages 101 - 110
|
Chris@16
|
65 * @endblockquote
|
Chris@16
|
66 */
|
Chris@16
|
67 template<class IntType = int, class RealType = double>
|
Chris@16
|
68 class binomial_distribution {
|
Chris@16
|
69 public:
|
Chris@16
|
70 typedef IntType result_type;
|
Chris@16
|
71 typedef RealType input_type;
|
Chris@16
|
72
|
Chris@16
|
73 class param_type {
|
Chris@16
|
74 public:
|
Chris@16
|
75 typedef binomial_distribution distribution_type;
|
Chris@16
|
76 /**
|
Chris@16
|
77 * Construct a param_type object. @c t and @c p
|
Chris@16
|
78 * are the parameters of the distribution.
|
Chris@16
|
79 *
|
Chris@16
|
80 * Requires: t >=0 && 0 <= p <= 1
|
Chris@16
|
81 */
|
Chris@16
|
82 explicit param_type(IntType t_arg = 1, RealType p_arg = RealType (0.5))
|
Chris@16
|
83 : _t(t_arg), _p(p_arg)
|
Chris@16
|
84 {}
|
Chris@16
|
85 /** Returns the @c t parameter of the distribution. */
|
Chris@16
|
86 IntType t() const { return _t; }
|
Chris@16
|
87 /** Returns the @c p parameter of the distribution. */
|
Chris@16
|
88 RealType p() const { return _p; }
|
Chris@16
|
89 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
|
Chris@16
|
90 /** Writes the parameters of the distribution to a @c std::ostream. */
|
Chris@16
|
91 template<class CharT, class Traits>
|
Chris@16
|
92 friend std::basic_ostream<CharT,Traits>&
|
Chris@16
|
93 operator<<(std::basic_ostream<CharT,Traits>& os,
|
Chris@16
|
94 const param_type& parm)
|
Chris@16
|
95 {
|
Chris@16
|
96 os << parm._p << " " << parm._t;
|
Chris@16
|
97 return os;
|
Chris@16
|
98 }
|
Chris@16
|
99
|
Chris@16
|
100 /** Reads the parameters of the distribution from a @c std::istream. */
|
Chris@16
|
101 template<class CharT, class Traits>
|
Chris@16
|
102 friend std::basic_istream<CharT,Traits>&
|
Chris@16
|
103 operator>>(std::basic_istream<CharT,Traits>& is, param_type& parm)
|
Chris@16
|
104 {
|
Chris@16
|
105 is >> parm._p >> std::ws >> parm._t;
|
Chris@16
|
106 return is;
|
Chris@16
|
107 }
|
Chris@16
|
108 #endif
|
Chris@16
|
109 /** Returns true if the parameters have the same values. */
|
Chris@16
|
110 friend bool operator==(const param_type& lhs, const param_type& rhs)
|
Chris@16
|
111 {
|
Chris@16
|
112 return lhs._t == rhs._t && lhs._p == rhs._p;
|
Chris@16
|
113 }
|
Chris@16
|
114 /** Returns true if the parameters have different values. */
|
Chris@16
|
115 friend bool operator!=(const param_type& lhs, const param_type& rhs)
|
Chris@16
|
116 {
|
Chris@16
|
117 return !(lhs == rhs);
|
Chris@16
|
118 }
|
Chris@16
|
119 private:
|
Chris@16
|
120 IntType _t;
|
Chris@16
|
121 RealType _p;
|
Chris@16
|
122 };
|
Chris@16
|
123
|
Chris@16
|
124 /**
|
Chris@16
|
125 * Construct a @c binomial_distribution object. @c t and @c p
|
Chris@16
|
126 * are the parameters of the distribution.
|
Chris@16
|
127 *
|
Chris@16
|
128 * Requires: t >=0 && 0 <= p <= 1
|
Chris@16
|
129 */
|
Chris@16
|
130 explicit binomial_distribution(IntType t_arg = 1,
|
Chris@16
|
131 RealType p_arg = RealType(0.5))
|
Chris@16
|
132 : _t(t_arg), _p(p_arg)
|
Chris@16
|
133 {
|
Chris@16
|
134 init();
|
Chris@16
|
135 }
|
Chris@16
|
136
|
Chris@16
|
137 /**
|
Chris@16
|
138 * Construct an @c binomial_distribution object from the
|
Chris@16
|
139 * parameters.
|
Chris@16
|
140 */
|
Chris@16
|
141 explicit binomial_distribution(const param_type& parm)
|
Chris@16
|
142 : _t(parm.t()), _p(parm.p())
|
Chris@16
|
143 {
|
Chris@16
|
144 init();
|
Chris@16
|
145 }
|
Chris@16
|
146
|
Chris@16
|
147 /**
|
Chris@16
|
148 * Returns a random variate distributed according to the
|
Chris@16
|
149 * binomial distribution.
|
Chris@16
|
150 */
|
Chris@16
|
151 template<class URNG>
|
Chris@16
|
152 IntType operator()(URNG& urng) const
|
Chris@16
|
153 {
|
Chris@16
|
154 if(use_inversion()) {
|
Chris@16
|
155 if(0.5 < _p) {
|
Chris@16
|
156 return _t - invert(_t, 1-_p, urng);
|
Chris@16
|
157 } else {
|
Chris@16
|
158 return invert(_t, _p, urng);
|
Chris@16
|
159 }
|
Chris@16
|
160 } else if(0.5 < _p) {
|
Chris@16
|
161 return _t - generate(urng);
|
Chris@16
|
162 } else {
|
Chris@16
|
163 return generate(urng);
|
Chris@16
|
164 }
|
Chris@16
|
165 }
|
Chris@16
|
166
|
Chris@16
|
167 /**
|
Chris@16
|
168 * Returns a random variate distributed according to the
|
Chris@16
|
169 * binomial distribution with parameters specified by @c param.
|
Chris@16
|
170 */
|
Chris@16
|
171 template<class URNG>
|
Chris@16
|
172 IntType operator()(URNG& urng, const param_type& parm) const
|
Chris@16
|
173 {
|
Chris@16
|
174 return binomial_distribution(parm)(urng);
|
Chris@16
|
175 }
|
Chris@16
|
176
|
Chris@16
|
177 /** Returns the @c t parameter of the distribution. */
|
Chris@16
|
178 IntType t() const { return _t; }
|
Chris@16
|
179 /** Returns the @c p parameter of the distribution. */
|
Chris@16
|
180 RealType p() const { return _p; }
|
Chris@16
|
181
|
Chris@16
|
182 /** Returns the smallest value that the distribution can produce. */
|
Chris@16
|
183 IntType min BOOST_PREVENT_MACRO_SUBSTITUTION() const { return 0; }
|
Chris@16
|
184 /** Returns the largest value that the distribution can produce. */
|
Chris@16
|
185 IntType max BOOST_PREVENT_MACRO_SUBSTITUTION() const { return _t; }
|
Chris@16
|
186
|
Chris@16
|
187 /** Returns the parameters of the distribution. */
|
Chris@16
|
188 param_type param() const { return param_type(_t, _p); }
|
Chris@16
|
189 /** Sets parameters of the distribution. */
|
Chris@16
|
190 void param(const param_type& parm)
|
Chris@16
|
191 {
|
Chris@16
|
192 _t = parm.t();
|
Chris@16
|
193 _p = parm.p();
|
Chris@16
|
194 init();
|
Chris@16
|
195 }
|
Chris@16
|
196
|
Chris@16
|
197 /**
|
Chris@16
|
198 * Effects: Subsequent uses of the distribution do not depend
|
Chris@16
|
199 * on values produced by any engine prior to invoking reset.
|
Chris@16
|
200 */
|
Chris@16
|
201 void reset() { }
|
Chris@16
|
202
|
Chris@16
|
203 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
|
Chris@16
|
204 /** Writes the parameters of the distribution to a @c std::ostream. */
|
Chris@16
|
205 template<class CharT, class Traits>
|
Chris@16
|
206 friend std::basic_ostream<CharT,Traits>&
|
Chris@16
|
207 operator<<(std::basic_ostream<CharT,Traits>& os,
|
Chris@16
|
208 const binomial_distribution& bd)
|
Chris@16
|
209 {
|
Chris@16
|
210 os << bd.param();
|
Chris@16
|
211 return os;
|
Chris@16
|
212 }
|
Chris@16
|
213
|
Chris@16
|
214 /** Reads the parameters of the distribution from a @c std::istream. */
|
Chris@16
|
215 template<class CharT, class Traits>
|
Chris@16
|
216 friend std::basic_istream<CharT,Traits>&
|
Chris@16
|
217 operator>>(std::basic_istream<CharT,Traits>& is, binomial_distribution& bd)
|
Chris@16
|
218 {
|
Chris@16
|
219 bd.read(is);
|
Chris@16
|
220 return is;
|
Chris@16
|
221 }
|
Chris@16
|
222 #endif
|
Chris@16
|
223
|
Chris@16
|
224 /** Returns true if the two distributions will produce the same
|
Chris@16
|
225 sequence of values, given equal generators. */
|
Chris@16
|
226 friend bool operator==(const binomial_distribution& lhs,
|
Chris@16
|
227 const binomial_distribution& rhs)
|
Chris@16
|
228 {
|
Chris@16
|
229 return lhs._t == rhs._t && lhs._p == rhs._p;
|
Chris@16
|
230 }
|
Chris@16
|
231 /** Returns true if the two distributions could produce different
|
Chris@16
|
232 sequences of values, given equal generators. */
|
Chris@16
|
233 friend bool operator!=(const binomial_distribution& lhs,
|
Chris@16
|
234 const binomial_distribution& rhs)
|
Chris@16
|
235 {
|
Chris@16
|
236 return !(lhs == rhs);
|
Chris@16
|
237 }
|
Chris@16
|
238
|
Chris@16
|
239 private:
|
Chris@16
|
240
|
Chris@16
|
241 /// @cond show_private
|
Chris@16
|
242
|
Chris@16
|
243 template<class CharT, class Traits>
|
Chris@16
|
244 void read(std::basic_istream<CharT, Traits>& is) {
|
Chris@16
|
245 param_type parm;
|
Chris@16
|
246 if(is >> parm) {
|
Chris@16
|
247 param(parm);
|
Chris@16
|
248 }
|
Chris@16
|
249 }
|
Chris@16
|
250
|
Chris@16
|
251 bool use_inversion() const
|
Chris@16
|
252 {
|
Chris@16
|
253 // BTRD is safe when np >= 10
|
Chris@16
|
254 return m < 11;
|
Chris@16
|
255 }
|
Chris@16
|
256
|
Chris@16
|
257 // computes the correction factor for the Stirling approximation
|
Chris@16
|
258 // for log(k!)
|
Chris@16
|
259 static RealType fc(IntType k)
|
Chris@16
|
260 {
|
Chris@16
|
261 if(k < 10) return detail::binomial_table<RealType>::table[k];
|
Chris@16
|
262 else {
|
Chris@16
|
263 RealType ikp1 = RealType(1) / (k + 1);
|
Chris@16
|
264 return (RealType(1)/12
|
Chris@16
|
265 - (RealType(1)/360
|
Chris@16
|
266 - (RealType(1)/1260)*(ikp1*ikp1))*(ikp1*ikp1))*ikp1;
|
Chris@16
|
267 }
|
Chris@16
|
268 }
|
Chris@16
|
269
|
Chris@16
|
270 void init()
|
Chris@16
|
271 {
|
Chris@16
|
272 using std::sqrt;
|
Chris@16
|
273 using std::pow;
|
Chris@16
|
274
|
Chris@16
|
275 RealType p = (0.5 < _p)? (1 - _p) : _p;
|
Chris@16
|
276 IntType t = _t;
|
Chris@16
|
277
|
Chris@16
|
278 m = static_cast<IntType>((t+1)*p);
|
Chris@16
|
279
|
Chris@16
|
280 if(use_inversion()) {
|
Chris@16
|
281 q_n = pow((1 - p), static_cast<RealType>(t));
|
Chris@16
|
282 } else {
|
Chris@16
|
283 btrd.r = p/(1-p);
|
Chris@16
|
284 btrd.nr = (t+1)*btrd.r;
|
Chris@16
|
285 btrd.npq = t*p*(1-p);
|
Chris@16
|
286 RealType sqrt_npq = sqrt(btrd.npq);
|
Chris@16
|
287 btrd.b = 1.15 + 2.53 * sqrt_npq;
|
Chris@16
|
288 btrd.a = -0.0873 + 0.0248*btrd.b + 0.01*p;
|
Chris@16
|
289 btrd.c = t*p + 0.5;
|
Chris@16
|
290 btrd.alpha = (2.83 + 5.1/btrd.b) * sqrt_npq;
|
Chris@16
|
291 btrd.v_r = 0.92 - 4.2/btrd.b;
|
Chris@16
|
292 btrd.u_rv_r = 0.86*btrd.v_r;
|
Chris@16
|
293 }
|
Chris@16
|
294 }
|
Chris@16
|
295
|
Chris@16
|
296 template<class URNG>
|
Chris@16
|
297 result_type generate(URNG& urng) const
|
Chris@16
|
298 {
|
Chris@16
|
299 using std::floor;
|
Chris@16
|
300 using std::abs;
|
Chris@16
|
301 using std::log;
|
Chris@16
|
302
|
Chris@16
|
303 while(true) {
|
Chris@16
|
304 RealType u;
|
Chris@16
|
305 RealType v = uniform_01<RealType>()(urng);
|
Chris@16
|
306 if(v <= btrd.u_rv_r) {
|
Chris@16
|
307 RealType u = v/btrd.v_r - 0.43;
|
Chris@16
|
308 return static_cast<IntType>(floor(
|
Chris@16
|
309 (2*btrd.a/(0.5 - abs(u)) + btrd.b)*u + btrd.c));
|
Chris@16
|
310 }
|
Chris@16
|
311
|
Chris@16
|
312 if(v >= btrd.v_r) {
|
Chris@16
|
313 u = uniform_01<RealType>()(urng) - 0.5;
|
Chris@16
|
314 } else {
|
Chris@16
|
315 u = v/btrd.v_r - 0.93;
|
Chris@16
|
316 u = ((u < 0)? -0.5 : 0.5) - u;
|
Chris@16
|
317 v = uniform_01<RealType>()(urng) * btrd.v_r;
|
Chris@16
|
318 }
|
Chris@16
|
319
|
Chris@16
|
320 RealType us = 0.5 - abs(u);
|
Chris@16
|
321 IntType k = static_cast<IntType>(floor((2*btrd.a/us + btrd.b)*u + btrd.c));
|
Chris@16
|
322 if(k < 0 || k > _t) continue;
|
Chris@16
|
323 v = v*btrd.alpha/(btrd.a/(us*us) + btrd.b);
|
Chris@16
|
324 RealType km = abs(k - m);
|
Chris@16
|
325 if(km <= 15) {
|
Chris@16
|
326 RealType f = 1;
|
Chris@16
|
327 if(m < k) {
|
Chris@16
|
328 IntType i = m;
|
Chris@16
|
329 do {
|
Chris@16
|
330 ++i;
|
Chris@16
|
331 f = f*(btrd.nr/i - btrd.r);
|
Chris@16
|
332 } while(i != k);
|
Chris@16
|
333 } else if(m > k) {
|
Chris@16
|
334 IntType i = k;
|
Chris@16
|
335 do {
|
Chris@16
|
336 ++i;
|
Chris@16
|
337 v = v*(btrd.nr/i - btrd.r);
|
Chris@16
|
338 } while(i != m);
|
Chris@16
|
339 }
|
Chris@16
|
340 if(v <= f) return k;
|
Chris@16
|
341 else continue;
|
Chris@16
|
342 } else {
|
Chris@16
|
343 // final acceptance/rejection
|
Chris@16
|
344 v = log(v);
|
Chris@16
|
345 RealType rho =
|
Chris@16
|
346 (km/btrd.npq)*(((km/3. + 0.625)*km + 1./6)/btrd.npq + 0.5);
|
Chris@16
|
347 RealType t = -km*km/(2*btrd.npq);
|
Chris@16
|
348 if(v < t - rho) return k;
|
Chris@16
|
349 if(v > t + rho) continue;
|
Chris@16
|
350
|
Chris@16
|
351 IntType nm = _t - m + 1;
|
Chris@16
|
352 RealType h = (m + 0.5)*log((m + 1)/(btrd.r*nm))
|
Chris@16
|
353 + fc(m) + fc(_t - m);
|
Chris@16
|
354
|
Chris@16
|
355 IntType nk = _t - k + 1;
|
Chris@16
|
356 if(v <= h + (_t+1)*log(static_cast<RealType>(nm)/nk)
|
Chris@16
|
357 + (k + 0.5)*log(nk*btrd.r/(k+1))
|
Chris@16
|
358 - fc(k)
|
Chris@16
|
359 - fc(_t - k))
|
Chris@16
|
360 {
|
Chris@16
|
361 return k;
|
Chris@16
|
362 } else {
|
Chris@16
|
363 continue;
|
Chris@16
|
364 }
|
Chris@16
|
365 }
|
Chris@16
|
366 }
|
Chris@16
|
367 }
|
Chris@16
|
368
|
Chris@16
|
369 template<class URNG>
|
Chris@16
|
370 IntType invert(IntType t, RealType p, URNG& urng) const
|
Chris@16
|
371 {
|
Chris@16
|
372 RealType q = 1 - p;
|
Chris@16
|
373 RealType s = p / q;
|
Chris@16
|
374 RealType a = (t + 1) * s;
|
Chris@16
|
375 RealType r = q_n;
|
Chris@16
|
376 RealType u = uniform_01<RealType>()(urng);
|
Chris@16
|
377 IntType x = 0;
|
Chris@16
|
378 while(u > r) {
|
Chris@16
|
379 u = u - r;
|
Chris@16
|
380 ++x;
|
Chris@101
|
381 RealType r1 = ((a/x) - s) * r;
|
Chris@101
|
382 // If r gets too small then the round-off error
|
Chris@101
|
383 // becomes a problem. At this point, p(i) is
|
Chris@101
|
384 // decreasing exponentially, so if we just call
|
Chris@101
|
385 // it 0, it's close enough. Note that the
|
Chris@101
|
386 // minimum value of q_n is about 1e-7, so we
|
Chris@101
|
387 // may need to be a little careful to make sure that
|
Chris@101
|
388 // we don't terminate the first time through the loop
|
Chris@101
|
389 // for float. (Hence the test that r is decreasing)
|
Chris@101
|
390 if(r1 < std::numeric_limits<RealType>::epsilon() && r1 < r) {
|
Chris@101
|
391 break;
|
Chris@101
|
392 }
|
Chris@101
|
393 r = r1;
|
Chris@16
|
394 }
|
Chris@16
|
395 return x;
|
Chris@16
|
396 }
|
Chris@16
|
397
|
Chris@16
|
398 // parameters
|
Chris@16
|
399 IntType _t;
|
Chris@16
|
400 RealType _p;
|
Chris@16
|
401
|
Chris@16
|
402 // common data
|
Chris@16
|
403 IntType m;
|
Chris@16
|
404
|
Chris@16
|
405 union {
|
Chris@16
|
406 // for btrd
|
Chris@16
|
407 struct {
|
Chris@16
|
408 RealType r;
|
Chris@16
|
409 RealType nr;
|
Chris@16
|
410 RealType npq;
|
Chris@16
|
411 RealType b;
|
Chris@16
|
412 RealType a;
|
Chris@16
|
413 RealType c;
|
Chris@16
|
414 RealType alpha;
|
Chris@16
|
415 RealType v_r;
|
Chris@16
|
416 RealType u_rv_r;
|
Chris@16
|
417 } btrd;
|
Chris@16
|
418 // for inversion
|
Chris@16
|
419 RealType q_n;
|
Chris@16
|
420 };
|
Chris@16
|
421
|
Chris@16
|
422 /// @endcond
|
Chris@16
|
423 };
|
Chris@16
|
424
|
Chris@16
|
425 }
|
Chris@16
|
426
|
Chris@16
|
427 // backwards compatibility
|
Chris@16
|
428 using random::binomial_distribution;
|
Chris@16
|
429
|
Chris@16
|
430 }
|
Chris@16
|
431
|
Chris@16
|
432 #include <boost/random/detail/enable_warnings.hpp>
|
Chris@16
|
433
|
Chris@16
|
434 #endif
|