Stan Math Library  2.20.0
reverse mode automatic differentiation
multi_normal_prec_lpdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_MULTI_NORMAL_PREC_LPDF_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_MULTI_NORMAL_PREC_LPDF_HPP
3 
16 
17 namespace stan {
18 namespace math {
19 
20 template <bool propto, typename T_y, typename T_loc, typename T_covar>
22  const T_y& y, const T_loc& mu, const T_covar& Sigma) {
23  static const char* function = "multi_normal_prec_lpdf";
24  typedef typename scalar_type<T_covar>::type T_covar_elem;
25  typedef typename return_type<T_y, T_loc, T_covar>::type lp_type;
26 
27  check_positive(function, "Precision matrix rows", Sigma.rows());
28  check_symmetric(function, "Precision matrix", Sigma);
29 
31  check_ldlt_factor(function, "LDLT_Factor of precision parameter", ldlt_Sigma);
32 
33  using Eigen::Matrix;
34  using std::vector;
35 
36  size_t number_of_y = length_mvt(y);
37  size_t number_of_mu = length_mvt(mu);
38  if (number_of_y == 0 || number_of_mu == 0)
39  return 0;
40  check_consistent_sizes_mvt(function, "y", y, "mu", mu);
41 
42  lp_type lp(0);
43  vector_seq_view<T_y> y_vec(y);
44  vector_seq_view<T_loc> mu_vec(mu);
45  size_t size_vec = max_size_mvt(y, mu);
46 
47  int size_y = y_vec[0].size();
48  int size_mu = mu_vec[0].size();
49  if (size_vec > 1) {
50  int size_y_old = size_y;
51  int size_y_new;
52  for (size_t i = 1, size_ = length_mvt(y); i < size_; i++) {
53  int size_y_new = y_vec[i].size();
54  check_size_match(function,
55  "Size of one of the vectors "
56  "of the random variable",
57  size_y_new,
58  "Size of another vector of "
59  "the random variable",
60  size_y_old);
61  size_y_old = size_y_new;
62  }
63  int size_mu_old = size_mu;
64  int size_mu_new;
65  for (size_t i = 1, size_ = length_mvt(mu); i < size_; i++) {
66  int size_mu_new = mu_vec[i].size();
67  check_size_match(function,
68  "Size of one of the vectors "
69  "of the location variable",
70  size_mu_new,
71  "Size of another vector of "
72  "the location variable",
73  size_mu_old);
74  size_mu_old = size_mu_new;
75  }
76  (void)size_y_old;
77  (void)size_y_new;
78  (void)size_mu_old;
79  (void)size_mu_new;
80  }
81 
82  check_size_match(function, "Size of random variable", size_y,
83  "size of location parameter", size_mu);
84  check_size_match(function, "Size of random variable", size_y,
85  "rows of covariance parameter", Sigma.rows());
86  check_size_match(function, "Size of random variable", size_y,
87  "columns of covariance parameter", Sigma.cols());
88 
89  for (size_t i = 0; i < size_vec; i++) {
90  check_finite(function, "Location parameter", mu_vec[i]);
91  check_not_nan(function, "Random variable", y_vec[i]);
92  }
93 
94  if (size_y == 0)
95  return lp;
96 
98  lp += 0.5 * log_determinant_ldlt(ldlt_Sigma) * size_vec;
99 
101  lp += NEG_LOG_SQRT_TWO_PI * size_y * size_vec;
102 
104  lp_type sum_lp_vec(0.0);
105  for (size_t i = 0; i < size_vec; i++) {
106  Eigen::Matrix<typename return_type<T_y, T_loc>::type, Eigen::Dynamic, 1>
107  y_minus_mu(size_y);
108  for (int j = 0; j < size_y; j++)
109  y_minus_mu(j) = y_vec[i](j) - mu_vec[i](j);
110  sum_lp_vec += trace_quad_form(Sigma, y_minus_mu);
111  }
112  lp -= 0.5 * sum_lp_vec;
113  }
114  return lp;
115 }
116 
117 template <typename T_y, typename T_loc, typename T_covar>
119  const T_y& y, const T_loc& mu, const T_covar& Sigma) {
120  return multi_normal_prec_lpdf<false>(y, mu, Sigma);
121 }
122 
123 } // namespace math
124 } // namespace stan
125 #endif
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
size_t max_size_mvt(const T1 &x1, const T2 &x2)
fvar< T > trace_quad_form(const Eigen::Matrix< fvar< T >, RA, CA > &A, const Eigen::Matrix< fvar< T >, RB, CB > &B)
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.
return_type< T_y, T_loc, T_covar >::type multi_normal_prec_lpdf(const T_y &y, const T_loc &mu, const T_covar &Sigma)
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
LDLT_factor is a thin wrapper on Eigen::LDLT to allow for reusing factorizations and efficient autodi...
Definition: LDLT_factor.hpp:63
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
Definition: return_type.hpp:36
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
const double NEG_LOG_SQRT_TWO_PI
Definition: constants.hpp:156
This class provides a low-cost wrapper for situations where you either need an Eigen Vector or RowVec...
void check_consistent_sizes_mvt(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
void check_symmetric(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl is symmetric.
size_t length_mvt(const Eigen::Matrix< T, R, C > &)
Definition: length_mvt.hpp:12
T log_determinant_ldlt(LDLT_factor< T, R, C > &A)

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