Stan Math Library  2.20.0
reverse mode automatic differentiation
uniform_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_UNIFORM_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_UNIFORM_RNG_HPP
3 
5 #include <boost/random/uniform_real_distribution.hpp>
6 #include <boost/random/variate_generator.hpp>
13 
14 namespace stan {
15 namespace math {
16 
35 template <typename T_alpha, typename T_beta, class RNG>
37  const T_alpha& alpha, const T_beta& beta, RNG& rng) {
38  using boost::random::uniform_real_distribution;
39  using boost::variate_generator;
40 
41  static const char* function = "uniform_rng";
42 
43  check_finite(function, "Lower bound parameter", alpha);
44  check_finite(function, "Upper bound parameter", beta);
45  check_consistent_sizes(function, "Lower bound parameter", alpha,
46  "Upper bound parameter", beta);
47  check_greater(function, "Upper bound parameter", beta, alpha);
48 
49  scalar_seq_view<T_alpha> alpha_vec(alpha);
50  scalar_seq_view<T_beta> beta_vec(beta);
51  size_t N = max_size(alpha, beta);
53 
54  variate_generator<RNG&, uniform_real_distribution<> > uniform_rng(
55  rng, uniform_real_distribution<>(0.0, 1.0));
56  for (size_t n = 0; n < N; ++n) {
57  output[n] = (beta_vec[n] - alpha_vec[n]) * uniform_rng() + alpha_vec[n];
58  }
59 
60  return output.data();
61 }
62 
63 } // namespace math
64 } // namespace stan
65 #endif
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
VectorBuilder< true, double, T_alpha, T_beta >::type uniform_rng(const T_alpha &alpha, const T_beta &beta, RNG &rng)
Return a uniform random variate for the given upper and lower bounds using the specified random numbe...
Definition: uniform_rng.hpp:36
fvar< T > beta(const fvar< T > &x1, const fvar< T > &x2)
Return fvar with the beta function applied to the specified arguments and its gradient.
Definition: beta.hpp:51
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
VectorBuilder allocates type T1 values to be used as intermediate values.
void check_greater(const char *function, const char *name, const T_y &y, const T_low &low)
Check if y is strictly greater than low.
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.

     [ Stan Home Page ] © 2011–2018, Stan Development Team.