inv_chi_square_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_INV_CHI_SQUARE_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_INV_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  * Return a pseudorandom inverse chi squared variate with the nu
17  * degrees of freedom using the specified random number generator.
18  *
19  * nu can be a scalar or a one-dimensional container.
20  *
21  * @tparam T_deg Type of degrees of freedom parameter
22  * @tparam RNG class of random number generator
23  * @param nu (Sequence of) positive degrees of freedom parameter(s)
24  * @param rng random number generator
25  * @return (Sequence of) inverse chi squared random variate(s)
26  * @throw std::domain_error if nu is nonpositive
27  */
28 template <typename T_deg, class RNG>
30  const T_deg& nu, RNG& rng) {
31  using boost::random::chi_squared_distribution;
32  using boost::variate_generator;
33
34  static const char* function = "inv_chi_square_rng";
35
36  check_positive_finite(function, "Degrees of freedom parameter", nu);
37
38  scalar_seq_view<T_deg> nu_vec(nu);
39  size_t N = length(nu);
41
42  for (size_t n = 0; n < N; ++n) {
43  variate_generator<RNG&, chi_squared_distribution<> > chi_square_rng(
44  rng, chi_squared_distribution<>(nu_vec[n]));
45  output[n] = 1 / chi_square_rng();
46  }
47
48  return output.data();
49 }
50
51 } // namespace math
52 } // namespace stan
53 #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)
VectorBuilder< true, double, T_deg >::type inv_chi_square_rng(const T_deg &nu, RNG &rng)