Automatic Differentiation
 
Loading...
Searching...
No Matches
multi_student_t_cholesky_rng.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_MULTI_STUDENT_T_CHOLESKY_RNG_HPP
2#define STAN_MATH_PRIM_PROB_MULTI_STUDENT_T_CHOLESKY_RNG_HPP
3
11#include <boost/random/normal_distribution.hpp>
12#include <boost/random/variate_generator.hpp>
13#include <cmath>
14
15namespace stan {
16namespace math {
17
40template <typename T_loc, class RNG>
42multi_student_t_cholesky_rng(double nu, const T_loc& mu,
43 const Eigen::MatrixXd& L, RNG& rng) {
44 using boost::normal_distribution;
45 using boost::variate_generator;
46 using boost::random::gamma_distribution;
47
48 static constexpr const char* function = "multi_student_t_cholesky_rng";
49 check_not_nan(function, "Degrees of freedom parameter", nu);
50 check_positive(function, "Degrees of freedom parameter", nu);
51 check_positive(function, "Scale matrix rows", L.rows());
52 vector_seq_view<T_loc> mu_vec(mu);
53
54 size_t N = size_mvt(mu);
55 for (size_t i = 1; i < N; i++) {
56 check_size_match(function,
57 "Size of one of the vectors of "
58 "the location variable",
59 mu_vec[i].size(),
60 "Size of the first vector of the "
61 "location variable",
62 mu_vec[i - 1].size());
63 }
64
65 check_size_match(function, "Size of random variable", mu_vec[0].size(),
66 "rows of scale parameter", L.rows());
67
68 for (size_t i = 0; i < N; i++) {
69 check_finite(function, "Location parameter", mu_vec[i]);
70 }
71 const auto& L_ref = to_ref(L);
72 check_cholesky_factor(function, "L matrix", L_ref);
73
75
76 variate_generator<RNG&, normal_distribution<> > std_normal_rng(
77 rng, normal_distribution<>(0, 1));
78
79 double w = inv_gamma_rng(nu / 2, nu / 2, rng);
80 for (size_t n = 0; n < N; ++n) {
81 Eigen::VectorXd z(L.cols());
82 for (int i = 0; i < L.cols(); i++) {
83 z(i) = std_normal_rng();
84 }
85 z *= std::sqrt(w);
86 output[n] = as_column_vector_or_scalar(mu_vec[n]) + L * z;
87 }
88
89 return output.data();
90}
91
92} // namespace math
93} // namespace stan
94#endif
typename helper::type type
StdVectorBuilder allocates type T1 values to be used as intermediate values.
This class provides a low-cost wrapper for situations where you either need an Eigen Vector or RowVec...
StdVectorBuilder< true, Eigen::VectorXd, T_loc >::type multi_student_t_cholesky_rng(double nu, const T_loc &mu, const Eigen::MatrixXd &L, RNG &rng)
Return a multivariate student-t random variate with the given degrees of freedom location and Cholesk...
auto as_column_vector_or_scalar(T &&a)
as_column_vector_or_scalar of a kernel generator expression.
VectorBuilder< true, double, T_shape, T_scale >::type inv_gamma_rng(const T_shape &alpha, const T_scale &beta, RNG &rng)
Return a pseudorandom inverse gamma variate for the given shape and scale parameters using the specif...
double std_normal_rng(RNG &rng)
Return a standard Normal random variate using the specified random number generator.
size_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:18
size_t size_mvt(const ScalarT &)
Provides the size of a multivariate argument.
Definition size_mvt.hpp:24
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
void check_cholesky_factor(const char *function, const char *name, const Mat &y)
Throw an exception if the specified matrix is not a valid Cholesky factor.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9