Stan Math Library  2.14.0
reverse mode automatic differentiation
multi_normal_lpdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_MULTI_NORMAL_LPDF_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_MULTI_NORMAL_LPDF_HPP
3 
17 #include <boost/random/normal_distribution.hpp>
18 #include <boost/random/variate_generator.hpp>
19 
20 namespace stan {
21  namespace math {
22 
23  template <bool propto,
24  typename T_y, typename T_loc, typename T_covar>
26  multi_normal_lpdf(const T_y& y,
27  const T_loc& mu,
28  const T_covar& Sigma) {
29  static const char* function("multi_normal_lpdf");
30  typedef typename scalar_type<T_covar>::type T_covar_elem;
31  typedef typename return_type<T_y, T_loc, T_covar>::type lp_type;
32  lp_type lp(0.0);
33 
34  using Eigen::Dynamic;
35 
36  check_positive(function, "Covariance matrix rows", Sigma.rows());
37  check_symmetric(function, "Covariance matrix", Sigma);
38 
40  check_ldlt_factor(function,
41  "LDLT_Factor of covariance parameter", ldlt_Sigma);
42 
43  VectorViewMvt<const T_y> y_vec(y);
44  VectorViewMvt<const 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 of "
56  "the random variable", size_y_new,
57  "Size of another vector of the "
58  "random variable", size_y_old);
59  size_y_old = size_y_new;
60  }
61  int size_mu_old = size_mu;
62  int size_mu_new;
63  for (size_t i = 1, size_ = length_mvt(mu); i < size_; i++) {
64  int size_mu_new = mu_vec[i].size();
65  check_size_match(function,
66  "Size of one of the vectors of "
67  "the location variable", size_mu_new,
68  "Size of another vector of the "
69  "location variable", size_mu_old);
70  size_mu_old = size_mu_new;
71  }
72  (void) size_y_old;
73  (void) size_y_new;
74  (void) size_mu_old;
75  (void) size_mu_new;
76  }
77 
78  check_size_match(function,
79  "Size of random variable", size_y,
80  "size of location parameter", size_mu);
81  check_size_match(function,
82  "Size of random variable", size_y,
83  "rows of covariance parameter", Sigma.rows());
84  check_size_match(function,
85  "Size of random variable", size_y,
86  "columns of covariance parameter", Sigma.cols());
87 
88  for (size_t i = 0; i < size_vec; i++) {
89  check_finite(function, "Location parameter", mu_vec[i]);
90  check_not_nan(function, "Random variable", y_vec[i]);
91  }
92 
93  if (size_y == 0)
94  return lp;
95 
97  lp += NEG_LOG_SQRT_TWO_PI * size_y * size_vec;
98 
100  lp -= 0.5 * log_determinant_ldlt(ldlt_Sigma) * size_vec;
101 
103  lp_type sum_lp_vec(0.0);
104  for (size_t i = 0; i < size_vec; i++) {
105  Eigen::Matrix<typename return_type<T_y, T_loc>::type, Dynamic, 1>
106  y_minus_mu(size_y);
107  for (int j = 0; j < size_y; j++)
108  y_minus_mu(j) = y_vec[i](j)-mu_vec[i](j);
109  sum_lp_vec += trace_inv_quad_form_ldlt(ldlt_Sigma, y_minus_mu);
110  }
111  lp -= 0.5*sum_lp_vec;
112  }
113  return lp;
114  }
115 
116  template <typename T_y, typename T_loc, typename T_covar>
117  inline
119  multi_normal_lpdf(const T_y& y,
120  const T_loc& mu,
121  const T_covar& Sigma) {
122  return multi_normal_lpdf<false>(y, mu, Sigma);
123  }
124 
125  }
126 }
127 #endif
boost::enable_if_c<!stan::is_var< T1 >::value &&!stan::is_var< T2 >::value, typename boost::math::tools::promote_args< T1, T2 >::type >::type trace_inv_quad_form_ldlt(const LDLT_factor< T1, R2, C2 > &A, const Eigen::Matrix< T2, R3, C3 > &B)
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)
return_type< T_y, T_loc, T_covar >::type multi_normal_lpdf(const T_y &y, const T_loc &mu, const T_covar &Sigma)
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.
scalar_type_helper< is_vector< T >::value, T >::type type
Definition: scalar_type.hpp:34
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...
boost::math::tools::promote_args< typename scalar_type< T1 >::type, typename scalar_type< T2 >::type, typename scalar_type< T3 >::type, typename scalar_type< T4 >::type, typename scalar_type< T5 >::type, typename scalar_type< T6 >::type >::type type
Definition: return_type.hpp:27
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
size_t size_
Definition: dot_self.hpp:18
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
VectorViewMvt is a template expression that wraps either an Eigen::Matrix or a std::vector<Eigen::Mat...
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
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–2016, Stan Development Team.