poisson_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_POISSON_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_POISSON_RNG_HPP
3 
11 #include <boost/random/poisson_distribution.hpp>
12 #include <boost/random/variate_generator.hpp>
13 
14 namespace stan {
15 namespace math {
16 
17 /**
18  * Return a Poisson random variate with specified rate parameter
19  * using the given random number generator.
20  *
21  * lambda can be a scalar or a one-dimensional container.
22  *
23  * @tparam T_rate type of rate parameter
24  * @tparam RNG type of random number generator
25  * @param lambda (Sequence of) rate parameter(s)
26  * @param rng random number generator
27  * @return (Sequence of) Poisson random variate(s)
28  * @throw std::domain_error if lambda is nonpositive
29  */
30 template <typename T_rate, class RNG>
32  const T_rate& lambda, RNG& rng) {
33  using boost::random::poisson_distribution;
34  using boost::variate_generator;
35 
36  static const char* function = "poisson_rng";
37 
38  check_not_nan(function, "Rate parameter", lambda);
39  check_positive(function, "Rate parameter", lambda);
40  check_less(function, "Rate parameter", lambda, POISSON_MAX_RATE);
41 
42  scalar_seq_view<T_rate> lambda_vec(lambda);
43  size_t N = length(lambda);
45 
46  for (size_t n = 0; n < N; ++n) {
47  variate_generator<RNG&, poisson_distribution<> > poisson_rng(
48  rng, poisson_distribution<>(lambda_vec[n]));
49  output[n] = poisson_rng();
50  }
51 
52  return output.data();
53 }
54 
55 } // namespace math
56 } // namespace stan
57 #endif
ofstream output
VectorBuilder< true, int, T_rate >::type poisson_rng(const T_rate &lambda, RNG &rng)
Definition: poisson_rng.hpp:31
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
void check_not_nan(const char *function, const char *name, const T_y &y)
const double POISSON_MAX_RATE
Definition: constants.hpp:68
void check_positive(const char *function, const char *name, const T_y &y)
void check_less(const char *function, const char *name, const T_y &y, const T_high &high)
Definition: check_less.hpp:72