1#ifndef STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_CORR_CONSTRAIN_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_CORR_CONSTRAIN_HPP
37template <CorrOrder Order>
61 const Eigen::Index k_choose_2 = (K * (K - 1)) / 2;
64 const auto row = (0.5 * (1.0 + (1.0 + 8.0 * pos).
sqrt())).floor();
65 return (
row + 1.0 - (pos - 0.5 *
row * (
row - 1.0))).matrix();
67 Eigen::ArrayXd::LinSpaced(k_choose_2, 0.0, k_choose_2 - 1.0));
85 const Eigen::Index k_choose_2 = (K * (K - 1)) / 2;
86 const double two_K_minus_one = 2.0 * K - 1.0;
88 [K =
static_cast<double>(K), two_K_minus_one](
auto&& pos) {
92 - (two_K_minus_one * two_K_minus_one - 8.0 * pos).
sqrt()))
96 Eigen::ArrayXd::LinSpaced(k_choose_2, 0.0, k_choose_2 - 1.0));
114template <
CorrOrder Order,
typename EigVec,
typename LogCoshVec,
116inline Eigen::Matrix<value_type_t<EigVec>, Eigen::Dynamic, Eigen::Dynamic>
119 Eigen::Matrix<T_scalar, Eigen::Dynamic, Eigen::Dynamic> L
120 = Eigen::Matrix<T_scalar, Eigen::Dynamic, Eigen::Dynamic>::Zero(K, K);
124 decltype(
auto) y_ref =
to_ref(std::forward<EigVec>(y));
125 decltype(
auto) log_cosh_y_ref =
to_ref(std::forward<LogCoshVec>(log_cosh_y));
126 L.coeffRef(0, 0) = 1;
127 for (Eigen::Index
row = 1;
row < K; ++
row) {
130 const Eigen::Index pos = cholesky_corr_position<Order>(
row,
col, K);
132 acc += log_cosh_y_ref.coeff(pos);
150template <
typename EigVec, require_eigen_col_vector_t<EigVec>* =
nullptr>
151inline Eigen::Matrix<value_type_t<EigVec>, Eigen::Dynamic, Eigen::Dynamic>
153 int k_choose_2 = (K * (K - 1)) / 2;
155 "k_choose_2", k_choose_2);
156 decltype(
auto) y_ref =
to_ref(std::forward<EigVec>(y));
157 return internal::cholesky_corr_factor<internal::CorrOrder::row>(
173template <
typename EigVec,
typename Lp,
176inline Eigen::Matrix<value_type_t<EigVec>, Eigen::Dynamic, Eigen::Dynamic>
178 int k_choose_2 = (K * (K - 1)) / 2;
180 "k_choose_2", k_choose_2);
181 decltype(
auto) y_ref =
to_ref(std::forward<EigVec>(y));
185 return internal::cholesky_corr_factor<internal::CorrOrder::row>(
186 y_ref, std::move(log_cosh_y), K);
199template <
typename T, require_std_vector_t<T>* =
nullptr>
219template <
typename T,
typename Lp, require_std_vector_t<T>* =
nullptr,
220 require_convertible_t<return_type_t<T>, Lp>* =
nullptr>
245template <
bool Jacobian,
typename T,
typename Lp,
248 if constexpr (Jacobian) {
require_t< std::is_convertible< std::decay_t< T >, std::decay_t< S > > > require_convertible_t
Require types T and S satisfies std::is_convertible.
require_t< is_eigen_col_vector< std::decay_t< T > > > require_eigen_col_vector_t
Require type satisfies is_eigen_col_vector.
require_all_t< is_eigen_vector< std::decay_t< Types > >... > require_all_eigen_vector_t
Require all of the types satisfy is_eigen_vector.
auto col(T_x &&x, size_t j)
Return the specified column of the specified kernel generator expression using start-at-1 indexing.
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
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...
CorrOrder
Order of the partial correlations in the unconstrained vector: row for cholesky_corr_constrain (row 1...
auto cholesky_corr_log_jacobian_coefficients(Eigen::Index K)
Return a lazy expression of the coefficients c for which the log absolute Jacobian determinant of cho...
Eigen::Index cholesky_corr_position(Eigen::Index row, Eigen::Index col, Eigen::Index K)
Return the position in the unconstrained vector of the partial correlation for element (row,...
Eigen::Matrix< value_type_t< EigVec >, Eigen::Dynamic, Eigen::Dynamic > cholesky_corr_factor(EigVec &&y, LogCoshVec &&log_cosh_y, Eigen::Index K)
Return the Cholesky factor of a correlation matrix from unconstrained values y, whose partial correla...
Eigen::Matrix< value_type_t< EigVec >, Eigen::Dynamic, Eigen::Dynamic > cholesky_corr_constrain(EigVec &&y, int K)
Return the Cholesky factor of a K x K correlation matrix from the K (K - 1) / 2 unconstrained values ...
auto make_holder(F &&func, Args &&... args)
Calls given function with given arguments.
fvar< T > sqrt(const fvar< T > &x)
fvar< T > floor(const fvar< T > &x)
fvar< T > tanh(const fvar< T > &x)
Return the hyperbolic tangent of the argument.
fvar< T > log_cosh(const fvar< T > &x)
Return the natural logarithm of the hyperbolic cosine of the argument.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
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.
fvar< T > exp(const fvar< T > &x)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...