| 1 | #ifndef BOOST_RANDOM_BETA_DISTRIBUTION_HPP
|
|---|
| 2 | #define BOOST_RANDOM_BETA_DISTRIBUTION_HPP
|
|---|
| 3 |
|
|---|
| 4 | #include <istream>
|
|---|
| 5 | #include <iosfwd>
|
|---|
| 6 | #include <boost/assert.hpp>
|
|---|
| 7 | #include <boost/static_assert.hpp>
|
|---|
| 8 | #include <boost/random/detail/config.hpp>
|
|---|
| 9 | #include <boost/random/detail/operators.hpp>
|
|---|
| 10 | #include <boost/random/gamma_distribution.hpp>
|
|---|
| 11 |
|
|---|
| 12 | namespace boost { namespace random {
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * Instantiations of class template beta_distribution model a
|
|---|
| 16 | * \random_distribiton. Such a distribution produces random numbers @c x
|
|---|
| 17 | * distributed with probability density function
|
|---|
| 18 | * \f$\displaystyle p(x) =
|
|---|
| 19 | * \frac{1}{B(\alpha,\beta)}x^{\alpha-1}(1-x)^{\beta-1}
|
|---|
| 20 | * \f$,
|
|---|
| 21 | * where shape1 and shape2 are the paramters of the distributions.
|
|---|
| 22 | */
|
|---|
| 23 | template <typename RealType = double>
|
|---|
| 24 | class beta_distribution
|
|---|
| 25 | {
|
|---|
| 26 | public :
|
|---|
| 27 |
|
|---|
| 28 | typedef RealType input_type;
|
|---|
| 29 | typedef RealType result_type;
|
|---|
| 30 |
|
|---|
| 31 | class param_type
|
|---|
| 32 | {
|
|---|
| 33 | public :
|
|---|
| 34 |
|
|---|
| 35 | typedef beta_distribution distribution_type;
|
|---|
| 36 |
|
|---|
| 37 | /**
|
|---|
| 38 | * Constructs a @c param_type with a given shape1 and shape2.
|
|---|
| 39 | *
|
|---|
| 40 | * Requires: shape1 > 0 && shape2 > 0
|
|---|
| 41 | */
|
|---|
| 42 | explicit param_type (
|
|---|
| 43 | RealType shape1_arg = RealType(1.0),
|
|---|
| 44 | RealType shape2_arg = RealType(1.0)) :
|
|---|
| 45 | _shape1(shape1_arg), _shape2(shape2_arg) {}
|
|---|
| 46 |
|
|---|
| 47 | /** Returns the shape1 of the distribution. */
|
|---|
| 48 | RealType shape1 () const {return _shape1;}
|
|---|
| 49 |
|
|---|
| 50 | /** Returns the shape2 of the distribution. */
|
|---|
| 51 | RealType shape2 () const {return _shape2;}
|
|---|
| 52 |
|
|---|
| 53 | /** Writes a @c param_type to a @c std::ostream. */
|
|---|
| 54 | BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
|
|---|
| 55 | { os << parm._shape1 << " " << parm._shape2 ; return os; }
|
|---|
| 56 |
|
|---|
| 57 | /** Reads a @c param_type from a @c std::istream. */
|
|---|
| 58 | BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
|
|---|
| 59 | { is >> parm._shape1 >> std::ws >> parm._shape2; return is; }
|
|---|
| 60 |
|
|---|
| 61 | /** Returns true if the two sets of parameters are the same. */
|
|---|
| 62 | BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
|
|---|
| 63 | { return lhs._shape1 == rhs._shape1 && lhs._shape2 == rhs._shape2; }
|
|---|
| 64 |
|
|---|
| 65 | /** Returns true if the two sets of parameters are the different. */
|
|---|
| 66 | BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
|
|---|
| 67 | private :
|
|---|
| 68 |
|
|---|
| 69 | RealType _shape1;
|
|---|
| 70 | RealType _shape2;
|
|---|
| 71 | }; // class param_type
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * Constructs a @c beta_distribution objet. @c shape1 and @c shape2 are
|
|---|
| 75 | * the parameters for the distribution.
|
|---|
| 76 | *
|
|---|
| 77 | * Requires: shape1 > 0 && shape2 > 0
|
|---|
| 78 | */
|
|---|
| 79 | explicit beta_distribution (
|
|---|
| 80 | const RealType &shape1_arg = RealType(1.0),
|
|---|
| 81 | const RealType &shape2_arg = RealType(1.0)) :
|
|---|
| 82 | _shape1(shape1_arg), _shape2(shape2_arg)
|
|---|
| 83 | {
|
|---|
| 84 | BOOST_ASSERT(_shape1 > RealType(0.0));
|
|---|
| 85 | BOOST_ASSERT(_shape2 > RealType(0.0));
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Constructs a @c beta_distribution object from its parameters.
|
|---|
| 90 | */
|
|---|
| 91 | explicit beta_distribution (const param_type &parm) :
|
|---|
| 92 | _shape1(parm.shape1()), _shape2(parm.shape2())
|
|---|
| 93 | {
|
|---|
| 94 | BOOST_ASSERT(_shape1 > RealType(0.0));
|
|---|
| 95 | BOOST_ASSERT(_shape2 > RealType(0.0));
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | /** Returns the shape1 of the distribution. */
|
|---|
| 99 | RealType shape1 () const {return _shape1;}
|
|---|
| 100 |
|
|---|
| 101 | /** Returns the shape2 of the distribution. */
|
|---|
| 102 | RealType shape2 () const {return _shape2;}
|
|---|
| 103 |
|
|---|
| 104 | /** Returns the smallest value that the distribution can produce. */
|
|---|
| 105 | RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
|
|---|
| 106 | {return RealType(0.0);}
|
|---|
| 107 |
|
|---|
| 108 | /** Returns the largest value that the distribution can produce. */
|
|---|
| 109 | RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
|
|---|
| 110 | {return RealType(1.0);}
|
|---|
| 111 |
|
|---|
| 112 | /** Returns the parameters of the distribution. */
|
|---|
| 113 | param_type param () const {return param_type(_shape1, _shape2);}
|
|---|
| 114 |
|
|---|
| 115 | /** Sets the parameters of the distribution. */
|
|---|
| 116 | void param (const param_type &parm)
|
|---|
| 117 | {
|
|---|
| 118 | _shape1 = parm.shape1();
|
|---|
| 119 | _shape2 = parm.shape2();
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * Effects: Subsequent uses of the distribution do not depend
|
|---|
| 124 | * on values produced by any engine prior to invoking reset.
|
|---|
| 125 | */
|
|---|
| 126 | void reset () {}
|
|---|
| 127 |
|
|---|
| 128 | /** Returns a beta variate. */
|
|---|
| 129 | template <typename Engine>
|
|---|
| 130 | result_type operator() (Engine &eng)
|
|---|
| 131 | {
|
|---|
| 132 | RealType x =
|
|---|
| 133 | boost::random::gamma_distribution<RealType>(_shape1, 1)(eng);
|
|---|
| 134 | RealType y =
|
|---|
| 135 | boost::random::gamma_distribution<RealType>(_shape2, 1)(eng);
|
|---|
| 136 |
|
|---|
| 137 | return x / (x + y);
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | /** Returns a beta variate with parameters specified by @c param. */
|
|---|
| 141 | template <typename URNG>
|
|---|
| 142 | result_type operator() (URNG &urng, const param_type &parm)
|
|---|
| 143 | {
|
|---|
| 144 | return beta_distribution(parm)(urng);
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /** Writes a @c beta_distribution to a @c std::ostream. */
|
|---|
| 148 | BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, beta_distribution, nd)
|
|---|
| 149 | {
|
|---|
| 150 | os << nd._shape1 << " " << nd._shape2 << " ";
|
|---|
| 151 | return os;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /** Reads a @c beta_distribution from a @c std::istream. */
|
|---|
| 155 | BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, beta_distribution, nd)
|
|---|
| 156 | {
|
|---|
| 157 | is >> std::ws >> nd._shape1 >> std::ws >> nd._shape2;
|
|---|
| 158 | return is;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /**
|
|---|
| 162 | * Returns true if the two instances of @c beta_distribution will
|
|---|
| 163 | * return identical sequences of values given equal generators.
|
|---|
| 164 | */
|
|---|
| 165 | BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(beta_distribution, lhs, rhs)
|
|---|
| 166 | {
|
|---|
| 167 | return lhs._shape1 == rhs._shape2 && lhs._shape1 == rhs._shape2;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | /**
|
|---|
| 171 | * Returns true if the two instances of @c beta_distribution will
|
|---|
| 172 | * return different sequences of values given equal generators.
|
|---|
| 173 | */
|
|---|
| 174 | BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(beta_distribution)
|
|---|
| 175 |
|
|---|
| 176 | private :
|
|---|
| 177 |
|
|---|
| 178 | RealType _shape1;
|
|---|
| 179 | RealType _shape2;
|
|---|
| 180 | }; // class beta_distribution
|
|---|
| 181 |
|
|---|
| 182 | } // namespace random
|
|---|
| 183 |
|
|---|
| 184 | using random::beta_distribution;
|
|---|
| 185 |
|
|---|
| 186 | } // namespace boost
|
|---|
| 187 |
|
|---|
| 188 | #endif // BOOST_RANDOM_BETA_DISTRIBUTION_HPP
|
|---|