Chris@16
|
1 /* boost random/extreme_value_distribution.hpp header file
|
Chris@16
|
2 *
|
Chris@16
|
3 * Copyright Steven Watanabe 2010
|
Chris@16
|
4 * Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
5 * accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 *
|
Chris@16
|
8 * See http://www.boost.org for most recent version including documentation.
|
Chris@16
|
9 *
|
Chris@101
|
10 * $Id$
|
Chris@16
|
11 */
|
Chris@16
|
12
|
Chris@16
|
13 #ifndef BOOST_RANDOM_EXTREME_VALUE_DISTRIBUTION_HPP
|
Chris@16
|
14 #define BOOST_RANDOM_EXTREME_VALUE_DISTRIBUTION_HPP
|
Chris@16
|
15
|
Chris@16
|
16 #include <boost/config/no_tr1/cmath.hpp>
|
Chris@16
|
17 #include <iosfwd>
|
Chris@16
|
18 #include <istream>
|
Chris@16
|
19 #include <boost/config.hpp>
|
Chris@16
|
20 #include <boost/limits.hpp>
|
Chris@16
|
21 #include <boost/random/detail/operators.hpp>
|
Chris@16
|
22 #include <boost/random/uniform_01.hpp>
|
Chris@16
|
23
|
Chris@16
|
24 namespace boost {
|
Chris@16
|
25 namespace random {
|
Chris@16
|
26
|
Chris@16
|
27 /**
|
Chris@16
|
28 * The extreme value distribution is a real valued distribution with two
|
Chris@16
|
29 * parameters a and b.
|
Chris@16
|
30 *
|
Chris@16
|
31 * It has \f$\displaystyle p(x) = \frac{1}{b}e^{\frac{a-x}{b} - e^\frac{a-x}{b}}\f$.
|
Chris@16
|
32 */
|
Chris@16
|
33 template<class RealType = double>
|
Chris@16
|
34 class extreme_value_distribution {
|
Chris@16
|
35 public:
|
Chris@16
|
36 typedef RealType result_type;
|
Chris@16
|
37 typedef RealType input_type;
|
Chris@16
|
38
|
Chris@16
|
39 class param_type {
|
Chris@16
|
40 public:
|
Chris@16
|
41 typedef extreme_value_distribution distribution_type;
|
Chris@16
|
42
|
Chris@16
|
43 /**
|
Chris@16
|
44 * Constructs a @c param_type from the "a" and "b" parameters
|
Chris@16
|
45 * of the distribution.
|
Chris@16
|
46 *
|
Chris@16
|
47 * Requires: b > 0
|
Chris@16
|
48 */
|
Chris@16
|
49 explicit param_type(RealType a_arg = 1.0, RealType b_arg = 1.0)
|
Chris@16
|
50 : _a(a_arg), _b(b_arg)
|
Chris@16
|
51 {}
|
Chris@16
|
52
|
Chris@16
|
53 /** Returns the "a" parameter of the distribtuion. */
|
Chris@16
|
54 RealType a() const { return _a; }
|
Chris@16
|
55 /** Returns the "b" parameter of the distribution. */
|
Chris@16
|
56 RealType b() const { return _b; }
|
Chris@16
|
57
|
Chris@16
|
58 /** Writes a @c param_type to a @c std::ostream. */
|
Chris@16
|
59 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
|
Chris@16
|
60 { os << parm._a << ' ' << parm._b; return os; }
|
Chris@16
|
61
|
Chris@16
|
62 /** Reads a @c param_type from a @c std::istream. */
|
Chris@16
|
63 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
|
Chris@16
|
64 { is >> parm._a >> std::ws >> parm._b; return is; }
|
Chris@16
|
65
|
Chris@16
|
66 /** Returns true if the two sets of parameters are the same. */
|
Chris@16
|
67 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
|
Chris@16
|
68 { return lhs._a == rhs._a && lhs._b == rhs._b; }
|
Chris@16
|
69
|
Chris@16
|
70 /** Returns true if the two sets of parameters are the different. */
|
Chris@16
|
71 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
|
Chris@16
|
72
|
Chris@16
|
73 private:
|
Chris@16
|
74 RealType _a;
|
Chris@16
|
75 RealType _b;
|
Chris@16
|
76 };
|
Chris@16
|
77
|
Chris@16
|
78 /**
|
Chris@16
|
79 * Constructs an @c extreme_value_distribution from its "a" and "b" parameters.
|
Chris@16
|
80 *
|
Chris@16
|
81 * Requires: b > 0
|
Chris@16
|
82 */
|
Chris@16
|
83 explicit extreme_value_distribution(RealType a_arg = 1.0, RealType b_arg = 1.0)
|
Chris@16
|
84 : _a(a_arg), _b(b_arg)
|
Chris@16
|
85 {}
|
Chris@16
|
86 /** Constructs an @c extreme_value_distribution from its parameters. */
|
Chris@16
|
87 explicit extreme_value_distribution(const param_type& parm)
|
Chris@16
|
88 : _a(parm.a()), _b(parm.b())
|
Chris@16
|
89 {}
|
Chris@16
|
90
|
Chris@16
|
91 /**
|
Chris@16
|
92 * Returns a random variate distributed according to the
|
Chris@16
|
93 * @c extreme_value_distribution.
|
Chris@16
|
94 */
|
Chris@16
|
95 template<class URNG>
|
Chris@16
|
96 RealType operator()(URNG& urng) const
|
Chris@16
|
97 {
|
Chris@16
|
98 using std::log;
|
Chris@16
|
99 return _a - log(-log(uniform_01<RealType>()(urng))) * _b;
|
Chris@16
|
100 }
|
Chris@16
|
101
|
Chris@16
|
102 /**
|
Chris@16
|
103 * Returns a random variate distributed accordint to the extreme
|
Chris@16
|
104 * value distribution with parameters specified by @c param.
|
Chris@16
|
105 */
|
Chris@16
|
106 template<class URNG>
|
Chris@16
|
107 RealType operator()(URNG& urng, const param_type& parm) const
|
Chris@16
|
108 {
|
Chris@16
|
109 return extreme_value_distribution(parm)(urng);
|
Chris@16
|
110 }
|
Chris@16
|
111
|
Chris@16
|
112 /** Returns the "a" parameter of the distribution. */
|
Chris@16
|
113 RealType a() const { return _a; }
|
Chris@16
|
114 /** Returns the "b" parameter of the distribution. */
|
Chris@16
|
115 RealType b() const { return _b; }
|
Chris@16
|
116
|
Chris@16
|
117 /** Returns the smallest value that the distribution can produce. */
|
Chris@16
|
118 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
|
Chris@16
|
119 { return -std::numeric_limits<RealType>::infinity(); }
|
Chris@16
|
120 /** Returns the largest value that the distribution can produce. */
|
Chris@16
|
121 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
|
Chris@16
|
122 { return std::numeric_limits<RealType>::infinity(); }
|
Chris@16
|
123
|
Chris@16
|
124 /** Returns the parameters of the distribution. */
|
Chris@16
|
125 param_type param() const { return param_type(_a, _b); }
|
Chris@16
|
126 /** Sets the parameters of the distribution. */
|
Chris@16
|
127 void param(const param_type& parm)
|
Chris@16
|
128 {
|
Chris@16
|
129 _a = parm.a();
|
Chris@16
|
130 _b = parm.b();
|
Chris@16
|
131 }
|
Chris@16
|
132
|
Chris@16
|
133 /**
|
Chris@16
|
134 * Effects: Subsequent uses of the distribution do not depend
|
Chris@16
|
135 * on values produced by any engine prior to invoking reset.
|
Chris@16
|
136 */
|
Chris@16
|
137 void reset() { }
|
Chris@16
|
138
|
Chris@16
|
139 /** Writes an @c extreme_value_distribution to a @c std::ostream. */
|
Chris@16
|
140 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, extreme_value_distribution, wd)
|
Chris@16
|
141 {
|
Chris@16
|
142 os << wd.param();
|
Chris@16
|
143 return os;
|
Chris@16
|
144 }
|
Chris@16
|
145
|
Chris@16
|
146 /** Reads an @c extreme_value_distribution from a @c std::istream. */
|
Chris@16
|
147 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, extreme_value_distribution, wd)
|
Chris@16
|
148 {
|
Chris@16
|
149 param_type parm;
|
Chris@16
|
150 if(is >> parm) {
|
Chris@16
|
151 wd.param(parm);
|
Chris@16
|
152 }
|
Chris@16
|
153 return is;
|
Chris@16
|
154 }
|
Chris@16
|
155
|
Chris@16
|
156 /**
|
Chris@16
|
157 * Returns true if the two instances of @c extreme_value_distribution will
|
Chris@16
|
158 * return identical sequences of values given equal generators.
|
Chris@16
|
159 */
|
Chris@16
|
160 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(extreme_value_distribution, lhs, rhs)
|
Chris@16
|
161 { return lhs._a == rhs._a && lhs._b == rhs._b; }
|
Chris@16
|
162
|
Chris@16
|
163 /**
|
Chris@16
|
164 * Returns true if the two instances of @c extreme_value_distribution will
|
Chris@16
|
165 * return different sequences of values given equal generators.
|
Chris@16
|
166 */
|
Chris@16
|
167 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(extreme_value_distribution)
|
Chris@16
|
168
|
Chris@16
|
169 private:
|
Chris@16
|
170 RealType _a;
|
Chris@16
|
171 RealType _b;
|
Chris@16
|
172 };
|
Chris@16
|
173
|
Chris@16
|
174 } // namespace random
|
Chris@16
|
175 } // namespace boost
|
Chris@16
|
176
|
Chris@16
|
177 #endif // BOOST_RANDOM_EXTREME_VALUE_DISTRIBUTION_HPP
|