1 #ifndef STAN_MATH_PRIM_PROB_MULTI_NORMAL_PREC_LPDF_HPP
2 #define STAN_MATH_PRIM_PROB_MULTI_NORMAL_PREC_LPDF_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/fun/as_column_vector_or_scalar.hpp>
7 #include <stan/math/prim/fun/constants.hpp>
8 #include <stan/math/prim/fun/log_determinant_ldlt.hpp>
9 #include <stan/math/prim/fun/max_size_mvt.hpp>
10 #include <stan/math/prim/fun/size_mvt.hpp>
11 #include <stan/math/prim/fun/sum.hpp>
12 #include <stan/math/prim/fun/to_ref.hpp>
13 #include <stan/math/prim/fun/trace_quad_form.hpp>
14 #include <stan/math/prim/fun/vector_seq_view.hpp>
15 
16 namespace stan {
17 namespace math {
18 
19 template <bool propto, typename T_y, typename T_loc, typename T_covar>
multi_normal_prec_lpdf(const T_y & y,const T_loc & mu,const T_covar & Sigma)20 return_type_t<T_y, T_loc, T_covar> multi_normal_prec_lpdf(
21     const T_y& y, const T_loc& mu, const T_covar& Sigma) {
22   using T_covar_elem = typename scalar_type<T_covar>::type;
23   using lp_type = return_type_t<T_y, T_loc, T_covar>;
24   using Eigen::Matrix;
25   using std::vector;
26   static const char* function = "multi_normal_prec_lpdf";
27   check_positive(function, "Precision matrix rows", Sigma.rows());
28 
29   size_t number_of_y = size_mvt(y);
30   size_t number_of_mu = size_mvt(mu);
31   if (number_of_y == 0 || number_of_mu == 0) {
32     return 0;
33   }
34   check_consistent_sizes_mvt(function, "y", y, "mu", mu);
35 
36   lp_type lp(0);
37   vector_seq_view<T_y> y_vec(y);
38   vector_seq_view<T_loc> mu_vec(mu);
39   size_t size_vec = max_size_mvt(y, mu);
40 
41   int size_y = y_vec[0].size();
42   int size_mu = mu_vec[0].size();
43   if (size_vec > 1) {
44     for (size_t i = 1, size_mvt_y = size_mvt(y); i < size_mvt_y; i++) {
45       check_size_match(function,
46                        "Size of one of the vectors "
47                        "of the random variable",
48                        y_vec[i].size(),
49                        "Size of the first vector of "
50                        "the random variable",
51                        size_y);
52     }
53     for (size_t i = 1, size_mvt_mu = size_mvt(mu); i < size_mvt_mu; i++) {
54       check_size_match(function,
55                        "Size of one of the vectors "
56                        "of the location variable",
57                        mu_vec[i].size(),
58                        "Size of the first vector of "
59                        "the location variable",
60                        size_mu);
61     }
62   }
63 
64   check_size_match(function, "Size of random variable", size_y,
65                    "size of location parameter", size_mu);
66   check_size_match(function, "Size of random variable", size_y,
67                    "rows of covariance parameter", Sigma.rows());
68   check_size_match(function, "Size of random variable", size_y,
69                    "columns of covariance parameter", Sigma.cols());
70 
71   for (size_t i = 0; i < size_vec; i++) {
72     check_finite(function, "Location parameter", mu_vec[i]);
73     check_not_nan(function, "Random variable", y_vec[i]);
74   }
75   const auto& Sigma_ref = to_ref(Sigma);
76   check_symmetric(function, "Precision matrix", Sigma_ref);
77 
78   auto ldlt_Sigma = make_ldlt_factor(Sigma_ref);
79   check_ldlt_factor(function, "LDLT_Factor of precision parameter", ldlt_Sigma);
80 
81   if (size_y == 0) {
82     return lp;
83   }
84 
85   if (include_summand<propto, T_covar_elem>::value) {
86     lp += 0.5 * log_determinant_ldlt(ldlt_Sigma) * size_vec;
87   }
88 
89   if (include_summand<propto>::value) {
90     lp += NEG_LOG_SQRT_TWO_PI * size_y * size_vec;
91   }
92 
93   if (include_summand<propto, T_y, T_loc, T_covar_elem>::value) {
94     lp_type sum_lp_vec(0.0);
95     for (size_t i = 0; i < size_vec; i++) {
96       const auto& y_col = as_column_vector_or_scalar(y_vec[i]);
97       const auto& mu_col = as_column_vector_or_scalar(mu_vec[i]);
98       sum_lp_vec += trace_quad_form(Sigma_ref, y_col - mu_col);
99     }
100     lp -= 0.5 * sum_lp_vec;
101   }
102   return lp;
103 }
104 
105 template <typename T_y, typename T_loc, typename T_covar>
multi_normal_prec_lpdf(const T_y & y,const T_loc & mu,const T_covar & Sigma)106 inline return_type_t<T_y, T_loc, T_covar> multi_normal_prec_lpdf(
107     const T_y& y, const T_loc& mu, const T_covar& Sigma) {
108   return multi_normal_prec_lpdf<false>(y, mu, Sigma);
109 }
110 
111 }  // namespace math
112 }  // namespace stan
113 #endif
114