Automatic Differentiation
 
Loading...
Searching...
No Matches
cov_matrix_constrain_lkj.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_CONSTRAINT_COV_MATRIX_CONSTRAIN_LKJ_HPP
2#define STAN_MATH_REV_CONSTRAINT_COV_MATRIX_CONSTRAIN_LKJ_HPP
3
17#include <utility>
18
19namespace stan {
20namespace math {
21
36template <typename T, require_var_vector_t<T>* = nullptr>
38 size_t k_choose_2 = (k * (k - 1)) / 2;
39 check_size_match("cov_matrix_constrain_lkj", "x.size()", x.size(),
40 "k_choose_2 + k", k_choose_2 + k);
41 auto arena_x = to_arena(std::forward<T>(x));
43 positive_constrain(arena_x.tail(k)),
44 internal::cholesky_corr_factor<internal::CorrOrder::column>(
45 arena_x.head(k_choose_2), k)));
46}
47
62template <typename T, require_var_vector_t<T>* = nullptr>
64 T&& x, size_t k, scalar_type_t<T>& lp) {
65 size_t k_choose_2 = (k * (k - 1)) / 2;
66 check_size_match("cov_matrix_constrain_lkj", "x.size()", x.size(),
67 "k_choose_2 + k", k_choose_2 + k);
68 auto arena_x = to_arena(std::forward<T>(x));
69 auto cpcs = arena_x.head(k_choose_2);
70 auto log_sds = arena_x.tail(k);
72 log_cosh(cpcs));
73 lp += (k + 1.0) * sum(log_sds) + k * LOG_TWO;
75 positive_constrain(log_sds),
76 internal::cholesky_corr_factor<internal::CorrOrder::column>(cpcs, k)));
77}
78
79} // namespace math
80} // namespace stan
81
82#endif
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
arena_t< T > to_arena(const T &a)
Converts given argument into a type that either has any dynamic allocation on AD stack or schedules i...
Definition to_arena.hpp:25
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
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.
typename scalar_type< T >::type scalar_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...