Automatic Differentiation
 
Loading...
Searching...
No Matches
read_cov_L.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_READ_COV_L_HPP
2#define STAN_MATH_REV_FUN_READ_COV_L_HPP
3
11
12namespace stan {
13namespace math {
14
29template <typename T_CPCs, typename T_sds,
30 require_any_var_vector_t<T_CPCs, T_sds>* = nullptr,
31 require_vt_same<T_CPCs, T_sds>* = nullptr>
32inline auto read_cov_L(const T_CPCs& CPCs, const T_sds& sds,
33 scalar_type_t<T_CPCs>& log_prob) {
34 size_t K = sds.rows();
35 // adjust due to transformation from correlations to covariances
36 log_prob += (sum(log(sds.val())) + LOG_TWO) * K;
37
38 auto corr_L = read_corr_L(CPCs, K, log_prob);
40 = sds.val().matrix().asDiagonal() * corr_L.val();
41
42 reverse_pass_callback([sds, corr_L, log_prob, res]() mutable {
43 size_t K = sds.size();
44
45 corr_L.adj() += sds.val().matrix().asDiagonal() * res.adj();
46 sds.adj() += (res.adj().cwiseProduct(corr_L.val())).rowwise().sum();
47
48 sds.adj() += (K * log_prob.adj() / sds.val().array()).matrix();
49 });
50
51 return res;
52}
53
54} // namespace math
55} // namespace stan
56
57#endif
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:15
static constexpr double LOG_TWO
The natural logarithm of 2, .
Definition constants.hpp:80
Eigen::Matrix< value_type_t< T_CPCs >, Eigen::Dynamic, Eigen::Dynamic > read_cov_L(const T_CPCs &CPCs, const T_sds &sds, value_type_t< T_CPCs > &log_prob)
This is the function that should be called prior to evaluating the density of any elliptical distribu...
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
Eigen::Matrix< value_type_t< T >, Eigen::Dynamic, Eigen::Dynamic > read_corr_L(const T &CPCs, size_t K)
Return the Cholesky factor of the correlation matrix of the specified dimensionality corresponding to...
typename scalar_type< T >::type scalar_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...