Chris@102
|
1 /* boost random/beta_distribution.hpp header file
|
Chris@102
|
2 *
|
Chris@102
|
3 * Copyright Steven Watanabe 2014
|
Chris@102
|
4 * Distributed under the Boost Software License, Version 1.0. (See
|
Chris@102
|
5 * accompanying file LICENSE_1_0.txt or copy at
|
Chris@102
|
6 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@102
|
7 *
|
Chris@102
|
8 * See http://www.boost.org for most recent version including documentation.
|
Chris@102
|
9 *
|
Chris@102
|
10 * $Id$
|
Chris@102
|
11 */
|
Chris@102
|
12
|
Chris@102
|
13 #ifndef BOOST_RANDOM_BETA_DISTRIBUTION_HPP
|
Chris@102
|
14 #define BOOST_RANDOM_BETA_DISTRIBUTION_HPP
|
Chris@102
|
15
|
Chris@102
|
16 #include <cassert>
|
Chris@102
|
17 #include <istream>
|
Chris@102
|
18 #include <iosfwd>
|
Chris@102
|
19 #include <boost/random/detail/operators.hpp>
|
Chris@102
|
20 #include <boost/random/gamma_distribution.hpp>
|
Chris@102
|
21
|
Chris@102
|
22 namespace boost {
|
Chris@102
|
23 namespace random {
|
Chris@102
|
24
|
Chris@102
|
25 /**
|
Chris@102
|
26 * The beta distribution is a real-valued distribution which produces
|
Chris@102
|
27 * values in the range [0, 1]. It has two parameters, alpha and beta.
|
Chris@102
|
28 *
|
Chris@102
|
29 * It has \f$\displaystyle p(x) = \frac{x^{\alpha-1}(1-x)^{\beta-1}}{B(\alpha, \beta)}\f$.
|
Chris@102
|
30 */
|
Chris@102
|
31 template<class RealType = double>
|
Chris@102
|
32 class beta_distribution {
|
Chris@102
|
33 public:
|
Chris@102
|
34 typedef RealType result_type;
|
Chris@102
|
35 typedef RealType input_type;
|
Chris@102
|
36
|
Chris@102
|
37 class param_type {
|
Chris@102
|
38 public:
|
Chris@102
|
39 typedef beta_distribution distribution_type;
|
Chris@102
|
40
|
Chris@102
|
41 /**
|
Chris@102
|
42 * Constructs a @c param_type from the "alpha" and "beta" parameters
|
Chris@102
|
43 * of the distribution.
|
Chris@102
|
44 *
|
Chris@102
|
45 * Requires: alpha > 0, beta > 0
|
Chris@102
|
46 */
|
Chris@102
|
47 explicit param_type(RealType alpha_arg = RealType(1.0),
|
Chris@102
|
48 RealType beta_arg = RealType(1.0))
|
Chris@102
|
49 : _alpha(alpha_arg), _beta(beta_arg)
|
Chris@102
|
50 {
|
Chris@102
|
51 assert(alpha_arg > 0);
|
Chris@102
|
52 assert(beta_arg > 0);
|
Chris@102
|
53 }
|
Chris@102
|
54
|
Chris@102
|
55 /** Returns the "alpha" parameter of the distribtuion. */
|
Chris@102
|
56 RealType alpha() const { return _alpha; }
|
Chris@102
|
57 /** Returns the "beta" parameter of the distribution. */
|
Chris@102
|
58 RealType beta() const { return _beta; }
|
Chris@102
|
59
|
Chris@102
|
60 /** Writes a @c param_type to a @c std::ostream. */
|
Chris@102
|
61 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
|
Chris@102
|
62 { os << parm._alpha << ' ' << parm._beta; return os; }
|
Chris@102
|
63
|
Chris@102
|
64 /** Reads a @c param_type from a @c std::istream. */
|
Chris@102
|
65 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
|
Chris@102
|
66 { is >> parm._alpha >> std::ws >> parm._beta; return is; }
|
Chris@102
|
67
|
Chris@102
|
68 /** Returns true if the two sets of parameters are the same. */
|
Chris@102
|
69 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
|
Chris@102
|
70 { return lhs._alpha == rhs._alpha && lhs._beta == rhs._beta; }
|
Chris@102
|
71
|
Chris@102
|
72 /** Returns true if the two sets of parameters are the different. */
|
Chris@102
|
73 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
|
Chris@102
|
74
|
Chris@102
|
75 private:
|
Chris@102
|
76 RealType _alpha;
|
Chris@102
|
77 RealType _beta;
|
Chris@102
|
78 };
|
Chris@102
|
79
|
Chris@102
|
80 /**
|
Chris@102
|
81 * Constructs an @c beta_distribution from its "alpha" and "beta" parameters.
|
Chris@102
|
82 *
|
Chris@102
|
83 * Requires: alpha > 0, beta > 0
|
Chris@102
|
84 */
|
Chris@102
|
85 explicit beta_distribution(RealType alpha_arg = RealType(1.0),
|
Chris@102
|
86 RealType beta_arg = RealType(1.0))
|
Chris@102
|
87 : _alpha(alpha_arg), _beta(beta_arg)
|
Chris@102
|
88 {
|
Chris@102
|
89 assert(alpha_arg > 0);
|
Chris@102
|
90 assert(beta_arg > 0);
|
Chris@102
|
91 }
|
Chris@102
|
92 /** Constructs an @c beta_distribution from its parameters. */
|
Chris@102
|
93 explicit beta_distribution(const param_type& parm)
|
Chris@102
|
94 : _alpha(parm.alpha()), _beta(parm.beta())
|
Chris@102
|
95 {}
|
Chris@102
|
96
|
Chris@102
|
97 /**
|
Chris@102
|
98 * Returns a random variate distributed according to the
|
Chris@102
|
99 * beta distribution.
|
Chris@102
|
100 */
|
Chris@102
|
101 template<class URNG>
|
Chris@102
|
102 RealType operator()(URNG& urng) const
|
Chris@102
|
103 {
|
Chris@102
|
104 RealType a = gamma_distribution<RealType>(_alpha, RealType(1.0))(urng);
|
Chris@102
|
105 RealType b = gamma_distribution<RealType>(_beta, RealType(1.0))(urng);
|
Chris@102
|
106 return a / (a + b);
|
Chris@102
|
107 }
|
Chris@102
|
108
|
Chris@102
|
109 /**
|
Chris@102
|
110 * Returns a random variate distributed accordint to the beta
|
Chris@102
|
111 * distribution with parameters specified by @c param.
|
Chris@102
|
112 */
|
Chris@102
|
113 template<class URNG>
|
Chris@102
|
114 RealType operator()(URNG& urng, const param_type& parm) const
|
Chris@102
|
115 {
|
Chris@102
|
116 return beta_distribution(parm)(urng);
|
Chris@102
|
117 }
|
Chris@102
|
118
|
Chris@102
|
119 /** Returns the "alpha" parameter of the distribution. */
|
Chris@102
|
120 RealType alpha() const { return _alpha; }
|
Chris@102
|
121 /** Returns the "beta" parameter of the distribution. */
|
Chris@102
|
122 RealType beta() const { return _beta; }
|
Chris@102
|
123
|
Chris@102
|
124 /** Returns the smallest value that the distribution can produce. */
|
Chris@102
|
125 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
|
Chris@102
|
126 { return RealType(0.0); }
|
Chris@102
|
127 /** Returns the largest value that the distribution can produce. */
|
Chris@102
|
128 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
|
Chris@102
|
129 { return RealType(1.0); }
|
Chris@102
|
130
|
Chris@102
|
131 /** Returns the parameters of the distribution. */
|
Chris@102
|
132 param_type param() const { return param_type(_alpha, _beta); }
|
Chris@102
|
133 /** Sets the parameters of the distribution. */
|
Chris@102
|
134 void param(const param_type& parm)
|
Chris@102
|
135 {
|
Chris@102
|
136 _alpha = parm.alpha();
|
Chris@102
|
137 _beta = parm.beta();
|
Chris@102
|
138 }
|
Chris@102
|
139
|
Chris@102
|
140 /**
|
Chris@102
|
141 * Effects: Subsequent uses of the distribution do not depend
|
Chris@102
|
142 * on values produced by any engine prior to invoking reset.
|
Chris@102
|
143 */
|
Chris@102
|
144 void reset() { }
|
Chris@102
|
145
|
Chris@102
|
146 /** Writes an @c beta_distribution to a @c std::ostream. */
|
Chris@102
|
147 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, beta_distribution, wd)
|
Chris@102
|
148 {
|
Chris@102
|
149 os << wd.param();
|
Chris@102
|
150 return os;
|
Chris@102
|
151 }
|
Chris@102
|
152
|
Chris@102
|
153 /** Reads an @c beta_distribution from a @c std::istream. */
|
Chris@102
|
154 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, beta_distribution, wd)
|
Chris@102
|
155 {
|
Chris@102
|
156 param_type parm;
|
Chris@102
|
157 if(is >> parm) {
|
Chris@102
|
158 wd.param(parm);
|
Chris@102
|
159 }
|
Chris@102
|
160 return is;
|
Chris@102
|
161 }
|
Chris@102
|
162
|
Chris@102
|
163 /**
|
Chris@102
|
164 * Returns true if the two instances of @c beta_distribution will
|
Chris@102
|
165 * return identical sequences of values given equal generators.
|
Chris@102
|
166 */
|
Chris@102
|
167 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(beta_distribution, lhs, rhs)
|
Chris@102
|
168 { return lhs._alpha == rhs._alpha && lhs._beta == rhs._beta; }
|
Chris@102
|
169
|
Chris@102
|
170 /**
|
Chris@102
|
171 * Returns true if the two instances of @c beta_distribution will
|
Chris@102
|
172 * return different sequences of values given equal generators.
|
Chris@102
|
173 */
|
Chris@102
|
174 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(beta_distribution)
|
Chris@102
|
175
|
Chris@102
|
176 private:
|
Chris@102
|
177 RealType _alpha;
|
Chris@102
|
178 RealType _beta;
|
Chris@102
|
179 };
|
Chris@102
|
180
|
Chris@102
|
181 } // namespace random
|
Chris@102
|
182 } // namespace boost
|
Chris@102
|
183
|
Chris@102
|
184 #endif // BOOST_RANDOM_BETA_DISTRIBUTION_HPP
|