Automatic Differentiation
 
Loading...
Searching...
No Matches
cholesky_corr_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_CONSTRAINT_CHOLESKY_CORR_CONSTRAIN_HPP
2#define STAN_MATH_REV_CONSTRAINT_CHOLESKY_CORR_CONSTRAIN_HPP
3
14#include <utility>
15
16namespace stan {
17namespace math {
18
19namespace internal {
32template <CorrOrder Order, typename T, require_var_vector_t<T>* = nullptr>
33inline var_value<Eigen::MatrixXd> cholesky_corr_factor(T&& y, Eigen::Index K) {
34 using Eigen::Index;
35 if (K == 0) {
36 return Eigen::MatrixXd();
37 }
38 auto arena_y = to_arena(std::forward<T>(y));
39 arena_t<Eigen::VectorXd> tanh_y(arena_y.size());
40 // derivative of each element with respect to its own unconstrained value
41 arena_t<Eigen::VectorXd> d_own(arena_y.size());
42 arena_t<Eigen::MatrixXd> L_val = Eigen::MatrixXd::Zero(K, K);
43 L_val.coeffRef(0, 0) = 1;
44 for (Index row = 1; row < K; ++row) {
45 // exp(-acc), as the product of sech(y) over the earlier elements of the
46 // row
47 double exp_neg_acc = 1.0;
48 for (Index col = 0; col < row; ++col) {
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;
56 }
57 L_val.coeffRef(row, row) = exp_neg_acc;
58 }
60 reverse_pass_callback([arena_y, L, tanh_y, d_own, K]() mutable {
61 for (Index row = 1; row < K; ++row) {
62 // sum of adj * val over the elements of the row after column col, which
63 // all depend on y_col through the shared exp(-acc)
64 double running_sum = L.adj().coeff(row, row) * L.val().coeff(row, row);
65 for (Index col = row - 1; col >= 0; --col) {
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);
71 }
72 }
73 });
74 return L;
75}
76} // namespace internal
77
89template <typename T, require_var_vector_t<T>* = nullptr>
91 int k_choose_2 = (K * (K - 1)) / 2;
92 check_size_match("cholesky_corr_constrain", "y.size()", y.size(),
93 "k_choose_2", k_choose_2);
94 return internal::cholesky_corr_factor<internal::CorrOrder::row>(
95 std::forward<T>(y), K);
96}
97
110template <typename T, require_var_vector_t<T>* = nullptr>
112 T&& y, int K, scalar_type_t<T>& lp) {
113 int k_choose_2 = (K * (K - 1)) / 2;
114 check_size_match("cholesky_corr_constrain", "y.size()", y.size(),
115 "k_choose_2", k_choose_2);
117 log_cosh(y));
118 return internal::cholesky_corr_factor<internal::CorrOrder::row>(
119 std::forward<T>(y), K);
120}
121
122} // namespace math
123} // namespace stan
124#endif
auto col(T_x &&x, size_t j)
Return the specified column of the specified kernel generator expression using start-at-1 indexing.
Definition col.hpp:23
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...
Definition to_arena.hpp:25
auto sech(T &&x)
Return the hyperbolic secant 1 / cosh(x) of the argument, elementwise for containers.
Definition sech.hpp:22
fvar< T > tanh(const fvar< T > &x)
Return the hyperbolic tangent of the argument.
Definition tanh.hpp:27
fvar< T > log_cosh(const fvar< T > &x)
Return the natural logarithm of the hyperbolic cosine of the argument.
Definition log_cosh.hpp:21
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 ...