Automatic Differentiation
 
Loading...
Searching...
No Matches
cov_matrix_constrain_lkj.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_COV_MATRIX_CONSTRAIN_LKJ_HPP
2#define STAN_MATH_PRIM_FUN_COV_MATRIX_CONSTRAIN_LKJ_HPP
3
9
10namespace stan {
11namespace math {
12
33template <typename T, require_eigen_vector_t<T>* = nullptr>
34inline Eigen::Matrix<value_type_t<T>, Eigen::Dynamic, Eigen::Dynamic>
35cov_matrix_constrain_lkj(const T& x, size_t k) {
36 size_t k_choose_2 = (k * (k - 1)) / 2;
37 const auto& x_ref = to_ref(x);
38 return read_cov_matrix(corr_constrain(x_ref.head(k_choose_2)),
39 positive_constrain(x_ref.tail(k)));
40}
41
57template <typename T, require_eigen_vector_t<T>* = nullptr>
58inline Eigen::Matrix<value_type_t<T>, Eigen::Dynamic, Eigen::Dynamic>
59cov_matrix_constrain_lkj(const T& x, size_t k, return_type_t<T>& lp) {
60 size_t k_choose_2 = (k * (k - 1)) / 2;
61 const auto& x_ref = x;
62 return read_cov_matrix(corr_constrain(x_ref.head(k_choose_2)),
63 positive_constrain(x_ref.tail(k)), lp);
64}
65
86template <bool Jacobian, typename T, require_not_std_vector_t<T>* = nullptr>
87inline auto cov_matrix_constrain_lkj(const T& x, size_t k,
88 return_type_t<T>& lp) {
89 if (Jacobian) {
90 return cov_matrix_constrain_lkj(x, k, lp);
91 } else {
92 return cov_matrix_constrain_lkj(x, k);
93 }
94}
95
116template <bool Jacobian, typename T, require_std_vector_t<T>* = nullptr>
117inline auto cov_matrix_constrain_lkj(const T& x, size_t k,
118 return_type_t<T>& lp) {
119 return apply_vector_unary<T>::apply(x, [&lp, k](auto&& v) {
120 return cov_matrix_constrain_lkj<Jacobian>(v, k, lp);
121 });
122}
123
124} // namespace math
125} // namespace stan
126
127#endif
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
Eigen::Matrix< value_type_t< T_CPCs >, Eigen::Dynamic, Eigen::Dynamic > read_cov_matrix(const T_CPCs &CPCs, const T_sds &sds, value_type_t< T_CPCs > &log_prob)
A generally worse alternative to call prior to evaluating the density of an elliptical distribution.
Eigen::Matrix< value_type_t< T >, Eigen::Dynamic, Eigen::Dynamic > cov_matrix_constrain_lkj(const T &x, size_t k)
Return the covariance matrix of the specified dimensionality derived from constraining the specified ...
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
plain_type_t< T > corr_constrain(const T &x)
Return the result of transforming the specified scalar or container of values to have a valid correla...
auto positive_constrain(const T &x)
Return the positive value for the specified unconstrained input.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9