bernoulli_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BERNOULLI_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BERNOULLI_RNG_HPP
3 
9 #include <boost/random/bernoulli_distribution.hpp>
10 #include <boost/random/variate_generator.hpp>
11 
12 namespace stan {
13 namespace math {
14 
15 /**
16  * Return a Bernoulli random variate with specified chance of success
17  * parameter using the specified random number generator.
18  *
19  * theta can be a scalar or a one-dimensional container.
20  *
21  * @tparam T_theta type of chance of success parameter
22  * @tparam RNG type of random number generator
23  * @param theta (Sequence of) chance of success parameter(s)
24  * @param rng random number generator
25  * @return (Sequence of) Bernoulli random variate(s)
26  * @throw std::domain_error if chance of success parameter is less than zero or
27  * greater than one.
28  */
29 template <typename T_theta, class RNG>
31  const T_theta& theta, RNG& rng) {
32  using boost::bernoulli_distribution;
33  using boost::variate_generator;
34 
35  static const char* function = "bernoulli_rng";
36 
37  check_finite(function, "Probability parameter", theta);
38  check_bounded(function, "Probability parameter", theta, 0.0, 1.0);
39 
40  scalar_seq_view<T_theta> theta_vec(theta);
41  size_t N = length(theta);
43 
44  for (size_t n = 0; n < N; ++n) {
45  variate_generator<RNG&, bernoulli_distribution<> > bernoulli_rng(
46  rng, bernoulli_distribution<>(theta_vec[n]));
47  output[n] = bernoulli_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)
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
VectorBuilder< true, int, T_theta >::type bernoulli_rng(const T_theta &theta, RNG &rng)
size_t length(const std::vector< T > &x)
Definition: length.hpp:10