Stan Math Library  2.20.0
reverse mode automatic differentiation
lkj_cov_lpdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_LKJ_COV_LPDF_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_LKJ_COV_LPDF_HPP
3 
11 
12 namespace stan {
13 namespace math {
14 
15 // LKJ_cov(y|mu, sigma, eta) [ y covariance matrix (not correlation matrix)
16 // mu vector, sigma > 0 vector, eta > 0 ]
17 template <bool propto, typename T_y, typename T_loc, typename T_scale,
18  typename T_shape>
19 typename boost::math::tools::promote_args<T_y, T_loc, T_scale, T_shape>::type
20 lkj_cov_lpdf(const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
21  const Eigen::Matrix<T_loc, Eigen::Dynamic, 1>& mu,
22  const Eigen::Matrix<T_scale, Eigen::Dynamic, 1>& sigma,
23  const T_shape& eta) {
24  static const char* function = "lkj_cov_lpdf";
25 
26  using boost::math::tools::promote_args;
27 
28  typename promote_args<T_y, T_loc, T_scale, T_shape>::type lp(0.0);
29  check_size_match(function, "Rows of location parameter", mu.rows(),
30  "columns of scale parameter", sigma.rows());
31  check_square(function, "random variable", y);
32  check_size_match(function, "Rows of random variable", y.rows(),
33  "rows of location parameter", mu.rows());
34  check_positive(function, "Shape parameter", eta);
35  check_finite(function, "Location parameter", mu);
36  check_finite(function, "Scale parameter", sigma);
37  for (int m = 0; m < y.rows(); ++m)
38  for (int n = 0; n < y.cols(); ++n)
39  check_finite(function, "Covariance matrix", y(m, n));
40 
41  const unsigned int K = y.rows();
42  const Eigen::Array<T_y, Eigen::Dynamic, 1> sds = y.diagonal().array().sqrt();
43  for (unsigned int k = 0; k < K; k++) {
44  lp += lognormal_lpdf<propto>(sds(k), mu(k), sigma(k));
45  }
47  && eta == 1.0) {
48  // no need to rescale y into a correlation matrix
49  lp += lkj_corr_lpdf<propto, T_y, T_shape>(y, eta);
50  return lp;
51  }
52  Eigen::DiagonalMatrix<T_y, Eigen::Dynamic> D(K);
53  D.diagonal() = sds.inverse();
54  lp += lkj_corr_lpdf<propto, T_y, T_shape>(D * y * D, eta);
55  return lp;
56 }
57 
58 template <typename T_y, typename T_loc, typename T_scale, typename T_shape>
59 inline typename boost::math::tools::promote_args<T_y, T_loc, T_scale,
60  T_shape>::type
61 lkj_cov_lpdf(const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
62  const Eigen::Matrix<T_loc, Eigen::Dynamic, 1>& mu,
63  const Eigen::Matrix<T_scale, Eigen::Dynamic, 1>& sigma,
64  const T_shape& eta) {
65  return lkj_cov_lpdf<false>(y, mu, sigma, eta);
66 }
67 
68 // LKJ_Cov(y|mu, sigma, eta) [ y covariance matrix (not correlation matrix)
69 // mu scalar, sigma > 0 scalar, eta > 0 ]
70 template <bool propto, typename T_y, typename T_loc, typename T_scale,
71  typename T_shape>
72 typename boost::math::tools::promote_args<T_y, T_loc, T_scale, T_shape>::type
73 lkj_cov_lpdf(const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
74  const T_loc& mu, const T_scale& sigma, const T_shape& eta) {
75  static const char* function = "lkj_cov_lpdf";
76 
77  using boost::math::tools::promote_args;
78 
79  typename promote_args<T_y, T_loc, T_scale, T_shape>::type lp(0.0);
80  check_positive(function, "Shape parameter", eta);
81  check_finite(function, "Location parameter", mu);
82  check_finite(function, "Scale parameter", sigma);
83 
84  const unsigned int K = y.rows();
85  const Eigen::Array<T_y, Eigen::Dynamic, 1> sds = y.diagonal().array().sqrt();
86  for (unsigned int k = 0; k < K; k++) {
87  lp += lognormal_lpdf<propto>(sds(k), mu, sigma);
88  }
90  && eta == 1.0) {
91  // no need to rescale y into a correlation matrix
92  lp += lkj_corr_lpdf<propto>(y, eta);
93  return lp;
94  }
95  Eigen::DiagonalMatrix<T_y, Eigen::Dynamic> D(K);
96  D.diagonal() = sds.inverse();
97  lp += lkj_corr_lpdf<propto, T_y, T_shape>(D * y * D, eta);
98  return lp;
99 }
100 
101 template <typename T_y, typename T_loc, typename T_scale, typename T_shape>
102 inline typename boost::math::tools::promote_args<T_y, T_loc, T_scale,
103  T_shape>::type
104 lkj_cov_lpdf(const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
105  const T_loc& mu, const T_scale& sigma, const T_shape& eta) {
106  return lkj_cov_lpdf<false>(y, mu, sigma, eta);
107 }
108 
109 } // namespace math
110 } // namespace stan
111 #endif
Metaprogram structure to determine the base scalar type of a template argument.
Definition: scalar_type.hpp:16
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
boost::math::tools::promote_args< T_y, T_loc, T_scale, T_shape >::type lkj_cov_lpdf(const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const Eigen::Matrix< T_loc, Eigen::Dynamic, 1 > &mu, const Eigen::Matrix< T_scale, Eigen::Dynamic, 1 > &sigma, const T_shape &eta)
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Definition: conjunction.hpp:14
void check_square(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl is square.
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.

     [ Stan Home Page ] © 2011–2018, Stan Development Team.