Skip to content

Commit 80cfead

Browse files
committed
add new test
1 parent ffeedb5 commit 80cfead

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

stan/math/prim/prob/inv_wishart_cholesky_rng.hpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33

44
#include <stan/math/prim/meta.hpp>
55
#include <stan/math/prim/err.hpp>
6-
#include <stan/math/prim/fun/mdivide_left_tri.hpp>
7-
#include <stan/math/prim/prob/wishart_cholesky_rng.hpp>
8-
#include <stan/math/prim/prob/wishart_rng.hpp>
6+
#include <stan/math/prim/fun/mdivide_left_tri_low.hpp>
97

108
namespace stan {
119
namespace math {
@@ -16,6 +14,9 @@ namespace math {
1614
* from the inverse Wishart distribution with the specified degrees of freedom
1715
* using the specified random number generator.
1816
*
17+
* Axen, Seth D. "Efficiently generating inverse-Wishart matrices and their Cholesky factors."
18+
* arXiv preprint arXiv:2310.15884 (2023).
19+
*
1920
* @tparam RNG Random number generator type
2021
* @param[in] nu scalar degrees of freedom
2122
* @param[in] L_S lower Cholesky factor of the scale matrix
@@ -38,8 +39,15 @@ inline Eigen::MatrixXd inv_wishart_cholesky_rng(double nu,
3839
check_positive(function, "Cholesky Scale matrix", L_S.diagonal());
3940
check_positive(function, "columns of Cholesky Scale matrix", L_S.cols());
4041

41-
MatrixXd L_Sinv = mdivide_left_tri<Eigen::Lower>(L_S);
42-
return mdivide_left_tri<Eigen::Lower>(wishart_cholesky_rng(nu, L_Sinv, rng));
42+
MatrixXd B = MatrixXd::Zero(k, k);
43+
for (int j = 0; j < k; ++j) {
44+
for (int i = 0; i < j; ++i) {
45+
B(j, i) = normal_rng(0, 1, rng);
46+
}
47+
B(j, j) = std::sqrt(chi_square_rng(nu - k + j + 1, rng));
48+
}
49+
50+
return mdivide_left_tri_low(B, L_S);
4351
}
4452

4553
} // namespace math

test/unit/math/prim/prob/inv_wishart_cholesky_rng_test.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ TEST(ProbDistributionsInvWishartCholesky, SpecialRNGTest) {
9191
using stan::math::inv_wishart_cholesky_rng;
9292
using stan::math::multiply_lower_tri_self_transpose;
9393

94-
boost::random::mt19937 rng(1234U);
94+
boost::random::mt19937 rng(92343U);
9595
int N = 1e5;
9696
double tol = 0.1;
9797
for (int k = 1; k < 5; k++) {
98-
MatrixXd sigma = MatrixXd::Identity(k, k);
98+
MatrixXd L = MatrixXd::Identity(k, k);
9999
MatrixXd Z = MatrixXd::Zero(k, k);
100100
for (int i = 0; i < N; i++) {
101-
Z += stan::math::crossprod(inv_wishart_cholesky_rng(k + 2, sigma, rng));
101+
Z += multiply_lower_tri_self_transpose(inv_wishart_cholesky_rng(k + 2, L, rng));
102102
}
103103
Z /= N;
104104
for (int j = 0; j < k; j++) {
@@ -111,3 +111,36 @@ TEST(ProbDistributionsInvWishartCholesky, SpecialRNGTest) {
111111
}
112112
}
113113
}
114+
115+
TEST(ProbDistributionsInvWishartCholesky, compareToInvWishart) {
116+
// Compare the marginal mean
117+
118+
using Eigen::MatrixXd;
119+
using Eigen::VectorXd;
120+
using stan::math::inv_wishart_cholesky_rng;
121+
using stan::math::multi_normal_rng;
122+
using stan::math::inv_wishart_rng;
123+
using stan::math::lkj_corr_cholesky_rng;
124+
using stan::math::qr_thin_R;
125+
using stan::math::multiply_lower_tri_self_transpose;
126+
127+
boost::random::mt19937 rng(92343U);
128+
int N = 1e5;
129+
double tol = 0.1;
130+
for (int k = 1; k < 5; k++) {
131+
MatrixXd sigma = inv_wishart_rng(15, MatrixXd::Identity(k, k), rng);
132+
MatrixXd L = stan::math::cholesky_decompose(sigma);
133+
MatrixXd Z_mean = sigma / (k + 3);
134+
MatrixXd Z_est = MatrixXd::Zero(k, k);
135+
for (int i = 0; i < N; i++) {
136+
Z_est += inv_wishart_cholesky_rng(k + 4, L, rng);
137+
}
138+
Z_est /= N;
139+
Z_est = multiply_lower_tri_self_transpose(Z_est);
140+
for (int j = 0; j < k; j++) {
141+
for (int i = 0; i < j; i++) {
142+
EXPECT_NEAR(Z_est(i, j), Z_mean(i, j), tol);
143+
}
144+
}
145+
}
146+
}

0 commit comments

Comments
 (0)