Chris@102: /* boost random/beta_distribution.hpp header file Chris@102: * Chris@102: * Copyright Steven Watanabe 2014 Chris@102: * Distributed under the Boost Software License, Version 1.0. (See Chris@102: * accompanying file LICENSE_1_0.txt or copy at Chris@102: * http://www.boost.org/LICENSE_1_0.txt) Chris@102: * Chris@102: * See http://www.boost.org for most recent version including documentation. Chris@102: * Chris@102: * $Id$ Chris@102: */ Chris@102: Chris@102: #ifndef BOOST_RANDOM_BETA_DISTRIBUTION_HPP Chris@102: #define BOOST_RANDOM_BETA_DISTRIBUTION_HPP Chris@102: Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: Chris@102: namespace boost { Chris@102: namespace random { Chris@102: Chris@102: /** Chris@102: * The beta distribution is a real-valued distribution which produces Chris@102: * values in the range [0, 1]. It has two parameters, alpha and beta. Chris@102: * Chris@102: * It has \f$\displaystyle p(x) = \frac{x^{\alpha-1}(1-x)^{\beta-1}}{B(\alpha, \beta)}\f$. Chris@102: */ Chris@102: template Chris@102: class beta_distribution { Chris@102: public: Chris@102: typedef RealType result_type; Chris@102: typedef RealType input_type; Chris@102: Chris@102: class param_type { Chris@102: public: Chris@102: typedef beta_distribution distribution_type; Chris@102: Chris@102: /** Chris@102: * Constructs a @c param_type from the "alpha" and "beta" parameters Chris@102: * of the distribution. Chris@102: * Chris@102: * Requires: alpha > 0, beta > 0 Chris@102: */ Chris@102: explicit param_type(RealType alpha_arg = RealType(1.0), Chris@102: RealType beta_arg = RealType(1.0)) Chris@102: : _alpha(alpha_arg), _beta(beta_arg) Chris@102: { Chris@102: assert(alpha_arg > 0); Chris@102: assert(beta_arg > 0); Chris@102: } Chris@102: Chris@102: /** Returns the "alpha" parameter of the distribtuion. */ Chris@102: RealType alpha() const { return _alpha; } Chris@102: /** Returns the "beta" parameter of the distribution. */ Chris@102: RealType beta() const { return _beta; } Chris@102: Chris@102: /** Writes a @c param_type to a @c std::ostream. */ Chris@102: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm) Chris@102: { os << parm._alpha << ' ' << parm._beta; return os; } Chris@102: Chris@102: /** Reads a @c param_type from a @c std::istream. */ Chris@102: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm) Chris@102: { is >> parm._alpha >> std::ws >> parm._beta; return is; } Chris@102: Chris@102: /** Returns true if the two sets of parameters are the same. */ Chris@102: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs) Chris@102: { return lhs._alpha == rhs._alpha && lhs._beta == rhs._beta; } Chris@102: Chris@102: /** Returns true if the two sets of parameters are the different. */ Chris@102: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type) Chris@102: Chris@102: private: Chris@102: RealType _alpha; Chris@102: RealType _beta; Chris@102: }; Chris@102: Chris@102: /** Chris@102: * Constructs an @c beta_distribution from its "alpha" and "beta" parameters. Chris@102: * Chris@102: * Requires: alpha > 0, beta > 0 Chris@102: */ Chris@102: explicit beta_distribution(RealType alpha_arg = RealType(1.0), Chris@102: RealType beta_arg = RealType(1.0)) Chris@102: : _alpha(alpha_arg), _beta(beta_arg) Chris@102: { Chris@102: assert(alpha_arg > 0); Chris@102: assert(beta_arg > 0); Chris@102: } Chris@102: /** Constructs an @c beta_distribution from its parameters. */ Chris@102: explicit beta_distribution(const param_type& parm) Chris@102: : _alpha(parm.alpha()), _beta(parm.beta()) Chris@102: {} Chris@102: Chris@102: /** Chris@102: * Returns a random variate distributed according to the Chris@102: * beta distribution. Chris@102: */ Chris@102: template Chris@102: RealType operator()(URNG& urng) const Chris@102: { Chris@102: RealType a = gamma_distribution(_alpha, RealType(1.0))(urng); Chris@102: RealType b = gamma_distribution(_beta, RealType(1.0))(urng); Chris@102: return a / (a + b); Chris@102: } Chris@102: Chris@102: /** Chris@102: * Returns a random variate distributed accordint to the beta Chris@102: * distribution with parameters specified by @c param. Chris@102: */ Chris@102: template Chris@102: RealType operator()(URNG& urng, const param_type& parm) const Chris@102: { Chris@102: return beta_distribution(parm)(urng); Chris@102: } Chris@102: Chris@102: /** Returns the "alpha" parameter of the distribution. */ Chris@102: RealType alpha() const { return _alpha; } Chris@102: /** Returns the "beta" parameter of the distribution. */ Chris@102: RealType beta() const { return _beta; } Chris@102: Chris@102: /** Returns the smallest value that the distribution can produce. */ Chris@102: RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const Chris@102: { return RealType(0.0); } Chris@102: /** Returns the largest value that the distribution can produce. */ Chris@102: RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const Chris@102: { return RealType(1.0); } Chris@102: Chris@102: /** Returns the parameters of the distribution. */ Chris@102: param_type param() const { return param_type(_alpha, _beta); } Chris@102: /** Sets the parameters of the distribution. */ Chris@102: void param(const param_type& parm) Chris@102: { Chris@102: _alpha = parm.alpha(); Chris@102: _beta = parm.beta(); Chris@102: } Chris@102: Chris@102: /** Chris@102: * Effects: Subsequent uses of the distribution do not depend Chris@102: * on values produced by any engine prior to invoking reset. Chris@102: */ Chris@102: void reset() { } Chris@102: Chris@102: /** Writes an @c beta_distribution to a @c std::ostream. */ Chris@102: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, beta_distribution, wd) Chris@102: { Chris@102: os << wd.param(); Chris@102: return os; Chris@102: } Chris@102: Chris@102: /** Reads an @c beta_distribution from a @c std::istream. */ Chris@102: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, beta_distribution, wd) Chris@102: { Chris@102: param_type parm; Chris@102: if(is >> parm) { Chris@102: wd.param(parm); Chris@102: } Chris@102: return is; Chris@102: } Chris@102: Chris@102: /** Chris@102: * Returns true if the two instances of @c beta_distribution will Chris@102: * return identical sequences of values given equal generators. Chris@102: */ Chris@102: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(beta_distribution, lhs, rhs) Chris@102: { return lhs._alpha == rhs._alpha && lhs._beta == rhs._beta; } Chris@102: Chris@102: /** Chris@102: * Returns true if the two instances of @c beta_distribution will Chris@102: * return different sequences of values given equal generators. Chris@102: */ Chris@102: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(beta_distribution) Chris@102: Chris@102: private: Chris@102: RealType _alpha; Chris@102: RealType _beta; Chris@102: }; Chris@102: Chris@102: } // namespace random Chris@102: } // namespace boost Chris@102: Chris@102: #endif // BOOST_RANDOM_BETA_DISTRIBUTION_HPP