| Stan Math Library 2.11.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

multi_normal_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_MULTI_NORMAL_LOG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_MULTI_NORMAL_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/trace_inv_quad_form_ldlt.hpp>
7 #include <stan/math/prim/mat/fun/log_determinant_ldlt.hpp>
8 #include <stan/math/prim/mat/meta/VectorViewMvt.hpp>
9 #include <stan/math/prim/scal/err/check_size_match.hpp>
10 #include <stan/math/prim/scal/err/check_finite.hpp>
11 #include <stan/math/prim/scal/err/check_not_nan.hpp>
12 #include <stan/math/prim/scal/err/check_positive.hpp>
13 #include <stan/math/prim/scal/fun/constants.hpp>
14 #include <stan/math/prim/scal/meta/return_type.hpp>
15 #include <stan/math/prim/scal/meta/max_size_mvt.hpp>
16 #include <stan/math/prim/scal/meta/include_summand.hpp>
17 #include <boost/random/normal_distribution.hpp>
18 #include <boost/random/variate_generator.hpp>
19
20 namespace stan {
21
22 namespace math {
23
24 template <bool propto,
25 typename T_y, typename T_loc, typename T_covar>
26 typename return_type<T_y, T_loc, T_covar>::type
27 multi_normal_log(const T_y& y,
28 const T_loc& mu,
29 const T_covar& Sigma) {
30 static const char* function("stan::math::multi_normal_log");
31 typedef typename scalar_type<T_covar>::type T_covar_elem;
32 typedef typename return_type<T_y, T_loc, T_covar>::type lp_type;
33 lp_type lp(0.0);
34
35 using stan::math::check_size_match;
36 using stan::math::check_finite;
37 using stan::math::check_not_nan;
38 using stan::math::check_positive;
39 using stan::math::check_symmetric;
40 using stan::math::check_ldlt_factor;
41 using Eigen::Dynamic;
42
43 check_positive(function, "Covariance matrix rows", Sigma.rows());
44 check_symmetric(function, "Covariance matrix", Sigma);
45
46 stan::math::LDLT_factor<T_covar_elem, Dynamic, Dynamic> ldlt_Sigma(Sigma);
47 check_ldlt_factor(function,
48 "LDLT_Factor of covariance parameter", ldlt_Sigma);
49
50 VectorViewMvt<const T_y> y_vec(y);
51 VectorViewMvt<const T_loc> mu_vec(mu);
52 // size of std::vector of Eigen vectors
53 size_t size_vec = max_size_mvt(y, mu);
54
55 // Check if every vector of the array has the same size
56 int size_y = y_vec[0].size();
57 int size_mu = mu_vec[0].size();
58 if (size_vec > 1) {
59 int size_y_old = size_y;
60 int size_y_new;
61 for (size_t i = 1, size_ = length_mvt(y); i < size_; i++) {
62 int size_y_new = y_vec[i].size();
63 check_size_match(function,
64 "Size of one of the vectors of "
65 "the random variable", size_y_new,
66 "Size of another vector of the "
67 "random variable", size_y_old);
68 size_y_old = size_y_new;
69 }
70 int size_mu_old = size_mu;
71 int size_mu_new;
72 for (size_t i = 1, size_ = length_mvt(mu); i < size_; i++) {
73 int size_mu_new = mu_vec[i].size();
74 check_size_match(function,
75 "Size of one of the vectors of "
76 "the location variable", size_mu_new,
77 "Size of another vector of the "
78 "location variable", size_mu_old);
79 size_mu_old = size_mu_new;
80 }
81 (void) size_y_old;
82 (void) size_y_new;
83 (void) size_mu_old;
84 (void) size_mu_new;
85 }
86
87 check_size_match(function,
88 "Size of random variable", size_y,
89 "size of location parameter", size_mu);
90 check_size_match(function,
91 "Size of random variable", size_y,
92 "rows of covariance parameter", Sigma.rows());
93 check_size_match(function,
94 "Size of random variable", size_y,
95 "columns of covariance parameter", Sigma.cols());
96
97 for (size_t i = 0; i < size_vec; i++) {
98 check_finite(function, "Location parameter", mu_vec[i]);
99 check_not_nan(function, "Random variable", y_vec[i]);
100 }
101
102 if (size_y == 0) // y_vec[0].size() == 0
103 return lp;
104
105 if (include_summand<propto>::value)
106 lp += NEG_LOG_SQRT_TWO_PI * size_y * size_vec;
107
108 if (include_summand<propto, T_covar_elem>::value)
109 lp -= 0.5 * log_determinant_ldlt(ldlt_Sigma) * size_vec;
110
111 if (include_summand<propto, T_y, T_loc, T_covar_elem>::value) {
112 lp_type sum_lp_vec(0.0);
113 for (size_t i = 0; i < size_vec; i++) {
114 Eigen::Matrix<typename return_type<T_y, T_loc>::type, Dynamic, 1>
115 y_minus_mu(size_y);
116 for (int j = 0; j < size_y; j++)
117 y_minus_mu(j) = y_vec[i](j)-mu_vec[i](j);
118 sum_lp_vec += trace_inv_quad_form_ldlt(ldlt_Sigma, y_minus_mu);
119 }
120 lp -= 0.5*sum_lp_vec;
121 }
122 return lp;
123 }
124
125 template <typename T_y, typename T_loc, typename T_covar>
126 inline
127 typename return_type<T_y, T_loc, T_covar>::type
128 multi_normal_log(const T_y& y,
129 const T_loc& mu,
130 const T_covar& Sigma) {
131 return multi_normal_log<false>(y, mu, Sigma);
132 }
133
134 }
135 }
136
137 #endif
check_not_nan.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::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
trace_inv_quad_form_ldlt.hpp
VectorViewMvt.hpp
stan::scalar_type::type
scalar_type_helper< is_vector< T >::value, T >::type type
Definition: scalar_type.hpp:35
stan::math::trace_inv_quad_form_ldlt
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 stan::math::LDLT_factor< T1, R2, C2 > &A, const Eigen::Matrix< T2, R3, C3 > &B)
Definition: trace_inv_quad_form_ldlt.hpp:27
check_symmetric.hpp
return_type.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::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
stan::math::LDLT_factor
Definition: LDLT_factor.hpp:18
size_
size_t size_
Definition: dot_self.hpp:18
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
stan::math::NEG_LOG_SQRT_TWO_PI
const double NEG_LOG_SQRT_TWO_PI
Definition: constants.hpp:184
stan::math::multi_normal_log
return_type< T_y, T_loc, T_covar >::type multi_normal_log(const T_y &y, const T_loc &mu, const T_covar &Sigma)
Definition: multi_normal_log.hpp:27
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
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
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.