multi_normal_lpdf.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_MAT_PROB_MULTI_NORMAL_LPDF_HPP
#define STAN_MATH_PRIM_MAT_PROB_MULTI_NORMAL_LPDF_HPP
#include <stan/math/prim/mat/err/check_ldlt_factor.hpp>
#include <stan/math/prim/mat/err/check_symmetric.hpp>
#include <stan/math/prim/mat/fun/trace_inv_quad_form_ldlt.hpp>
#include <stan/math/prim/mat/fun/log_determinant_ldlt.hpp>
#include <stan/math/prim/mat/meta/VectorViewMvt.hpp>
#include <stan/math/prim/scal/err/check_size_match.hpp>
#include <stan/math/prim/scal/err/check_finite.hpp>
#include <stan/math/prim/scal/err/check_not_nan.hpp>
#include <stan/math/prim/scal/err/check_positive.hpp>
#include <stan/math/prim/scal/fun/constants.hpp>
#include <stan/math/prim/scal/meta/return_type.hpp>
#include <stan/math/prim/scal/meta/max_size_mvt.hpp>
#include <stan/math/prim/scal/meta/include_summand.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/variate_generator.hpp>
namespace stan {
namespace math {
template <bool propto,
typename T_y, typename T_loc, typename T_covar>
typename return_type<T_y, T_loc, T_covar>::type
multi_normal_lpdf(const T_y& y,
const T_loc& mu,
const T_covar& Sigma) {
static const char* function("multi_normal_lpdf");
typedef typename scalar_type<T_covar>::type T_covar_elem;
typedef typename return_type<T_y, T_loc, T_covar>::type lp_type;
lp_type lp(0.0);
using Eigen::Dynamic;
check_positive(function, "Covariance matrix rows", Sigma.rows());
check_symmetric(function, "Covariance matrix", Sigma);
LDLT_factor<T_covar_elem, Dynamic, Dynamic> ldlt_Sigma(Sigma);
check_ldlt_factor(function,
"LDLT_Factor of covariance parameter", ldlt_Sigma);
VectorViewMvt<const T_y> y_vec(y);
VectorViewMvt<const T_loc> mu_vec(mu);
size_t size_vec = max_size_mvt(y, mu);
int size_y = y_vec[0].size();
int size_mu = mu_vec[0].size();
if (size_vec > 1) {
int size_y_old = size_y;
int size_y_new;
for (size_t i = 1, size_ = length_mvt(y); i < size_; i++) {
int size_y_new = y_vec[i].size();
check_size_match(function,
"Size of one of the vectors of "
"the random variable", size_y_new,
"Size of another vector of the "
"random variable", size_y_old);
size_y_old = size_y_new;
}
int size_mu_old = size_mu;
int size_mu_new;
for (size_t i = 1, size_ = length_mvt(mu); i < size_; i++) {
int size_mu_new = mu_vec[i].size();
check_size_match(function,
"Size of one of the vectors of "
"the location variable", size_mu_new,
"Size of another vector of the "
"location variable", size_mu_old);
size_mu_old = size_mu_new;
}
(void) size_y_old;
(void) size_y_new;
(void) size_mu_old;
(void) size_mu_new;
}
check_size_match(function,
"Size of random variable", size_y,
"size of location parameter", size_mu);
check_size_match(function,
"Size of random variable", size_y,
"rows of covariance parameter", Sigma.rows());
check_size_match(function,
"Size of random variable", size_y,
"columns of covariance parameter", Sigma.cols());
for (size_t i = 0; i < size_vec; i++) {
check_finite(function, "Location parameter", mu_vec[i]);
check_not_nan(function, "Random variable", y_vec[i]);
}
if (size_y == 0)
return lp;
if (include_summand<propto>::value)
lp += NEG_LOG_SQRT_TWO_PI * size_y * size_vec;
if (include_summand<propto, T_covar_elem>::value)
lp -= 0.5 * log_determinant_ldlt(ldlt_Sigma) * size_vec;
if (include_summand<propto, T_y, T_loc, T_covar_elem>::value) {
lp_type sum_lp_vec(0.0);
for (size_t i = 0; i < size_vec; i++) {
Eigen::Matrix<typename return_type<T_y, T_loc>::type, Dynamic, 1>
y_minus_mu(size_y);
for (int j = 0; j < size_y; j++)
y_minus_mu(j) = y_vec[i](j)-mu_vec[i](j);
sum_lp_vec += trace_inv_quad_form_ldlt(ldlt_Sigma, y_minus_mu);
}
lp -= 0.5*sum_lp_vec;
}
return lp;
}
template <typename T_y, typename T_loc, typename T_covar>
inline
typename return_type<T_y, T_loc, T_covar>::type
multi_normal_lpdf(const T_y& y,
const T_loc& mu,
const T_covar& Sigma) {
return multi_normal_lpdf<false>(y, mu, Sigma);
}
}
}
#endif
check_not_nan.hpp
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 LDLT_factor< T1, R2, C2 > &A, const Eigen::Matrix< T2, R3, C3 > &B)
Definition: trace_inv_quad_form_ldlt.hpp:27
stan::math::check_finite
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
Definition: check_finite.hpp:57
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::multi_normal_lpdf
return_type< T_y, T_loc, T_covar >::type multi_normal_lpdf(const T_y &y, const T_loc &mu, const T_covar &Sigma)
Definition: multi_normal_lpdf.hpp:26
check_size_match.hpp
check_positive.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::check_ldlt_factor
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.
Definition: check_ldlt_factor.hpp:33
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:34
stan::math::check_size_match
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.
Definition: check_size_match.hpp:29
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:35
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:13
stan::math::check_not_nan
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
Definition: check_not_nan.hpp:57
size_
size_t size_
Definition: dot_self.hpp:18
stan::math::check_symmetric
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.
Definition: check_symmetric.hpp:35
stan::math::NEG_LOG_SQRT_TWO_PI
const double NEG_LOG_SQRT_TWO_PI
Definition: constants.hpp:181
constants.hpp
check_finite.hpp
stan::VectorViewMvt
VectorViewMvt is a template expression that wraps either an Eigen::Matrix or a std::vector<Eigen::Mat...
Definition: VectorViewMvt.hpp:29
stan::math::check_positive
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
Definition: check_positive.hpp:63
stan::length_mvt
size_t length_mvt(const Eigen::Matrix< T, R, C > &)
Definition: length_mvt.hpp:12
stan::math::log_determinant_ldlt
T log_determinant_ldlt(LDLT_factor< T, R, C > &A)
Definition: log_determinant_ldlt.hpp:12
log_determinant_ldlt.hpp

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