Automatic Differentiation
 
Loading...
Searching...
No Matches
matrix_normal_prec_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_MATRIX_NORMAL_PREC_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_MATRIX_NORMAL_PREC_LPDF_HPP
3
10
11namespace stan {
12namespace math {
13
33template <bool propto, typename T_y, typename T_Mu, typename T_Sigma,
34 typename T_D,
35 require_all_matrix_t<T_y, T_Mu, T_Sigma, T_D>* = nullptr>
37 const T_y& y, const T_Mu& Mu, const T_Sigma& Sigma, const T_D& D) {
38 static constexpr const char* function = "matrix_normal_prec_lpdf";
39 check_positive(function, "Sigma rows", Sigma.rows());
40 check_finite(function, "Sigma", Sigma);
41 check_symmetric(function, "Sigma", Sigma);
42
43 auto ldlt_Sigma = make_ldlt_factor(Sigma);
44 check_ldlt_factor(function, "LDLT_Factor of Sigma", ldlt_Sigma);
45 check_positive(function, "D rows", D.rows());
46 check_finite(function, "D", D);
47 check_symmetric(function, "D", D);
48
49 auto ldlt_D = make_ldlt_factor(D);
50 check_ldlt_factor(function, "LDLT_Factor of D", ldlt_D);
51 check_size_match(function, "Rows of random variable", y.rows(),
52 "Rows of location parameter", Mu.rows());
53 check_size_match(function, "Columns of random variable", y.cols(),
54 "Columns of location parameter", Mu.cols());
55 check_size_match(function, "Rows of random variable", y.rows(),
56 "Rows of Sigma", Sigma.rows());
57 check_size_match(function, "Columns of random variable", y.cols(),
58 "Rows of D", D.rows());
59 check_finite(function, "Location parameter", Mu);
60 check_finite(function, "Random variable", y);
61
63
65 lp += NEG_LOG_SQRT_TWO_PI * y.cols() * y.rows();
66 }
67
69 lp += log_determinant_ldlt(ldlt_Sigma) * (0.5 * y.rows());
70 }
71
73 lp += log_determinant_ldlt(ldlt_D) * (0.5 * y.cols());
74 }
75
77 lp -= 0.5 * trace_gen_quad_form(D, Sigma, subtract(y, Mu));
78 }
79 return lp;
80}
81
82template <typename T_y, typename T_Mu, typename T_Sigma, typename T_D,
85 const T_y& y, const T_Mu& Mu, const T_Sigma& Sigma, const T_D& D) {
86 return matrix_normal_prec_lpdf<false>(y, Mu, Sigma, D);
87}
88
89} // namespace math
90} // namespace stan
91#endif
void check_symmetric(const char *function, const char *name, const matrix_cl< T > &y)
Check if the matrix_cl is symmetric.
require_all_t< is_matrix< std::decay_t< Types > >... > require_all_matrix_t
Require all of the types satisfy is_matrix.
Definition is_matrix.hpp:38
return_type_t< T_y, T_Mu, T_Sigma, T_D > matrix_normal_prec_lpdf(const T_y &y, const T_Mu &Mu, const T_Sigma &Sigma, const T_D &D)
The log of the matrix normal density for the given y, mu, Sigma and D where Sigma and D are given as ...
subtraction_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > subtract(T_a &&a, T_b &&b)
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
value_type_t< T > log_determinant_ldlt(LDLT_factor< T > &A)
Returns log(abs(det(A))) given a LDLT_factor of A.
auto make_ldlt_factor(const T &A)
Make an LDLT_factor with matrix type plain_type_t<T>
const double NEG_LOG_SQRT_TWO_PI
The value of minus the natural logarithm of the square root of , .
auto trace_gen_quad_form(const TD &D, const TA &A, const TB &B)
Return the trace of D times the quadratic form of B and A.
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
void check_ldlt_factor(const char *function, const char *name, LDLT_factor< T > &A)
Raise domain error if the specified LDLT factor is invalid.
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 ...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...