rayleigh_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_RAYLEIGH_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_RAYLEIGH_RNG_HPP
3 
8 #include <boost/random/uniform_real_distribution.hpp>
9 #include <boost/random/variate_generator.hpp>
10 
11 namespace stan {
12 namespace math {
13 
14 /**
15  * Return a Rayleigh random variate with scale parameter sigma
16  * using the specified random number generator.
17  *
18  * sigma can be a scalar or a one-dimensional container.
19  *
20  * @tparam T_scale Type of scale parameter
21  * @tparam RNG class of random number generator
22  * @param sigma (Sequence of) positive scale parameter(s)
23  * @param rng random number generator
24  * @return (Sequence of) Rayleigh random variate(s)
25  * @throw std::domain_error if sigma is nonpositive
26  */
27 template <typename T_scale, class RNG>
29  const T_scale& sigma, RNG& rng) {
30  using boost::random::uniform_real_distribution;
31  using boost::variate_generator;
32 
33  static const char* function = "rayleigh_rng";
34 
35  check_positive_finite(function, "Scale parameter", sigma);
36 
37  scalar_seq_view<T_scale> sigma_vec(sigma);
38  size_t N = length(sigma);
40 
41  variate_generator<RNG&, uniform_real_distribution<> > uniform_rng(
42  rng, uniform_real_distribution<>(0.0, 1.0));
43  for (size_t n = 0; n < N; ++n) {
44  output[n] = sigma_vec[n] * std::sqrt(-2.0 * std::log(uniform_rng()));
45  }
46 
47  return output.data();
48 }
49 
50 } // namespace math
51 } // namespace stan
52 #endif
ofstream output
T sqrt(T number)
Definition: d0nt_math.hpp:156
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
VectorBuilder< true, double, T_alpha, T_beta >::type uniform_rng(const T_alpha &alpha, const T_beta &beta, RNG &rng)
Definition: uniform_rng.hpp:37
void check_positive_finite(const char *function, const char *name, const T_y &y)
VectorBuilder< true, double, T_scale >::type rayleigh_rng(const T_scale &sigma, RNG &rng)
double sigma(TH1F *hist, double percentile)