File indexing completed on 2025-01-18 09:51:10
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef BOOST_RANDOM_BETA_DISTRIBUTION_HPP
0014 #define BOOST_RANDOM_BETA_DISTRIBUTION_HPP
0015
0016 #include <cassert>
0017 #include <istream>
0018 #include <iosfwd>
0019 #include <boost/random/detail/operators.hpp>
0020 #include <boost/random/gamma_distribution.hpp>
0021
0022 namespace boost {
0023 namespace random {
0024
0025
0026
0027
0028
0029
0030
0031 template<class RealType = double>
0032 class beta_distribution {
0033 public:
0034 typedef RealType result_type;
0035 typedef RealType input_type;
0036
0037 class param_type {
0038 public:
0039 typedef beta_distribution distribution_type;
0040
0041
0042
0043
0044
0045
0046
0047 explicit param_type(RealType alpha_arg = RealType(1.0),
0048 RealType beta_arg = RealType(1.0))
0049 : _alpha(alpha_arg), _beta(beta_arg)
0050 {
0051 assert(alpha_arg > 0);
0052 assert(beta_arg > 0);
0053 }
0054
0055
0056 RealType alpha() const { return _alpha; }
0057
0058 RealType beta() const { return _beta; }
0059
0060
0061 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
0062 { os << parm._alpha << ' ' << parm._beta; return os; }
0063
0064
0065 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
0066 { is >> parm._alpha >> std::ws >> parm._beta; return is; }
0067
0068
0069 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
0070 { return lhs._alpha == rhs._alpha && lhs._beta == rhs._beta; }
0071
0072
0073 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
0074
0075 private:
0076 RealType _alpha;
0077 RealType _beta;
0078 };
0079
0080
0081
0082
0083
0084
0085 explicit beta_distribution(RealType alpha_arg = RealType(1.0),
0086 RealType beta_arg = RealType(1.0))
0087 : _alpha(alpha_arg), _beta(beta_arg)
0088 {
0089 assert(alpha_arg > 0);
0090 assert(beta_arg > 0);
0091 }
0092
0093 explicit beta_distribution(const param_type& parm)
0094 : _alpha(parm.alpha()), _beta(parm.beta())
0095 {}
0096
0097
0098
0099
0100
0101 template<class URNG>
0102 RealType operator()(URNG& urng) const
0103 {
0104 RealType a = gamma_distribution<RealType>(_alpha, RealType(1.0))(urng);
0105 RealType b = gamma_distribution<RealType>(_beta, RealType(1.0))(urng);
0106 return a / (a + b);
0107 }
0108
0109
0110
0111
0112
0113 template<class URNG>
0114 RealType operator()(URNG& urng, const param_type& parm) const
0115 {
0116 return beta_distribution(parm)(urng);
0117 }
0118
0119
0120 RealType alpha() const { return _alpha; }
0121
0122 RealType beta() const { return _beta; }
0123
0124
0125 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
0126 { return RealType(0.0); }
0127
0128 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
0129 { return RealType(1.0); }
0130
0131
0132 param_type param() const { return param_type(_alpha, _beta); }
0133
0134 void param(const param_type& parm)
0135 {
0136 _alpha = parm.alpha();
0137 _beta = parm.beta();
0138 }
0139
0140
0141
0142
0143
0144 void reset() { }
0145
0146
0147 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, beta_distribution, wd)
0148 {
0149 os << wd.param();
0150 return os;
0151 }
0152
0153
0154 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, beta_distribution, wd)
0155 {
0156 param_type parm;
0157 if(is >> parm) {
0158 wd.param(parm);
0159 }
0160 return is;
0161 }
0162
0163
0164
0165
0166
0167 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(beta_distribution, lhs, rhs)
0168 { return lhs._alpha == rhs._alpha && lhs._beta == rhs._beta; }
0169
0170
0171
0172
0173
0174 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(beta_distribution)
0175
0176 private:
0177 RealType _alpha;
0178 RealType _beta;
0179 };
0180
0181 }
0182 }
0183
0184 #endif