Warning, file /include/boost/random/inverse_gaussian_distribution.hpp was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef BOOST_RANDOM_INVERSE_GAUSSIAN_DISTRIBUTION_HPP
0014 #define BOOST_RANDOM_INVERSE_GAUSSIAN_DISTRIBUTION_HPP
0015
0016 #include <boost/config/no_tr1/cmath.hpp>
0017 #include <istream>
0018 #include <iosfwd>
0019 #include <limits>
0020 #include <boost/assert.hpp>
0021 #include <boost/limits.hpp>
0022 #include <boost/random/detail/config.hpp>
0023 #include <boost/random/detail/operators.hpp>
0024 #include <boost/random/uniform_01.hpp>
0025 #include <boost/random/chi_squared_distribution.hpp>
0026
0027 namespace boost {
0028 namespace random {
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 template<class RealType = double>
0047 class inverse_gaussian_distribution
0048 {
0049 public:
0050 typedef RealType result_type;
0051 typedef RealType input_type;
0052
0053 class param_type {
0054 public:
0055 typedef inverse_gaussian_distribution distribution_type;
0056
0057
0058
0059
0060
0061
0062
0063 explicit param_type(RealType alpha_arg = RealType(1.0),
0064 RealType beta_arg = RealType(1.0))
0065 : _alpha(alpha_arg), _beta(beta_arg)
0066 {
0067 BOOST_ASSERT(alpha_arg > 0);
0068 BOOST_ASSERT(beta_arg > 0);
0069 }
0070
0071
0072 RealType alpha() const { return _alpha; }
0073
0074 RealType beta() const { return _beta; }
0075
0076
0077 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
0078 { os << parm._alpha << ' ' << parm._beta; return os; }
0079
0080
0081 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
0082 { is >> parm._alpha >> std::ws >> parm._beta; return is; }
0083
0084
0085 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
0086 { return lhs._alpha == rhs._alpha && lhs._beta == rhs._beta; }
0087
0088
0089 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
0090
0091 private:
0092 RealType _alpha;
0093 RealType _beta;
0094 };
0095
0096 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
0097 BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
0098 #endif
0099
0100
0101
0102
0103
0104
0105 explicit inverse_gaussian_distribution(RealType alpha_arg = RealType(1.0),
0106 RealType beta_arg = RealType(1.0))
0107 : _alpha(alpha_arg), _beta(beta_arg)
0108 {
0109 BOOST_ASSERT(alpha_arg > 0);
0110 BOOST_ASSERT(beta_arg > 0);
0111 init();
0112 }
0113
0114
0115 explicit inverse_gaussian_distribution(const param_type& parm)
0116 : _alpha(parm.alpha()), _beta(parm.beta())
0117 {
0118 init();
0119 }
0120
0121
0122
0123
0124
0125 template<class URNG>
0126 RealType operator()(URNG& urng) const
0127 {
0128 #ifndef BOOST_NO_STDC_NAMESPACE
0129 using std::sqrt;
0130 #endif
0131 RealType w = _alpha * chi_squared_distribution<RealType>(result_type(1))(urng);
0132 RealType cand = _alpha + _c * (w - sqrt(w * (result_type(4) * _beta + w)));
0133 RealType u = uniform_01<RealType>()(urng);
0134 if (u < _alpha / (_alpha + cand)) {
0135 return cand;
0136 }
0137 return _alpha * _alpha / cand;
0138 }
0139
0140
0141
0142
0143
0144 template<class URNG>
0145 RealType operator()(URNG& urng, const param_type& parm) const
0146 {
0147 return inverse_gaussian_distribution(parm)(urng);
0148 }
0149
0150
0151 RealType alpha() const { return _alpha; }
0152
0153 RealType beta() const { return _beta; }
0154
0155
0156 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
0157 { return RealType(0.0); }
0158
0159 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
0160 { return (std::numeric_limits<RealType>::infinity)(); }
0161
0162
0163 param_type param() const { return param_type(_alpha, _beta); }
0164
0165 void param(const param_type& parm)
0166 {
0167 _alpha = parm.alpha();
0168 _beta = parm.beta();
0169 init();
0170 }
0171
0172
0173
0174
0175
0176 void reset() { }
0177
0178
0179 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, inverse_gaussian_distribution, wd)
0180 {
0181 os << wd.param();
0182 return os;
0183 }
0184
0185
0186 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, inverse_gaussian_distribution, wd)
0187 {
0188 param_type parm;
0189 if(is >> parm) {
0190 wd.param(parm);
0191 }
0192 return is;
0193 }
0194
0195
0196
0197
0198
0199 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(inverse_gaussian_distribution, lhs, rhs)
0200 { return lhs._alpha == rhs._alpha && lhs._beta == rhs._beta; }
0201
0202
0203
0204
0205
0206 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(inverse_gaussian_distribution)
0207
0208 private:
0209 result_type _alpha;
0210 result_type _beta;
0211
0212 result_type _c;
0213
0214 void init()
0215 {
0216 _c = _alpha / (result_type(2) * _beta);
0217 }
0218 };
0219
0220 }
0221
0222 using random::inverse_gaussian_distribution;
0223
0224 }
0225
0226 #endif