Stan Math Library  2.14.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 
18 
19 namespace stan {
20  namespace math {
39  template <bool propto,
40  typename T_y, typename T_Mu, typename T_Sigma, typename T_D>
41  typename boost::math::tools::promote_args<T_y, T_Mu, T_Sigma, T_D>::type
42  matrix_normal_prec_lpdf(const Eigen::Matrix
43  <T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
44  const Eigen::Matrix
45  <T_Mu, Eigen::Dynamic, Eigen::Dynamic>& Mu,
46  const Eigen::Matrix
47  <T_Sigma, Eigen::Dynamic, Eigen::Dynamic>& Sigma,
48  const Eigen::Matrix
49  <T_D, Eigen::Dynamic, Eigen::Dynamic>& D) {
50  static const char* function("matrix_normal_prec_lpdf");
51  typename
52  boost::math::tools::promote_args<T_y, T_Mu, T_Sigma, T_D>::type lp(0.0);
53 
54  check_positive(function, "Sigma rows", Sigma.rows());
55  check_finite(function, "Sigma", Sigma);
56  check_symmetric(function, "Sigma", Sigma);
57 
59  check_ldlt_factor(function, "LDLT_Factor of Sigma", ldlt_Sigma);
60  check_positive(function, "D rows", D.rows());
61  check_finite(function, "D", D);
62  check_symmetric(function, "Sigma", D);
63 
65  check_ldlt_factor(function, "LDLT_Factor of D", ldlt_D);
66  check_size_match(function,
67  "Rows of random variable", y.rows(),
68  "Rows of location parameter", Mu.rows());
69  check_size_match(function,
70  "Columns of random variable", y.cols(),
71  "Columns of location parameter", Mu.cols());
72  check_size_match(function,
73  "Rows of random variable", y.rows(),
74  "Rows of Sigma", Sigma.rows());
75  check_size_match(function,
76  "Columns of random variable", y.cols(),
77  "Rows of D", D.rows());
78  check_finite(function, "Location parameter", Mu);
79  check_finite(function, "Random variable", y);
80 
82  lp += NEG_LOG_SQRT_TWO_PI * y.cols() * y.rows();
83 
85  lp += log_determinant_ldlt(ldlt_Sigma) * (0.5 * y.rows());
86  }
87 
89  lp += log_determinant_ldlt(ldlt_D) * (0.5 * y.cols());
90  }
91 
93  lp -= 0.5 * trace_gen_quad_form(D, Sigma, subtract(y, Mu));
94  }
95  return lp;
96  }
97 
98  template <typename T_y, typename T_Mu, typename T_Sigma, typename T_D>
99  typename boost::math::tools::promote_args<T_y, T_Mu, T_Sigma, T_D>::type
100  matrix_normal_prec_lpdf(const Eigen::Matrix
101  <T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
102  const Eigen::Matrix
103  <T_Mu, Eigen::Dynamic, Eigen::Dynamic>& Mu,
104  const Eigen::Matrix
105  <T_Sigma, Eigen::Dynamic, Eigen::Dynamic>& Sigma,
106  const Eigen::Matrix
107  <T_D, Eigen::Dynamic, Eigen::Dynamic>& D) {
108  return matrix_normal_prec_lpdf<false>(y, Mu, Sigma, D);
109  }
110 
111  }
112 }
113 #endif
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R, C > subtract(const Eigen::Matrix< T1, R, C > &m1, const Eigen::Matrix< T2, R, C > &m2)
Return the result of subtracting the second specified matrix from the first specified matrix...
Definition: subtract.hpp:27
void check_ldlt_factor(const char *function, const char *name, LDLT_factor< T, R, C > &A)
Check if the argument is a valid LDLT_factor.
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)
void check_symmetric(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Check if the specified matrix is symmetric.
const double NEG_LOG_SQRT_TWO_PI
Definition: constants.hpp:181
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 ...
T log_determinant_ldlt(LDLT_factor< T, R, C > &A)

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