Automatic Differentiation
 
Loading...
Searching...
No Matches
cov_matrix_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_CONSTRAINT_COV_MATRIX_CONSTRAIN_HPP
2#define STAN_MATH_REV_CONSTRAINT_COV_MATRIX_CONSTRAIN_HPP
3
12#include <cmath>
13
14namespace stan {
15namespace math {
16
31template <typename T, require_var_vector_t<T>* = nullptr>
33 using std::exp;
34
35 check_size_match("cov_matrix_constrain", "x.size()", x.size(),
36 "K + (K choose 2)", (K * (K + 1)) / 2);
37 arena_t<Eigen::MatrixXd> L_val = Eigen::MatrixXd::Zero(K, K);
38 int i = 0;
39 for (Eigen::Index m = 0; m < K; ++m) {
40 L_val.row(m).head(m) = x.val().segment(i, m);
41 i += m;
42 L_val.coeffRef(m, m) = exp(x.val().coeff(i));
43 i++;
44 }
45
47
48 reverse_pass_callback([x, L]() mutable {
49 Eigen::Index K = L.rows();
50 int i = x.size();
51 for (int m = K - 1; m >= 0; --m) {
52 i--;
53 x.adj().coeffRef(i) += L.adj().coeff(m, m) * L.val().coeff(m, m);
54 i -= m;
55 x.adj().segment(i, m) += L.adj().row(m).head(m);
56 }
57 });
58
60}
61
76template <typename T, require_var_vector_t<T>* = nullptr>
78 scalar_type_t<T>& lp) {
79 using std::exp;
80 using std::log;
81
82 check_size_match("cov_matrix_constrain", "x.size()", x.size(),
83 "K + (K choose 2)", (K * (K + 1)) / 2);
84 arena_t<Eigen::MatrixXd> L_val = Eigen::MatrixXd::Zero(K, K);
85 int pos = 0;
86 for (Eigen::Index m = 0; m < K; ++m) {
87 L_val.row(m).head(m) = x.val().segment(pos, m);
88 pos += m;
89 L_val.coeffRef(m, m) = exp(x.val().coeff(pos));
90 pos++;
91 }
92
93 // Jacobian for complete transform, including exp() above
94 double lp_val = (K * LOG_TWO);
95 for (Eigen::Index k = 0; k < K; ++k) {
96 // only +1 because index from 0
97 lp_val += (K - k + 1) * log(L_val.coeff(k, k));
98 }
99
100 lp += lp_val;
101
103
104 reverse_pass_callback([x, L, lp]() mutable {
105 Eigen::Index K = L.rows();
106 for (Eigen::Index k = 0; k < K; ++k) {
107 L.adj().coeffRef(k, k) += (K - k + 1) * lp.adj() / L.val().coeff(k, k);
108 }
109 int pos = x.size();
110 for (int m = K - 1; m >= 0; --m) {
111 pos--;
112 x.adj().coeffRef(pos) += L.adj().coeff(m, m) * L.val().coeff(m, m);
113 pos -= m;
114 x.adj().segment(pos, m) += L.adj().row(m).head(m);
115 }
116 });
117
119}
120
121} // namespace math
122} // namespace stan
123
124#endif
Eigen::Matrix< value_type_t< EigMat >, EigMat::RowsAtCompileTime, EigMat::RowsAtCompileTime > multiply_lower_tri_self_transpose(const EigMat &m)
Eigen::Matrix< value_type_t< T >, Eigen::Dynamic, Eigen::Dynamic > cov_matrix_constrain(const T &x, Eigen::Index K)
Return the symmetric, positive-definite matrix of dimensions K by K resulting from transforming the s...
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
static constexpr double LOG_TWO
The natural logarithm of 2, .
Definition constants.hpp:80
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 > exp(const fvar< T > &x)
Definition exp.hpp:15
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 ...