1 /* boost random/weibull_distribution.hpp header file
3 * Copyright Steven Watanabe 2010
4 * Distributed under the Boost Software License, Version 1.0. (See
5 * accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
8 * See http://www.boost.org for most recent version including documentation.
10 * $Id: weibull_distribution.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
13 #ifndef BOOST_RANDOM_WEIBULL_DISTRIBUTION_HPP
14 #define BOOST_RANDOM_WEIBULL_DISTRIBUTION_HPP
16 #include <boost/config/no_tr1/cmath.hpp>
19 #include <boost/config.hpp>
20 #include <boost/limits.hpp>
21 #include <boost/random/detail/operators.hpp>
22 #include <boost/random/uniform_01.hpp>
28 * The Weibull distribution is a real valued distribution with two
29 * parameters a and b, producing values >= 0.
31 * It has \f$\displaystyle p(x) = \frac{a}{b}\left(\frac{x}{b}\right)^{a-1}e^{-\left(\frac{x}{b}\right)^a}\f$.
33 template<class RealType = double>
34 class weibull_distribution {
36 typedef RealType result_type;
37 typedef RealType input_type;
41 typedef weibull_distribution distribution_type;
44 * Constructs a @c param_type from the "a" and "b" parameters
45 * of the distribution.
47 * Requires: a > 0 && b > 0
49 explicit param_type(RealType a_arg = 1.0, RealType b_arg = 1.0)
50 : _a(a_arg), _b(b_arg)
53 /** Returns the "a" parameter of the distribtuion. */
54 RealType a() const { return _a; }
55 /** Returns the "b" parameter of the distribution. */
56 RealType b() const { return _b; }
58 /** Writes a @c param_type to a @c std::ostream. */
59 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
60 { os << parm._a << ' ' << parm._b; return os; }
62 /** Reads a @c param_type from a @c std::istream. */
63 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
64 { is >> parm._a >> std::ws >> parm._b; return is; }
66 /** Returns true if the two sets of parameters are the same. */
67 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
68 { return lhs._a == rhs._a && lhs._b == rhs._b; }
70 /** Returns true if the two sets of parameters are the different. */
71 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
79 * Constructs a @c weibull_distribution from its "a" and "b" parameters.
81 * Requires: a > 0 && b > 0
83 explicit weibull_distribution(RealType a_arg = 1.0, RealType b_arg = 1.0)
84 : _a(a_arg), _b(b_arg)
86 /** Constructs a @c weibull_distribution from its parameters. */
87 explicit weibull_distribution(const param_type& parm)
88 : _a(parm.a()), _b(parm.b())
92 * Returns a random variate distributed according to the
93 * @c weibull_distribution.
96 RealType operator()(URNG& urng) const
100 return _b*pow(-log(1 - uniform_01<RealType>()(urng)), 1/_a);
104 * Returns a random variate distributed accordint to the Weibull
105 * distribution with parameters specified by @c param.
108 RealType operator()(URNG& urng, const param_type& parm) const
110 return weibull_distribution(parm)(urng);
113 /** Returns the "a" parameter of the distribution. */
114 RealType a() const { return _a; }
115 /** Returns the "b" parameter of the distribution. */
116 RealType b() const { return _b; }
118 /** Returns the smallest value that the distribution can produce. */
119 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
120 /** Returns the largest value that the distribution can produce. */
121 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
122 { return std::numeric_limits<RealType>::infinity(); }
124 /** Returns the parameters of the distribution. */
125 param_type param() const { return param_type(_a, _b); }
126 /** Sets the parameters of the distribution. */
127 void param(const param_type& parm)
134 * Effects: Subsequent uses of the distribution do not depend
135 * on values produced by any engine prior to invoking reset.
139 /** Writes a @c weibull_distribution to a @c std::ostream. */
140 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, weibull_distribution, wd)
146 /** Reads a @c weibull_distribution from a @c std::istream. */
147 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, weibull_distribution, wd)
157 * Returns true if the two instances of @c weibull_distribution will
158 * return identical sequences of values given equal generators.
160 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(weibull_distribution, lhs, rhs)
161 { return lhs._a == rhs._a && lhs._b == rhs._b; }
164 * Returns true if the two instances of @c weibull_distribution will
165 * return different sequences of values given equal generators.
167 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(weibull_distribution)
174 } // namespace random
177 #endif // BOOST_RANDOM_WEIBULL_DISTRIBUTION_HPP