Automatic Differentiation
 
Loading...
Searching...
No Matches
lkj_corr_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_LKJ_CORR_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_LKJ_CORR_LPDF_HPP
3
11
12namespace stan {
13namespace math {
14
15template <typename T_shape>
17 const unsigned int& K) {
18 // Lewandowski, Kurowicka, and Joe (2009) theorem 5
20 const int Km1 = K - 1;
22 if (eta == 1.0) {
23 // C++ integer division is appropriate in this block
24 Eigen::VectorXd denominator(Km1 / 2);
25 for (int k = 1; k <= denominator.rows(); k++) {
26 denominator(k - 1) = lgamma(2.0 * k);
27 }
28 constant = -denominator.sum();
29 if ((K % 2) == 1) {
30 constant -= 0.25 * (K * K - 1) * LOG_PI - 0.25 * (Km1 * Km1) * LOG_TWO
31 - Km1 * lgamma(0.5 * (K + 1));
32 } else {
33 constant -= 0.25 * K * (K - 2) * LOG_PI
34 + 0.25 * (3 * K * K - 4 * K) * LOG_TWO + K * lgamma(0.5 * K)
35 - Km1 * lgamma(static_cast<double>(K));
36 }
37 } else {
38 constant = Km1 * lgamma(eta + 0.5 * Km1);
39 for (int k = 1; k <= Km1; k++) {
40 constant -= 0.5 * k * LOG_PI + lgamma(eta + 0.5 * (Km1 - k));
41 }
42 }
43 return constant;
44}
45
46// LKJ_Corr(y|eta) [ y correlation matrix (not covariance matrix)
47// eta > 0; eta == 1 <-> uniform]
48template <bool propto, typename T_y, typename T_shape>
49return_type_t<T_y, T_shape> lkj_corr_lpdf(const T_y& y, const T_shape& eta) {
50 static constexpr const char* function = "lkj_corr_lpdf";
51
53 const auto& y_ref = to_ref(y);
54 check_positive(function, "Shape parameter", eta);
55 check_corr_matrix(function, "Correlation matrix", y_ref);
56
57 const unsigned int K = y.rows();
58 if (K == 0) {
59 return 0.0;
60 }
61
63 lp += do_lkj_constant(eta, K);
64 }
65
66 if (eta == 1.0 && is_constant_all<scalar_type<T_shape>>::value) {
67 return lp;
68 }
69
71 return lp;
72 }
73
74 value_type_t<T_y> sum_values = sum(log(y_ref.ldlt().vectorD()));
75 lp += (eta - 1.0) * sum_values;
76 return lp;
77}
78
79template <typename T_y, typename T_shape>
81 const T_shape& eta) {
82 return lkj_corr_lpdf<false>(y, eta);
83}
84
85} // namespace math
86} // namespace stan
87#endif
auto constant(const T a, int rows, int cols)
Matrix of repeated values in kernel generator expressions.
Definition constant.hpp:130
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
void check_corr_matrix(const char *function, const char *name, const Mat &y)
Throw an exception if the specified matrix is not a valid correlation matrix.
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
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
static constexpr double LOG_PI
The natural logarithm of , .
Definition constants.hpp:86
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition lgamma.hpp:21
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
return_type_t< T_y, T_shape > lkj_corr_lpdf(const T_y &y, const T_shape &eta)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
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.