Automatic Differentiation
 
Loading...
Searching...
No Matches
lkj_cov_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_LKJ_COV_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_LKJ_COV_LPDF_HPP
3
9
10namespace stan {
11namespace math {
12
16// LKJ_cov(y|mu, sigma, eta) [ y covariance matrix (not correlation matrix)
17// mu vector, sigma > 0 vector, eta > 0 ]
18template <bool propto, typename T_y, typename T_loc, typename T_scale,
19 typename T_shape, require_eigen_matrix_dynamic_t<T_y>* = nullptr,
20 require_all_eigen_col_vector_t<T_loc, T_scale>* = nullptr>
22 const T_loc& mu,
23 const T_scale& sigma,
24 const T_shape& eta) {
25 static constexpr const char* function = "lkj_cov_lpdf";
26 check_size_match(function, "Rows of location parameter", mu.rows(),
27 "columns of scale parameter", sigma.rows());
28 check_square(function, "random variable", y);
29 check_size_match(function, "Rows of random variable", y.rows(),
30 "rows of location parameter", mu.rows());
31 const auto& y_ref = to_ref(y);
32 const auto& mu_ref = to_ref(mu);
33 const auto& sigma_ref = to_ref(sigma);
34 check_positive(function, "Shape parameter", eta);
35 check_finite(function, "Location parameter", mu_ref);
36 check_finite(function, "Scale parameter", sigma_ref);
37 check_finite(function, "Covariance matrix", y_ref);
38
40
41 const unsigned int K = y.rows();
42 const Eigen::Array<value_type_t<T_y>, Eigen::Dynamic, 1> sds
43 = y_ref.diagonal().array().sqrt();
44 for (unsigned int k = 0; k < K; k++) {
45 lp += lognormal_lpdf<propto>(sds(k), mu_ref(k), sigma_ref(k));
46 }
47 if (stan::is_constant_all<T_shape>::value && eta == 1.0) {
48 // no need to rescale y into a correlation matrix
49 lp += lkj_corr_lpdf<propto>(y_ref, eta);
50 return lp;
51 }
52 Eigen::DiagonalMatrix<value_type_t<T_y>, Eigen::Dynamic> D(K);
53 D.diagonal() = sds.inverse();
54 lp += lkj_corr_lpdf<propto>(D * y_ref * D, eta);
55 return lp;
56}
57
61// LKJ_Cov(y|mu, sigma, eta) [ y covariance matrix (not correlation matrix)
62// mu scalar, sigma > 0 scalar, eta > 0 ]
63template <bool propto, typename T_y, typename T_loc, typename T_scale,
64 typename T_shape, require_eigen_matrix_dynamic_t<T_y>* = nullptr,
67 const T_loc& mu,
68 const T_scale& sigma,
69 const T_shape& eta) {
70 static constexpr const char* function = "lkj_cov_lpdf";
71 check_positive(function, "Shape parameter", eta);
72 check_finite(function, "Location parameter", mu);
73 check_finite(function, "Scale parameter", sigma);
74 const auto& y_ref = to_ref(y);
75 check_finite(function, "Covariance matrix", y_ref);
76
78
79 const unsigned int K = y.rows();
80 const Eigen::Array<value_type_t<T_y>, Eigen::Dynamic, 1> sds
81 = y_ref.diagonal().array().sqrt();
82 for (unsigned int k = 0; k < K; k++) {
83 lp += lognormal_lpdf<propto>(sds(k), mu, sigma);
84 }
85 if (stan::is_constant_all<T_shape>::value && eta == 1.0) {
86 // no need to rescale y into a correlation matrix
87 lp += lkj_corr_lpdf<propto>(y_ref, eta);
88 return lp;
89 }
90 Eigen::DiagonalMatrix<value_type_t<T_y>, Eigen::Dynamic> D(K);
91 D.diagonal() = sds.inverse();
92 lp += lkj_corr_lpdf<propto>(D * y_ref * D, eta);
93 return lp;
94}
95
99template <typename T_y, typename T_loc, typename T_scale, typename T_shape>
101 const T_y& y, const T_loc& mu, const T_scale& sigma, const T_shape& eta) {
102 return lkj_cov_lpdf<false>(y, mu, sigma, eta);
103}
104
105} // namespace math
106} // namespace stan
107#endif
require_t< is_eigen_matrix_dynamic< std::decay_t< T > > > require_eigen_matrix_dynamic_t
Require type satisfies is_eigen_matrix_dynamic.
require_all_t< is_stan_scalar< std::decay_t< Types > >... > require_all_stan_scalar_t
Require all of the types satisfy is_stan_scalar.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
return_type_t< T_y, T_loc, T_scale, T_shape > lkj_cov_lpdf(const T_y &y, const T_loc &mu, const T_scale &sigma, const T_shape &eta)
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
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.
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...