Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing squareness check in cholesky functions #3012

Merged
merged 3 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions stan/math/prim/prob/inv_wishart_cholesky_lpdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ return_type_t<T_y, T_dof, T_scale> inv_wishart_cholesky_lpdf(
static const char* function = "inv_wishart_cholesky_lpdf";
Eigen::Index k = L_Y.rows();
check_greater(function, "Degrees of freedom parameter", nu, k - 1);
check_size_match(function, "Rows of random variable", L_Y.rows(),
"columns of scale parameter", L_S.rows());

check_square(function, "Cholesky random variable", L_Y);
check_square(function, "Cholesky Scale parameter", L_S);
check_size_match(function, "side length of random variable", L_Y.rows(),
"side length of scale parameter", L_S.rows());

T_L_Y_ref L_Y_ref = L_Y;
check_cholesky_factor(function, "Cholesky random variable", L_Y_ref);
Expand Down
1 change: 1 addition & 0 deletions stan/math/prim/prob/inv_wishart_cholesky_rng.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ inline Eigen::MatrixXd inv_wishart_cholesky_rng(double nu,
using Eigen::MatrixXd;
static const char* function = "inv_wishart_cholesky_rng";
index_type_t<MatrixXd> k = L_S.rows();
check_square(function, "Cholesky Scale matrix", L_S);
check_greater(function, "degrees of freedom > dims - 1", nu, k - 1);
check_cholesky_factor(function, "Cholesky Scale matrix", L_S);

Expand Down
2 changes: 2 additions & 0 deletions stan/math/prim/prob/multi_normal_cholesky_lpdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ return_type_t<T_y, T_loc, T_covar> multi_normal_cholesky_lpdf(
"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]);
Expand Down
7 changes: 5 additions & 2 deletions stan/math/prim/prob/wishart_cholesky_lpdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ return_type_t<T_y, T_dof, T_scale> wishart_cholesky_lpdf(const T_y& L_Y,
Eigen::Index k = L_Y.rows();

check_greater(function, "Degrees of freedom parameter", nu, k - 1);
check_size_match(function, "Rows of random variable", L_Y.rows(),
"columns of scale parameter", L_S.rows());

check_square(function, "Cholesky random variable", L_Y);
check_square(function, "Cholesky scale parameter", L_S);
check_size_match(function, "side length of random variable", L_Y.rows(),
"side length of scale parameter", L_S.rows());

T_L_Y_ref L_Y_ref = L_Y;
check_cholesky_factor(function, "Cholesky random variable", L_Y_ref);
Expand Down
1 change: 1 addition & 0 deletions stan/math/prim/prob/wishart_cholesky_rng.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ inline Eigen::MatrixXd wishart_cholesky_rng(double nu,
static const char* function = "wishart_cholesky_rng";
index_type_t<MatrixXd> k = L_S.rows();
check_greater(function, "degrees of freedom > dims - 1", nu, k - 1);
check_square(function, "Cholesky Scale matrix", L_S);
check_cholesky_factor(function, "Cholesky Scale matrix", L_S);

MatrixXd B = MatrixXd::Zero(k, k);
Expand Down
3 changes: 2 additions & 1 deletion test/unit/math/prim/prob/inv_wishart_cholesky_rng_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ TEST(ProbDistributionsInvWishartCholesky, rng) {
boost::random::mt19937 rng;

MatrixXd omega(3, 4);
EXPECT_THROW(inv_wishart_cholesky_rng(3.0, omega, rng), std::domain_error);
EXPECT_THROW(inv_wishart_cholesky_rng(3.0, omega, rng),
std::invalid_argument);

MatrixXd sigma(3, 3);
sigma << 9.0, -3.0, 0.0, -3.0, 4.0, 1.0, 0.0, 1.0, 3.0;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/math/prim/prob/inv_wishart_cholesky_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ TEST(ProbDistributionsInvWishartCholesky, Error) {
nu = 5;
MatrixXd Y(2, 1);
EXPECT_THROW(inv_wishart_cholesky_lpdf(Y, nu, MatrixXd::Identity(2, 2)),
std::domain_error);
std::invalid_argument);

nu = 5;
EXPECT_THROW(inv_wishart_cholesky_lpdf(MatrixXd::Identity(3, 3), nu,
Expand Down
2 changes: 1 addition & 1 deletion test/unit/math/prim/prob/wishart_cholesky_rng_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ TEST(ProbDistributionsWishartCholesky, rng) {
boost::random::mt19937 rng;

MatrixXd omega(3, 4);
EXPECT_THROW(wishart_cholesky_rng(3.0, omega, rng), std::domain_error);
EXPECT_THROW(wishart_cholesky_rng(3.0, omega, rng), std::invalid_argument);

MatrixXd sigma(3, 3);
sigma << 9.0, -3.0, 0.0, -3.0, 4.0, 1.0, 0.0, 1.0, 3.0;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/math/prim/prob/wishart_cholesky_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ TEST(ProbDistributionsWishartCholesky, error) {
nu = 5;
MatrixXd Y(2, 1);
EXPECT_THROW(wishart_cholesky_lpdf(Y, nu, MatrixXd::Identity(2, 2)),
std::domain_error);
std::invalid_argument);

nu = 5;
EXPECT_THROW(wishart_cholesky_lpdf(MatrixXd::Identity(3, 3), nu,
Expand Down
Loading