Automatic Differentiation
 
Loading...
Searching...
No Matches
lkj_corr_cholesky_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_LKJ_CORR_CHOLESKY_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_LKJ_CORR_CHOLESKY_LPDF_HPP
3
11
12namespace stan {
13namespace math {
14
15// LKJ_Corr(L|eta) [ L Cholesky factor of correlation matrix
16// eta > 0; eta == 1 <-> uniform]
17template <bool propto, typename T_covar, typename T_shape>
19 const T_shape& eta) {
21 static constexpr const char* function = "lkj_corr_cholesky_lpdf";
22 check_positive(function, "Shape parameter", eta);
23
24 const auto& L_ref = to_ref(L);
25 check_cholesky_factor(function, "Random variable", L_ref);
26
27 const unsigned int K = L.rows();
28 if (K == 0) {
29 return 0.0;
30 }
31
32 lp_ret lp(0.0);
33
35 lp += do_lkj_constant(eta, K);
36 }
38 const int Km1 = K - 1;
39 Eigen::Matrix<value_type_t<T_covar>, Eigen::Dynamic, 1> log_diagonals
40 = log(L_ref.diagonal().tail(Km1).array());
41 Eigen::Matrix<lp_ret, Eigen::Dynamic, 1> values(Km1);
42 for (int k = 0; k < Km1; k++) {
43 values(k) = (Km1 - k - 1) * log_diagonals(k);
44 }
45 if (eta == 1.0 && is_constant_all<scalar_type<T_shape>>::value) {
46 lp += sum(values);
47 return (lp);
48 }
49 values += multiply(2.0 * eta - 2.0, log_diagonals);
50 lp += sum(values);
51 }
52
53 return lp;
54}
55
56template <typename T_covar, typename T_shape>
58 const T_covar& L, const T_shape& eta) {
59 return lkj_corr_cholesky_lpdf<false>(L, eta);
60}
61
62} // namespace math
63} // namespace stan
64#endif
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
auto multiply(const Mat1 &m1, const Mat2 &m2)
Return the product of the specified matrices.
Definition multiply.hpp:18
return_type_t< T_covar, T_shape > lkj_corr_cholesky_lpdf(const T_covar &L, const T_shape &eta)
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:22
return_type_t< double, T_shape > do_lkj_constant(const T_shape &eta, const unsigned int &K)
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
void check_cholesky_factor(const char *function, const char *name, const Mat &y)
Throw an exception if the specified matrix is not a valid Cholesky factor.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Metaprogram structure to determine the base scalar type of a template argument.