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

Update RNG tests to use mixmax RNG - declare stan::rng_t in Math library instead #3071

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion test/unit/math/fwd/prob/categorical_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <stan/math/fwd.hpp>
#include <gtest/gtest.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <boost/math/distributions.hpp>
#include <vector>

Expand Down
2 changes: 1 addition & 1 deletion test/unit/math/fwd/prob/dirichlet_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <stan/math/fwd.hpp>
#include <gtest/gtest.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <boost/math/distributions.hpp>

TEST(ProbDistributions, fvar_double) {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/math/fwd/prob/inv_wishart_test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stan/math/fwd.hpp>
#include <gtest/gtest.h>

#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <boost/math/distributions.hpp>
#include <boost/math/special_functions/digamma.hpp>

Expand Down
10 changes: 5 additions & 5 deletions test/unit/math/fwd/prob/lkj_corr_test.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <stan/math/fwd.hpp>
#include <gtest/gtest.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <boost/math/distributions.hpp>

TEST(ProbDistributionsLkjCorr, fvar_double) {
using stan::math::fvar;
boost::random::mt19937 rng;
boost::random::mixmax rng;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spinkney another LKJ question for you. This test is now failing on the derivative comparison below:

EXPECT_FLOAT_EQ(2.5177896, stan::math::lkj_corr_log(Sigma, eta).d_);

Would you know where the fixed value 2.5177896 came from? (I know this wasn't a test you wrote, but I figured you probably understand this better than I do!)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this doesn't seem right. The derivative is wrt to eta, right? And eta is changing. I think the derivative should be

eta <- 0.3
d <- 4

d_eta <- -(d - 2) * digamma((d - 1) / 2 + eta) + 2 * digamma(d/2 + eta - 1) - 2 * digamma(d + 2 * eta - 2) + log(4)
for (k in 2:(d-1)) {
  d_eta <- d_eta + digamma(eta + (d-1-k)/2)            
}
d_eta

Copy link
Member

@WardBrian WardBrian May 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using an RNG in this test seems like it isn't necessary anyway. There is no randomness used, the value of eta is the same every time, we just avoid writing it down explicitly I guess?

If you replace the definition for eta with the line fvar<double> eta = 1.6294473838061094;, which is just what the result of the default-constructed mt19937 was giving before, the test passes again.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes more sense and the rng should be removed.

int K = 4;
Eigen::Matrix<fvar<double>, Eigen::Dynamic, Eigen::Dynamic> Sigma(K, K);
Sigma.setZero();
Expand All @@ -24,7 +24,7 @@ TEST(ProbDistributionsLkjCorr, fvar_double) {

TEST(ProbDistributionsLkjCorrCholesky, fvar_double) {
using stan::math::fvar;
boost::random::mt19937 rng;
boost::random::mixmax rng;
int K = 4;
Eigen::Matrix<fvar<double>, Eigen::Dynamic, Eigen::Dynamic> Sigma(K, K);
Sigma.setZero();
Expand All @@ -43,7 +43,7 @@ TEST(ProbDistributionsLkjCorrCholesky, fvar_double) {

TEST(ProbDistributionsLkjCorr, fvar_fvar_double) {
using stan::math::fvar;
boost::random::mt19937 rng;
boost::random::mixmax rng;
int K = 4;
Eigen::Matrix<fvar<fvar<double> >, Eigen::Dynamic, Eigen::Dynamic> Sigma(K,
K);
Expand All @@ -63,7 +63,7 @@ TEST(ProbDistributionsLkjCorr, fvar_fvar_double) {

TEST(ProbDistributionsLkjCorrCholesky, fvar_fvar_double) {
using stan::math::fvar;
boost::random::mt19937 rng;
boost::random::mixmax rng;
int K = 4;
Eigen::Matrix<fvar<fvar<double> >, Eigen::Dynamic, Eigen::Dynamic> Sigma(K,
K);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/math/fwd/prob/multi_normal_cholesky_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <stan/math/fwd.hpp>
#include <gtest/gtest.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <boost/math/distributions.hpp>

TEST(ProbDistributionsMultiNormalCholesky, fvar_double) {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/math/fwd/prob/multi_normal_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <stan/math/fwd.hpp>
#include <gtest/gtest.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <boost/math/distributions.hpp>

TEST(ProbDistributionsMultiNormal, fvar_double) {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/math/fwd/prob/multi_student_t_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <stan/math/fwd.hpp>
#include <gtest/gtest.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <boost/math/distributions.hpp>

TEST(ProbDistributionsMultiStudentT, fvar_double) {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/math/fwd/prob/multinomial_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <stan/math/fwd.hpp>
#include <gtest/gtest.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <boost/math/distributions.hpp>
#include <vector>

Expand Down
2 changes: 1 addition & 1 deletion test/unit/math/fwd/prob/wishart_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <stan/math/fwd.hpp>
#include <gtest/gtest.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <boost/math/special_functions/digamma.hpp>
#include <boost/math/distributions.hpp>

Expand Down
2 changes: 1 addition & 1 deletion test/unit/math/mix/prob/categorical_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <stan/math/mix.hpp>
#include <gtest/gtest.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <boost/math/distributions.hpp>
#include <vector>

Expand Down
10 changes: 5 additions & 5 deletions test/unit/math/mix/prob/lkj_corr_test.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#include <stan/math/mix.hpp>
#include <gtest/gtest.h>
#include <test/unit/math/rev/prob/lkj_corr_cholesky_test_functors.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <boost/math/distributions.hpp>
#include <test/unit/math/mix/prob/higher_order_utils.hpp>
#include <vector>

TEST(ProbDistributionsLkjCorr, fvar_var) {
using stan::math::fvar;
using stan::math::var;
boost::random::mt19937 rng;
boost::random::mixmax rng;
int K = 4;
Eigen::Matrix<fvar<var>, Eigen::Dynamic, Eigen::Dynamic> Sigma(K, K);
Sigma.setZero();
Expand All @@ -31,7 +31,7 @@ TEST(ProbDistributionsLkjCorr, fvar_var) {
TEST(ProbDistributionsLkjCorrCholesky, fvar_var) {
using stan::math::fvar;
using stan::math::var;
boost::random::mt19937 rng;
boost::random::mixmax rng;
int K = 4;
Eigen::Matrix<fvar<var>, Eigen::Dynamic, Eigen::Dynamic> Sigma(K, K);
Sigma.setZero();
Expand All @@ -54,7 +54,7 @@ TEST(ProbDistributionsLkjCorrCholesky, fvar_var) {
TEST(ProbDistributionsLkjCorr, fvar_fvar_var) {
using stan::math::fvar;
using stan::math::var;
boost::random::mt19937 rng;
boost::random::mixmax rng;
int K = 4;
Eigen::Matrix<fvar<fvar<var> >, Eigen::Dynamic, Eigen::Dynamic> Sigma(K, K);
Sigma.setZero();
Expand All @@ -78,7 +78,7 @@ TEST(ProbDistributionsLkjCorr, fvar_fvar_var) {
TEST(ProbDistributionsLkjCorrCholesky, fvar_fvar_var) {
using stan::math::fvar;
using stan::math::var;
boost::random::mt19937 rng;
boost::random::mixmax rng;
int K = 4;
Eigen::Matrix<fvar<fvar<var> >, Eigen::Dynamic, Eigen::Dynamic> Sigma(K, K);
Sigma.setZero();
Expand Down
2 changes: 1 addition & 1 deletion test/unit/math/mix/prob/multinomial_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <stan/math/mix.hpp>
#include <gtest/gtest.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <boost/math/distributions.hpp>
#include <vector>

Expand Down
4 changes: 2 additions & 2 deletions test/unit/math/opencl/multiply_transpose_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#include <stan/math/opencl/multiply_transpose.hpp>
#include <stan/math/opencl/copy.hpp>
#include <test/unit/util.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <gtest/gtest.h>
#include <algorithm>
boost::random::mt19937 rng;
boost::random::mixmax rng;

TEST(MathMatrixOpenCL, multiply_transpose_exception_fail_zero) {
stan::math::row_vector_d rv(0);
Expand Down
6 changes: 3 additions & 3 deletions test/unit/math/opencl/prim/mdivide_left_tri_low_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <stan/math/opencl/prim.hpp>
#include <test/unit/math/expect_near_rel.hpp>
#include <test/unit/util.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <gtest/gtest.h>
#include <algorithm>

Expand All @@ -29,7 +29,7 @@ TEST(MathMatrixCL, mdivide_left_tri_low_cl_exception) {
}

void mdivide_left_tri_low_Ab_test(int size) {
boost::random::mt19937 rng;
boost::random::mixmax rng;
auto m1 = stan::math::matrix_d(size, size);
for (int i = 0; i < size; i++) {
for (int j = 0; j < i; j++) {
Expand Down Expand Up @@ -57,7 +57,7 @@ void mdivide_left_tri_low_Ab_test(int size) {
}

void mdivide_left_tri_low_A_test(int size) {
boost::random::mt19937 rng;
boost::random::mixmax rng;
auto m1 = stan::math::matrix_d(size, size);
for (int i = 0; i < size; i++) {
for (int j = 0; j < i; j++) {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/math/opencl/prim/mdivide_right_tri_low_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <stan/math/opencl/prim.hpp>
#include <test/unit/math/expect_near_rel.hpp>
#include <test/unit/util.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <gtest/gtest.h>
#include <algorithm>

Expand All @@ -29,7 +29,7 @@ TEST(MathMatrixCL, mdivide_right_tri_low_cl_exception) {
}

void mdivide_right_tri_low_Ab_test(int size) {
boost::random::mt19937 rng;
boost::random::mixmax rng;
auto m1 = stan::math::matrix_d(size, size);
for (int i = 0; i < size; i++) {
for (int j = 0; j < i; j++) {
Expand Down
6 changes: 3 additions & 3 deletions test/unit/math/opencl/tri_inverse_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <stan/math/prim.hpp>
#include <stan/math/opencl/prim.hpp>
#include <test/unit/util.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <gtest/gtest.h>
#include <algorithm>

Expand All @@ -25,7 +25,7 @@ TEST(MathMatrixCL, inverse_cl_exception) {
}

void lower_inverse_test(int size) {
boost::random::mt19937 rng;
boost::random::mixmax rng;
auto m1 = stan::math::matrix_d(size, size);
for (int i = 0; i < size; i++) {
for (int j = 0; j < i; j++) {
Expand Down Expand Up @@ -56,7 +56,7 @@ void lower_inverse_test(int size) {
}

void upper_inverse_test(int size) {
boost::random::mt19937 rng;
boost::random::mixmax rng;
auto m1 = stan::math::matrix_d(size, size);
for (int i = 0; i < size; i++) {
for (int j = 0; j < i; j++) {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/math/prim/fun/chol2inv_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <stan/math/prim.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <gtest/gtest.h>

TEST(MathMatrixPrimMat, chol2inv_exception) {
Expand All @@ -25,7 +25,7 @@ TEST(MathMatrixPrimMat, chol2inv) {
using stan::math::matrix_d;
using stan::math::wishart_rng;

boost::random::mt19937 rng;
boost::random::mixmax rng;
matrix_d I(3, 3);
I.setZero();
I.diagonal().setOnes();
Expand Down
8 changes: 4 additions & 4 deletions test/unit/math/prim/prob/bernoulli_logit_glm_rng_test.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#include <gtest/gtest.h>
#include <stan/math/prim.hpp>
#include <boost/math/distributions.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <test/unit/math/prim/prob/util.hpp>
#include <limits>
#include <vector>

TEST(ProbDistributionsBernoulliLogitGlm, vectorized) {
using stan::math::bernoulli_logit_glm_rng;
// Test scalar/vector combinations.
boost::random::mt19937 rng;
boost::random::mixmax rng;

Eigen::MatrixXd x(2, 3);
x << 3.5, -1.5, 0.0, 2.0, 1.0, 3.0;
Expand Down Expand Up @@ -70,7 +70,7 @@ TEST(ProbDistributionsBernoulliLogitGlm, vectorized) {
TEST(ProbDistributionsBernoulliLogitGlm, errorCheck) {
using stan::math::bernoulli_logit_glm_rng;
// Check errors for nonfinite and wrong sizes.
boost::random::mt19937 rng;
boost::random::mixmax rng;

int N = 3;
int M = 2;
Expand Down Expand Up @@ -107,7 +107,7 @@ TEST(ProbDistributionsBernoulliLogitGlm, errorCheck) {
TEST(ProbDistributionsBernoulliLogitGlm, marginalChiSquareGoodnessFitTest) {
using stan::math::bernoulli_logit_glm_rng;
// Check distribution of result.
boost::random::mt19937 rng;
boost::random::mixmax rng;
Eigen::MatrixXd x(2, 2);
x << 3.5, -1.5, 2.0, -1.2;
std::vector<double> alpha{2.0, 1.0};
Expand Down
6 changes: 3 additions & 3 deletions test/unit/math/prim/prob/bernoulli_logit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <test/unit/math/prim/prob/VectorIntRNGTestRig.hpp>
#include <test/unit/math/prim/prob/util.hpp>
#include <boost/math/distributions.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <gtest/gtest.h>
#include <limits>
#include <vector>
Expand Down Expand Up @@ -35,7 +35,7 @@ TEST(ProbDistributionsBernoulliLogit, distributionCheck) {
}

TEST(ProbDistributionsBernoulliLogit, error_check) {
boost::random::mt19937 rng;
boost::random::mixmax rng;

EXPECT_NO_THROW(stan::math::bernoulli_logit_rng(-3.5, rng));
EXPECT_THROW(
Expand All @@ -44,7 +44,7 @@ TEST(ProbDistributionsBernoulliLogit, error_check) {
}

TEST(ProbDistributionsBernoulliLogit, logitChiSquareGoodnessFitTest) {
boost::random::mt19937 rng;
boost::random::mixmax rng;
// number of samples
int N = 10000;

Expand Down
6 changes: 3 additions & 3 deletions test/unit/math/prim/prob/bernoulli_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <test/unit/math/prim/prob/VectorIntRNGTestRig.hpp>
#include <test/unit/math/prim/prob/util.hpp>
#include <boost/math/distributions.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <gtest/gtest.h>
#include <limits>
#include <vector>
Expand Down Expand Up @@ -35,7 +35,7 @@ TEST(ProbDistributionsBernoulli, distributionCheck) {
}

TEST(ProbDistributionsBernoulli, error_check) {
boost::random::mt19937 rng;
boost::random::mixmax rng;
EXPECT_NO_THROW(stan::math::bernoulli_rng(0.6, rng));

EXPECT_THROW(stan::math::bernoulli_rng(1.6, rng), std::domain_error);
Expand All @@ -45,7 +45,7 @@ TEST(ProbDistributionsBernoulli, error_check) {
}

TEST(ProbDistributionsBernoulli, chiSquareGoodnessFitTest) {
boost::random::mt19937 rng;
boost::random::mixmax rng;
int N = 10000;

std::vector<double> expected;
Expand Down
4 changes: 2 additions & 2 deletions test/unit/math/prim/prob/beta_binomial_test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stan/math/prim.hpp>
#include <test/unit/math/prim/prob/vector_rng_test_helper.hpp>
#include <test/unit/math/prim/prob/VectorIntRNGTestRig.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/mixmax.hpp>
#include <boost/math/distributions.hpp>
#include <gtest/gtest.h>
#include <limits>
Expand Down Expand Up @@ -40,7 +40,7 @@ TEST(ProbDistributionsBetaBinomial, distributionCheck) {
}

TEST(ProbDistributionBetaBinomial, error_check) {
boost::random::mt19937 rng;
boost::random::mixmax rng;
EXPECT_NO_THROW(stan::math::beta_binomial_rng(4, 0.6, 2.0, rng));

EXPECT_THROW(stan::math::beta_binomial_rng(-4, 0.6, 2, rng),
Expand Down
Loading
Loading