| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

multi_normal_cholesky_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_MULTI_NORMAL_CHOLESKY_LOG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_MULTI_NORMAL_CHOLESKY_LOG_HPP
3
4 #include <stan/math/prim/mat/fun/columns_dot_product.hpp>
5 #include <stan/math/prim/mat/fun/columns_dot_self.hpp>
6 #include <stan/math/prim/mat/fun/dot_product.hpp>
7 #include <stan/math/prim/mat/fun/dot_self.hpp>
8 #include <stan/math/prim/mat/fun/log.hpp>
9 #include <stan/math/prim/mat/fun/log_determinant.hpp>
10 #include <stan/math/prim/mat/fun/mdivide_left_spd.hpp>
11 #include <stan/math/prim/mat/fun/mdivide_left_tri_low.hpp>
12 #include <stan/math/prim/mat/fun/multiply.hpp>
13 #include <stan/math/prim/mat/fun/subtract.hpp>
14 #include <stan/math/prim/mat/fun/sum.hpp>
15 #include <stan/math/prim/mat/meta/VectorViewMvt.hpp>
16 #include <stan/math/prim/scal/err/check_size_match.hpp>
17 #include <stan/math/prim/scal/err/check_finite.hpp>
18 #include <stan/math/prim/scal/err/check_not_nan.hpp>
19 #include <stan/math/prim/scal/fun/constants.hpp>
20 #include <stan/math/prim/scal/meta/max_size_mvt.hpp>
21 #include <stan/math/prim/scal/meta/include_summand.hpp>
22 #include <boost/random/normal_distribution.hpp>
23 #include <boost/random/variate_generator.hpp>
24
25 namespace stan {
26 namespace math {
44 template <bool propto,
45 typename T_y, typename T_loc, typename T_covar>
46 typename return_type<T_y, T_loc, T_covar>::type
47 multi_normal_cholesky_log(const T_y& y,
48 const T_loc& mu,
49 const T_covar& L) {
50 static const char* function("multi_normal_cholesky_log");
51 typedef typename scalar_type<T_covar>::type T_covar_elem;
52 typedef typename return_type<T_y, T_loc, T_covar>::type lp_type;
53 lp_type lp(0.0);
54
55
56 VectorViewMvt<const T_y> y_vec(y);
57 VectorViewMvt<const T_loc> mu_vec(mu);
58 size_t size_vec = max_size_mvt(y, mu);
59
60 int size_y = y_vec[0].size();
61 int size_mu = mu_vec[0].size();
62 if (size_vec > 1) {
63 int size_y_old = size_y;
64 int size_y_new;
65 for (size_t i = 1, size_ = length_mvt(y); i < size_; i++) {
66 int size_y_new = y_vec[i].size();
67 check_size_match(function,
68 "Size of one of the vectors of "
69 "the random variable", size_y_new,
70 "Size of another vector of the "
71 "random variable", size_y_old);
72 size_y_old = size_y_new;
73 }
74 int size_mu_old = size_mu;
75 int size_mu_new;
76 for (size_t i = 1, size_ = length_mvt(mu); i < size_; i++) {
77 int size_mu_new = mu_vec[i].size();
78 check_size_match(function,
79 "Size of one of the vectors of "
80 "the location variable", size_mu_new,
81 "Size of another vector of the "
82 "location variable", size_mu_old);
83 size_mu_old = size_mu_new;
84 }
85 (void) size_y_old;
86 (void) size_y_new;
87 (void) size_mu_old;
88 (void) size_mu_new;
89 }
90
91 check_size_match(function,
92 "Size of random variable", size_y,
93 "size of location parameter", size_mu);
94 check_size_match(function,
95 "Size of random variable", size_y,
96 "rows of covariance parameter", L.rows());
97 check_size_match(function,
98 "Size of random variable", size_y,
99 "columns of covariance parameter", L.cols());
100
101 for (size_t i = 0; i < size_vec; i++) {
102 check_finite(function, "Location parameter", mu_vec[i]);
103 check_not_nan(function, "Random variable", y_vec[i]);
104 }
105
106 if (size_y == 0)
107 return lp;
108
109 if (include_summand<propto>::value)
110 lp += NEG_LOG_SQRT_TWO_PI * size_y * size_vec;
111
112 if (include_summand<propto, T_covar_elem>::value)
113 lp -= L.diagonal().array().log().sum() * size_vec;
114
115 if (include_summand<propto, T_y, T_loc, T_covar_elem>::value) {
116 lp_type sum_lp_vec(0.0);
117 for (size_t i = 0; i < size_vec; i++) {
118 Eigen::Matrix<typename return_type<T_y, T_loc>::type,
119 Eigen::Dynamic, 1> y_minus_mu(size_y);
120 for (int j = 0; j < size_y; j++)
121 y_minus_mu(j) = y_vec[i](j)-mu_vec[i](j);
122 Eigen::Matrix<typename return_type<T_y, T_loc, T_covar>::type,
123 Eigen::Dynamic, 1>
124 half(mdivide_left_tri_low(L, y_minus_mu));
125 // FIXME: this code does not compile. revert after fixing subtract()
126 // Eigen::Matrix<typename
127 // boost::math::tools::promote_args<T_covar,
128 // typename value_type<T_loc>::type,
129 // typename value_type<T_y>::type>::type>::type,
130 // Eigen::Dynamic, 1>
131 // half(mdivide_left_tri_low(L, subtract(y, mu)));
132 sum_lp_vec += dot_self(half);
133 }
134 lp -= 0.5*sum_lp_vec;
135 }
136 return lp;
137 }
138
139 template <typename T_y, typename T_loc, typename T_covar>
140 inline
141 typename return_type<T_y, T_loc, T_covar>::type
142 multi_normal_cholesky_log(const T_y& y, const T_loc& mu, const T_covar& L) {
143 return multi_normal_cholesky_log<false>(y, mu, L);
144 }
145
146 }
147 }
148 #endif
check_not_nan.hpp
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
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:60
check_size_match.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:34
stan::math::dot_self
fvar< T > dot_self(const Eigen::Matrix< fvar< T >, R, C > &v)
Definition: dot_self.hpp:16
stan::math::include_summand
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: include_summand.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
columns_dot_product.hpp
stan::math::multi_normal_cholesky_log
return_type< T_y, T_loc, T_covar >::type multi_normal_cholesky_log(const T_y &y, const T_loc &mu, const T_covar &L)
The log of the multivariate normal density for the given y, mu, and a Cholesky factor L of the varian...
Definition: multi_normal_cholesky_log.hpp:47
size_
size_t size_
Definition: dot_self.hpp:18
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:181
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:60
constants.hpp
check_finite.hpp
stan::math::mdivide_left_tri_low
Eigen::Matrix< fvar< T >, R1, C1 > mdivide_left_tri_low(const Eigen::Matrix< fvar< T >, R1, C1 > &A, const Eigen::Matrix< fvar< T >, R2, C2 > &b)
Definition: mdivide_left_tri_low.hpp:22
sum.hpp
stan::VectorViewMvt
VectorViewMvt is a template expression that wraps either an Eigen::Matrix or a std::vector
Definition: VectorViewMvt.hpp:29
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 Home Page ] © 2011–2016, Stan Development Team.