| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

lkj_cov_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_LKJ_COV_LOG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_LKJ_COV_LOG_HPP
3
4 #include <stan/math/prim/scal/err/check_size_match.hpp>
5 #include <stan/math/prim/mat/err/check_square.hpp>
6 #include <stan/math/prim/scal/err/check_finite.hpp>
7 #include <stan/math/prim/scal/err/check_positive.hpp>
8 #include <stan/math/prim/scal/fun/constants.hpp>
9 #include <stan/math/prim/scal/prob/lognormal_log.hpp>
10 #include <stan/math/prim/mat/prob/lkj_corr_log.hpp>
11 #include <stan/math/prim/scal/meta/include_summand.hpp>
12
13 namespace stan {
14 namespace math {
15
16 // LKJ_cov(y|mu, sigma, eta) [ y covariance matrix (not correlation matrix)
17 // mu vector, sigma > 0 vector, eta > 0 ]
18 template <bool propto,
19 typename T_y, typename T_loc, typename T_scale, typename T_shape>
20 typename
21 boost::math::tools::promote_args<T_y, T_loc, T_scale, T_shape>::type
22 lkj_cov_log(const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
23 const Eigen::Matrix<T_loc, Eigen::Dynamic, 1>& mu,
24 const Eigen::Matrix<T_scale, Eigen::Dynamic, 1>& sigma,
25 const T_shape& eta) {
26 static const char* function("lkj_cov_log");
27
28 using boost::math::tools::promote_args;
29
30 typename promote_args<T_y, T_loc, T_scale, T_shape>::type lp(0.0);
31 check_size_match(function,
32 "Rows of location parameter", mu.rows(),
33 "columns of scale parameter", sigma.rows());
34 check_square(function, "random variable", y);
35 check_size_match(function,
36 "Rows of random variable", y.rows(),
37 "rows of location parameter", mu.rows());
38 check_positive(function, "Shape parameter", eta);
39 check_finite(function, "Location parameter", mu);
40 check_finite(function, "Scale parameter", sigma);
41 for (int m = 0; m < y.rows(); ++m)
42 for (int n = 0; n < y.cols(); ++n)
43 check_finite(function, "Covariance matrix", y(m, n));
44
45 const unsigned int K = y.rows();
46 const Eigen::Array<T_y, Eigen::Dynamic, 1> sds
47 = y.diagonal().array().sqrt();
48 for (unsigned int k = 0; k < K; k++) {
49 lp += lognormal_log<propto>(sds(k), mu(k), sigma(k));
50 }
51 if (stan::is_constant<typename stan::scalar_type<T_shape> >::value
52 && eta == 1.0) {
53 // no need to rescale y into a correlation matrix
54 lp += lkj_corr_log<propto, T_y, T_shape>(y, eta);
55 return lp;
56 }
57 Eigen::DiagonalMatrix<T_y, Eigen::Dynamic> D(K);
58 D.diagonal() = sds.inverse();
59 lp += lkj_corr_log<propto, T_y, T_shape>(D * y * D, eta);
60 return lp;
61 }
62
63 template <typename T_y, typename T_loc, typename T_scale, typename T_shape>
64 inline
65 typename
66 boost::math::tools::promote_args<T_y, T_loc, T_scale, T_shape>::type
67 lkj_cov_log(const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
68 const Eigen::Matrix<T_loc, Eigen::Dynamic, 1>& mu,
69 const Eigen::Matrix<T_scale, Eigen::Dynamic, 1>& sigma,
70 const T_shape& eta) {
71 return lkj_cov_log<false>(y, mu, sigma, eta);
72 }
73
74 // LKJ_Cov(y|mu, sigma, eta) [ y covariance matrix (not correlation matrix)
75 // mu scalar, sigma > 0 scalar, eta > 0 ]
76 template <bool propto,
77 typename T_y, typename T_loc, typename T_scale, typename T_shape>
78 typename
79 boost::math::tools::promote_args<T_y, T_loc, T_scale, T_shape>::type
80 lkj_cov_log(const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
81 const T_loc& mu,
82 const T_scale& sigma,
83 const T_shape& eta) {
84 static const char* function("lkj_cov_log");
85
86 using boost::math::tools::promote_args;
87
88 typename promote_args<T_y, T_loc, T_scale, T_shape>::type lp(0.0);
89 check_positive(function, "Shape parameter", eta);
90 check_finite(function, "Location parameter", mu);
91 check_finite(function, "Scale parameter", sigma);
92
93 const unsigned int K = y.rows();
94 const Eigen::Array<T_y, Eigen::Dynamic, 1> sds
95 = y.diagonal().array().sqrt();
96 for (unsigned int k = 0; k < K; k++) {
97 lp += lognormal_log<propto>(sds(k), mu, sigma);
98 }
99 if (stan::is_constant<typename stan::scalar_type<T_shape> >::value
100 && eta == 1.0) {
101 // no need to rescale y into a correlation matrix
102 lp += lkj_corr_log<propto>(y, eta);
103 return lp;
104 }
105 Eigen::DiagonalMatrix<T_y, Eigen::Dynamic> D(K);
106 D.diagonal() = sds.inverse();
107 lp += lkj_corr_log<propto, T_y, T_shape>(D * y * D, eta);
108 return lp;
109 }
110
111 template <typename T_y, typename T_loc, typename T_scale, typename T_shape>
112 inline
113 typename boost::math::tools::promote_args
114 <T_y, T_loc, T_scale, T_shape>::type
115 lkj_cov_log(const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
116 const T_loc& mu,
117 const T_scale& sigma,
118 const T_shape& eta) {
119 return lkj_cov_log<false>(y, mu, sigma, eta);
120 }
121
122 }
123 }
124 #endif
stan::math::lkj_cov_log
boost::math::tools::promote_args< T_y, T_loc, T_scale, T_shape >::type lkj_cov_log(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)
Definition: lkj_cov_log.hpp:22
lkj_corr_log.hpp
stan::is_constant
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
Definition: is_constant.hpp:22
stan::scalar_type
Metaprogram structure to determine the base scalar type of a template argument.
Definition: scalar_type.hpp:33
lognormal_log.hpp
check_size_match.hpp
check_positive.hpp
stan
Definition: log_sum_exp.hpp:8
include_summand.hpp
stan::math::check_positive
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
Definition: check_positive.hpp:66
stan::math::check_size_match
bool check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Return true if the provided sizes match.
Definition: check_size_match.hpp:30
stan::math::check_finite
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.
Definition: check_finite.hpp:60
constants.hpp
check_finite.hpp
stan::math::check_square
bool check_square(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is square.
Definition: check_square.hpp:28
check_square.hpp

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