1 #ifndef STAN_MATH_PRIM_PROB_SCALED_INV_CHI_SQUARE_RNG_HPP
2 #define STAN_MATH_PRIM_PROB_SCALED_INV_CHI_SQUARE_RNG_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/fun/max_size.hpp>
7 #include <stan/math/prim/fun/scalar_seq_view.hpp>
8 #include <stan/math/prim/fun/to_ref.hpp>
9 #include <boost/random/chi_squared_distribution.hpp>
10 #include <boost/random/variate_generator.hpp>
11 
12 namespace stan {
13 namespace math {
14 
15 /** \ingroup prob_dists
16  * Return a scaled chi square random variate for the given
17  * number of degrees of freedom and scale using the specified random
18  * number generator.
19  *
20  * nu and sigma can each be a scalar or a one-dimensional container. Any
21  * non-scalar inputs must be the same size.
22  *
23  * @tparam T_deg type of degrees of freedom parameter
24  * @tparam T_scale type of scale parameter
25  * @tparam RNG type of random number generator
26  * @param nu (Sequence of) positive degrees of freedom parameter(s)
27  * @param s (Sequence of) positive scale parameter(s)
28  * @param rng random number generator
29  * @return (Sequence of) scaled chi square random variate(s)
30  * @throw std::domain_error if nu or sigma are nonpositive
31  * @throw std::invalid_argument if non-scalar arguments are of different
32  * sizes
33  */
34 template <typename T_deg, typename T_scale, class RNG>
35 inline typename VectorBuilder<true, double, T_deg, T_scale>::type
scaled_inv_chi_square_rng(const T_deg & nu,const T_scale & s,RNG & rng)36 scaled_inv_chi_square_rng(const T_deg& nu, const T_scale& s, RNG& rng) {
37   using boost::variate_generator;
38   using boost::random::chi_squared_distribution;
39   static const char* function = "scaled_inv_chi_square_rng";
40   check_consistent_sizes(function, "Location parameter", nu, "Scale Parameter",
41                          s);
42   const auto& nu_ref = to_ref(nu);
43   const auto& s_ref = to_ref(s);
44   check_positive_finite(function, "Degrees of freedom parameter", nu_ref);
45   check_positive_finite(function, "Scale parameter", s_ref);
46 
47   scalar_seq_view<T_deg> nu_vec(nu_ref);
48   scalar_seq_view<T_scale> s_vec(s_ref);
49   size_t N = max_size(nu, s);
50   VectorBuilder<true, double, T_deg, T_scale> output(N);
51 
52   for (size_t n = 0; n < N; ++n) {
53     variate_generator<RNG&, chi_squared_distribution<> > chi_square_rng(
54         rng, chi_squared_distribution<>(nu_vec[n]));
55     output[n] = nu_vec[n] * s_vec[n] * s_vec[n] / chi_square_rng();
56   }
57 
58   return output.data();
59 }
60 
61 }  // namespace math
62 }  // namespace stan
63 #endif
64