exponential_distribution.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* boost random/exponential_distribution.hpp header file
  2. *
  3. * Copyright Jens Maurer 2000-2001
  4. * Copyright Steven Watanabe 2011
  5. * Distributed under the Boost Software License, Version 1.0. (See
  6. * accompanying file LICENSE_1_0.txt or copy at
  7. * http://www.boost.org/LICENSE_1_0.txt)
  8. *
  9. * See http://www.boost.org for most recent version including documentation.
  10. *
  11. * $Id$
  12. *
  13. * Revision history
  14. * 2001-02-18 moved to individual header files
  15. */
  16. #ifndef BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP
  17. #define BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP
  18. #include <boost/config/no_tr1/cmath.hpp>
  19. #include <iosfwd>
  20. #include <boost/assert.hpp>
  21. #include <boost/limits.hpp>
  22. #include <boost/random/detail/config.hpp>
  23. #include <boost/random/detail/operators.hpp>
  24. #include <boost/random/uniform_01.hpp>
  25. namespace boost {
  26. namespace random {
  27. /**
  28. * The exponential distribution is a model of \random_distribution with
  29. * a single parameter lambda.
  30. *
  31. * It has \f$\displaystyle p(x) = \lambda e^{-\lambda x}\f$
  32. */
  33. template<class RealType = double>
  34. class exponential_distribution
  35. {
  36. public:
  37. typedef RealType input_type;
  38. typedef RealType result_type;
  39. class param_type
  40. {
  41. public:
  42. typedef exponential_distribution distribution_type;
  43. /**
  44. * Constructs parameters with a given lambda.
  45. *
  46. * Requires: lambda > 0
  47. */
  48. param_type(RealType lambda_arg = RealType(1.0))
  49. : _lambda(lambda_arg) { BOOST_ASSERT(_lambda > RealType(0)); }
  50. /** Returns the lambda parameter of the distribution. */
  51. RealType lambda() const { return _lambda; }
  52. /** Writes the parameters to a @c std::ostream. */
  53. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
  54. {
  55. os << parm._lambda;
  56. return os;
  57. }
  58. /** Reads the parameters from a @c std::istream. */
  59. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
  60. {
  61. is >> parm._lambda;
  62. return is;
  63. }
  64. /** Returns true if the two sets of parameters are equal. */
  65. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
  66. { return lhs._lambda == rhs._lambda; }
  67. /** Returns true if the two sets of parameters are different. */
  68. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
  69. private:
  70. RealType _lambda;
  71. };
  72. /**
  73. * Constructs an exponential_distribution with a given lambda.
  74. *
  75. * Requires: lambda > 0
  76. */
  77. explicit exponential_distribution(RealType lambda_arg = RealType(1.0))
  78. : _lambda(lambda_arg) { BOOST_ASSERT(_lambda > RealType(0)); }
  79. /**
  80. * Constructs an exponential_distribution from its parameters
  81. */
  82. explicit exponential_distribution(const param_type& parm)
  83. : _lambda(parm.lambda()) {}
  84. // compiler-generated copy ctor and assignment operator are fine
  85. /** Returns the lambda parameter of the distribution. */
  86. RealType lambda() const { return _lambda; }
  87. /** Returns the smallest value that the distribution can produce. */
  88. RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
  89. { return RealType(0); }
  90. /** Returns the largest value that the distribution can produce. */
  91. RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
  92. { return (std::numeric_limits<RealType>::infinity)(); }
  93. /** Returns the parameters of the distribution. */
  94. param_type param() const { return param_type(_lambda); }
  95. /** Sets the parameters of the distribution. */
  96. void param(const param_type& parm) { _lambda = parm.lambda(); }
  97. /**
  98. * Effects: Subsequent uses of the distribution do not depend
  99. * on values produced by any engine prior to invoking reset.
  100. */
  101. void reset() { }
  102. /**
  103. * Returns a random variate distributed according to the
  104. * exponential distribution.
  105. */
  106. template<class Engine>
  107. result_type operator()(Engine& eng) const
  108. {
  109. using std::log;
  110. return -result_type(1) /
  111. _lambda * log(result_type(1)-uniform_01<RealType>()(eng));
  112. }
  113. /**
  114. * Returns a random variate distributed according to the exponential
  115. * distribution with parameters specified by param.
  116. */
  117. template<class Engine>
  118. result_type operator()(Engine& eng, const param_type& parm) const
  119. {
  120. return exponential_distribution(parm)(eng);
  121. }
  122. /** Writes the distribution to a std::ostream. */
  123. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, exponential_distribution, ed)
  124. {
  125. os << ed._lambda;
  126. return os;
  127. }
  128. /** Reads the distribution from a std::istream. */
  129. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, exponential_distribution, ed)
  130. {
  131. is >> ed._lambda;
  132. return is;
  133. }
  134. /**
  135. * Returns true iff the two distributions will produce identical
  136. * sequences of values given equal generators.
  137. */
  138. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(exponential_distribution, lhs, rhs)
  139. { return lhs._lambda == rhs._lambda; }
  140. /**
  141. * Returns true iff the two distributions will produce different
  142. * sequences of values given equal generators.
  143. */
  144. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(exponential_distribution)
  145. private:
  146. result_type _lambda;
  147. };
  148. } // namespace random
  149. using random::exponential_distribution;
  150. } // namespace boost
  151. #endif // BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP