Automatic Differentiation
 
Loading...
Searching...
No Matches
cov_matrix_free_lkj.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_COV_MATRIX_FREE_LKJ_HPP
2#define STAN_MATH_PRIM_FUN_COV_MATRIX_FREE_LKJ_HPP
3
8
9namespace stan {
10namespace math {
11
30template <typename T, require_eigen_t<T>* = nullptr>
31Eigen::Matrix<value_type_t<T>, Eigen::Dynamic, 1> cov_matrix_free_lkj(
32 const T& y) {
33 using Eigen::Array;
34 using Eigen::Dynamic;
35 using Eigen::Matrix;
36 using T_scalar = value_type_t<T>;
37
38 check_nonzero_size("cov_matrix_free_lkj", "y", y);
39 check_square("cov_matrix_free_lkj", "y", y);
40 Eigen::Index k = y.rows();
41 Eigen::Index k_choose_2 = (k * (k - 1)) / 2;
42 Matrix<T_scalar, Dynamic, 1> x(k_choose_2 + k);
43 bool successful
44 = factor_cov_matrix(y, x.head(k_choose_2).array(), x.tail(k).array());
45 if (!successful) {
46 throw_domain_error("cov_matrix_free_lkj", "factor_cov_matrix failed on y",
47 "", "");
48 }
49 return x;
50}
51
59template <typename T, require_std_vector_t<T>* = nullptr>
60auto cov_matrix_free_lkj(const T& x) {
62 x, [](auto&& v) { return cov_matrix_free_lkj(v); });
63}
64
65} // namespace math
66} // namespace stan
67
68#endif
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
void throw_domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
void check_nonzero_size(const char *function, const char *name, const T_y &y)
Check if the specified matrix/vector is of non-zero size.
Eigen::Matrix< value_type_t< T >, Eigen::Dynamic, 1 > cov_matrix_free_lkj(const T &y)
Return the vector of unconstrained partial correlations and deviations that transform to the specifie...
bool factor_cov_matrix(const T_Sigma &Sigma, T_CPCs &&CPCs, T_sds &&sds)
This function is intended to make starting values, given a covariance matrix Sigma.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9