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;
38 int32_t a = gen() >> 5;
39 int32_t b = gen() >> 6;
40 return (a * 67108864.0 + b) / 9007199254740992.0;
47 constexpr double inv_divisor = 1.0 /
static_cast<double>(std::mt19937::max());
48 return (gen() * inv_divisor);
56class standard_normal {
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);
102 return u * r * sd + mean;
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);
125 if (u <= 1.0 - shape) {
126 double x = std::pow(u, 1.0 / shape);
131 double y = -std::log((1 - u) / shape);
132 double x = std::pow(1.0 - shape + shape * y, 1.0 / 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)) {
151 return b * v * scale;
153 if (std::log(u) < 0.5 * x * x + b * (1.0 - v + std::log(v))) {
154 return b * v * scale;
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_;
193inline double sample_gamma(std::mt19937& gen,
double shape,
double scale) {
196 }
else if (shape < 1.0) {
202 double v = -std::log(v0);
203 if (u <= 1.0 - shape) {
204 double x = std::pow(u, 1.0 / shape);
209 double y = -std::log((1 - u) / shape);
210 double x = std::pow(1.0 - shape + shape * y, 1.0 / 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);
228 s = u1 * u1 + u2 * u2;
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)) {
236 return b * v * scale;
238 if (std::log(u) < 0.5 * x * x + b * (1.0 - v + std::log(v))) {
239 return b * v * scale;
255 template <
typename Iterator>
256 walker_vose(Iterator first, Iterator last) {
257 n_ = std::distance(first, last);
258 probability_.resize(n_);
263 for (
auto it = first; it != last; ++it) {
268 std::vector<double> p(n_);
269 std::vector<int> below_average, above_average;
271 for (
int i = 0; i < n_; ++i) {
272 p[i] = (*(first + i)) * n_ / sum;
274 below_average.push_back(i);
276 above_average.push_back(i);
280 while (!below_average.empty() && !above_average.empty()) {
281 int j = below_average.back();
282 below_average.pop_back();
283 int i = above_average.back();
284 above_average.pop_back();
286 probability_[j] = p[j];
288 p[i] = (p[i] + p[j]) - 1.0;
291 below_average.push_back(i);
293 above_average.push_back(i);
297 while (!above_average.empty()) {
298 probability_[above_average.back()] = 1.0;
299 above_average.pop_back();
302 while (!below_average.empty()) {
303 probability_[below_average.back()] = 1.0;
304 below_average.pop_back();
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) {
322 double sum_weight = std::accumulate(weights.begin(), weights.end(), 0.0);
324 double running_total_weight = 0.0;
325 for (
int i = 0; i < weights.size(); ++i) {
326 running_total_weight += weights[i];
327 if (running_total_weight > u) {
331 return weights.size() - 1;
334inline int sample_discrete_stateless(std::mt19937& gen, std::vector<double>& weights,
double sum_weights) {
336 double running_total_weight = 0.0;
337 for (
int i = 0; i < weights.size(); ++i) {
338 running_total_weight += weights[i];
339 if (running_total_weight > u) {
343 return weights.size() - 1;
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