Chris@16: /* boost random/negative_binomial_distribution.hpp header file Chris@16: * Chris@16: * Copyright Steven Watanabe 2010 Chris@16: * Distributed under the Boost Software License, Version 1.0. (See Chris@16: * accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@16: * See http://www.boost.org for most recent version including documentation. Chris@16: * Chris@101: * $Id$ Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_HPP_INCLUDED Chris@16: #define BOOST_RANDOM_NEGATIVE_BINOMIAL_DISTRIBUTION_HPP_INCLUDED Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace random { Chris@16: Chris@16: /** Chris@16: * The negative binomial distribution is an integer valued Chris@16: * distribution with two parameters, @c k and @c p. The Chris@16: * distribution produces non-negative values. Chris@16: * Chris@16: * The distribution function is Chris@16: * \f$\displaystyle P(i) = {k+i-1\choose i}p^k(1-p)^i\f$. Chris@16: * Chris@16: * This implementation uses a gamma-poisson mixture. Chris@16: */ Chris@16: template Chris@16: class negative_binomial_distribution { Chris@16: public: Chris@16: typedef IntType result_type; Chris@16: typedef RealType input_type; Chris@16: Chris@16: class param_type { Chris@16: public: Chris@16: typedef negative_binomial_distribution distribution_type; Chris@16: /** Chris@16: * Construct a param_type object. @c k and @c p Chris@16: * are the parameters of the distribution. Chris@16: * Chris@16: * Requires: k >=0 && 0 <= p <= 1 Chris@16: */ Chris@16: explicit param_type(IntType k_arg = 1, RealType p_arg = RealType (0.5)) Chris@16: : _k(k_arg), _p(p_arg) Chris@16: {} Chris@16: /** Returns the @c k parameter of the distribution. */ Chris@16: IntType k() const { return _k; } Chris@16: /** Returns the @c p parameter of the distribution. */ Chris@16: RealType p() const { return _p; } Chris@16: #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS Chris@16: /** Writes the parameters of the distribution to a @c std::ostream. */ Chris@16: template Chris@16: friend std::basic_ostream& Chris@16: operator<<(std::basic_ostream& os, Chris@16: const param_type& parm) Chris@16: { Chris@16: os << parm._p << " " << parm._k; Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads the parameters of the distribution from a @c std::istream. */ Chris@16: template Chris@16: friend std::basic_istream& Chris@16: operator>>(std::basic_istream& is, param_type& parm) Chris@16: { Chris@16: is >> parm._p >> std::ws >> parm._k; Chris@16: return is; Chris@16: } Chris@16: #endif Chris@16: /** Returns true if the parameters have the same values. */ Chris@16: friend bool operator==(const param_type& lhs, const param_type& rhs) Chris@16: { Chris@16: return lhs._k == rhs._k && lhs._p == rhs._p; Chris@16: } Chris@16: /** Returns true if the parameters have different values. */ Chris@16: friend bool operator!=(const param_type& lhs, const param_type& rhs) Chris@16: { Chris@16: return !(lhs == rhs); Chris@16: } Chris@16: private: Chris@16: IntType _k; Chris@16: RealType _p; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * Construct a @c negative_binomial_distribution object. @c k and @c p Chris@16: * are the parameters of the distribution. Chris@16: * Chris@16: * Requires: k >=0 && 0 <= p <= 1 Chris@16: */ Chris@16: explicit negative_binomial_distribution(IntType k_arg = 1, Chris@16: RealType p_arg = RealType(0.5)) Chris@16: : _k(k_arg), _p(p_arg) Chris@16: {} Chris@16: Chris@16: /** Chris@16: * Construct an @c negative_binomial_distribution object from the Chris@16: * parameters. Chris@16: */ Chris@16: explicit negative_binomial_distribution(const param_type& parm) Chris@16: : _k(parm.k()), _p(parm.p()) Chris@16: {} Chris@16: Chris@16: /** Chris@16: * Returns a random variate distributed according to the Chris@16: * negative binomial distribution. Chris@16: */ Chris@16: template Chris@16: IntType operator()(URNG& urng) const Chris@16: { Chris@16: gamma_distribution gamma(_k, (1-_p)/_p); Chris@16: poisson_distribution poisson(gamma(urng)); Chris@16: return poisson(urng); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns a random variate distributed according to the negative Chris@16: * binomial distribution with parameters specified by @c param. Chris@16: */ Chris@16: template Chris@16: IntType operator()(URNG& urng, const param_type& parm) const Chris@16: { Chris@16: return negative_binomial_distribution(parm)(urng); Chris@16: } Chris@16: Chris@16: /** Returns the @c k parameter of the distribution. */ Chris@16: IntType k() const { return _k; } Chris@16: /** Returns the @c p parameter of the distribution. */ Chris@16: RealType p() const { return _p; } Chris@16: Chris@16: /** Returns the smallest value that the distribution can produce. */ Chris@16: IntType min BOOST_PREVENT_MACRO_SUBSTITUTION() const { return 0; } Chris@16: /** Returns the largest value that the distribution can produce. */ Chris@16: IntType max BOOST_PREVENT_MACRO_SUBSTITUTION() const Chris@16: { return (std::numeric_limits::max)(); } Chris@16: Chris@16: /** Returns the parameters of the distribution. */ Chris@16: param_type param() const { return param_type(_k, _p); } Chris@16: /** Sets parameters of the distribution. */ Chris@16: void param(const param_type& parm) Chris@16: { Chris@16: _k = parm.k(); Chris@16: _p = parm.p(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Effects: Subsequent uses of the distribution do not depend Chris@16: * on values produced by any engine prior to invoking reset. Chris@16: */ Chris@16: void reset() { } Chris@16: Chris@16: #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS Chris@16: /** Writes the parameters of the distribution to a @c std::ostream. */ Chris@16: template Chris@16: friend std::basic_ostream& Chris@16: operator<<(std::basic_ostream& os, Chris@16: const negative_binomial_distribution& bd) Chris@16: { Chris@16: os << bd.param(); Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads the parameters of the distribution from a @c std::istream. */ Chris@16: template Chris@16: friend std::basic_istream& Chris@16: operator>>(std::basic_istream& is, Chris@16: negative_binomial_distribution& bd) Chris@16: { Chris@16: bd.read(is); Chris@16: return is; Chris@16: } Chris@16: #endif Chris@16: Chris@16: /** Returns true if the two distributions will produce the same Chris@16: sequence of values, given equal generators. */ Chris@16: friend bool operator==(const negative_binomial_distribution& lhs, Chris@16: const negative_binomial_distribution& rhs) Chris@16: { Chris@16: return lhs._k == rhs._k && lhs._p == rhs._p; Chris@16: } Chris@16: /** Returns true if the two distributions could produce different Chris@16: sequences of values, given equal generators. */ Chris@16: friend bool operator!=(const negative_binomial_distribution& lhs, Chris@16: const negative_binomial_distribution& rhs) Chris@16: { Chris@16: return !(lhs == rhs); Chris@16: } Chris@16: Chris@16: private: Chris@16: Chris@16: /// @cond \show_private Chris@16: Chris@16: template Chris@16: void read(std::basic_istream& is) { Chris@16: param_type parm; Chris@16: if(is >> parm) { Chris@16: param(parm); Chris@16: } Chris@16: } Chris@16: Chris@16: // parameters Chris@16: IntType _k; Chris@16: RealType _p; Chris@16: Chris@16: /// @endcond Chris@16: }; Chris@16: Chris@16: } Chris@16: Chris@16: } Chris@16: Chris@16: #endif