]> git.donarmstrong.com Git - rsem.git/blob - boost/random/binomial_distribution.hpp
f0e46a50eaa3f01e59565efa506b2cd8dffaead8
[rsem.git] / boost / random / binomial_distribution.hpp
1 /* boost random/binomial_distribution.hpp header file
2  *
3  * Copyright Jens Maurer 2002
4  * Distributed under the Boost Software License, Version 1.0. (See
5  * accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  * See http://www.boost.org for most recent version including documentation.
9  *
10  * $Id: binomial_distribution.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
11  *
12  */
13
14 #ifndef BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP
15 #define BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP
16
17 #include <boost/config/no_tr1/cmath.hpp>
18 #include <cassert>
19 #include <boost/random/detail/config.hpp>
20 #include <boost/random/bernoulli_distribution.hpp>
21
22 namespace boost {
23
24 /**
25  * The binomial distribution is an integer valued distribution with
26  * two parameters, @c t and @c p.  The values of the distribution
27  * are within the range [0,t].
28  *
29  * The probability that the distribution produces a value k is
30  * \f${t \choose k}p^k(1-p)^{t-k}\f$.
31  */
32 template<class IntType = int, class RealType = double>
33 class binomial_distribution
34 {
35 public:
36   typedef typename bernoulli_distribution<RealType>::input_type input_type;
37   typedef IntType result_type;
38
39   /**
40    * Construct an @c binomial_distribution object. @c t and @c p
41    * are the parameters of the distribution.
42    *
43    * Requires: t >=0 && 0 <= p <= 1
44    */
45   explicit binomial_distribution(IntType t = 1,
46                                  const RealType& p = RealType(0.5))
47     : _bernoulli(p), _t(t)
48   {
49     assert(_t >= 0);
50     assert(RealType(0) <= p && p <= RealType(1));
51   }
52
53   // compiler-generated copy ctor and assignment operator are fine
54
55   /** Returns: the @c t parameter of the distribution */
56   IntType t() const { return _t; }
57   /** Returns: the @c p parameter of the distribution */
58   RealType p() const { return _bernoulli.p(); }
59   /**
60    * Effects: Subsequent uses of the distribution do not depend
61    * on values produced by any engine prior to invoking reset.
62    */
63   void reset() { }
64
65   /**
66    * Returns: a random variate distributed according to the
67    * binomial distribution.
68    */
69   template<class Engine>
70   result_type operator()(Engine& eng)
71   {
72     // TODO: This is O(_t), but it should be O(log(_t)) for large _t
73     result_type n = 0;
74     for(IntType i = 0; i < _t; ++i)
75       if(_bernoulli(eng))
76         ++n;
77     return n;
78   }
79
80 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
81   /**
82    * Writes the parameters of the distribution to a @c std::ostream.
83    */
84   template<class CharT, class Traits>
85   friend std::basic_ostream<CharT,Traits>&
86   operator<<(std::basic_ostream<CharT,Traits>& os, const binomial_distribution& bd)
87   {
88     os << bd._bernoulli << " " << bd._t;
89     return os;
90   }
91
92   /**
93    * Reads the parameters of the distribution from a @c std::istream.
94    */
95   template<class CharT, class Traits>
96   friend std::basic_istream<CharT,Traits>&
97   operator>>(std::basic_istream<CharT,Traits>& is, binomial_distribution& bd)
98   {
99     is >> std::ws >> bd._bernoulli >> std::ws >> bd._t;
100     return is;
101   }
102 #endif
103
104 private:
105   bernoulli_distribution<RealType> _bernoulli;
106   IntType _t;
107 };
108
109 } // namespace boost
110
111 #endif // BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP