scaled_inv_chi_square_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_SCALED_INV_CHI_SQUARE_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_SCALED_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 /**
17  * Return a scaled chi square random variate for the given
18  * number of degrees of freedom and scale using the specified random
19  * number generator.
20  *
21  * nu and sigma can each be a scalar or a one-dimensional container. Any
22  * non-scalar inputs must be the same size.
23  *
24  * @tparam T_deg Type of degrees of freedom parameter
25  * @tparam T_scale Type of scale parameter
26  * @tparam RNG type of random number generator
27  * @param nu (Sequence of) positive degrees of freedom parameter(s)
28  * @param s (Sequence of) positive scale parameter(s)
29  * @param rng random number generator
30  * @return (Sequence of) scaled chi square random variate(s)
31  * @throw std::domain_error if nu or sigma are nonpositive
32  * @throw std::invalid_argument if non-scalar arguments are of different
33  * sizes
34  */
35 template <typename T_deg, typename T_scale, class RNG>
37 scaled_inv_chi_square_rng(const T_deg& nu, const T_scale& s, RNG& rng) {
38  using boost::random::chi_squared_distribution;
39  using boost::variate_generator;
40 
41  static const char* function = "scaled_inv_chi_square_rng";
42 
43  check_positive_finite(function, "Degrees of freedom parameter", nu);
44  check_positive_finite(function, "Scale parameter", s);
45  check_consistent_sizes(function, "Location parameter", nu, "Scale Parameter",
46  s);
47 
48  scalar_seq_view<T_deg> nu_vec(nu);
49  scalar_seq_view<T_scale> s_vec(s);
50  size_t N = max_size(nu, s);
52 
53  for (size_t n = 0; n < N; ++n) {
54  variate_generator<RNG&, chi_squared_distribution<> > chi_square_rng(
55  rng, chi_squared_distribution<>(nu_vec[n]));
56  output[n] = nu_vec[n] * s_vec[n] * s_vec[n] / chi_square_rng();
57  }
58 
59  return output.data();
60 }
61 
62 } // namespace math
63 } // namespace stan
64 #endif
ofstream output
VectorBuilder< true, double, T_deg, T_scale >::type scaled_inv_chi_square_rng(const T_deg &nu, const T_scale &s, RNG &rng)
const XML_Char * s
Definition: expat.h:262
void check_positive_finite(const char *function, const char *name, const T_y &y)
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)
VectorBuilder< true, double, T_deg >::type chi_square_rng(const T_deg &nu, RNG &rng)