1#ifndef STAN_MATH_REV_CONSTRAINT_CHOLESKY_CORR_CONSTRAIN_HPP
2#define STAN_MATH_REV_CONSTRAINT_CHOLESKY_CORR_CONSTRAIN_HPP
32template <CorrOrder Order,
typename T, require_var_vector_t<T>* =
nullptr>
36 return Eigen::MatrixXd();
38 auto arena_y =
to_arena(std::forward<T>(y));
43 L_val.coeffRef(0, 0) = 1;
47 double exp_neg_acc = 1.0;
49 const Index pos = cholesky_corr_position<Order>(
row,
col, K);
50 const double y_val = arena_y.val().coeff(pos);
51 const double sech_val =
sech(y_val);
52 tanh_y.coeffRef(pos) =
tanh(y_val);
53 d_own.coeffRef(pos) = sech_val * sech_val * exp_neg_acc;
54 L_val.coeffRef(
row,
col) = tanh_y.coeff(pos) * exp_neg_acc;
55 exp_neg_acc *= sech_val;
57 L_val.coeffRef(
row,
row) = exp_neg_acc;
64 double running_sum = L.adj().coeff(
row,
row) * L.val().coeff(
row,
row);
66 const Index pos = cholesky_corr_position<Order>(
row,
col, K);
67 const double adj_rc = L.adj().coeff(
row,
col);
68 arena_y.adj().coeffRef(pos)
69 += adj_rc * d_own.coeff(pos) - tanh_y.coeff(pos) * running_sum;
70 running_sum += adj_rc * L.val().coeff(
row,
col);
89template <
typename T, require_var_vector_t<T>* =
nullptr>
91 int k_choose_2 = (K * (K - 1)) / 2;
93 "k_choose_2", k_choose_2);
94 return internal::cholesky_corr_factor<internal::CorrOrder::row>(
95 std::forward<T>(y), K);
110template <
typename T, require_var_vector_t<T>* =
nullptr>
113 int k_choose_2 = (K * (K - 1)) / 2;
115 "k_choose_2", k_choose_2);
118 return internal::cholesky_corr_factor<internal::CorrOrder::row>(
119 std::forward<T>(y), K);
auto col(T_x &&x, size_t j)
Return the specified column of the specified kernel generator expression using start-at-1 indexing.
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::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 ...
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
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...
auto sech(T &&x)
Return the hyperbolic secant 1 / cosh(x) of the argument, elementwise for containers.
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.
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
typename internal::arena_type_impl< std::decay_t< T > >::type arena_t
Determines a type that can be used in place of T that does any dynamic allocations on the AD stack.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...