extreme_value_distribution.hpp 5.5 KB

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