Chris@16: /* boost random/extreme_value_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_EXTREME_VALUE_DISTRIBUTION_HPP Chris@16: #define BOOST_RANDOM_EXTREME_VALUE_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 extreme value distribution is a real valued distribution with two Chris@16: * parameters a and b. Chris@16: * Chris@16: * It has \f$\displaystyle p(x) = \frac{1}{b}e^{\frac{a-x}{b} - e^\frac{a-x}{b}}\f$. Chris@16: */ Chris@16: template Chris@16: class extreme_value_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 extreme_value_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: 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 an @c extreme_value_distribution from its "a" and "b" parameters. Chris@16: * Chris@16: * Requires: b > 0 Chris@16: */ Chris@16: explicit extreme_value_distribution(RealType a_arg = 1.0, RealType b_arg = 1.0) Chris@16: : _a(a_arg), _b(b_arg) Chris@16: {} Chris@16: /** Constructs an @c extreme_value_distribution from its parameters. */ Chris@16: explicit extreme_value_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 extreme_value_distribution. Chris@16: */ Chris@16: template Chris@16: RealType operator()(URNG& urng) const Chris@16: { Chris@16: using std::log; Chris@16: return _a - log(-log(uniform_01()(urng))) * _b; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns a random variate distributed accordint to the extreme Chris@16: * value 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 extreme_value_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 Chris@16: { return -std::numeric_limits::infinity(); } 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 an @c extreme_value_distribution to a @c std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, extreme_value_distribution, wd) Chris@16: { Chris@16: os << wd.param(); Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads an @c extreme_value_distribution from a @c std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, extreme_value_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 extreme_value_distribution will Chris@16: * return identical sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(extreme_value_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 extreme_value_distribution will Chris@16: * return different sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(extreme_value_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_EXTREME_VALUE_DISTRIBUTION_HPP