Chris@16: /* boost random/weibull_distribution.hpp header file Chris@16: * Chris@16: * Copyright Steven Watanabe 2010 Chris@16: * Distributed under the Boost Software License, Version 1.0. (See Chris@16: * accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@16: * See http://www.boost.org for most recent version including documentation. Chris@16: * Chris@101: * $Id$ Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_RANDOM_WEIBULL_DISTRIBUTION_HPP Chris@16: #define BOOST_RANDOM_WEIBULL_DISTRIBUTION_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace random { Chris@16: Chris@16: /** Chris@16: * The Weibull distribution is a real valued distribution with two Chris@16: * parameters a and b, producing values >= 0. Chris@16: * Chris@16: * It has \f$\displaystyle p(x) = \frac{a}{b}\left(\frac{x}{b}\right)^{a-1}e^{-\left(\frac{x}{b}\right)^a}\f$. Chris@16: */ Chris@16: template Chris@16: class weibull_distribution { Chris@16: public: Chris@16: typedef RealType result_type; Chris@16: typedef RealType input_type; Chris@16: Chris@16: class param_type { Chris@16: public: Chris@16: typedef weibull_distribution distribution_type; Chris@16: Chris@16: /** Chris@16: * Constructs a @c param_type from the "a" and "b" parameters Chris@16: * of the distribution. Chris@16: * Chris@16: * Requires: a > 0 && b > 0 Chris@16: */ Chris@16: explicit param_type(RealType a_arg = 1.0, RealType b_arg = 1.0) Chris@16: : _a(a_arg), _b(b_arg) Chris@16: {} Chris@16: Chris@16: /** Returns the "a" parameter of the distribtuion. */ Chris@16: RealType a() const { return _a; } Chris@16: /** Returns the "b" parameter of the distribution. */ Chris@16: RealType b() const { return _b; } Chris@16: Chris@16: /** Writes a @c param_type to a @c std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm) Chris@16: { os << parm._a << ' ' << parm._b; return os; } Chris@16: Chris@16: /** Reads a @c param_type from a @c std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm) Chris@16: { is >> parm._a >> std::ws >> parm._b; return is; } Chris@16: Chris@16: /** Returns true if the two sets of parameters are the same. */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs) Chris@16: { return lhs._a == rhs._a && lhs._b == rhs._b; } Chris@16: Chris@16: /** Returns true if the two sets of parameters are the different. */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type) Chris@16: Chris@16: private: Chris@16: RealType _a; Chris@16: RealType _b; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * Constructs a @c weibull_distribution from its "a" and "b" parameters. Chris@16: * Chris@16: * Requires: a > 0 && b > 0 Chris@16: */ Chris@16: explicit weibull_distribution(RealType a_arg = 1.0, RealType b_arg = 1.0) Chris@16: : _a(a_arg), _b(b_arg) Chris@16: {} Chris@16: /** Constructs a @c weibull_distribution from its parameters. */ Chris@16: explicit weibull_distribution(const param_type& parm) Chris@16: : _a(parm.a()), _b(parm.b()) Chris@16: {} Chris@16: Chris@16: /** Chris@16: * Returns a random variate distributed according to the Chris@16: * @c weibull_distribution. Chris@16: */ Chris@16: template Chris@16: RealType operator()(URNG& urng) const Chris@16: { Chris@16: using std::pow; Chris@16: using std::log; Chris@16: return _b*pow(-log(1 - uniform_01()(urng)), 1/_a); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns a random variate distributed accordint to the Weibull Chris@16: * distribution with parameters specified by @c param. Chris@16: */ Chris@16: template Chris@16: RealType operator()(URNG& urng, const param_type& parm) const Chris@16: { Chris@16: return weibull_distribution(parm)(urng); Chris@16: } Chris@16: Chris@16: /** Returns the "a" parameter of the distribution. */ Chris@16: RealType a() const { return _a; } Chris@16: /** Returns the "b" parameter of the distribution. */ Chris@16: RealType b() const { return _b; } Chris@16: Chris@16: /** Returns the smallest value that the distribution can produce. */ Chris@16: RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; } Chris@16: /** Returns the largest value that the distribution can produce. */ Chris@16: RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const Chris@16: { return std::numeric_limits::infinity(); } Chris@16: Chris@16: /** Returns the parameters of the distribution. */ Chris@16: param_type param() const { return param_type(_a, _b); } Chris@16: /** Sets the parameters of the distribution. */ Chris@16: void param(const param_type& parm) Chris@16: { Chris@16: _a = parm.a(); Chris@16: _b = parm.b(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Effects: Subsequent uses of the distribution do not depend Chris@16: * on values produced by any engine prior to invoking reset. Chris@16: */ Chris@16: void reset() { } Chris@16: Chris@16: /** Writes a @c weibull_distribution to a @c std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, weibull_distribution, wd) Chris@16: { Chris@16: os << wd.param(); Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads a @c weibull_distribution from a @c std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, weibull_distribution, wd) Chris@16: { Chris@16: param_type parm; Chris@16: if(is >> parm) { Chris@16: wd.param(parm); Chris@16: } Chris@16: return is; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns true if the two instances of @c weibull_distribution will Chris@16: * return identical sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(weibull_distribution, lhs, rhs) Chris@16: { return lhs._a == rhs._a && lhs._b == rhs._b; } Chris@16: Chris@16: /** Chris@16: * Returns true if the two instances of @c weibull_distribution will Chris@16: * return different sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(weibull_distribution) Chris@16: Chris@16: private: Chris@16: RealType _a; Chris@16: RealType _b; Chris@16: }; Chris@16: Chris@16: } // namespace random Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_RANDOM_WEIBULL_DISTRIBUTION_HPP