Skip to content

Commit

Permalink
updating test; test was incorrect. It passed in a non-cholesky factor…
Browse files Browse the repository at this point in the history
…. New code properly catches the error.
  • Loading branch information
syclik committed Feb 23, 2024
1 parent 8cd890f commit 44920e1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 49 deletions.
55 changes: 31 additions & 24 deletions test/unit/math/mix/prob/inv_wishart_cholesky_test.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
#include <test/unit/math/test_ad.hpp>

TEST(ProbDistributionsInvWishartCholesky, matvar) {
auto f = [](const auto& L_Y, const auto& dof, const auto& L_S) {
return stan::math::inv_wishart_cholesky_lpdf(L_Y, dof, L_S);
auto f = [](const auto& Y, const auto& dof, const auto& Sigma) {
auto symmetric_Y = ((Y + Y.transpose()) * 0.5).eval();
auto symmetric_Sigma = ((Sigma + Sigma.transpose()) * 0.5).eval();

auto L_Y = stan::math::cholesky_decompose(symmetric_Y);
stan::plain_type_t<decltype(L_Y)> L_Y_plain = L_Y;

auto L_S = stan::math::cholesky_decompose(symmetric_Sigma);
stan::plain_type_t<decltype(L_S)> L_S_plain = L_S;

return stan::math::inv_wishart_cholesky_lpdf(L_Y, dof, L_S_plain);
};

double dof = 3.4;
Eigen::MatrixXd L_Y11(1, 1);
L_Y11 << 1;
Eigen::MatrixXd L_S11(1, 1);
L_S11 << 1;
stan::test::expect_ad(f, L_Y11, dof, L_S11);
stan::test::expect_ad_matvar(f, L_Y11, dof, L_S11);

Eigen::MatrixXd L_Y00(0, 0);
Eigen::MatrixXd L_S00(0, 0);
stan::test::expect_ad(f, L_Y00, dof, L_S00);
stan::test::expect_ad_matvar(f, L_Y00, dof, L_S00);

Eigen::MatrixXd y22(2, 2);
y22 << 1.0, 0.1, 0.1, 1.5;
Eigen::MatrixXd L_Y22 = y22.llt().matrixL();
Eigen::MatrixXd Y11(1, 1);
Y11 << 1;
Eigen::MatrixXd Sigma11(1, 1);
Sigma11 << 1;
stan::test::expect_ad(f, Y11, dof, Sigma11);
stan::test::expect_ad_matvar(f, Y11, dof, Sigma11);

Eigen::MatrixXd Y00(0, 0);
Eigen::MatrixXd Sigma00(0, 0);
stan::test::expect_ad(f, Y00, dof, Sigma00);
stan::test::expect_ad_matvar(f, Y00, dof, Sigma00);

Eigen::MatrixXd Y22(2, 2);
Y22 << 1.0, 0.1, 0.1, 1.5;
Eigen::MatrixXd Sigma22(2, 2);
Sigma22 << 1.5, 0.5, 0.5, 1.1;
Eigen::MatrixXd L_S22 = Sigma22.llt().matrixL();
stan::test::expect_ad(f, L_Y22, dof, L_S22);
stan::test::expect_ad_matvar(f, L_Y22, dof, L_S22);
stan::test::expect_ad(f, Y22, dof, Sigma22);
stan::test::expect_ad_matvar(f, Y22, dof, Sigma22);

// Error sizes
stan::test::expect_ad(f, L_Y00, dof, L_S11);
stan::test::expect_ad(f, L_Y11, dof, L_S00);
stan::test::expect_ad_matvar(f, L_Y00, dof, L_S11);
stan::test::expect_ad_matvar(f, L_Y11, dof, L_S00);
stan::test::expect_ad(f, Y00, dof, Sigma11);
stan::test::expect_ad(f, Y11, dof, Sigma00);
stan::test::expect_ad_matvar(f, Y00, dof, Sigma11);
stan::test::expect_ad_matvar(f, Y11, dof, Sigma00);
}

TEST(ProbDistributionsInvWishartCholesky, fvar_var) {
Expand Down
55 changes: 31 additions & 24 deletions test/unit/math/mix/prob/wishart_cholesky_test.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
#include <test/unit/math/test_ad.hpp>

TEST(ProbDistributionsWishartCholesky, matvar) {
auto f = [](const auto& L_Y, const auto& dof, const auto& L_S) {
return stan::math::wishart_cholesky_lpdf(L_Y, dof, L_S);
auto f = [](const auto& Y, const auto& dof, const auto& Sigma) {
auto symmetric_Y = ((Y + Y.transpose()) * 0.5).eval();
auto symmetric_Sigma = ((Sigma + Sigma.transpose()) * 0.5).eval();

auto L_Y = stan::math::cholesky_decompose(symmetric_Y);
stan::plain_type_t<decltype(L_Y)> L_Y_plain = L_Y;

auto L_S = stan::math::cholesky_decompose(symmetric_Sigma);
stan::plain_type_t<decltype(L_S)> L_S_plain = L_S;

return stan::math::wishart_cholesky_lpdf(L_Y, dof, L_S_plain);
};

double dof = 3.4;
Eigen::MatrixXd L_Y11(1, 1);
L_Y11 << 1;
Eigen::MatrixXd L_S11(1, 1);
L_S11 << 1;
stan::test::expect_ad(f, L_Y11, dof, L_S11);
stan::test::expect_ad_matvar(f, L_Y11, dof, L_S11);

Eigen::MatrixXd L_Y00(0, 0);
Eigen::MatrixXd L_S00(0, 0);
stan::test::expect_ad(f, L_Y00, dof, L_S00);
stan::test::expect_ad_matvar(f, L_Y00, dof, L_S00);

Eigen::MatrixXd y22(2, 2);
y22 << 1.0, 0.1, 0.1, 1.5;
Eigen::MatrixXd L_Y22 = y22.llt().matrixL();
Eigen::MatrixXd Y11(1, 1);
Y11 << 1;
Eigen::MatrixXd Sigma11(1, 1);
Sigma11 << 1;
stan::test::expect_ad(f, Y11, dof, Sigma11);
stan::test::expect_ad_matvar(f, Y11, dof, Sigma11);

Eigen::MatrixXd Y00(0, 0);
Eigen::MatrixXd Sigma00(0, 0);
stan::test::expect_ad(f, Y00, dof, Sigma00);
stan::test::expect_ad_matvar(f, Y00, dof, Sigma00);

Eigen::MatrixXd Y22(2, 2);
Y22 << 1.0, 0.1, 0.1, 1.5;
Eigen::MatrixXd Sigma22(2, 2);
Sigma22 << 1.5, 0.5, 0.5, 1.1;
Eigen::MatrixXd L_S22 = Sigma22.llt().matrixL();
stan::test::expect_ad(f, L_Y22, dof, L_S22);
stan::test::expect_ad_matvar(f, L_Y22, dof, L_S22);
stan::test::expect_ad(f, Y22, dof, Sigma22);
stan::test::expect_ad_matvar(f, Y22, dof, Sigma22);

// Error sizes
stan::test::expect_ad(f, L_Y00, dof, L_S11);
stan::test::expect_ad(f, L_Y11, dof, L_S00);
stan::test::expect_ad_matvar(f, L_Y00, dof, L_S11);
stan::test::expect_ad_matvar(f, L_Y11, dof, L_S00);
stan::test::expect_ad(f, Y00, dof, Sigma11);
stan::test::expect_ad(f, Y11, dof, Sigma00);
stan::test::expect_ad_matvar(f, Y00, dof, Sigma11);
stan::test::expect_ad_matvar(f, Y11, dof, Sigma00);
}

TEST(ProbDistributionsWishartCholesky, fvar_var) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ TEST(ProbDistributionsMultiNormalCholesky, opencl_matches_cpu_small) {
std::vector<Eigen::VectorXd> mu3{mu1, mu2};
std::vector<Eigen::RowVectorXd> mu4{mu2, mu1};
Eigen::MatrixXd L(N, N);
L << 1, 0, 0, 2, 3, 0, -4, 5, -6;
L << 4, 0, 0, -1, 1, 0, -4, -2, 3;

stan::math::test::compare_cpu_opencl_prim_rev(
multi_normal_cholesky_lpdf_functor, y1, mu1, L);
Expand Down

0 comments on commit 44920e1

Please sign in to comment.