1 #ifndef __STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__CONTINUOUS__LKJ_CORR_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__CONTINUOUS__LKJ_CORR_HPP__
16 using Eigen::NumericalIssue;
18 template <
typename T_shape>
25 double constant = 0.0;
26 double beta_arg = 0.0;
27 for (
unsigned int k = 1; k < K; k++) {
28 beta_arg = 0.5 * (k + 1.0);
29 constant += k * (2.0 *
lgamma(beta_arg) -
lgamma(2.0 * beta_arg));
30 sum +=
pow(
static_cast<double>(k),2.0);
32 constant +=
sum * LOG_TWO;
36 T_shape constant = 0.0;
38 for (
unsigned int k = 1; k < K; k++) {
39 unsigned int diff = K - k;
40 beta_arg = eta + 0.5 * (diff - 1);
41 constant += diff * (2.0 *
lgamma(beta_arg) -
lgamma(2.0 * beta_arg));
42 sum += (2.0 * eta - 2.0 + diff) * diff;
44 constant +=
sum * LOG_TWO;
50 template <
bool propto,
51 typename T_covar,
typename T_shape,
53 typename boost::math::tools::promote_args<T_covar, T_shape>::type
55 const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& L,
58 static const char*
function
59 =
"stan::prob::lkj_corr_cholesky_log(%1%)";
61 using boost::math::tools::promote_args;
64 typename promote_args<T_covar,T_shape>::type lp(0.0);
65 if (!
check_positive(
function, eta,
"Shape parameter", &lp, Policy()))
68 const unsigned int K = L.rows();
78 lp += (eta - 1.0) * 2.0 * L.diagonal().array().log().sum();
84 template <
bool propto,
85 typename T_covar,
typename T_shape>
87 typename boost::math::tools::promote_args<T_covar, T_shape>::type
89 const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& L,
95 template <
typename T_covar,
typename T_shape,
98 typename boost::math::tools::promote_args<T_covar, T_shape>::type
100 const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& L,
103 return lkj_corr_cholesky_log<false>(L,eta,Policy());
106 template <
typename T_covar,
typename T_shape>
108 typename boost::math::tools::promote_args<T_covar, T_shape>::type
110 const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& L,
111 const T_shape& eta) {
119 template <
bool propto,
120 typename T_y,
typename T_shape,
122 typename boost::math::tools::promote_args<T_y, T_shape>::type
123 lkj_corr_log(
const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
126 static const char*
function =
"stan::prob::lkj_corr_log(%1%)";
132 using boost::math::tools::promote_args;
134 typename promote_args<T_y,T_shape>::type lp;
135 if (!
check_positive(
function, eta,
"Shape parameter", &lp, Policy()))
139 if (!
check_not_nan(
function, y,
"Correlation matrix", &lp, Policy()))
145 const unsigned int K = y.rows();
149 LLT< Matrix<T_y, Dynamic, Dynamic> > Cholesky = y.llt();
151 if (Cholesky.info() == Eigen::NumericalIssue)
154 Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic> L = Cholesky.matrixL();
155 return lkj_corr_cholesky_log<propto>(L, eta, Policy());
161 template <
bool propto,
162 typename T_y,
typename T_shape>
164 typename boost::math::tools::promote_args<T_y, T_shape>::type
165 lkj_corr_log(
const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
166 const T_shape& eta) {
171 template <
typename T_y,
typename T_shape,
174 typename boost::math::tools::promote_args<T_y, T_shape>::type
175 lkj_corr_log(
const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
178 return lkj_corr_log<false>(y,eta,Policy());
182 template <
typename T_y,
typename T_shape>
184 typename boost::math::tools::promote_args<T_y, T_shape>::type
185 lkj_corr_log(
const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
186 const T_shape& eta) {
var lgamma(const stan::agrad::var &a)
The log gamma function for variables (C99).
var pow(const var &base, const var &exponent)
Return the base raised to the power of the exponent (cmath).
var sum(const Eigen::Matrix< var, R, C > &m)
Returns the sum of the coefficients of the specified matrix, column vector or row vector.
bool check_corr_matrix(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const char *name, T_result *result, const Policy &)
Return true if the specified matrix is a valid correlation matrix.
bool check_not_nan(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
Checks if the variable y is nan.
bool check_positive(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
boost::math::policies::policy default_policy
Default error-handling policy from Boost.
bool check_size_match(const char *function, T_size1 i, T_size2 j, T_result *result, const Policy &)
T_shape do_lkj_constant(const T_shape &eta, const unsigned int &K)
boost::math::tools::promote_args< T_y, T_shape >::type lkj_corr_log(const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const T_shape &eta, const Policy &)
boost::math::tools::promote_args< T_covar, T_shape >::type lkj_corr_cholesky_log(const Eigen::Matrix< T_covar, Eigen::Dynamic, Eigen::Dynamic > &L, const T_shape &eta, const Policy &)
Probability, optimization and sampling library.
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
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.