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_y& y, const T_loc& mu, const T_scale& sigma, const T_shape& eta) {
23 static constexpr const char* function = "lkj_cov_lpdf";
24 check_size_match(function, "Rows of location parameter", mu.rows(),
25 "columns of scale parameter", sigma.rows());
26 check_square(function, "random variable", y);
27 check_size_match(function, "Rows of random variable", y.rows(),
28 "rows of location parameter", mu.rows());
29 const auto& y_ref = to_ref(y);
30 const auto& mu_ref = to_ref(mu);
31 const auto& sigma_ref = to_ref(sigma);
32 check_positive(function, "Shape parameter", eta);
33 check_finite(function, "Location parameter", mu_ref);
34 check_finite(function, "Scale parameter", sigma_ref);
35 check_finite(function, "Covariance matrix", y_ref);
36
38
39 const unsigned int K = y.rows();
40 const Eigen::Array<value_type_t<T_y>, Eigen::Dynamic, 1> sds
41 = y_ref.diagonal().array().sqrt();
42 for (unsigned int k = 0; k < K; k++) {
43 lp += lognormal_lpdf<propto>(sds(k), mu_ref(k), sigma_ref(k));
44 }
46 if (eta == 1.0) {
47 // no need to rescale y into a correlation matrix
48 lp += lkj_corr_lpdf<propto>(y_ref, eta);
49 return lp;
50 }
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_y& y, const T_loc& mu, const T_scale& sigma, const T_shape& eta) {
68 static constexpr const char* function = "lkj_cov_lpdf";
69 check_positive(function, "Shape parameter", eta);
70 check_finite(function, "Location parameter", mu);
71 check_finite(function, "Scale parameter", sigma);
72 const auto& y_ref = to_ref(y);
73 check_finite(function, "Covariance matrix", y_ref);
74
76
77 const unsigned int K = y.rows();
78 const Eigen::Array<value_type_t<T_y>, Eigen::Dynamic, 1> sds
79 = y_ref.diagonal().array().sqrt();
80 for (unsigned int k = 0; k < K; k++) {
81 lp += lognormal_lpdf<propto>(sds(k), mu, sigma);
82 }
84 if (eta == 1.0) {
85 // no need to rescale y into a correlation matrix
86 lp += lkj_corr_lpdf<propto>(y_ref, eta);
87 return lp;
88 }
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)
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.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
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...