From c1c2960f647f21b2ea0980d607e59035012952d3 Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Fri, 26 Jan 2024 09:36:11 -0500 Subject: [PATCH] Re-add missing checks, revert test changes from #3007 --- stan/math/prim/prob/inv_wishart_cholesky_lpdf.hpp | 7 +++++-- stan/math/prim/prob/inv_wishart_cholesky_rng.hpp | 1 + stan/math/prim/prob/multi_normal_cholesky_lpdf.hpp | 4 +++- stan/math/prim/prob/wishart_cholesky_lpdf.hpp | 7 +++++-- stan/math/prim/prob/wishart_cholesky_rng.hpp | 1 + test/unit/math/prim/prob/inv_wishart_cholesky_rng_test.cpp | 3 ++- test/unit/math/prim/prob/wishart_cholesky_rng_test.cpp | 2 +- test/unit/math/prim/prob/wishart_cholesky_test.cpp | 2 +- 8 files changed, 19 insertions(+), 8 deletions(-) diff --git a/stan/math/prim/prob/inv_wishart_cholesky_lpdf.hpp b/stan/math/prim/prob/inv_wishart_cholesky_lpdf.hpp index e074687598f..2fe0f2edb5c 100644 --- a/stan/math/prim/prob/inv_wishart_cholesky_lpdf.hpp +++ b/stan/math/prim/prob/inv_wishart_cholesky_lpdf.hpp @@ -50,8 +50,11 @@ return_type_t 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); diff --git a/stan/math/prim/prob/inv_wishart_cholesky_rng.hpp b/stan/math/prim/prob/inv_wishart_cholesky_rng.hpp index 2b397d4b6aa..d40709a0d68 100644 --- a/stan/math/prim/prob/inv_wishart_cholesky_rng.hpp +++ b/stan/math/prim/prob/inv_wishart_cholesky_rng.hpp @@ -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 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); diff --git a/stan/math/prim/prob/multi_normal_cholesky_lpdf.hpp b/stan/math/prim/prob/multi_normal_cholesky_lpdf.hpp index aa47e47a547..667b28cae74 100644 --- a/stan/math/prim/prob/multi_normal_cholesky_lpdf.hpp +++ b/stan/math/prim/prob/multi_normal_cholesky_lpdf.hpp @@ -99,7 +99,9 @@ return_type_t 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]); check_not_nan(function, "Random variable", y_vec[i]); diff --git a/stan/math/prim/prob/wishart_cholesky_lpdf.hpp b/stan/math/prim/prob/wishart_cholesky_lpdf.hpp index 9bc8a2f9e7d..dc83974479b 100644 --- a/stan/math/prim/prob/wishart_cholesky_lpdf.hpp +++ b/stan/math/prim/prob/wishart_cholesky_lpdf.hpp @@ -51,8 +51,11 @@ return_type_t 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); diff --git a/stan/math/prim/prob/wishart_cholesky_rng.hpp b/stan/math/prim/prob/wishart_cholesky_rng.hpp index 39ca01850e7..0f910ee5adb 100644 --- a/stan/math/prim/prob/wishart_cholesky_rng.hpp +++ b/stan/math/prim/prob/wishart_cholesky_rng.hpp @@ -35,6 +35,7 @@ inline Eigen::MatrixXd wishart_cholesky_rng(double nu, static const char* function = "wishart_cholesky_rng"; index_type_t 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); diff --git a/test/unit/math/prim/prob/inv_wishart_cholesky_rng_test.cpp b/test/unit/math/prim/prob/inv_wishart_cholesky_rng_test.cpp index d7d73040894..95369fffa58 100644 --- a/test/unit/math/prim/prob/inv_wishart_cholesky_rng_test.cpp +++ b/test/unit/math/prim/prob/inv_wishart_cholesky_rng_test.cpp @@ -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; diff --git a/test/unit/math/prim/prob/wishart_cholesky_rng_test.cpp b/test/unit/math/prim/prob/wishart_cholesky_rng_test.cpp index c3f0e99669f..dab129f4ab9 100644 --- a/test/unit/math/prim/prob/wishart_cholesky_rng_test.cpp +++ b/test/unit/math/prim/prob/wishart_cholesky_rng_test.cpp @@ -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; diff --git a/test/unit/math/prim/prob/wishart_cholesky_test.cpp b/test/unit/math/prim/prob/wishart_cholesky_test.cpp index 89c2f47765b..3a9d6d4f769 100644 --- a/test/unit/math/prim/prob/wishart_cholesky_test.cpp +++ b/test/unit/math/prim/prob/wishart_cholesky_test.cpp @@ -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,