All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
multi_normal_cholesky_log.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_MAT_PROB_MULTI_NORMAL_CHOLESKY_LOG_HPP
#define STAN_MATH_PRIM_MAT_PROB_MULTI_NORMAL_CHOLESKY_LOG_HPP
#include <boost/random/normal_distribution.hpp>
#include <boost/random/variate_generator.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/mat/fun/columns_dot_product.hpp>
#include <stan/math/prim/mat/fun/columns_dot_self.hpp>
#include <stan/math/prim/mat/fun/dot_product.hpp>
#include <stan/math/prim/mat/fun/dot_self.hpp>
#include <stan/math/prim/mat/fun/log.hpp>
#include <stan/math/prim/mat/fun/log_determinant.hpp>
#include <stan/math/prim/mat/fun/mdivide_left_spd.hpp>
#include <stan/math/prim/mat/fun/mdivide_left_tri_low.hpp>
#include <stan/math/prim/mat/fun/multiply.hpp>
#include <stan/math/prim/mat/fun/subtract.hpp>
#include <stan/math/prim/mat/fun/sum.hpp>
#include <stan/math/prim/scal/meta/VectorViewMvt.hpp>
#include <stan/math/prim/scal/meta/max_size_mvt.hpp>
#include <stan/math/prim/scal/fun/constants.hpp>
#include <stan/math/prim/scal/meta/include_summand.hpp>
namespace stan {
namespace math {
using Eigen::Dynamic;
template <bool propto,
typename T_y, typename T_loc, typename T_covar>
typename 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) {
static const char* function("stan::math::multi_normal_cholesky_log");
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 stan::math::mdivide_left_tri_low;
using stan::math::dot_self;
using stan::math::multiply;
using stan::math::subtract;
using stan::math::sum;
using stan::math::check_size_match;
using stan::math::check_finite;
using stan::math::check_not_nan;
VectorViewMvt<const T_y> y_vec(y);
VectorViewMvt<const T_loc> mu_vec(mu);
// size of std::vector of Eigen vectors
size_t size_vec = max_size_mvt(y, mu);
// Check if every vector of the array has the same size
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", L.rows());
check_size_match(function,
"Size of random variable", size_y,
"columns of covariance parameter", L.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 -= L.diagonal().array().log().sum() * 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);
Eigen::Matrix<typename return_type<T_y, T_loc, T_covar>::type,
Dynamic, 1>
half(mdivide_left_tri_low(L, y_minus_mu));
// FIXME: this code does not compile. revert after fixing subtract()
// Eigen::Matrix<typename
// boost::math::tools::promote_args<T_covar,
// typename value_type<T_loc>::type,
// typename value_type<T_y>::type>::type>::type,
// Dynamic, 1>
// half(mdivide_left_tri_low(L, subtract(y, mu)));
sum_lp_vec += dot_self(half);
}
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_cholesky_log(const T_y& y, const T_loc& mu, const T_covar& L) {
return multi_normal_cholesky_log<false>(y, mu, L);
}
}
}
#endif
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
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
stan::math::subtract
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R, C > subtract(const Eigen::Matrix< T1, R, C > &m1, const Eigen::Matrix< T2, R, C > &m2)
Return the result of subtracting the second specified matrix from the first specified matrix...
Definition: subtract.hpp:27
VectorViewMvt.hpp
stan::math::multiply
Eigen::Matrix< fvar< T >, R1, C1 > multiply(const Eigen::Matrix< fvar< T >, R1, C1 > &m, const fvar< T > &c)
Definition: multiply.hpp:20
stan::scalar_type::type
scalar_type_helper< is_vector< T >::value, T >::type type
Definition: scalar_type.hpp:38
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:36
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:50
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
stan::length_mvt
size_t length_mvt(const T &)
Definition: length_mvt.hpp:12
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
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
Definition: VectorViewMvt.hpp:16
dot_self.hpp
columns_dot_self.hpp
subtract.hpp
mdivide_left_tri_low.hpp

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