| Stan Math Library 2.11.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

multi_normal_prec_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_MULTI_NORMAL_PREC_LOG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_MULTI_NORMAL_PREC_LOG_HPP
3
4 #include <stan/math/prim/mat/err/check_ldlt_factor.hpp>
5 #include <stan/math/prim/mat/err/check_symmetric.hpp>
6 #include <stan/math/prim/mat/fun/columns_dot_product.hpp>
7 #include <stan/math/prim/mat/fun/columns_dot_self.hpp>
8 #include <stan/math/prim/mat/fun/dot_product.hpp>
9 #include <stan/math/prim/mat/fun/dot_self.hpp>
10 #include <stan/math/prim/mat/fun/log_determinant_ldlt.hpp>
11 #include <stan/math/prim/mat/fun/log.hpp>
12 #include <stan/math/prim/mat/fun/log_determinant.hpp>
13 #include <stan/math/prim/mat/fun/mdivide_left_spd.hpp>
14 #include <stan/math/prim/mat/fun/mdivide_left_tri_low.hpp>
15 #include <stan/math/prim/mat/fun/multiply.hpp>
16 #include <stan/math/prim/mat/fun/subtract.hpp>
17 #include <stan/math/prim/mat/fun/sum.hpp>
18 #include <stan/math/prim/mat/fun/trace_quad_form.hpp>
19 #include <stan/math/prim/mat/meta/VectorViewMvt.hpp>
20 #include <stan/math/prim/scal/err/check_size_match.hpp>
21 #include <stan/math/prim/scal/err/check_finite.hpp>
22 #include <stan/math/prim/scal/err/check_not_nan.hpp>
23 #include <stan/math/prim/scal/err/check_positive.hpp>
24 #include <stan/math/prim/scal/fun/constants.hpp>
25 #include <stan/math/prim/scal/meta/max_size_mvt.hpp>
26 #include <stan/math/prim/scal/meta/include_summand.hpp>
27
28 namespace stan {
29
30 namespace math {
31
32 template <bool propto,
33 typename T_y, typename T_loc, typename T_covar>
34 typename return_type<T_y, T_loc, T_covar>::type
35 multi_normal_prec_log(const T_y& y,
36 const T_loc& mu,
37 const T_covar& Sigma) {
38 static const char* function("stan::math::multi_normal_prec_log");
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 using stan::math::check_not_nan;
44 using stan::math::check_symmetric;
45 using stan::math::check_size_match;
46 using stan::math::check_positive;
47 using stan::math::check_finite;
48 using stan::math::sum;
49 using stan::math::trace_quad_form;
50 using stan::math::log_determinant_ldlt;
51 using stan::math::LDLT_factor;
52 using stan::math::check_ldlt_factor;
53
54 check_positive(function, "Precision matrix rows", Sigma.rows());
55 check_symmetric(function, "Precision matrix", Sigma);
56
57 LDLT_factor<T_covar_elem,
58 Eigen::Dynamic, Eigen::Dynamic> ldlt_Sigma(Sigma);
59 check_ldlt_factor(function, "LDLT_Factor of precision parameter",
60 ldlt_Sigma);
61
62 using Eigen::Matrix;
63 using std::vector;
64 VectorViewMvt<const T_y> y_vec(y);
65 VectorViewMvt<const T_loc> mu_vec(mu);
66 // size of std::vector of Eigen vectors
67 size_t size_vec = max_size_mvt(y, mu);
68
69
70 // Check if every vector of the array has the same size
71 int size_y = y_vec[0].size();
72 int size_mu = mu_vec[0].size();
73 if (size_vec > 1) {
74 int size_y_old = size_y;
75 int size_y_new;
76 for (size_t i = 1, size_ = length_mvt(y); i < size_; i++) {
77 int size_y_new = y_vec[i].size();
78 check_size_match(function,
79 "Size of one of the vectors "
80 "of the random variable", size_y_new,
81 "Size of another vector of "
82 "the random variable", size_y_old);
83 size_y_old = size_y_new;
84 }
85 int size_mu_old = size_mu;
86 int size_mu_new;
87 for (size_t i = 1, size_ = length_mvt(mu); i < size_; i++) {
88 int size_mu_new = mu_vec[i].size();
89 check_size_match(function,
90 "Size of one of the vectors "
91 "of the location variable", size_mu_new,
92 "Size of another vector of "
93 "the location variable", size_mu_old);
94 size_mu_old = size_mu_new;
95 }
96 (void) size_y_old;
97 (void) size_y_new;
98 (void) size_mu_old;
99 (void) size_mu_new;
100 }
101
102 check_size_match(function,
103 "Size of random variable", size_y,
104 "size of location parameter", size_mu);
105 check_size_match(function,
106 "Size of random variable", size_y,
107 "rows of covariance parameter", Sigma.rows());
108 check_size_match(function,
109 "Size of random variable", size_y,
110 "columns of covariance parameter", Sigma.cols());
111
112 for (size_t i = 0; i < size_vec; i++) {
113 check_finite(function, "Location parameter", mu_vec[i]);
114 check_not_nan(function, "Random variable", y_vec[i]);
115 }
116
117 if (size_y == 0) // y_vec[0].size() == 0
118 return lp;
119
120 if (include_summand<propto, T_covar_elem>::value)
121 lp += 0.5 * log_determinant_ldlt(ldlt_Sigma) * size_vec;
122
123 if (include_summand<propto>::value)
124 lp += NEG_LOG_SQRT_TWO_PI * size_y * size_vec;
125
126 if (include_summand<propto, T_y, T_loc, T_covar_elem>::value) {
127 lp_type sum_lp_vec(0.0);
128 for (size_t i = 0; i < size_vec; i++) {
129 Eigen::Matrix<typename return_type<T_y, T_loc>::type,
130 Eigen::Dynamic, 1> y_minus_mu(size_y);
131 for (int j = 0; j < size_y; j++)
132 y_minus_mu(j) = y_vec[i](j) - mu_vec[i](j);
133 sum_lp_vec += trace_quad_form(Sigma, y_minus_mu);
134 }
135 lp -= 0.5*sum_lp_vec;
136 }
137 return lp;
138 }
139
140 template <typename T_y, typename T_loc, typename T_covar>
141 inline
142 typename return_type<T_y, T_loc, T_covar>::type
143 multi_normal_prec_log(const T_y& y, const T_loc& mu, const T_covar& Sigma) {
144 return multi_normal_prec_log<false>(y, mu, Sigma);
145 }
146
147 }
148 }
149 #endif
150
check_not_nan.hpp
stan::math::sum
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
multiply.hpp
max_size_mvt.hpp
stan::max_size_mvt
size_t max_size_mvt(const T1 &x1, const T2 &x2)
Definition: max_size_mvt.hpp:10
stan::math::trace_quad_form
fvar< T > trace_quad_form(const Eigen::Matrix< fvar< T >, RA, CA > &A, const Eigen::Matrix< fvar< T >, RB, CB > &B)
Definition: trace_quad_form.hpp:18
mdivide_left_spd.hpp
log_determinant.hpp
stan::math::check_not_nan
bool check_not_nan(const char *function, const char *name, const T_y &y)
Return true if y is not NaN.
Definition: check_not_nan.hpp:63
check_size_match.hpp
check_positive.hpp
stan
Definition: log_sum_exp.hpp:8
VectorViewMvt.hpp
stan::scalar_type::type
scalar_type_helper< is_vector< T >::value, T >::type type
Definition: scalar_type.hpp:35
check_symmetric.hpp
stan::math::include_summand
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: include_summand.hpp:36
stan::math::multi_normal_prec_log
return_type< T_y, T_loc, T_covar >::type multi_normal_prec_log(const T_y &y, const T_loc &mu, const T_covar &Sigma)
Definition: multi_normal_prec_log.hpp:35
log.hpp
stan::return_type::type
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
include_summand.hpp
check_ldlt_factor.hpp
columns_dot_product.hpp
stan::math::LDLT_factor
Definition: LDLT_factor.hpp:18
size_
size_t size_
Definition: dot_self.hpp:18
trace_quad_form.hpp
stan::math::check_positive
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
Definition: check_positive.hpp:68
stan::math::check_size_match
bool check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Return true if the provided sizes match.
Definition: check_size_match.hpp:30
dot_product.hpp
stan::math::NEG_LOG_SQRT_TWO_PI
const double NEG_LOG_SQRT_TWO_PI
Definition: constants.hpp:184
stan::math::check_finite
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.
Definition: check_finite.hpp:62
constants.hpp
check_finite.hpp
sum.hpp
stan::VectorViewMvt
Definition: VectorViewMvt.hpp:16
stan::math::check_symmetric
bool check_symmetric(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is symmetric.
Definition: check_symmetric.hpp:37
stan::math::log_determinant_ldlt
T log_determinant_ldlt(stan::math::LDLT_factor< T, R, C > &A)
Definition: log_determinant_ldlt.hpp:12
dot_self.hpp
columns_dot_self.hpp
subtract.hpp
mdivide_left_tri_low.hpp
stan::length_mvt
size_t length_mvt(const Eigen::Matrix< T, R, C > &)
Definition: length_mvt.hpp:12
stan::math::check_ldlt_factor
bool check_ldlt_factor(const char *function, const char *name, stan::math::LDLT_factor< T, R, C > &A)
Return true if the argument is a valid stan::math::LDLT_factor.
Definition: check_ldlt_factor.hpp:34
log_determinant_ldlt.hpp

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