1#ifndef STOCHTREE_DISTRIBUTIONS_H
2#define STOCHTREE_DISTRIBUTIONS_H
5#include <boost/math/special_functions/erf.hpp>
20 return 0.5 * boost::math::erfc(-
x / std::sqrt(2.0));
27 return -std::sqrt(2.0) * boost::math::erfc_inv(2.0 * p);
31static constexpr double Phi_0 = 0.5;
40 return (
a * 67108864.0 +
b) / 9007199254740992.0;
47 constexpr double inv_divisor = 1.0 /
static_cast<double>(std::mt19937::max());
59 has_cached_value_ =
false;
63 inline double operator()(std::mt19937&
gen) {
64 if (has_cached_value_) {
65 has_cached_value_ =
false;
73 }
while (
s >= 1.0 ||
s == 0.0);
74 r = std::sqrt(-2.0 * std::log(
s) /
s);
75 has_cached_value_ =
true;
76 cached_value_ =
v *
r;
82 bool has_cached_value_;
100 }
while (
s >= 1.0 ||
s == 0.0);
101 r = std::sqrt(-2.0 * std::log(
s) /
s);
111 has_cached_normal_value_ =
false;
112 cached_normal_value_ = 0.0;
115 inline double operator()(std::mt19937&
gen,
double shape,
double scale) {
118 }
else if (
shape < 1.0) {
124 double v = -std::log(
v0);
126 double x = std::pow(
u, 1.0 /
shape);
131 double y = -std::log((1 -
u) /
shape);
138 }
else if (
shape > 1.0) {
140 double b =
shape - 1.0 / 3.0;
141 double c = 1.0 / std::sqrt(9.0 *
b);
145 x = normal_draw(
gen);
150 if (
u < 1.0 - 0.0331 * (
x *
x) * (
x *
x)) {
153 if (std::log(
u) < 0.5 *
x *
x +
b * (1.0 -
v + std::log(
v))) {
162 inline double normal_draw(std::mt19937&
gen) {
163 if (has_cached_normal_value_) {
164 has_cached_normal_value_ =
false;
165 return cached_normal_value_;
172 }
while (
s >= 1.0 ||
s == 0.0);
173 r = std::sqrt(-2.0 * std::log(
s) /
s);
174 has_cached_normal_value_ =
true;
175 cached_normal_value_ =
v *
r;
181 bool has_cached_normal_value_;
182 double cached_normal_value_;
196 }
else if (
shape < 1.0) {
202 double v = -std::log(
v0);
204 double x = std::pow(
u, 1.0 /
shape);
209 double y = -std::log((1 -
u) /
shape);
216 }
else if (
shape > 1.0) {
218 double b =
shape - 1.0 / 3.0;
219 double c = 1.0 / std::sqrt(9.0 *
b);
229 }
while (
s >= 1.0 ||
s == 0.0);
230 x =
u1 * std::sqrt(-2.0 * std::log(
s) /
s);
235 if (
u < 1.0 - 0.0331 * (
x *
x) * (
x *
x)) {
238 if (std::log(
u) < 0.5 *
x *
x +
b * (1.0 -
v + std::log(
v))) {
255 template <
typename Iterator>
258 probability_.resize(n_);
268 std::vector<double> p(n_);
271 for (
int i = 0;
i < n_; ++
i) {
286 probability_[
j] = p[
j];
288 p[
i] = (p[
i] + p[
j]) - 1.0;
308 int operator()(std::mt19937&
gen) {
310 int i =
static_cast<int>(
u * n_);
311 double y =
u * n_ -
i;
312 return (
y < probability_[
i]) ?
i : alias_[
i];
316 std::vector<double> probability_;
317 std::vector<int> alias_;
321inline int sample_discrete_stateless(std::mt19937&
gen, std::vector<double>&
weights) {
334inline int sample_discrete_stateless(std::mt19937&
gen, std::vector<double>&
weights,
double sum_weights) {
Definition distributions.h:108
Definition distributions.h:56
Definition distributions.h:253
static void GFRSampleOneIter(TreeEnsemble &active_forest, ForestTracker &tracker, ForestContainer &forests, LeafModel &leaf_model, ForestDataset &dataset, ColumnVector &residual, TreePrior &tree_prior, std::mt19937 &gen, std::vector< double > &variable_weights, std::vector< int > &sweep_update_indices, double global_variance, std::vector< FeatureType > &feature_types, int cutpoint_grid_size, bool keep_forest, bool pre_initialized, bool backfitting, int num_features_subsample, int num_threads, LeafSuffStatConstructorArgs &... leaf_suff_stat_args)
Definition tree_sampler.h:816
A collection of random number generation utilities.
Definition bart.h:15
double norm_cdf(double x)
Definition distributions.h:19
double sample_standard_normal(double mean, double sd, std::mt19937 &gen)
Definition distributions.h:94
double sample_gamma(std::mt19937 &gen, double shape, double scale)
Definition distributions.h:193
double standard_uniform_draw_53bit(std::mt19937 &gen)
Definition distributions.h:37
double standard_uniform_draw_32bit(std::mt19937 &gen)
Definition distributions.h:46
static constexpr double Phi_0
Definition distributions.h:31
double norm_inv_cdf(double p)
Definition distributions.h:26
double sample_std_truncnorm_lower(std::mt19937 &gen)
Definition distributions.h:357
double sample_std_truncnorm_upper(std::mt19937 &gen)
Definition distributions.h:349