chi_square_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_CHI_SQUARE_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_CHI_SQUARE_RNG_HPP
3
10 #include <boost/random/chi_squared_distribution.hpp>
11 #include <boost/random/variate_generator.hpp>
12
13 namespace stan {
14 namespace math {
15
16 /**
17  * Return a chi squared random variate with nu degrees of
18  * freedom using the specified random number generator.
19  *
20  * nu can be a scalar or a one-dimensional container.
21  *
22  * @tparam T_deg Type of degrees of freedom parameter
23  * @tparam RNG class of random number generator
24  * @param nu (Sequence of) positive degrees of freedom parameter(s)
25  * @param rng random number generator
26  * @return (Sequence of) chi squared random variate(s)
27  * @throw std::domain_error if nu is nonpositive
28  */
29 template <typename T_deg, class RNG>
31  const T_deg& nu, RNG& rng) {
32  using boost::random::chi_squared_distribution;
33  using boost::variate_generator;
34
35  static const char* function = "chi_square_rng";
36
37  check_positive_finite(function, "Degrees of freedom parameter", nu);
38
39  scalar_seq_view<T_deg> nu_vec(nu);
40  size_t N = length(nu);
42
43  for (size_t n = 0; n < N; ++n) {
44  variate_generator<RNG&, chi_squared_distribution<> > chi_square_rng(
45  rng, chi_squared_distribution<>(nu_vec[n]));
46  output[n] = chi_square_rng();
47  }
48
49  return output.data();
50 }
51
52 } // namespace math
53 } // namespace stan
54 #endif
ofstream output
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
void check_positive_finite(const char *function, const char *name, const T_y &y)
VectorBuilder< true, double, T_deg >::type chi_square_rng(const T_deg &nu, RNG &rng)