123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- #ifndef BOOST_RANDOM_BETA_DISTRIBUTION_HPP
- #define BOOST_RANDOM_BETA_DISTRIBUTION_HPP
- #include <cassert>
- #include <istream>
- #include <iosfwd>
- #include <boost/random/detail/operators.hpp>
- #include <boost/random/gamma_distribution.hpp>
- namespace boost {
- namespace random {
- template<class RealType = double>
- class beta_distribution {
- public:
- typedef RealType result_type;
- typedef RealType input_type;
- class param_type {
- public:
- typedef beta_distribution distribution_type;
-
- explicit param_type(RealType alpha_arg = RealType(1.0),
- RealType beta_arg = RealType(1.0))
- : _alpha(alpha_arg), _beta(beta_arg)
- {
- assert(alpha_arg > 0);
- assert(beta_arg > 0);
- }
-
- RealType alpha() const { return _alpha; }
-
- RealType beta() const { return _beta; }
-
- BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
- { os << parm._alpha << ' ' << parm._beta; return os; }
-
- BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
- { is >> parm._alpha >> std::ws >> parm._beta; return is; }
-
- BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
- { return lhs._alpha == rhs._alpha && lhs._beta == rhs._beta; }
-
-
- BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
- private:
- RealType _alpha;
- RealType _beta;
- };
-
- explicit beta_distribution(RealType alpha_arg = RealType(1.0),
- RealType beta_arg = RealType(1.0))
- : _alpha(alpha_arg), _beta(beta_arg)
- {
- assert(alpha_arg > 0);
- assert(beta_arg > 0);
- }
-
- explicit beta_distribution(const param_type& parm)
- : _alpha(parm.alpha()), _beta(parm.beta())
- {}
-
- template<class URNG>
- RealType operator()(URNG& urng) const
- {
- RealType a = gamma_distribution<RealType>(_alpha, RealType(1.0))(urng);
- RealType b = gamma_distribution<RealType>(_beta, RealType(1.0))(urng);
- return a / (a + b);
- }
-
- template<class URNG>
- RealType operator()(URNG& urng, const param_type& parm) const
- {
- return beta_distribution(parm)(urng);
- }
-
- RealType alpha() const { return _alpha; }
-
- RealType beta() const { return _beta; }
-
- RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
- { return RealType(0.0); }
-
- RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
- { return RealType(1.0); }
-
- param_type param() const { return param_type(_alpha, _beta); }
-
- void param(const param_type& parm)
- {
- _alpha = parm.alpha();
- _beta = parm.beta();
- }
-
- void reset() { }
-
- BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, beta_distribution, wd)
- {
- os << wd.param();
- return os;
- }
-
- BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, beta_distribution, wd)
- {
- param_type parm;
- if(is >> parm) {
- wd.param(parm);
- }
- return is;
- }
-
- BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(beta_distribution, lhs, rhs)
- { return lhs._alpha == rhs._alpha && lhs._beta == rhs._beta; }
-
-
- BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(beta_distribution)
- private:
- RealType _alpha;
- RealType _beta;
- };
- }
- }
- #endif
|