Skip to content

Commit

Permalink
make test in mix for multi normal with matrix inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveBronder committed Jul 20, 2021
1 parent cc3f2f5 commit 0a293e4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
5 changes: 1 addition & 4 deletions stan/math/prim/prob/multi_normal_lpdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ return_type_t<T_y, T_loc, T_covar> multi_normal_lpdf(const T_y& y,
const T_loc& mu,
const T_covar& Sigma) {
using T_covar_elem = typename scalar_type<T_covar>::type;
using lp_type = return_type_t<T_y, T_loc, T_covar>;
using Eigen::Dynamic;
static const char* function = "multi_normal_lpdf";
check_positive(function, "Covariance matrix rows", Sigma.rows());
auto&& Sigma_ref = to_ref(Sigma);
Expand All @@ -125,7 +123,7 @@ return_type_t<T_y, T_loc, T_covar> multi_normal_lpdf(const T_y& y,
"columns of location parameter", mu.cols());
check_size_match(function, "Columns of random variable", y.cols(),
"rows of covariance parameter", Sigma.rows());
lp_type lp(0.0);
return_type_t<T_y, T_loc, T_covar> lp(0.0);
auto&& y_ref = to_ref(y);
auto&& mu_ref = to_ref(mu);
const Eigen::Index size_y = y_ref.cols();
Expand All @@ -146,7 +144,6 @@ return_type_t<T_y, T_loc, T_covar> multi_normal_lpdf(const T_y& y,
}

if (include_summand<propto, T_y, T_loc, T_covar_elem>::value) {
lp_type sum_lp_vec(0.0);
lp -= 0.5 * trace_inv_quad_form_ldlt(ldlt_Sigma, (y_ref - mu_ref).transpose());
}
return lp;
Expand Down
16 changes: 16 additions & 0 deletions test/unit/math/mix/prob/multi_normal_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ TEST(ProbDistributionsMultiNormal, matvar) {
stan::test::expect_ad_matvar(f, y1, mu1, Sigma00);
}

TEST(ProbDistributionsMultiNormal, matrix) {
auto f = [](const auto& y, const auto& mu, const auto& sigma) {
auto sigma_sym = stan::math::multiply(0.5, sigma + sigma.transpose());
return stan::math::multi_normal_lpdf(y, mu, sigma_sym);
};

Eigen::MatrixXd y(2, 3);
y << 2.0, -2.0, 11.0, 2.0, -2.0, 11.0;
Eigen::MatrixXd mu(2, 3);
mu << 1.0, -1.0, 3.0, 1.0, -1.0, 3.0;
Eigen::MatrixXd Sigma(3, 3);
Sigma << 9.0, -3.0, 0.0, -3.0, 4.0, 0.0, 0.0, 0.0, 5.0;
stan::test::expect_ad(f, y, mu, Sigma);
stan::test::expect_ad_matvar(f, y, mu, Sigma);
}

TEST(ProbDistributionsMultiNormal, fvar_var) {
using Eigen::Dynamic;
using Eigen::Matrix;
Expand Down

0 comments on commit 0a293e4

Please sign in to comment.