Skip to content

Commit

Permalink
Update remaining tests to use rng_t
Browse files Browse the repository at this point in the history
  • Loading branch information
WardBrian committed Jan 30, 2024
1 parent 467762d commit a92cdb1
Show file tree
Hide file tree
Showing 45 changed files with 344 additions and 408 deletions.
3 changes: 0 additions & 3 deletions src/stan/model/model_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
#include <stan/model/indexing.hpp>
#include <stan/services/util/create_rng.hpp>

#include <boost/random/additive_combine.hpp>
#include <boost/random/linear_congruential.hpp>

#include <cmath>
#include <cstddef>
#include <fstream>
Expand Down
1 change: 0 additions & 1 deletion src/stan/services/experimental/advi/fullrank.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <stan/services/error_codes.hpp>
#include <stan/io/var_context.hpp>
#include <stan/variational/advi.hpp>
#include <boost/random/additive_combine.hpp>
#include <string>
#include <vector>

Expand Down
1 change: 0 additions & 1 deletion src/stan/services/experimental/advi/meanfield.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <stan/services/error_codes.hpp>
#include <stan/io/var_context.hpp>
#include <stan/variational/advi.hpp>
#include <boost/random/additive_combine.hpp>
#include <string>
#include <vector>

Expand Down
6 changes: 3 additions & 3 deletions src/test/unit/io/random_var_context_test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stan/io/random_var_context.hpp>
#include <stan/io/empty_var_context.hpp>
#include <stan/services/util/create_rng.hpp>
#include <gtest/gtest.h>
#include <boost/random/additive_combine.hpp> // L'Ecuyer RNG
#include <boost/random/uniform_real_distribution.hpp>
#include <test/test-models/good/services/test_lp.hpp>
#include <test/unit/util.hpp>
Expand Down Expand Up @@ -95,12 +95,12 @@ class random_var_context : public testing::Test {
random_var_context()
: empty_context(),
model(empty_context, 0, static_cast<std::stringstream*>(0)),
rng(0),
rng(stan::services::util::create_rng(0, 0)),
throwing_model() {}

stan::io::empty_var_context empty_context;
stan_model model;
boost::ecuyer1988 rng;
stan::rng_t rng;
test::mock_throwing_model_in_write_array throwing_model;
};

Expand Down
1 change: 0 additions & 1 deletion src/test/unit/mcmc/chains_test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <stan/mcmc/chains.hpp>
#include <stan/io/stan_csv_reader.hpp>
#include <gtest/gtest.h>
#include <boost/random/additive_combine.hpp>
#include <set>
#include <exception>
#include <utility>
Expand Down
26 changes: 12 additions & 14 deletions src/test/unit/mcmc/hmc/base_hmc_test.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
#include <test/unit/mcmc/hmc/mock_hmc.hpp>
#include <stan/callbacks/stream_writer.hpp>
#include <stan/mcmc/hmc/base_hmc.hpp>
#include <boost/random/additive_combine.hpp>
#include <stan/services/util/create_rng.hpp>
#include <boost/algorithm/string/split.hpp>
#include <test/unit/util.hpp>
#include <gtest/gtest.h>

typedef boost::ecuyer1988 rng_t;

namespace stan {
namespace mcmc {

class mock_hmc
: public base_hmc<mock_model, mock_hamiltonian, mock_integrator, rng_t> {
class mock_hmc : public base_hmc<mock_model, mock_hamiltonian, mock_integrator,
stan::rng_t> {
public:
mock_hmc(const mock_model& m, rng_t& rng)
: base_hmc<mock_model, mock_hamiltonian, mock_integrator, rng_t>(m, rng) {
}
mock_hmc(const mock_model& m, stan::rng_t& rng)
: base_hmc<mock_model, mock_hamiltonian, mock_integrator, stan::rng_t>(
m, rng) {}

sample transition(sample& init_sample, callbacks::logger& logger) {
this->seed(init_sample.cont_params());
Expand All @@ -33,7 +31,7 @@ class mock_hmc
} // namespace stan

TEST(McmcBaseHMC, point_construction) {
rng_t base_rng(0);
stan::rng_t base_rng = stan::services::util::create_rng(0, 0);

Eigen::VectorXd q(2);
q(0) = 5;
Expand All @@ -47,14 +45,14 @@ TEST(McmcBaseHMC, point_construction) {
}

TEST(McmcBaseHMC, point_access_from_const_hmc) {
rng_t base_rng(0);
stan::rng_t base_rng = stan::services::util::create_rng(0, 0);
const stan::mcmc::mock_hmc sampler(stan::mcmc::mock_model(2), base_rng);
EXPECT_EQ(2, sampler.z().q.size());
EXPECT_EQ(2, sampler.z().g.size());
}

TEST(McmcBaseHMC, seed) {
rng_t base_rng(0);
stan::rng_t base_rng = stan::services::util::create_rng(0, 0);

Eigen::VectorXd q(2);
q(0) = 5;
Expand All @@ -70,7 +68,7 @@ TEST(McmcBaseHMC, seed) {
}

TEST(McmcBaseHMC, set_nominal_stepsize) {
rng_t base_rng(0);
stan::rng_t base_rng = stan::services::util::create_rng(0, 0);

Eigen::VectorXd q(2);
q(0) = 5;
Expand All @@ -88,7 +86,7 @@ TEST(McmcBaseHMC, set_nominal_stepsize) {
}

TEST(McmcBaseHMC, set_stepsize_jitter) {
rng_t base_rng(0);
stan::rng_t base_rng = stan::services::util::create_rng(0, 0);

Eigen::VectorXd q(2);
q(0) = 5;
Expand All @@ -108,7 +106,7 @@ TEST(McmcBaseHMC, set_stepsize_jitter) {
TEST(McmcBaseHMC, streams) {
stan::test::capture_std_streams();

rng_t base_rng(0);
stan::rng_t base_rng = stan::services::util::create_rng(0, 0);

Eigen::VectorXd q(2);
q(0) = 5;
Expand Down
6 changes: 2 additions & 4 deletions src/test/unit/mcmc/hmc/hamiltonians/base_hamiltonian_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
#include <stan/callbacks/stream_logger.hpp>
#include <stan/io/empty_var_context.hpp>
#include <stan/callbacks/stream_writer.hpp>
#include <boost/random/additive_combine.hpp>
#include <stan/services/util/create_rng.hpp>
#include <test/unit/util.hpp>
#include <gtest/gtest.h>

typedef boost::ecuyer1988 rng_t;

TEST(BaseHamiltonian, update_potential_gradient) {
stan::io::empty_var_context data_var_context;

std::stringstream model_output;
funnel_model_namespace::funnel_model model(data_var_context, 0,
&model_output);

stan::mcmc::mock_hamiltonian<funnel_model_namespace::funnel_model, rng_t>
stan::mcmc::mock_hamiltonian<funnel_model_namespace::funnel_model, stan::rng_t>
metric(model);
stan::mcmc::ps_point z(11);
z.q.setOnes();
Expand Down
15 changes: 6 additions & 9 deletions src/test/unit/mcmc/hmc/hamiltonians/dense_e_metric_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <string>
#include <boost/random/additive_combine.hpp>
#include <stan/services/util/create_rng.hpp>
#include <stan/io/empty_var_context.hpp>
#include <test/unit/mcmc/hmc/mock_hmc.hpp>
#include <stan/mcmc/hmc/hamiltonians/dense_e_metric.hpp>
Expand All @@ -8,10 +8,8 @@
#include <test/unit/util.hpp>
#include <gtest/gtest.h>

typedef boost::ecuyer1988 rng_t;

TEST(McmcDenseEMetric, sample_p) {
rng_t base_rng(0);
stan::rng_t base_rng = stan::services::util::create_rng(0, 0);

Eigen::Matrix2d m(2, 2);
m(0, 0) = 3.0;
Expand All @@ -23,7 +21,7 @@ TEST(McmcDenseEMetric, sample_p) {

stan::mcmc::mock_model model(2);

stan::mcmc::dense_e_metric<stan::mcmc::mock_model, rng_t> metric(model);
stan::mcmc::dense_e_metric<stan::mcmc::mock_model, stan::rng_t> metric(model);
stan::mcmc::dense_e_point z(2);
z.set_metric(m_inv);

Expand Down Expand Up @@ -62,7 +60,6 @@ TEST(McmcDenseEMetric, sample_p) {
}

TEST(McmcDenseEMetric, gradients) {
rng_t base_rng(0);

Eigen::VectorXd q = Eigen::VectorXd::Ones(11);

Expand All @@ -79,7 +76,7 @@ TEST(McmcDenseEMetric, gradients) {
funnel_model_namespace::funnel_model model(data_var_context, 0,
&model_output);

stan::mcmc::dense_e_metric<funnel_model_namespace::funnel_model, rng_t>
stan::mcmc::dense_e_metric<funnel_model_namespace::funnel_model, stan::rng_t>
metric(model);

double epsilon = 1e-6;
Expand Down Expand Up @@ -156,7 +153,6 @@ TEST(McmcDenseEMetric, gradients) {
TEST(McmcDenseEMetric, streams) {
stan::test::capture_std_streams();

rng_t base_rng(0);

Eigen::VectorXd q(2);
q(0) = 5;
Expand All @@ -165,7 +161,8 @@ TEST(McmcDenseEMetric, streams) {
stan::mcmc::mock_model model(q.size());

// typedef to use within Google Test macros
typedef stan::mcmc::dense_e_metric<stan::mcmc::mock_model, rng_t> dense_e;
typedef stan::mcmc::dense_e_metric<stan::mcmc::mock_model, stan::rng_t>
dense_e;

EXPECT_NO_THROW(dense_e metric(model));

Expand Down
16 changes: 6 additions & 10 deletions src/test/unit/mcmc/hmc/hamiltonians/diag_e_metric_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <string>
#include <boost/random/additive_combine.hpp>
#include <stan/services/util/create_rng.hpp>
#include <stan/io/empty_var_context.hpp>
#include <test/unit/mcmc/hmc/mock_hmc.hpp>
#include <stan/mcmc/hmc/hamiltonians/diag_e_metric.hpp>
Expand All @@ -8,17 +8,15 @@
#include <test/unit/util.hpp>
#include <gtest/gtest.h>

typedef boost::ecuyer1988 rng_t;

TEST(McmcDiagEMetric, sample_p) {
rng_t base_rng(0);
stan::rng_t base_rng = stan::services::util::create_rng(0, 0);

Eigen::VectorXd q(2);
q(0) = 5;
q(1) = 1;

stan::mcmc::mock_model model(q.size());
stan::mcmc::diag_e_metric<stan::mcmc::mock_model, rng_t> metric(model);
stan::mcmc::diag_e_metric<stan::mcmc::mock_model, stan::rng_t> metric(model);
stan::mcmc::diag_e_point z(q.size());

int n_samples = 1000;
Expand All @@ -44,7 +42,6 @@ TEST(McmcDiagEMetric, sample_p) {
}

TEST(McmcDiagEMetric, gradients) {
rng_t base_rng(0);

Eigen::VectorXd q = Eigen::VectorXd::Ones(11);

Expand All @@ -61,8 +58,8 @@ TEST(McmcDiagEMetric, gradients) {
std::stringstream debug, info, warn, error, fatal;
stan::callbacks::stream_logger logger(debug, info, warn, error, fatal);

stan::mcmc::diag_e_metric<funnel_model_namespace::funnel_model, rng_t> metric(
model);
stan::mcmc::diag_e_metric<funnel_model_namespace::funnel_model, stan::rng_t>
metric(model);

double epsilon = 1e-6;

Expand Down Expand Up @@ -137,7 +134,6 @@ TEST(McmcDiagEMetric, gradients) {
TEST(McmcDiagEMetric, streams) {
stan::test::capture_std_streams();

rng_t base_rng(0);

Eigen::VectorXd q(2);
q(0) = 5;
Expand All @@ -146,7 +142,7 @@ TEST(McmcDiagEMetric, streams) {
stan::mcmc::mock_model model(q.size());

// typedef to use within Google Test macros
typedef stan::mcmc::diag_e_metric<stan::mcmc::mock_model, rng_t> diag_e;
typedef stan::mcmc::diag_e_metric<stan::mcmc::mock_model, stan::rng_t> diag_e;

EXPECT_NO_THROW(diag_e metric(model));

Expand Down
16 changes: 6 additions & 10 deletions src/test/unit/mcmc/hmc/hamiltonians/softabs_metric_test.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
#include <stan/io/empty_var_context.hpp>
#include <stan/mcmc/hmc/hamiltonians/softabs_metric.hpp>
#include <stan/services/util/create_rng.hpp>
#include <stan/callbacks/stream_logger.hpp>
#include <test/unit/mcmc/hmc/mock_hmc.hpp>
#include <test/test-models/good/mcmc/hmc/hamiltonians/funnel.hpp>
#include <test/unit/util.hpp>

#include <boost/random/additive_combine.hpp>

#include <gtest/gtest.h>

#include <string>

typedef boost::ecuyer1988 rng_t;

TEST(McmcSoftAbs, sample_p) {
rng_t base_rng(0);
stan::rng_t base_rng = stan::services::util::create_rng(0, 0);

Eigen::VectorXd q(2);
q(0) = 5;
q(1) = 1;

stan::mcmc::mock_model model(q.size());
stan::mcmc::softabs_metric<stan::mcmc::mock_model, rng_t> metric(model);
stan::mcmc::softabs_metric<stan::mcmc::mock_model, stan::rng_t> metric(model);
stan::mcmc::softabs_point z(q.size());

int n_samples = 1000;
Expand Down Expand Up @@ -60,7 +57,6 @@ TEST(McmcSoftAbs, sample_p) {
}

TEST(McmcSoftAbs, gradients) {
rng_t base_rng(0);

Eigen::VectorXd q = Eigen::VectorXd::Ones(11);

Expand All @@ -77,7 +73,7 @@ TEST(McmcSoftAbs, gradients) {
funnel_model_namespace::funnel_model model(data_var_context, 0,
&model_output);

stan::mcmc::softabs_metric<funnel_model_namespace::funnel_model, rng_t>
stan::mcmc::softabs_metric<funnel_model_namespace::funnel_model, stan::rng_t>
metric(model);

double epsilon = 1e-6;
Expand Down Expand Up @@ -152,15 +148,15 @@ TEST(McmcSoftAbs, gradients) {

TEST(McmcSoftAbs, streams) {
stan::test::capture_std_streams();
rng_t base_rng(0);

Eigen::VectorXd q(2);
q(0) = 5;
q(1) = 1;
stan::mcmc::mock_model model(q.size());

// for use in Google Test macros below
typedef stan::mcmc::softabs_metric<stan::mcmc::mock_model, rng_t> softabs;
typedef stan::mcmc::softabs_metric<stan::mcmc::mock_model, stan::rng_t>
softabs;

EXPECT_NO_THROW(softabs metric(model));

Expand Down
Loading

0 comments on commit a92cdb1

Please sign in to comment.