geometric_distribution.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* boost random/geometric_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_GEOMETRIC_DISTRIBUTION_HPP
  17. #define BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
  18. #include <boost/config/no_tr1/cmath.hpp> // std::log
  19. #include <iosfwd>
  20. #include <ios>
  21. #include <boost/assert.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. * An instantiation of the class template @c geometric_distribution models
  29. * a \random_distribution. The distribution produces positive
  30. * integers which are the number of bernoulli trials
  31. * with probability @c p required to get one that fails.
  32. *
  33. * For the geometric distribution, \f$p(i) = p(1-p)^{i}\f$.
  34. *
  35. * @xmlwarning
  36. * This distribution has been updated to match the C++ standard.
  37. * Its behavior has changed from the original
  38. * boost::geometric_distribution. A backwards compatible
  39. * wrapper is provided in namespace boost.
  40. * @endxmlwarning
  41. */
  42. template<class IntType = int, class RealType = double>
  43. class geometric_distribution
  44. {
  45. public:
  46. typedef RealType input_type;
  47. typedef IntType result_type;
  48. class param_type
  49. {
  50. public:
  51. typedef geometric_distribution distribution_type;
  52. /** Constructs the parameters with p. */
  53. explicit param_type(RealType p_arg = RealType(0.5))
  54. : _p(p_arg)
  55. {
  56. BOOST_ASSERT(RealType(0) < _p && _p < RealType(1));
  57. }
  58. /** Returns the p parameter of the distribution. */
  59. RealType p() const { return _p; }
  60. /** Writes the parameters to a std::ostream. */
  61. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
  62. {
  63. os << parm._p;
  64. return os;
  65. }
  66. /** Reads the parameters from a std::istream. */
  67. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
  68. {
  69. double p_in;
  70. if(is >> p_in) {
  71. if(p_in > RealType(0) && p_in < RealType(1)) {
  72. parm._p = p_in;
  73. } else {
  74. is.setstate(std::ios_base::failbit);
  75. }
  76. }
  77. return is;
  78. }
  79. /** Returns true if the two sets of parameters are equal. */
  80. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
  81. { return lhs._p == rhs._p; }
  82. /** Returns true if the two sets of parameters are different. */
  83. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
  84. private:
  85. RealType _p;
  86. };
  87. /**
  88. * Contructs a new geometric_distribution with the paramter @c p.
  89. *
  90. * Requires: 0 < p < 1
  91. */
  92. explicit geometric_distribution(const RealType& p_arg = RealType(0.5))
  93. : _p(p_arg)
  94. {
  95. BOOST_ASSERT(RealType(0) < _p && _p < RealType(1));
  96. init();
  97. }
  98. /** Constructs a new geometric_distribution from its parameters. */
  99. explicit geometric_distribution(const param_type& parm)
  100. : _p(parm.p())
  101. {
  102. init();
  103. }
  104. // compiler-generated copy ctor and assignment operator are fine
  105. /** Returns: the distribution parameter @c p */
  106. RealType p() const { return _p; }
  107. /** Returns the smallest value that the distribution can produce. */
  108. IntType min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return IntType(0); }
  109. /** Returns the largest value that the distribution can produce. */
  110. IntType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
  111. { return (std::numeric_limits<IntType>::max)(); }
  112. /** Returns the parameters of the distribution. */
  113. param_type param() const { return param_type(_p); }
  114. /** Sets the parameters of the distribution. */
  115. void param(const param_type& parm)
  116. {
  117. _p = parm.p();
  118. init();
  119. }
  120. /**
  121. * Effects: Subsequent uses of the distribution do not depend
  122. * on values produced by any engine prior to invoking reset.
  123. */
  124. void reset() { }
  125. /**
  126. * Returns a random variate distributed according to the
  127. * geometric_distribution.
  128. */
  129. template<class Engine>
  130. result_type operator()(Engine& eng) const
  131. {
  132. using std::log;
  133. using std::floor;
  134. RealType x = RealType(1) - boost::uniform_01<RealType>()(eng);
  135. return IntType(floor(log(x) / _log_1mp));
  136. }
  137. /**
  138. * Returns a random variate distributed according to the
  139. * geometric distribution with parameters specified by param.
  140. */
  141. template<class Engine>
  142. result_type operator()(Engine& eng, const param_type& parm) const
  143. { return geometric_distribution(parm)(eng); }
  144. /** Writes the distribution to a @c std::ostream. */
  145. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, geometric_distribution, gd)
  146. {
  147. os << gd._p;
  148. return os;
  149. }
  150. /** Reads the distribution from a @c std::istream. */
  151. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, geometric_distribution, gd)
  152. {
  153. param_type parm;
  154. if(is >> parm) {
  155. gd.param(parm);
  156. }
  157. return is;
  158. }
  159. /**
  160. * Returns true if the two distributions will produce identical
  161. * sequences of values given equal generators.
  162. */
  163. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(geometric_distribution, lhs, rhs)
  164. { return lhs._p == rhs._p; }
  165. /**
  166. * Returns true if the two distributions may produce different
  167. * sequences of values given equal generators.
  168. */
  169. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(geometric_distribution)
  170. private:
  171. /// \cond show_private
  172. void init()
  173. {
  174. using std::log;
  175. _log_1mp = log(1 - _p);
  176. }
  177. RealType _p;
  178. RealType _log_1mp;
  179. /// \endcond
  180. };
  181. } // namespace random
  182. /// \cond show_deprecated
  183. /**
  184. * Provided for backwards compatibility. This class is
  185. * deprecated. It provides the old behavior of geometric_distribution
  186. * with \f$p(i) = (1-p) p^{i-1}\f$.
  187. */
  188. template<class IntType = int, class RealType = double>
  189. class geometric_distribution
  190. {
  191. public:
  192. typedef RealType input_type;
  193. typedef IntType result_type;
  194. explicit geometric_distribution(RealType p_arg = RealType(0.5))
  195. : _impl(1 - p_arg) {}
  196. RealType p() const { return 1 - _impl.p(); }
  197. void reset() {}
  198. template<class Engine>
  199. IntType operator()(Engine& eng) const { return _impl(eng) + IntType(1); }
  200. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, geometric_distribution, gd)
  201. {
  202. os << gd.p();
  203. return os;
  204. }
  205. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, geometric_distribution, gd)
  206. {
  207. RealType val;
  208. if(is >> val) {
  209. typename impl_type::param_type impl_param(1 - val);
  210. gd._impl.param(impl_param);
  211. }
  212. return is;
  213. }
  214. private:
  215. typedef random::geometric_distribution<IntType, RealType> impl_type;
  216. impl_type _impl;
  217. };
  218. /// \endcond
  219. } // namespace boost
  220. #endif // BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP