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