Automatic Differentiation
 
Loading...
Searching...
No Matches
cov_matrix_constrain_lkj.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_COV_MATRIX_CONSTRAIN_LKJ_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_COV_MATRIX_CONSTRAIN_LKJ_HPP
3
16#include <utility>
17
18namespace stan {
19namespace math {
20
35template <typename T, require_eigen_col_vector_t<T>* = nullptr>
36inline Eigen::Matrix<value_type_t<T>, Eigen::Dynamic, Eigen::Dynamic>
37cov_matrix_constrain_lkj(T&& x, size_t k) {
38 size_t k_choose_2 = (k * (k - 1)) / 2;
39 decltype(auto) x_ref = to_ref(std::forward<T>(x));
40 check_size_match("cov_matrix_constrain_lkj", "x.size()", x_ref.size(),
41 "k_choose_2 + k", k_choose_2 + k);
42 auto cpcs = x_ref.head(k_choose_2);
44 to_ref(positive_constrain(x_ref.tail(k))),
45 internal::cholesky_corr_factor<internal::CorrOrder::column>(
46 cpcs, log_cosh(cpcs), k)));
47}
48
63template <typename T, require_eigen_col_vector_t<T>* = nullptr>
64inline Eigen::Matrix<value_type_t<T>, Eigen::Dynamic, Eigen::Dynamic>
66 size_t k_choose_2 = (k * (k - 1)) / 2;
67 decltype(auto) x_ref = to_ref(std::forward<T>(x));
68 check_size_match("cov_matrix_constrain_lkj", "x.size()", x_ref.size(),
69 "k_choose_2 + k", k_choose_2 + k);
70 auto cpcs = x_ref.head(k_choose_2);
71 auto log_sds = x_ref.tail(k);
72 decltype(auto) log_cosh_cpcs = to_ref(log_cosh(cpcs));
74 log_cosh_cpcs);
75 lp += (k + 1.0) * sum(log_sds) + k * LOG_TWO;
77 to_ref(positive_constrain(log_sds)),
78 internal::cholesky_corr_factor<internal::CorrOrder::column>(
79 cpcs, log_cosh_cpcs, k)));
80}
81
102template <bool Jacobian, typename T, require_not_std_vector_t<T>* = nullptr>
103inline auto cov_matrix_constrain_lkj(T&& x, size_t k, return_type_t<T>& lp) {
104 if constexpr (Jacobian) {
105 return cov_matrix_constrain_lkj(std::forward<T>(x), k, lp);
106 } else {
107 return cov_matrix_constrain_lkj(std::forward<T>(x), k);
108 }
109}
110
131template <bool Jacobian, typename T, require_std_vector_t<T>* = nullptr>
132inline auto cov_matrix_constrain_lkj(T&& x, size_t k, return_type_t<T>& lp) {
133 return apply_vector_unary<T>::apply(std::forward<T>(x), [&lp, k](auto&& v) {
134 return cov_matrix_constrain_lkj<Jacobian>(std::forward<decltype(v)>(v), k,
135 lp);
136 });
137}
138
139} // namespace math
140} // namespace stan
141
142#endif
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
auto corr_matrix_log_jacobian_coefficients(Eigen::Index K)
Return a lazy expression of the coefficients c for which the log absolute Jacobian determinant of cor...
Eigen::Matrix< value_type_t< EigMat >, EigMat::RowsAtCompileTime, EigMat::RowsAtCompileTime > multiply_lower_tri_self_transpose(const EigMat &m)
auto diag_pre_multiply(const T1 &m1, const T2 &m2)
Return the product of the diagonal matrix formed from the vector or row_vector and a matrix.
auto positive_constrain(T &&x)
Return the positive value for the specified unconstrained input.
static constexpr double LOG_TWO
The natural logarithm of 2, .
Definition constants.hpp:78
Eigen::Matrix< value_type_t< T >, Eigen::Dynamic, Eigen::Dynamic > cov_matrix_constrain_lkj(T &&x, size_t k)
Return the k x k covariance matrix (diag(sds) L) (diag(sds) L)^T, with L the Cholesky factor of the c...
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
fvar< T > log_cosh(const fvar< T > &x)
Return the natural logarithm of the hyperbolic cosine of the argument.
Definition log_cosh.hpp:21
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
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.
auto dot_product(const T_a &a, const T_b &b)
Returns the dot product of the specified vectors.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...