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
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(T&& x, size_t k) {
36 size_t k_choose_2 = (k * (k - 1)) / 2;
37 auto&& x_ref = to_ref(std::forward<T>(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>
60 size_t k_choose_2 = (k * (k - 1)) / 2;
61 auto&& x_ref = to_ref(std::forward<T>(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(T&& x, size_t k, return_type_t<T>& lp) {
88 if constexpr (Jacobian) {
89 return cov_matrix_constrain_lkj(std::forward<T>(x), k, lp);
90 } else {
91 return cov_matrix_constrain_lkj(std::forward<T>(x), k);
92 }
93}
94
115template <bool Jacobian, typename T, require_std_vector_t<T>* = nullptr>
116inline auto cov_matrix_constrain_lkj(T&& x, size_t k, return_type_t<T>& lp) {
117 return apply_vector_unary<T>::apply(std::forward<T>(x), [&lp, k](auto&& v) {
118 return cov_matrix_constrain_lkj<Jacobian>(std::forward<decltype(v)>(v), k,
119 lp);
120 });
121}
122
123} // namespace math
124} // namespace stan
125
126#endif
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
plain_type_t< T > corr_constrain(T &&x)
Return the result of transforming the specified scalar or container of values to have a valid correla...
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.
auto positive_constrain(T &&x)
Return the positive value for the specified unconstrained input.
Eigen::Matrix< value_type_t< T >, Eigen::Dynamic, Eigen::Dynamic > cov_matrix_constrain_lkj(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:18
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...