1#ifndef STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_CORR_CONSTRAIN_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_CORR_CONSTRAIN_HPP
15template <
typename EigVec, require_eigen_col_vector_t<EigVec>* =
nullptr>
16inline Eigen::Matrix<value_type_t<EigVec>, Eigen::Dynamic, Eigen::Dynamic>
22 int k_choose_2 = (K * (K - 1)) / 2;
24 "k_choose_2", k_choose_2);
26 Matrix<T_scalar, Dynamic, Dynamic> x(K, K);
33 for (
int i = 1; i < K; ++i) {
34 x.coeffRef(i, 0) = z.coeff(k++);
35 T_scalar sum_sqs =
square(x.coeff(i, 0));
36 for (
int j = 1; j < i; ++j) {
37 x.coeffRef(i, j) = z.coeff(k++) *
sqrt(1.0 - sum_sqs);
38 sum_sqs +=
square(x.coeff(i, j));
40 x.coeffRef(i, i) =
sqrt(1.0 - sum_sqs);
46template <
typename EigVec, require_eigen_vector_t<EigVec>* =
nullptr>
47inline Eigen::Matrix<value_type_t<EigVec>, Eigen::Dynamic, Eigen::Dynamic>
53 int k_choose_2 = (K * (K - 1)) / 2;
55 "k_choose_2", k_choose_2);
57 Matrix<T_scalar, Dynamic, Dynamic> x(K, K);
64 for (
int i = 1; i < K; ++i) {
65 x.coeffRef(i, 0) = z.coeff(k++);
66 T_scalar sum_sqs =
square(x.coeff(i, 0));
67 for (
int j = 1; j < i; ++j) {
68 lp += 0.5 *
log1m(sum_sqs);
69 x.coeffRef(i, j) = z.coeff(k++) *
sqrt(1.0 - sum_sqs);
70 sum_sqs +=
square(x.coeff(i, j));
72 x.coeffRef(i, i) =
sqrt(1.0 - sum_sqs);
93template <
bool Jacobian,
typename T, require_not_std_vector_t<T>* =
nullptr>
118template <
bool Jacobian,
typename T, require_std_vector_t<T>* =
nullptr>
120 return apply_vector_unary<T>::apply(y, [&lp, K](
auto&& v) {
121 return cholesky_corr_constrain<Jacobian>(v, K, lp);
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
fvar< T > sqrt(const fvar< T > &x)
Eigen::Matrix< value_type_t< EigVec >, Eigen::Dynamic, Eigen::Dynamic > cholesky_corr_constrain(const EigVec &y, int K)
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...
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.
fvar< T > log1m(const fvar< T > &x)
fvar< T > square(const fvar< T > &x)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...