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 Eigen::Index K) {
34 using std::exp;
35
36 check_size_match("cov_matrix_constrain", "x.size()", x.size(),
37 "K + (K choose 2)", (K * (K + 1)) / 2);
38 arena_t<Eigen::MatrixXd> L_val = Eigen::MatrixXd::Zero(K, K);
39 int i = 0;
40 for (Eigen::Index m = 0; m < K; ++m) {
41 L_val.row(m).head(m) = x.val().segment(i, m);
42 i += m;
43 L_val.coeffRef(m, m) = exp(x.val().coeff(i));
44 i++;
45 }
46
48
49 reverse_pass_callback([x, L]() mutable {
50 Eigen::Index K = L.rows();
51 int i = x.size();
52 for (int m = K - 1; m >= 0; --m) {
53 i--;
54 x.adj().coeffRef(i) += L.adj().coeff(m, m) * L.val().coeff(m, m);
55 i -= m;
56 x.adj().segment(i, m) += L.adj().row(m).head(m);
57 }
58 });
59
61}
62
77template <typename T, require_var_vector_t<T>* = nullptr>
79 Eigen::Index K,
80 scalar_type_t<T>& lp) {
81 using std::exp;
82 using std::log;
83
84 check_size_match("cov_matrix_constrain", "x.size()", x.size(),
85 "K + (K choose 2)", (K * (K + 1)) / 2);
86 arena_t<Eigen::MatrixXd> L_val = Eigen::MatrixXd::Zero(K, K);
87 int pos = 0;
88 for (Eigen::Index m = 0; m < K; ++m) {
89 L_val.row(m).head(m) = x.val().segment(pos, m);
90 pos += m;
91 L_val.coeffRef(m, m) = exp(x.val().coeff(pos));
92 pos++;
93 }
94
95 // Jacobian for complete transform, including exp() above
96 double lp_val = (K * LOG_TWO);
97 for (Eigen::Index k = 0; k < K; ++k) {
98 // only +1 because index from 0
99 lp_val += (K - k + 1) * log(L_val.coeff(k, k));
100 }
101
102 lp += lp_val;
103
105
106 reverse_pass_callback([x, L, lp]() mutable {
107 Eigen::Index K = L.rows();
108 for (Eigen::Index k = 0; k < K; ++k) {
109 L.adj().coeffRef(k, k) += (K - k + 1) * lp.adj() / L.val().coeff(k, k);
110 }
111 int pos = x.size();
112 for (int m = K - 1; m >= 0; --m) {
113 pos--;
114 x.adj().coeffRef(pos) += L.adj().coeff(m, m) * L.val().coeff(m, m);
115 pos -= m;
116 x.adj().segment(pos, m) += L.adj().row(m).head(m);
117 }
118 });
119
121}
122
123} // namespace math
124} // namespace stan
125
126#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 ...