1 #ifndef STAN_MATH_PRIM_PROB_MULTI_STUDENT_T_RNG_HPP
2 #define STAN_MATH_PRIM_PROB_MULTI_STUDENT_T_RNG_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/fun/as_column_vector_or_scalar.hpp>
7 #include <stan/math/prim/fun/size_mvt.hpp>
8 #include <stan/math/prim/fun/to_ref.hpp>
9 #include <stan/math/prim/fun/vector_seq_view.hpp>
10 #include <boost/random/normal_distribution.hpp>
11 #include <boost/random/gamma_distribution.hpp>
12 #include <boost/random/variate_generator.hpp>
13 #include <cmath>
14 
15 namespace stan {
16 namespace math {
17 
18 /** \ingroup multivar_dists
19  * Return a multivariate student-t random variate with the given degrees of
20  * freedom location and covariance using the specified random number generator.
21  *
22  * mu can be either an Eigen::VectorXd, an Eigen::RowVectorXd, or a std::vector
23  * of either of those types.
24  *
25  * @tparam T_loc Type of location parameter
26  * @tparam RNG Type of pseudo-random number generator
27  * @param nu degrees of freedom parameter
28  * @param mu (Sequence of) location parameter(s)
29  * @param S Covariance matrix
30  * @param rng random number generator
31  * @throw std::domain_error if S is not positive definite, any value in mu is
32  *    not finite, nu is not positive, or nu is NaN
33  * @throw std::invalid_argument if the length of (each) mu is not equal to the
34  *    number of rows and columns in S
35  */
36 template <typename T_loc, class RNG>
37 inline typename StdVectorBuilder<true, Eigen::VectorXd, T_loc>::type
multi_student_t_rng(double nu,const T_loc & mu,const Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> & S,RNG & rng)38 multi_student_t_rng(
39     double nu, const T_loc& mu,
40     const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>& S, RNG& rng) {
41   using boost::normal_distribution;
42   using boost::variate_generator;
43   using boost::random::gamma_distribution;
44 
45   static const char* function = "multi_student_t_rng";
46   check_not_nan(function, "Degrees of freedom parameter", nu);
47   check_positive(function, "Degrees of freedom parameter", nu);
48   check_positive(function, "Covariance matrix rows", S.rows());
49   vector_seq_view<T_loc> mu_vec(mu);
50   size_t size_mu = mu_vec[0].size();
51 
52   size_t N = size_mvt(mu);
53   for (size_t i = 1; i < N; i++) {
54     check_size_match(function,
55                      "Size of one of the vectors of "
56                      "the location variable",
57                      mu_vec[i].size(),
58                      "Size of the first vector of the "
59                      "location variable",
60                      size_mu);
61   }
62 
63   for (size_t i = 0; i < N; i++) {
64     check_finite(function, "Location parameter", mu_vec[i]);
65   }
66   const auto& S_ref = to_ref(S);
67   check_not_nan(function, "Covariance matrix", S_ref);
68   check_symmetric(function, "Covariance matrix", S_ref);
69   Eigen::LLT<Eigen::MatrixXd> llt_of_S = S_ref.llt();
70   check_pos_definite(function, "covariance matrix argument", llt_of_S);
71 
72   StdVectorBuilder<true, Eigen::VectorXd, T_loc> output(N);
73 
74   variate_generator<RNG&, normal_distribution<> > std_normal_rng(
75       rng, normal_distribution<>(0, 1));
76   variate_generator<RNG&, gamma_distribution<> > gamma_rng(
77       rng, gamma_distribution<>(nu / 2.0, 2.0 / nu));
78 
79   double w = 1.0 / gamma_rng();
80   for (size_t n = 0; n < N; ++n) {
81     Eigen::VectorXd z(S.cols());
82     for (int i = 0; i < S.cols(); i++) {
83       z(i) = std::sqrt(w) * std_normal_rng();
84     }
85 
86     output[n] = as_column_vector_or_scalar(mu_vec[n]) + llt_of_S.matrixL() * z;
87   }
88 
89   return output.data();
90 }
91 
92 }  // namespace math
93 }  // namespace stan
94 #endif
95