1 #ifndef __STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__CONTINUOUS__LKJ_CORR_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__CONTINUOUS__LKJ_CORR_HPP__
14 template <
typename T_shape>
22 double constant = 0.0;
23 double beta_arg = 0.0;
24 for (
unsigned int k = 1; k < K; k++) {
25 beta_arg = 0.5 * (k + 1.0);
26 constant += k * (2.0 *
lgamma(beta_arg) -
lgamma(2.0 * beta_arg));
27 sum +=
pow(static_cast<double>(k),2.0);
29 constant += sum * LOG_TWO;
33 T_shape constant = 0.0;
35 for (
unsigned int k = 1; k < K; k++) {
36 unsigned int diff = K - k;
37 beta_arg = eta + 0.5 * (diff - 1);
38 constant += diff * (2.0 *
lgamma(beta_arg) -
lgamma(2.0 * beta_arg));
39 sum += (2.0 * eta - 2.0 + diff) * diff;
41 constant += sum * LOG_TWO;
47 template <
bool propto,
48 typename T_covar,
typename T_shape,
50 typename boost::math::tools::promote_args<T_covar, T_shape>::type
52 const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& L,
56 static const char*
function
57 =
"stan::prob::lkj_corr_cholesky_log(%1%)";
59 using boost::math::tools::promote_args;
62 typename promote_args<T_covar,T_shape>::type lp(0.0);
63 if (!
check_positive(
function, eta,
"Shape parameter", &lp, Policy()))
66 const unsigned int K = L.rows();
76 lp += (eta - 1.0) * 2.0 * L.diagonal().array().log().sum();
82 template <
bool propto,
83 typename T_covar,
typename T_shape>
85 typename boost::math::tools::promote_args<T_covar, T_shape>::type
87 const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& L,
93 template <
typename T_covar,
typename T_shape,
96 typename boost::math::tools::promote_args<T_covar, T_shape>::type
98 const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& L,
101 return lkj_corr_cholesky_log<false>(L,eta,Policy());
104 template <
typename T_covar,
typename T_shape>
106 typename boost::math::tools::promote_args<T_covar, T_shape>::type
108 const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& L,
109 const T_shape& eta) {
117 template <
bool propto,
118 typename T_y,
typename T_shape,
120 typename boost::math::tools::promote_args<T_y, T_shape>::type
121 lkj_corr_log(
const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
124 static const char*
function =
"stan::prob::lkj_corr_log(%1%)";
130 using boost::math::tools::promote_args;
132 typename promote_args<T_y,T_shape>::type lp;
133 if (!
check_positive(
function, eta,
"Shape parameter", &lp, Policy()))
136 y.rows(),
"Rows of correlation matrix",
137 y.cols(),
"columns of correlation matrix",
140 if (!
check_not_nan(
function, y,
"Correlation matrix", &lp, Policy()))
146 const unsigned int K = y.rows();
150 Eigen::LLT< Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic> > Cholesky = y.llt();
152 if (Cholesky.info() == Eigen::NumericalIssue)
155 Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic> L = Cholesky.matrixL();
156 return lkj_corr_cholesky_log<propto>(L, eta, Policy());
162 template <
bool propto,
163 typename T_y,
typename T_shape>
165 typename boost::math::tools::promote_args<T_y, T_shape>::type
166 lkj_corr_log(
const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
167 const T_shape& eta) {
172 template <
typename T_y,
typename T_shape,
175 typename boost::math::tools::promote_args<T_y, T_shape>::type
176 lkj_corr_log(
const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
179 return lkj_corr_log<false>(y,eta,Policy());
183 template <
typename T_y,
typename T_shape>
185 typename boost::math::tools::promote_args<T_y, T_shape>::type
186 lkj_corr_log(
const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
187 const T_shape& eta) {
192 inline Eigen::MatrixXd
197 Eigen::ArrayXd CPCs( (K * (K - 1)) / 2 );
198 double alpha = eta + 0.5 * (K - 1);
199 unsigned int count = 0;
200 for (
size_t i = 0; i < (K - 1); i++) {
202 for (
size_t j = i + 1; j < K; j++) {
211 inline Eigen::MatrixXd