1 | /* boost random/extreme_value_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_EXTREME_VALUE_DISTRIBUTION_HPP |
14 | #define BOOST_RANDOM_EXTREME_VALUE_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 | |
24 | namespace boost { |
25 | namespace random { |
26 | |
27 | /** |
28 | * The extreme value distribution is a real valued distribution with two |
29 | * parameters a and b. |
30 | * |
31 | * It has \f$\displaystyle p(x) = \frac{1}{b}e^{\frac{a-x}{b} - e^\frac{a-x}{b}}\f$. |
32 | */ |
33 | template<class RealType = double> |
34 | class extreme_value_distribution { |
35 | public: |
36 | typedef RealType result_type; |
37 | typedef RealType input_type; |
38 | |
39 | class param_type { |
40 | public: |
41 | typedef extreme_value_distribution distribution_type; |
42 | |
43 | /** |
44 | * Constructs a @c param_type from the "a" and "b" parameters |
45 | * of the distribution. |
46 | * |
47 | * Requires: 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 an @c extreme_value_distribution from its "a" and "b" parameters. |
80 | * |
81 | * Requires: b > 0 |
82 | */ |
83 | explicit extreme_value_distribution(RealType a_arg = 1.0, RealType b_arg = 1.0) |
84 | : _a(a_arg), _b(b_arg) |
85 | {} |
86 | /** Constructs an @c extreme_value_distribution from its parameters. */ |
87 | explicit extreme_value_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 extreme_value_distribution. |
94 | */ |
95 | template<class URNG> |
96 | RealType operator()(URNG& urng) const |
97 | { |
98 | using std::log; |
99 | return _a - log(-log(uniform_01<RealType>()(urng))) * _b; |
100 | } |
101 | |
102 | /** |
103 | * Returns a random variate distributed accordint to the extreme |
104 | * value distribution with parameters specified by @c param. |
105 | */ |
106 | template<class URNG> |
107 | RealType operator()(URNG& urng, const param_type& parm) const |
108 | { |
109 | return extreme_value_distribution(parm)(urng); |
110 | } |
111 | |
112 | /** Returns the "a" parameter of the distribution. */ |
113 | RealType a() const { return _a; } |
114 | /** Returns the "b" parameter of the distribution. */ |
115 | RealType b() const { return _b; } |
116 | |
117 | /** Returns the smallest value that the distribution can produce. */ |
118 | RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const |
119 | { return -std::numeric_limits<RealType>::infinity(); } |
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 an @c extreme_value_distribution to a @c std::ostream. */ |
140 | BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, extreme_value_distribution, wd) |
141 | { |
142 | os << wd.param(); |
143 | return os; |
144 | } |
145 | |
146 | /** Reads an @c extreme_value_distribution from a @c std::istream. */ |
147 | BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, extreme_value_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 extreme_value_distribution will |
158 | * return identical sequences of values given equal generators. |
159 | */ |
160 | BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(extreme_value_distribution, lhs, rhs) |
161 | { return lhs._a == rhs._a && lhs._b == rhs._b; } |
162 | |
163 | /** |
164 | * Returns true if the two instances of @c extreme_value_distribution will |
165 | * return different sequences of values given equal generators. |
166 | */ |
167 | BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(extreme_value_distribution) |
168 | |
169 | private: |
170 | RealType _a; |
171 | RealType _b; |
172 | }; |
173 | |
174 | } // namespace random |
175 | } // namespace boost |
176 | |
177 | #endif // BOOST_RANDOM_EXTREME_VALUE_DISTRIBUTION_HPP |
178 | |