weibull_distribution.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #ifndef BOOST_RANDOM_WEIBULL_DISTRIBUTION_HPP
  13. #define BOOST_RANDOM_WEIBULL_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 Weibull distribution is a real valued distribution with two
  25. * parameters a and b, producing values >= 0.
  26. *
  27. * It has \f$\displaystyle p(x) = \frac{a}{b}\left(\frac{x}{b}\right)^{a-1}e^{-\left(\frac{x}{b}\right)^a}\f$.
  28. */
  29. template<class RealType = double>
  30. class weibull_distribution {
  31. public:
  32. typedef RealType result_type;
  33. typedef RealType input_type;
  34. class param_type {
  35. public:
  36. typedef weibull_distribution distribution_type;
  37. /**
  38. * Constructs a @c param_type from the "a" and "b" parameters
  39. * of the distribution.
  40. *
  41. * Requires: a > 0 && 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 a @c weibull_distribution from its "a" and "b" parameters.
  67. *
  68. * Requires: a > 0 && b > 0
  69. */
  70. explicit weibull_distribution(RealType a_arg = 1.0, RealType b_arg = 1.0)
  71. : _a(a_arg), _b(b_arg)
  72. {}
  73. /** Constructs a @c weibull_distribution from its parameters. */
  74. explicit weibull_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 weibull_distribution.
  80. */
  81. template<class URNG>
  82. RealType operator()(URNG& urng) const
  83. {
  84. using std::pow;
  85. using std::log;
  86. return _b*pow(-log(1 - uniform_01<RealType>()(urng)), 1/_a);
  87. }
  88. /**
  89. * Returns a random variate distributed accordint to the Weibull
  90. * distribution with parameters specified by @c param.
  91. */
  92. template<class URNG>
  93. RealType operator()(URNG& urng, const param_type& parm) const
  94. {
  95. return weibull_distribution(parm)(urng);
  96. }
  97. /** Returns the "a" parameter of the distribution. */
  98. RealType a() const { return _a; }
  99. /** Returns the "b" parameter of the distribution. */
  100. RealType b() const { return _b; }
  101. /** Returns the smallest value that the distribution can produce. */
  102. RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
  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 a @c weibull_distribution to a @c std::ostream. */
  120. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, weibull_distribution, wd)
  121. {
  122. os << wd.param();
  123. return os;
  124. }
  125. /** Reads a @c weibull_distribution from a @c std::istream. */
  126. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, weibull_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 weibull_distribution will
  136. * return identical sequences of values given equal generators.
  137. */
  138. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(weibull_distribution, lhs, rhs)
  139. { return lhs._a == rhs._a && lhs._b == rhs._b; }
  140. /**
  141. * Returns true if the two instances of @c weibull_distribution will
  142. * return different sequences of values given equal generators.
  143. */
  144. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(weibull_distribution)
  145. private:
  146. RealType _a;
  147. RealType _b;
  148. };
  149. } // namespace random
  150. } // namespace boost
  151. #endif // BOOST_RANDOM_WEIBULL_DISTRIBUTION_HPP