Stan Math Library  2.20.0
reverse mode automatic differentiation
matrix_normal_prec_lpdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_MATRIX_NORMAL_PREC_LPDF_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_MATRIX_NORMAL_PREC_LPDF_HPP
3 
14 
15 namespace stan {
16 namespace math {
35 template <bool propto, typename T_y, typename T_Mu, typename T_Sigma,
36  typename T_D>
37 typename boost::math::tools::promote_args<T_y, T_Mu, T_Sigma, T_D>::type
39  const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
40  const Eigen::Matrix<T_Mu, Eigen::Dynamic, Eigen::Dynamic>& Mu,
41  const Eigen::Matrix<T_Sigma, Eigen::Dynamic, Eigen::Dynamic>& Sigma,
42  const Eigen::Matrix<T_D, Eigen::Dynamic, Eigen::Dynamic>& D) {
43  static const char* function = "matrix_normal_prec_lpdf";
44  typename boost::math::tools::promote_args<T_y, T_Mu, T_Sigma, T_D>::type lp(
45  0.0);
46 
47  check_positive(function, "Sigma rows", Sigma.rows());
48  check_finite(function, "Sigma", Sigma);
49  check_symmetric(function, "Sigma", Sigma);
50 
52  check_ldlt_factor(function, "LDLT_Factor of Sigma", ldlt_Sigma);
53  check_positive(function, "D rows", D.rows());
54  check_finite(function, "D", D);
55  check_symmetric(function, "D", D);
56 
58  check_ldlt_factor(function, "LDLT_Factor of D", ldlt_D);
59  check_size_match(function, "Rows of random variable", y.rows(),
60  "Rows of location parameter", Mu.rows());
61  check_size_match(function, "Columns of random variable", y.cols(),
62  "Columns of location parameter", Mu.cols());
63  check_size_match(function, "Rows of random variable", y.rows(),
64  "Rows of Sigma", Sigma.rows());
65  check_size_match(function, "Columns of random variable", y.cols(),
66  "Rows of D", D.rows());
67  check_finite(function, "Location parameter", Mu);
68  check_finite(function, "Random variable", y);
69 
71  lp += NEG_LOG_SQRT_TWO_PI * y.cols() * y.rows();
72 
74  lp += log_determinant_ldlt(ldlt_Sigma) * (0.5 * y.rows());
75  }
76 
78  lp += log_determinant_ldlt(ldlt_D) * (0.5 * y.cols());
79  }
80 
82  lp -= 0.5 * trace_gen_quad_form(D, Sigma, subtract(y, Mu));
83  }
84  return lp;
85 }
86 
87 template <typename T_y, typename T_Mu, typename T_Sigma, typename T_D>
88 typename boost::math::tools::promote_args<T_y, T_Mu, T_Sigma, T_D>::type
90  const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
91  const Eigen::Matrix<T_Mu, Eigen::Dynamic, Eigen::Dynamic>& Mu,
92  const Eigen::Matrix<T_Sigma, Eigen::Dynamic, Eigen::Dynamic>& Sigma,
93  const Eigen::Matrix<T_D, Eigen::Dynamic, Eigen::Dynamic>& D) {
94  return matrix_normal_prec_lpdf<false>(y, Mu, Sigma, D);
95 }
96 
97 } // namespace math
98 } // namespace stan
99 #endif
auto subtract(const matrix_cl &A, const matrix_cl &B)
Matrix subtraction on the OpenCL device Subtracts the second matrix from the first matrix and stores ...
Definition: subtract.hpp:28
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
void check_ldlt_factor(const char *function, const char *name, LDLT_factor< T, R, C > &A)
Raise domain error if the specified LDLT factor is invalid.
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.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
fvar< T > trace_gen_quad_form(const Eigen::Matrix< fvar< T >, RD, CD > &D, const Eigen::Matrix< fvar< T >, RA, CA > &A, const Eigen::Matrix< fvar< T >, RB, CB > &B)
LDLT_factor is a thin wrapper on Eigen::LDLT to allow for reusing factorizations and efficient autodi...
Definition: LDLT_factor.hpp:63
const double NEG_LOG_SQRT_TWO_PI
Definition: constants.hpp:156
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
boost::math::tools::promote_args< T_y, T_Mu, T_Sigma, T_D >::type matrix_normal_prec_lpdf(const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const Eigen::Matrix< T_Mu, Eigen::Dynamic, Eigen::Dynamic > &Mu, const Eigen::Matrix< T_Sigma, Eigen::Dynamic, Eigen::Dynamic > &Sigma, const Eigen::Matrix< T_D, Eigen::Dynamic, Eigen::Dynamic > &D)
The log of the matrix normal density for the given y, mu, Sigma and D where Sigma and D are given as ...
void check_symmetric(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl is symmetric.
T log_determinant_ldlt(LDLT_factor< T, R, C > &A)

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