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 int N) {
32 using std::exp;
33 check_greater_or_equal("cholesky_factor_constrain",
34 "num rows (must be greater or equal to num cols)", M,
35 N);
36 check_size_match("cholesky_factor_constrain", "x.size()", x.size(),
37 "((N * (N + 1)) / 2 + (M - N) * N)",
38 ((N * (N + 1)) / 2 + (M - N) * N));
39 arena_t<Eigen::MatrixXd> y_val = Eigen::MatrixXd::Zero(M, N);
40
41 int pos = 0;
42 for (int m = 0; m < N; ++m) {
43 y_val.row(m).head(m) = x.val().segment(pos, m);
44 pos += m;
45 y_val.coeffRef(m, m) = exp(x.val().coeff(pos));
46 pos++;
47 }
48
49 for (int m = N; m < M; ++m) {
50 y_val.row(m) = x.val().segment(pos, N);
51 pos += N;
52 }
53
55
56 reverse_pass_callback([x, M, N, y]() mutable {
57 int pos = x.size();
58 for (int m = M - 1; m >= N; --m) {
59 pos -= N;
60 x.adj().segment(pos, N) += y.adj().row(m);
61 }
62
63 for (int m = N - 1; m >= 0; --m) {
64 pos--;
65 x.adj().coeffRef(pos) += y.adj().coeff(m, m) * y.val().coeff(m, m);
66 pos -= m;
67 x.adj().segment(pos, m) += y.adj().row(m).head(m);
68 }
69 });
70
71 return y;
72}
73
89template <typename T, require_var_vector_t<T>* = nullptr>
91 const T& x, int M, int N, scalar_type_t<T>& lp) {
92 check_size_match("cholesky_factor_constrain", "x.size()", x.size(),
93 "((N * (N + 1)) / 2 + (M - N) * N)",
94 ((N * (N + 1)) / 2 + (M - N) * N));
95 int pos = 0;
96 double lp_val = 0.0;
97 for (int n = 0; n < N; ++n) {
98 pos += n;
99 lp_val += x.val().coeff(pos);
100 pos++;
101 }
102 lp += lp_val;
103
104 reverse_pass_callback([x, N, lp]() mutable {
105 int pos = 0;
106 for (int n = 0; n < N; ++n) {
107 pos += n;
108 x.adj().coeffRef(pos) += lp.adj();
109 pos++;
110 }
111 });
112
113 return cholesky_factor_constrain(x, M, N);
114}
115
116} // namespace math
117} // namespace stan
118
119#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: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 ...