1/* boost random/weibull_distribution.hpp header file
2 *
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)
7 *
8 * See http://www.boost.org for most recent version including documentation.
9 *
10 * $Id$
11 */
12
13#ifndef BOOST_RANDOM_WEIBULL_DISTRIBUTION_HPP
14#define BOOST_RANDOM_WEIBULL_DISTRIBUTION_HPP
15
16#include <boost/config/no_tr1/cmath.hpp>
17#include <iosfwd>
18#include <istream>
19#include <boost/config.hpp>
20#include <boost/limits.hpp>
21#include <boost/random/detail/operators.hpp>
22#include <boost/random/uniform_01.hpp>
23
24namespace boost {
25namespace random {
26
27/**
28 * The Weibull distribution is a real valued distribution with two
29 * parameters a and b, producing values >= 0.
30 *
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$.
32 */
33template<class RealType = double>
34class weibull_distribution {
35public:
36 typedef RealType result_type;
37 typedef RealType input_type;
38
39 class param_type {
40 public:
41 typedef weibull_distribution distribution_type;
42
43 /**
44 * Constructs a @c param_type from the "a" and "b" parameters
45 * of the distribution.
46 *
47 * Requires: a > 0 && b > 0
48 */
49 explicit param_type(RealType a_arg = 1.0, RealType b_arg = 1.0)
50 : _a(a_arg), _b(b_arg)
51 {}
52
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; }
57
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; }
61
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; }
65
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; }
69
70 /** Returns true if the two sets of parameters are the different. */
71 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
72
73 private:
74 RealType _a;
75 RealType _b;
76 };
77
78 /**
79 * Constructs a @c weibull_distribution from its "a" and "b" parameters.
80 *
81 * Requires: a > 0 && b > 0
82 */
83 explicit weibull_distribution(RealType a_arg = 1.0, RealType b_arg = 1.0)
84 : _a(a_arg), _b(b_arg)
85 {}
86 /** Constructs a @c weibull_distribution from its parameters. */
87 explicit weibull_distribution(const param_type& parm)
88 : _a(parm.a()), _b(parm.b())
89 {}
90
91 /**
92 * Returns a random variate distributed according to the
93 * @c weibull_distribution.
94 */
95 template<class URNG>
96 RealType operator()(URNG& urng) const
97 {
98 using std::pow;
99 using std::log;
100 return _b*pow(-log(1 - uniform_01<RealType>()(urng)), 1/_a);
101 }
102
103 /**
104 * Returns a random variate distributed accordint to the Weibull
105 * distribution with parameters specified by @c param.
106 */
107 template<class URNG>
108 RealType operator()(URNG& urng, const param_type& parm) const
109 {
110 return weibull_distribution(parm)(urng);
111 }
112
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; }
117
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(); }
123
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)
128 {
129 _a = parm.a();
130 _b = parm.b();
131 }
132
133 /**
134 * Effects: Subsequent uses of the distribution do not depend
135 * on values produced by any engine prior to invoking reset.
136 */
137 void reset() { }
138
139 /** Writes a @c weibull_distribution to a @c std::ostream. */
140 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, weibull_distribution, wd)
141 {
142 os << wd.param();
143 return os;
144 }
145
146 /** Reads a @c weibull_distribution from a @c std::istream. */
147 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, weibull_distribution, wd)
148 {
149 param_type parm;
150 if(is >> parm) {
151 wd.param(parm);
152 }
153 return is;
154 }
155
156 /**
157 * Returns true if the two instances of @c weibull_distribution will
158 * return identical sequences of values given equal generators.
159 */
160 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(weibull_distribution, lhs, rhs)
161 { return lhs._a == rhs._a && lhs._b == rhs._b; }
162
163 /**
164 * Returns true if the two instances of @c weibull_distribution will
165 * return different sequences of values given equal generators.
166 */
167 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(weibull_distribution)
168
169private:
170 RealType _a;
171 RealType _b;
172};
173
174} // namespace random
175} // namespace boost
176
177#endif // BOOST_RANDOM_WEIBULL_DISTRIBUTION_HPP
178