Automatic Differentiation
 
Loading...
Searching...
No Matches
cholesky_factor_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_CONSTRAINT_CHOLESKY_FACTOR_CONSTRAIN_HPP
2#define STAN_MATH_REV_CONSTRAINT_CHOLESKY_FACTOR_CONSTRAIN_HPP
3
10#include <cmath>
11#include <stdexcept>
12#include <vector>
13
14namespace stan {
15namespace math {
16
29template <typename T, require_var_vector_t<T>* = nullptr>
31 using std::exp;
32 check_greater_or_equal("cholesky_factor_constrain",
33 "num rows (must be greater or equal to num cols)", M,
34 N);
35 check_size_match("cholesky_factor_constrain", "x.size()", x.size(),
36 "((N * (N + 1)) / 2 + (M - N) * N)",
37 ((N * (N + 1)) / 2 + (M - N) * N));
38 arena_t<Eigen::MatrixXd> y_val = Eigen::MatrixXd::Zero(M, N);
39
40 int pos = 0;
41 for (int m = 0; m < N; ++m) {
42 y_val.row(m).head(m) = x.val().segment(pos, m);
43 pos += m;
44 y_val.coeffRef(m, m) = exp(x.val().coeff(pos));
45 pos++;
46 }
47
48 for (int m = N; m < M; ++m) {
49 y_val.row(m) = x.val().segment(pos, N);
50 pos += N;
51 }
52
54
55 reverse_pass_callback([x, M, N, y]() mutable {
56 int pos = x.size();
57 for (int m = M - 1; m >= N; --m) {
58 pos -= N;
59 x.adj().segment(pos, N) += y.adj().row(m);
60 }
61
62 for (int m = N - 1; m >= 0; --m) {
63 pos--;
64 x.adj().coeffRef(pos) += y.adj().coeff(m, m) * y.val().coeff(m, m);
65 pos -= m;
66 x.adj().segment(pos, m) += y.adj().row(m).head(m);
67 }
68 });
69
70 return y;
71}
72
88template <typename T, require_var_vector_t<T>* = nullptr>
90 scalar_type_t<T>& lp) {
91 check_size_match("cholesky_factor_constrain", "x.size()", x.size(),
92 "((N * (N + 1)) / 2 + (M - N) * N)",
93 ((N * (N + 1)) / 2 + (M - N) * N));
94 int pos = 0;
95 double lp_val = 0.0;
96 for (int n = 0; n < N; ++n) {
97 pos += n;
98 lp_val += x.val().coeff(pos);
99 pos++;
100 }
101 lp += lp_val;
102
103 reverse_pass_callback([x, N, lp]() mutable {
104 int pos = 0;
105 for (int n = 0; n < N; ++n) {
106 pos += n;
107 x.adj().coeffRef(pos) += lp.adj();
108 pos++;
109 }
110 });
111
112 return cholesky_factor_constrain(x, M, N);
113}
114
115} // namespace math
116} // namespace stan
117
118#endif
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
auto segment(T_x &&x, size_t i, size_t n)
Return the specified number of elements as a row/column vector starting from the specified element - ...
Definition segment.hpp:24
void check_greater_or_equal(const char *function, const char *name, const T_y &y, const T_low &low, Idxs... idxs)
Throw an exception if y is not greater or equal than low.
Eigen::Matrix< value_type_t< T >, Eigen::Dynamic, Eigen::Dynamic > cholesky_factor_constrain(const T &x, int M, int N)
Return the Cholesky factor of the specified size read from the specified vector.
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:13
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 ...