normal_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_NORMAL_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_NORMAL_RNG_HPP
3 
10 #include <boost/random/normal_distribution.hpp>
11 #include <boost/random/variate_generator.hpp>
12 
13 namespace stan {
14 namespace math {
15 
16 /**
17  * Return a Normal random variate for the given location and scale
18  * using the specified random number generator.
19  *
20  * mu and sigma can each be a scalar or a vector. Any non-scalar
21  * inputs must be the same length.
22  *
23  * @tparam T_loc Type of location parameter
24  * @tparam T_scale Type of scale parameter
25  * @tparam RNG type of random number generator
26  * @param mu (Sequence of) location parameter(s)
27  * @param sigma (Sequence of) positive scale parameter(s)
28  * @param rng random number generator
29  * @return (Sequence of) normal random variate(s)
30  * @throw std::domain_error if mu is infinite or sigma is nonpositive
31  * @throw std::invalid_argument if non-scalar arguments are of different
32  * sizes
33  */
34 template <typename T_loc, typename T_scale, class RNG>
36  const T_loc& mu, const T_scale& sigma, RNG& rng) {
37  using boost::normal_distribution;
38  using boost::variate_generator;
39  static const char* function = "normal_rng";
40 
41  check_finite(function, "Location parameter", mu);
42  check_positive_finite(function, "Scale parameter", sigma);
43  check_consistent_sizes(function, "Location parameter", mu, "Scale Parameter",
44  sigma);
45 
46  scalar_seq_view<T_loc> mu_vec(mu);
47  scalar_seq_view<T_scale> sigma_vec(sigma);
48  size_t N = max_size(mu, sigma);
50 
51  for (size_t n = 0; n < N; ++n) {
52  variate_generator<RNG&, normal_distribution<> > norm_rng(
53  rng, normal_distribution<>(mu_vec[n], sigma_vec[n]));
54  output[n] = norm_rng();
55  }
56 
57  return output.data();
58 }
59 
60 } // namespace math
61 } // namespace stan
62 #endif
ofstream output
void check_finite(const char *function, const char *name, const T_y &y)
VectorBuilder< true, double, T_loc, T_scale >::type normal_rng(const T_loc &mu, const T_scale &sigma, RNG &rng)
Definition: normal_rng.hpp:35
void check_positive_finite(const char *function, const char *name, const T_y &y)
double sigma(TH1F *hist, double percentile)
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)