negative_binomial_distribution.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* boost random/negative_binomial_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_NEGATIVE_BINOMIAL_DISTRIBUTION_HPP_INCLUDED
  13. #define BOOST_RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_HPP_INCLUDED
  14. #include <iosfwd>
  15. #include <boost/limits.hpp>
  16. #include <boost/random/detail/config.hpp>
  17. #include <boost/random/gamma_distribution.hpp>
  18. #include <boost/random/poisson_distribution.hpp>
  19. namespace boost {
  20. namespace random {
  21. /**
  22. * The negative binomial distribution is an integer valued
  23. * distribution with two parameters, @c k and @c p. The
  24. * distribution produces non-negative values.
  25. *
  26. * The distribution function is
  27. * \f$\displaystyle P(i) = {k+i-1\choose i}p^k(1-p)^i\f$.
  28. *
  29. * This implementation uses a gamma-poisson mixture.
  30. */
  31. template<class IntType = int, class RealType = double>
  32. class negative_binomial_distribution {
  33. public:
  34. typedef IntType result_type;
  35. typedef RealType input_type;
  36. class param_type {
  37. public:
  38. typedef negative_binomial_distribution distribution_type;
  39. /**
  40. * Construct a param_type object. @c k and @c p
  41. * are the parameters of the distribution.
  42. *
  43. * Requires: k >=0 && 0 <= p <= 1
  44. */
  45. explicit param_type(IntType k_arg = 1, RealType p_arg = RealType (0.5))
  46. : _k(k_arg), _p(p_arg)
  47. {}
  48. /** Returns the @c k parameter of the distribution. */
  49. IntType k() const { return _k; }
  50. /** Returns the @c p parameter of the distribution. */
  51. RealType p() const { return _p; }
  52. #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
  53. /** Writes the parameters of the distribution to a @c std::ostream. */
  54. template<class CharT, class Traits>
  55. friend std::basic_ostream<CharT,Traits>&
  56. operator<<(std::basic_ostream<CharT,Traits>& os,
  57. const param_type& parm)
  58. {
  59. os << parm._p << " " << parm._k;
  60. return os;
  61. }
  62. /** Reads the parameters of the distribution from a @c std::istream. */
  63. template<class CharT, class Traits>
  64. friend std::basic_istream<CharT,Traits>&
  65. operator>>(std::basic_istream<CharT,Traits>& is, param_type& parm)
  66. {
  67. is >> parm._p >> std::ws >> parm._k;
  68. return is;
  69. }
  70. #endif
  71. /** Returns true if the parameters have the same values. */
  72. friend bool operator==(const param_type& lhs, const param_type& rhs)
  73. {
  74. return lhs._k == rhs._k && lhs._p == rhs._p;
  75. }
  76. /** Returns true if the parameters have different values. */
  77. friend bool operator!=(const param_type& lhs, const param_type& rhs)
  78. {
  79. return !(lhs == rhs);
  80. }
  81. private:
  82. IntType _k;
  83. RealType _p;
  84. };
  85. /**
  86. * Construct a @c negative_binomial_distribution object. @c k and @c p
  87. * are the parameters of the distribution.
  88. *
  89. * Requires: k >=0 && 0 <= p <= 1
  90. */
  91. explicit negative_binomial_distribution(IntType k_arg = 1,
  92. RealType p_arg = RealType(0.5))
  93. : _k(k_arg), _p(p_arg)
  94. {}
  95. /**
  96. * Construct an @c negative_binomial_distribution object from the
  97. * parameters.
  98. */
  99. explicit negative_binomial_distribution(const param_type& parm)
  100. : _k(parm.k()), _p(parm.p())
  101. {}
  102. /**
  103. * Returns a random variate distributed according to the
  104. * negative binomial distribution.
  105. */
  106. template<class URNG>
  107. IntType operator()(URNG& urng) const
  108. {
  109. gamma_distribution<RealType> gamma(_k, (1-_p)/_p);
  110. poisson_distribution<IntType, RealType> poisson(gamma(urng));
  111. return poisson(urng);
  112. }
  113. /**
  114. * Returns a random variate distributed according to the negative
  115. * binomial distribution with parameters specified by @c param.
  116. */
  117. template<class URNG>
  118. IntType operator()(URNG& urng, const param_type& parm) const
  119. {
  120. return negative_binomial_distribution(parm)(urng);
  121. }
  122. /** Returns the @c k parameter of the distribution. */
  123. IntType k() const { return _k; }
  124. /** Returns the @c p parameter of the distribution. */
  125. RealType p() const { return _p; }
  126. /** Returns the smallest value that the distribution can produce. */
  127. IntType min BOOST_PREVENT_MACRO_SUBSTITUTION() const { return 0; }
  128. /** Returns the largest value that the distribution can produce. */
  129. IntType max BOOST_PREVENT_MACRO_SUBSTITUTION() const
  130. { return (std::numeric_limits<IntType>::max)(); }
  131. /** Returns the parameters of the distribution. */
  132. param_type param() const { return param_type(_k, _p); }
  133. /** Sets parameters of the distribution. */
  134. void param(const param_type& parm)
  135. {
  136. _k = parm.k();
  137. _p = parm.p();
  138. }
  139. /**
  140. * Effects: Subsequent uses of the distribution do not depend
  141. * on values produced by any engine prior to invoking reset.
  142. */
  143. void reset() { }
  144. #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
  145. /** Writes the parameters of the distribution to a @c std::ostream. */
  146. template<class CharT, class Traits>
  147. friend std::basic_ostream<CharT,Traits>&
  148. operator<<(std::basic_ostream<CharT,Traits>& os,
  149. const negative_binomial_distribution& bd)
  150. {
  151. os << bd.param();
  152. return os;
  153. }
  154. /** Reads the parameters of the distribution from a @c std::istream. */
  155. template<class CharT, class Traits>
  156. friend std::basic_istream<CharT,Traits>&
  157. operator>>(std::basic_istream<CharT,Traits>& is,
  158. negative_binomial_distribution& bd)
  159. {
  160. bd.read(is);
  161. return is;
  162. }
  163. #endif
  164. /** Returns true if the two distributions will produce the same
  165. sequence of values, given equal generators. */
  166. friend bool operator==(const negative_binomial_distribution& lhs,
  167. const negative_binomial_distribution& rhs)
  168. {
  169. return lhs._k == rhs._k && lhs._p == rhs._p;
  170. }
  171. /** Returns true if the two distributions could produce different
  172. sequences of values, given equal generators. */
  173. friend bool operator!=(const negative_binomial_distribution& lhs,
  174. const negative_binomial_distribution& rhs)
  175. {
  176. return !(lhs == rhs);
  177. }
  178. private:
  179. /// @cond \show_private
  180. template<class CharT, class Traits>
  181. void read(std::basic_istream<CharT, Traits>& is) {
  182. param_type parm;
  183. if(is >> parm) {
  184. param(parm);
  185. }
  186. }
  187. // parameters
  188. IntType _k;
  189. RealType _p;
  190. /// @endcond
  191. };
  192. }
  193. }
  194. #endif