Skip to content

Commit 2f304ee

Browse files
authored
Merge pull request #3412 from stan-dev/boost-random
Replace boost/random with stdlib random
2 parents 8639d12 + 1485d9a commit 2f304ee

20 files changed

Lines changed: 90 additions & 99 deletions

src/stan/io/random_var_context.hpp

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

44
#include <stan/io/var_context.hpp>
55
#include <stan/io/validate_dims.hpp>
6-
#include <boost/random/uniform_real_distribution.hpp>
76
#include <algorithm>
87
#include <limits>
8+
#include <random>
99
#include <string>
1010
#include <vector>
1111

@@ -50,8 +50,7 @@ class random_var_context : public var_context {
5050
for (size_t n = 0; n < num_unconstrained_; ++n)
5151
unconstrained_params_[n] = 0.0;
5252
} else {
53-
boost::random::uniform_real_distribution<double> unif(-init_radius,
54-
init_radius);
53+
std::uniform_real_distribution<double> unif(-init_radius, init_radius);
5554
for (size_t n = 0; n < num_unconstrained_; ++n)
5655
unconstrained_params_[n] = unif(rng);
5756
}

src/stan/mcmc/hmc/base_hmc.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
#include <stan/callbacks/structured_writer.hpp>
77
#include <stan/mcmc/base_mcmc.hpp>
88
#include <stan/mcmc/hmc/hamiltonians/ps_point.hpp>
9-
#include <boost/random/uniform_01.hpp>
109
#include <cmath>
1110
#include <limits>
11+
#include <random>
1212
#include <stdexcept>
1313
#include <string>
1414
#include <vector>
@@ -35,7 +35,7 @@ class base_hmc : public base_mcmc {
3535
integrator_(),
3636
hamiltonian_(model),
3737
rand_int_(rng),
38-
rand_uniform_(rand_int_),
38+
rand_uniform_(),
3939
nom_epsilon_(0.1),
4040
epsilon_(nom_epsilon_),
4141
epsilon_jitter_(0.0) {}
@@ -196,7 +196,9 @@ class base_hmc : public base_mcmc {
196196
this->epsilon_ = this->nom_epsilon_;
197197
if (this->epsilon_jitter_)
198198
this->epsilon_
199-
*= 1.0 + this->epsilon_jitter_ * (2.0 * this->rand_uniform_() - 1.0);
199+
*= 1.0
200+
+ this->epsilon_jitter_
201+
* (2.0 * this->rand_uniform_(this->rand_int_) - 1.0);
200202
}
201203

202204
protected:
@@ -207,7 +209,7 @@ class base_hmc : public base_mcmc {
207209
BaseRNG& rand_int_;
208210

209211
// Uniform(0, 1) RNG
210-
boost::uniform_01<BaseRNG&> rand_uniform_;
212+
std::uniform_real_distribution<> rand_uniform_;
211213

212214
double nom_epsilon_;
213215
double epsilon_;

src/stan/mcmc/hmc/hamiltonians/dense_e_metric.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
#include <stan/math/prim.hpp>
66
#include <stan/mcmc/hmc/hamiltonians/base_hamiltonian.hpp>
77
#include <stan/mcmc/hmc/hamiltonians/dense_e_point.hpp>
8-
#include <boost/random/variate_generator.hpp>
9-
#include <boost/random/normal_distribution.hpp>
8+
#include <random>
109

1110
namespace stan {
1211
namespace mcmc {
@@ -42,13 +41,12 @@ class dense_e_metric : public base_hamiltonian<Model, dense_e_point, BaseRNG> {
4241

4342
void sample_p(dense_e_point& z, BaseRNG& rng) {
4443
typedef typename stan::math::index_type<Eigen::VectorXd>::type idx_t;
45-
boost::variate_generator<BaseRNG&, boost::normal_distribution<> >
46-
rand_dense_gaus(rng, boost::normal_distribution<>());
44+
std::normal_distribution<> rand_dense_gaus;
4745

4846
Eigen::VectorXd u(z.p.size());
4947

5048
for (idx_t i = 0; i < u.size(); ++i)
51-
u(i) = rand_dense_gaus();
49+
u(i) = rand_dense_gaus(rng);
5250

5351
z.p = z.inv_e_metric_.llt().matrixU().solve(u);
5452
}

src/stan/mcmc/hmc/hamiltonians/diag_e_metric.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
#include <stan/callbacks/logger.hpp>
55
#include <stan/mcmc/hmc/hamiltonians/base_hamiltonian.hpp>
66
#include <stan/mcmc/hmc/hamiltonians/diag_e_point.hpp>
7-
#include <boost/random/variate_generator.hpp>
8-
#include <boost/random/normal_distribution.hpp>
7+
#include <random>
98

109
namespace stan {
1110
namespace mcmc {
@@ -42,11 +41,10 @@ class diag_e_metric : public base_hamiltonian<Model, diag_e_point, BaseRNG> {
4241
}
4342

4443
void sample_p(diag_e_point& z, BaseRNG& rng) {
45-
boost::variate_generator<BaseRNG&, boost::normal_distribution<> >
46-
rand_diag_gaus(rng, boost::normal_distribution<>());
44+
std::normal_distribution<> rand_diag_gaus;
4745

4846
for (int i = 0; i < z.p.size(); ++i)
49-
z.p(i) = rand_diag_gaus() / sqrt(z.inv_e_metric_(i));
47+
z.p(i) = rand_diag_gaus(rng) / sqrt(z.inv_e_metric_(i));
5048
}
5149
};
5250

src/stan/mcmc/hmc/hamiltonians/softabs_metric.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
#include <stan/math/mix.hpp>
55
#include <stan/mcmc/hmc/hamiltonians/base_hamiltonian.hpp>
66
#include <stan/mcmc/hmc/hamiltonians/softabs_point.hpp>
7-
#include <boost/random/variate_generator.hpp>
8-
#include <boost/random/normal_distribution.hpp>
7+
#include <random>
98

109
namespace stan {
1110
namespace mcmc {
@@ -83,13 +82,12 @@ class softabs_metric : public base_hamiltonian<Model, softabs_point, BaseRNG> {
8382
}
8483

8584
void sample_p(softabs_point& z, BaseRNG& rng) {
86-
boost::variate_generator<BaseRNG&, boost::normal_distribution<> >
87-
rand_unit_gaus(rng, boost::normal_distribution<>());
85+
std::normal_distribution<> rand_unit_gaus;
8886

8987
Eigen::VectorXd a(z.p.size());
9088

9189
for (idx_t n = 0; n < z.p.size(); ++n)
92-
a(n) = sqrt(z.softabs_lambda(n)) * rand_unit_gaus();
90+
a(n) = sqrt(z.softabs_lambda(n)) * rand_unit_gaus(rng);
9391

9492
z.p = z.eigen_deco.eigenvectors() * a;
9593
}

src/stan/mcmc/hmc/hamiltonians/unit_e_metric.hpp

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

44
#include <stan/mcmc/hmc/hamiltonians/base_hamiltonian.hpp>
55
#include <stan/mcmc/hmc/hamiltonians/unit_e_point.hpp>
6-
#include <boost/random/variate_generator.hpp>
7-
#include <boost/random/normal_distribution.hpp>
6+
#include <random>
87

98
namespace stan {
109
namespace mcmc {
@@ -37,11 +36,10 @@ class unit_e_metric : public base_hamiltonian<Model, unit_e_point, BaseRNG> {
3736
}
3837

3938
void sample_p(unit_e_point& z, BaseRNG& rng) {
40-
boost::variate_generator<BaseRNG&, boost::normal_distribution<> >
41-
rand_unit_gaus(rng, boost::normal_distribution<>());
39+
std::normal_distribution<> rand_unit_gaus;
4240

4341
for (int i = 0; i < z.p.size(); ++i)
44-
z.p(i) = rand_unit_gaus();
42+
z.p(i) = rand_unit_gaus(rng);
4543
}
4644
};
4745

src/stan/mcmc/hmc/nuts/base_nuts.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class base_nuts : public base_hmc<Model, Hamiltonian, Integrator, BaseRNG> {
128128
bool valid_subtree = false;
129129
double log_sum_weight_subtree = -std::numeric_limits<double>::infinity();
130130

131-
if (this->rand_uniform_() > 0.5) {
131+
if (this->rand_uniform_(this->rand_int_) > 0.5) {
132132
// Extend the current trajectory forward
133133
this->z_.ps_point::operator=(z_fwd);
134134
rho_bck = rho;
@@ -164,7 +164,7 @@ class base_nuts : public base_hmc<Model, Hamiltonian, Integrator, BaseRNG> {
164164
z_sample = z_propose;
165165
} else {
166166
double accept_prob = std::exp(log_sum_weight_subtree - log_sum_weight);
167-
if (this->rand_uniform_() < accept_prob)
167+
if (this->rand_uniform_(this->rand_int_) < accept_prob)
168168
z_sample = z_propose;
169169
}
170170

@@ -328,7 +328,7 @@ class base_nuts : public base_hmc<Model, Hamiltonian, Integrator, BaseRNG> {
328328
} else {
329329
double accept_prob
330330
= std::exp(log_sum_weight_final - log_sum_weight_subtree);
331-
if (this->rand_uniform_() < accept_prob)
331+
if (this->rand_uniform_(this->rand_int_) < accept_prob)
332332
z_propose = z_propose_final;
333333
}
334334

src/stan/mcmc/hmc/nuts_classic/base_nuts_classic.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class base_nuts_classic
8484
util.H0 = this->hamiltonian_.H(this->z_);
8585

8686
// Sample the slice variable
87-
util.log_u = std::log(this->rand_uniform_());
87+
util.log_u = std::log(this->rand_uniform_(this->rand_int_));
8888

8989
// Build a balanced binary tree until the NUTS criterion fails
9090
util.criterion = true;
@@ -101,7 +101,7 @@ class base_nuts_classic
101101
ps_point* z = 0;
102102
Eigen::VectorXd* rho = 0;
103103

104-
if (this->rand_uniform_() > 0.5) {
104+
if (this->rand_uniform_(this->rand_int_) > 0.5) {
105105
z = &z_plus;
106106
rho = &rho_plus;
107107
util.sign = 1;
@@ -133,7 +133,7 @@ class base_nuts_classic
133133
subtree_prob = n_valid_subtree ? 1 : 0;
134134
}
135135

136-
if (this->rand_uniform_() < subtree_prob)
136+
if (this->rand_uniform_(this->rand_int_) < subtree_prob)
137137
z_sample = z_propose;
138138

139139
n_valid += n_valid_subtree;
@@ -227,7 +227,8 @@ class base_nuts_classic
227227
double accept_prob
228228
= static_cast<double>(n2) / static_cast<double>(n1 + n2);
229229

230-
if (util.criterion && (this->rand_uniform_() < accept_prob))
230+
if (util.criterion
231+
&& (this->rand_uniform_(this->rand_int_) < accept_prob))
231232
z_propose = z_propose_right;
232233

233234
Eigen::VectorXd& subtree_rho = left_subtree_rho;

src/stan/mcmc/hmc/static/base_static_hmc.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class base_static_hmc
5959

6060
double acceptProb = std::exp(H0 - h);
6161

62-
if (acceptProb < 1 && this->rand_uniform_() > acceptProb)
62+
if (acceptProb < 1 && this->rand_uniform_(this->rand_int_) > acceptProb)
6363
this->z_.ps_point::operator=(z_init);
6464

6565
acceptProb = acceptProb > 1 ? 1 : acceptProb;

src/stan/mcmc/hmc/static_uniform/base_static_uniform.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
#include <stan/callbacks/logger.hpp>
55
#include <stan/mcmc/hmc/base_hmc.hpp>
66
#include <stan/mcmc/hmc/hamiltonians/ps_point.hpp>
7-
#include <boost/random/uniform_int_distribution.hpp>
87
#include <cmath>
98
#include <limits>
9+
#include <random>
1010
#include <string>
1111
#include <vector>
1212

@@ -46,7 +46,7 @@ class base_static_uniform
4646
double sum_prob = 1;
4747
double sum_metro_prob = 1;
4848

49-
boost::random::uniform_int_distribution<> uniform(0, L_ - 1);
49+
std::uniform_int_distribution<> uniform(0, L_ - 1);
5050
int Lp = uniform(this->rand_int_);
5151

5252
for (int l = 0; l < Lp; ++l) {
@@ -61,7 +61,7 @@ class base_static_uniform
6161
sum_prob += prob;
6262
sum_metro_prob += prob > 1 ? 1 : prob;
6363

64-
if (this->rand_uniform_() < prob / sum_prob)
64+
if (this->rand_uniform_(this->rand_int_) < prob / sum_prob)
6565
z_sample = this->z_;
6666
}
6767

@@ -79,7 +79,7 @@ class base_static_uniform
7979
sum_prob += prob;
8080
sum_metro_prob += prob > 1 ? 1 : prob;
8181

82-
if (this->rand_uniform_() < prob / sum_prob)
82+
if (this->rand_uniform_(this->rand_int_) < prob / sum_prob)
8383
z_sample = this->z_;
8484
}
8585

0 commit comments

Comments
 (0)