123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- #ifndef BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP
- #define BOOST_RANDOM_LOGNORMAL_DISTRIBUTION_HPP
- #include <boost/config/no_tr1/cmath.hpp>
- #include <cassert>
- #include <iosfwd>
- #include <istream>
- #include <boost/limits.hpp>
- #include <boost/random/detail/config.hpp>
- #include <boost/random/detail/operators.hpp>
- #include <boost/random/normal_distribution.hpp>
- namespace boost {
- namespace random {
- template<class RealType = double>
- class lognormal_distribution
- {
- public:
- typedef typename normal_distribution<RealType>::input_type input_type;
- typedef RealType result_type;
- class param_type
- {
- public:
- typedef lognormal_distribution distribution_type;
-
- explicit param_type(RealType m_arg = RealType(0.0),
- RealType s_arg = RealType(1.0))
- : _m(m_arg), _s(s_arg) {}
-
- RealType m() const { return _m; }
-
- RealType s() const { return _s; }
-
- BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
- {
- os << parm._m << " " << parm._s;
- return os;
- }
-
- BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
- {
- is >> parm._m >> std::ws >> parm._s;
- return is;
- }
-
- BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
- { return lhs._m == rhs._m && lhs._s == rhs._s; }
-
- BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
- private:
- RealType _m;
- RealType _s;
- };
-
- explicit lognormal_distribution(RealType m_arg = RealType(0.0),
- RealType s_arg = RealType(1.0))
- : _normal(m_arg, s_arg) {}
-
- explicit lognormal_distribution(const param_type& parm)
- : _normal(parm.m(), parm.s()) {}
-
-
- RealType m() const { return _normal.mean(); }
-
- RealType s() const { return _normal.sigma(); }
-
- RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
- { return RealType(0); }
-
- RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
- { return (std::numeric_limits<RealType>::infinity)(); }
-
- param_type param() const { return param_type(m(), s()); }
-
- void param(const param_type& parm)
- {
- typedef normal_distribution<RealType> normal_type;
- typename normal_type::param_type normal_param(parm.m(), parm.s());
- _normal.param(normal_param);
- }
-
-
- void reset() { _normal.reset(); }
-
- template<class Engine>
- result_type operator()(Engine& eng)
- {
- using std::exp;
- return exp(_normal(eng));
- }
-
- template<class Engine>
- result_type operator()(Engine& eng, const param_type& parm)
- { return lognormal_distribution(parm)(eng); }
-
- BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, lognormal_distribution, ld)
- {
- os << ld._normal;
- return os;
- }
-
- BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, lognormal_distribution, ld)
- {
- is >> ld._normal;
- return is;
- }
-
- BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(lognormal_distribution, lhs, rhs)
- { return lhs._normal == rhs._normal; }
-
- BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(lognormal_distribution)
- private:
- normal_distribution<result_type> _normal;
- };
- }
- template<class RealType = double>
- class lognormal_distribution
- {
- public:
- typedef typename normal_distribution<RealType>::input_type input_type;
- typedef RealType result_type;
- lognormal_distribution(RealType mean_arg = RealType(1.0),
- RealType sigma_arg = RealType(1.0))
- : _mean(mean_arg), _sigma(sigma_arg)
- {
- init();
- }
- RealType mean() const { return _mean; }
- RealType sigma() const { return _sigma; }
- void reset() { _normal.reset(); }
- template<class Engine>
- RealType operator()(Engine& eng)
- {
- using std::exp;
- return exp(_normal(eng) * _nsigma + _nmean);
- }
- BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, lognormal_distribution, ld)
- {
- os << ld._normal << " " << ld._mean << " " << ld._sigma;
- return os;
- }
- BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, lognormal_distribution, ld)
- {
- is >> ld._normal >> std::ws >> ld._mean >> std::ws >> ld._sigma;
- ld.init();
- return is;
- }
- private:
-
- void init()
- {
- using std::log;
- using std::sqrt;
- _nmean = log(_mean*_mean/sqrt(_sigma*_sigma + _mean*_mean));
- _nsigma = sqrt(log(_sigma*_sigma/_mean/_mean+result_type(1)));
- }
- RealType _mean;
- RealType _sigma;
- RealType _nmean;
- RealType _nsigma;
- normal_distribution<RealType> _normal;
-
- };
- }
- #endif
|