1 /* boost random/beta_distribution.hpp header file
3 * Copyright Steven Watanabe 2014
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)
8 * See http://www.boost.org for most recent version including documentation.
13 #ifndef BOOST_RANDOM_BETA_DISTRIBUTION_HPP
14 #define BOOST_RANDOM_BETA_DISTRIBUTION_HPP
19 #include <boost/random/detail/operators.hpp>
20 #include <boost/random/gamma_distribution.hpp>
26 * The beta distribution is a real-valued distribution which produces
27 * values in the range [0, 1]. It has two parameters, alpha and beta.
29 * It has \f$\displaystyle p(x) = \frac{x^{\alpha-1}(1-x)^{\beta-1}}{B(\alpha, \beta)}\f$.
31 template<class RealType = double>
32 class beta_distribution {
34 typedef RealType result_type;
35 typedef RealType input_type;
39 typedef beta_distribution distribution_type;
42 * Constructs a @c param_type from the "alpha" and "beta" parameters
43 * of the distribution.
45 * Requires: alpha > 0, beta > 0
47 explicit param_type(RealType alpha_arg = RealType(1.0),
48 RealType beta_arg = RealType(1.0))
49 : _alpha(alpha_arg), _beta(beta_arg)
51 assert(alpha_arg > 0);
55 /** Returns the "alpha" parameter of the distribtuion. */
56 RealType alpha() const { return _alpha; }
57 /** Returns the "beta" parameter of the distribution. */
58 RealType beta() const { return _beta; }
60 /** Writes a @c param_type to a @c std::ostream. */
61 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
62 { os << parm._alpha << ' ' << parm._beta; return os; }
64 /** Reads a @c param_type from a @c std::istream. */
65 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
66 { is >> parm._alpha >> std::ws >> parm._beta; return is; }
68 /** Returns true if the two sets of parameters are the same. */
69 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
70 { return lhs._alpha == rhs._alpha && lhs._beta == rhs._beta; }
72 /** Returns true if the two sets of parameters are the different. */
73 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
81 * Constructs an @c beta_distribution from its "alpha" and "beta" parameters.
83 * Requires: alpha > 0, beta > 0
85 explicit beta_distribution(RealType alpha_arg = RealType(1.0),
86 RealType beta_arg = RealType(1.0))
87 : _alpha(alpha_arg), _beta(beta_arg)
89 assert(alpha_arg > 0);
92 /** Constructs an @c beta_distribution from its parameters. */
93 explicit beta_distribution(const param_type& parm)
94 : _alpha(parm.alpha()), _beta(parm.beta())
98 * Returns a random variate distributed according to the
102 RealType operator()(URNG& urng) const
104 RealType a = gamma_distribution<RealType>(_alpha, RealType(1.0))(urng);
105 RealType b = gamma_distribution<RealType>(_beta, RealType(1.0))(urng);
110 * Returns a random variate distributed accordint to the beta
111 * distribution with parameters specified by @c param.
114 RealType operator()(URNG& urng, const param_type& parm) const
116 return beta_distribution(parm)(urng);
119 /** Returns the "alpha" parameter of the distribution. */
120 RealType alpha() const { return _alpha; }
121 /** Returns the "beta" parameter of the distribution. */
122 RealType beta() const { return _beta; }
124 /** Returns the smallest value that the distribution can produce. */
125 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
126 { return RealType(0.0); }
127 /** Returns the largest value that the distribution can produce. */
128 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
129 { return RealType(1.0); }
131 /** Returns the parameters of the distribution. */
132 param_type param() const { return param_type(_alpha, _beta); }
133 /** Sets the parameters of the distribution. */
134 void param(const param_type& parm)
136 _alpha = parm.alpha();
141 * Effects: Subsequent uses of the distribution do not depend
142 * on values produced by any engine prior to invoking reset.
146 /** Writes an @c beta_distribution to a @c std::ostream. */
147 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, beta_distribution, wd)
153 /** Reads an @c beta_distribution from a @c std::istream. */
154 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, beta_distribution, wd)
164 * Returns true if the two instances of @c beta_distribution will
165 * return identical sequences of values given equal generators.
167 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(beta_distribution, lhs, rhs)
168 { return lhs._alpha == rhs._alpha && lhs._beta == rhs._beta; }
171 * Returns true if the two instances of @c beta_distribution will
172 * return different sequences of values given equal generators.
174 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(beta_distribution)
181 } // namespace random
184 #endif // BOOST_RANDOM_BETA_DISTRIBUTION_HPP