Stan Math Library  2.14.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 
28 
29 namespace stan {
30  namespace math {
31 
32  template <bool propto,
33  typename T_y, typename T_loc, typename T_covar>
35  multi_normal_prec_lpdf(const T_y& y,
36  const T_loc& mu,
37  const T_covar& Sigma) {
38  static const char* function("multi_normal_prec_lpdf");
39  typedef typename scalar_type<T_covar>::type T_covar_elem;
40  typedef typename return_type<T_y, T_loc, T_covar>::type lp_type;
41  lp_type lp(0.0);
42 
43  check_positive(function, "Precision matrix rows", Sigma.rows());
44  check_symmetric(function, "Precision matrix", Sigma);
45 
46  LDLT_factor<T_covar_elem,
47  Eigen::Dynamic, Eigen::Dynamic> ldlt_Sigma(Sigma);
48  check_ldlt_factor(function, "LDLT_Factor of precision parameter",
49  ldlt_Sigma);
50 
51  using Eigen::Matrix;
52  using std::vector;
53  VectorViewMvt<const T_y> y_vec(y);
54  VectorViewMvt<const T_loc> mu_vec(mu);
55  size_t size_vec = max_size_mvt(y, mu);
56 
57  int size_y = y_vec[0].size();
58  int size_mu = mu_vec[0].size();
59  if (size_vec > 1) {
60  int size_y_old = size_y;
61  int size_y_new;
62  for (size_t i = 1, size_ = length_mvt(y); i < size_; i++) {
63  int size_y_new = y_vec[i].size();
64  check_size_match(function,
65  "Size of one of the vectors "
66  "of the random variable", size_y_new,
67  "Size of another vector of "
68  "the random variable", size_y_old);
69  size_y_old = size_y_new;
70  }
71  int size_mu_old = size_mu;
72  int size_mu_new;
73  for (size_t i = 1, size_ = length_mvt(mu); i < size_; i++) {
74  int size_mu_new = mu_vec[i].size();
75  check_size_match(function,
76  "Size of one of the vectors "
77  "of the location variable", size_mu_new,
78  "Size of another vector of "
79  "the location variable", size_mu_old);
80  size_mu_old = size_mu_new;
81  }
82  (void) size_y_old;
83  (void) size_y_new;
84  (void) size_mu_old;
85  (void) size_mu_new;
86  }
87 
88  check_size_match(function,
89  "Size of random variable", size_y,
90  "size of location parameter", size_mu);
91  check_size_match(function,
92  "Size of random variable", size_y,
93  "rows of covariance parameter", Sigma.rows());
94  check_size_match(function,
95  "Size of random variable", size_y,
96  "columns of covariance parameter", Sigma.cols());
97 
98  for (size_t i = 0; i < size_vec; i++) {
99  check_finite(function, "Location parameter", mu_vec[i]);
100  check_not_nan(function, "Random variable", y_vec[i]);
101  }
102 
103  if (size_y == 0)
104  return lp;
105 
107  lp += 0.5 * log_determinant_ldlt(ldlt_Sigma) * size_vec;
108 
110  lp += NEG_LOG_SQRT_TWO_PI * size_y * size_vec;
111 
113  lp_type sum_lp_vec(0.0);
114  for (size_t i = 0; i < size_vec; i++) {
115  Eigen::Matrix<typename return_type<T_y, T_loc>::type,
116  Eigen::Dynamic, 1> y_minus_mu(size_y);
117  for (int j = 0; j < size_y; j++)
118  y_minus_mu(j) = y_vec[i](j) - mu_vec[i](j);
119  sum_lp_vec += trace_quad_form(Sigma, y_minus_mu);
120  }
121  lp -= 0.5*sum_lp_vec;
122  }
123  return lp;
124  }
125 
126  template <typename T_y, typename T_loc, typename T_covar>
127  inline
129  multi_normal_prec_lpdf(const T_y& y, const T_loc& mu,
130  const T_covar& Sigma) {
131  return multi_normal_prec_lpdf<false>(y, mu, Sigma);
132  }
133 
134  }
135 }
136 #endif
137 
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)
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.
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...
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.