4 #ifndef THEORETICA_SAMPLING_H
5 #define THEORETICA_SAMPLING_H
8 #include "../core/function.h"
36 real x = (g() % prec) /
static_cast<real>(prec);
39 return a + (b - a) * x;
49 if(theta.size() != 2) {
50 TH_MATH_ERROR(
"rand_uniform", theta.size(), INVALID_ARGUMENT);
64 return (g() % 2 == 0) ? 1 : -1;
89 return (g() % faces) + 1;
99 if(theta.size() != 1) {
100 TH_MATH_ERROR(
"rand_diceroll", theta.size(), INVALID_ARGUMENT);
142 unsigned int iter = 0;
148 }
while(y > f(x, theta) && iter <= max_iter);
150 if(iter > max_iter) {
172 PRNG& g,
unsigned int max_tries = 100) {
174 for (
unsigned int i = 0; i < max_tries; ++i) {
180 const real x_p = Pinv(u_1);
185 if(u_2 * p(x_p) < f(x_p, theta))
189 TH_MATH_ERROR(
"rand_reject_sample", max_tries, NO_ALGO_CONVERGENCE);
202 static bool has_spare =
false;
206 return mean + spare * sigma;
227 return mean + sigma * x * s;
239 static bool has_spare =
false;
243 return mean + spare * sigma;
259 return mean + sigma * u;
277 constexpr
unsigned int N = 12;
280 for (
unsigned int i = 0; i < N; ++i)
287 return mean + (s /
static_cast<real>(N)) * sigma * 6;
309 PRNG& g,
unsigned int N) {
312 for (
unsigned int i = 0; i < N; ++i)
319 return mean + (s /
static_cast<real>(N)) * sigma *
sqrt(3 * N);
336 if(theta.size() != 2) {
337 TH_MATH_ERROR(
"rand_gaussian", theta.size(), INVALID_ARGUMENT);
364 if(theta.size() != 1) {
365 TH_MATH_ERROR(
"rand_exponential", theta.size(), INVALID_ARGUMENT);
387 if(theta.size() != 1) {
388 TH_MATH_ERROR(
"rand_rayleigh", theta.size(), INVALID_ARGUMENT);
410 if(theta.size() != 2) {
411 TH_MATH_ERROR(
"rand_cauchy", theta.size(), INVALID_ARGUMENT);
432 if(theta.size() != 2) {
433 TH_MATH_ERROR(
"rand_laplace", theta.size(), INVALID_ARGUMENT);
455 if(theta.size() != 2) {
456 TH_MATH_ERROR(
"rand_pareto", theta.size(), INVALID_ARGUMENT);
483 const std::vector<real>&
theta,
498 template<
typename Vector>
499 inline void fill(Vector& x,
size_t N) {
501 for (
size_t i = 0; i < N; ++i)
507 template<
typename Vector>
510 for (
size_t i = 0; i < x.size(); ++i)
578 real current = x0, next;
580 for(
unsigned int i = 0; i < depth; i++) {
583 next = current + g();
606 return metropolis(pdf, g, x0, g.generator, depth);
A pseudorandom number generator.
Definition: prng.h:18
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition: error.h:219
real mean(const histogram &h)
Compute the mean of the values of a histogram.
Definition: histogram.h:296
Main namespace of the library which contains all functions and objects.
Definition: algebra.h:27
real rand_laplace(real mu, real b, PRNG &g)
Generate a random number following a Laplace distribution using the quantile (inverse) function metho...
Definition: sampling.h:421
double real
A real number, defined as a floating point type.
Definition: constants.h:188
real(*)(const std::vector< real > &, PRNG &) pdf_sampling_function
A p.d.f sampling function taking as input the parameters of the distribution and a pseudorandom numbe...
Definition: sampling.h:18
dual2 sqrt(dual2 x)
Compute the square root of a second order dual number.
Definition: dual2_functions.h:54
constexpr unsigned int STATISTICS_METROPOLIS_DEPTH
Default depth of the Metropolis algorithm.
Definition: constants.h:317
dual2 ln(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition: dual2_functions.h:139
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition: dual2_functions.h:183
constexpr uint64_t STATISTICS_RAND_PREC
Default precision for random number generation using rand_uniform()
Definition: constants.h:314
real rand_pareto(real x_m, real alpha, PRNG &g)
Generate a random number following a Pareto distribution using the quantile (inverse) function method...
Definition: sampling.h:443
real rand_exponential(real lambda, PRNG &g)
Generate a random number following an exponential distribution using the quantile (inverse) function ...
Definition: sampling.h:347
real rand_rayleigh(real sigma, PRNG &g)
Generate a random number following a Rayleigh distribution using the quantile (inverse) function meth...
Definition: sampling.h:375
real rand_uniform(real a, real b, PRNG &g, uint64_t prec=STATISTICS_RAND_PREC)
Generate a pseudorandom real number in [a, b] using a preexisting generator.
Definition: sampling.h:33
real metropolis(real_function pdf, pdf_sampler &g, real x0, PRNG &rnd, unsigned int depth=STATISTICS_METROPOLIS_DEPTH)
Metropolis algorithm for distribution sampling using a symmetric proposal distribution.
Definition: sampling.h:574
constexpr unsigned int STATISTICS_TRYANDCATCH_ITER
Maximum number of failed iterations for the Try-and-Catch algorithm.
Definition: constants.h:299
real powf(real x, real a)
Approximate x elevated to a real exponent.
Definition: real_analysis.h:796
real rand_rejectsamp(stat_function f, const vec< real > &theta, real_function p, real_function Pinv, PRNG &g, unsigned int max_tries=100)
Generate a random number following any given distribution using rejection sampling.
Definition: sampling.h:169
real rand_gaussian(real mean, real sigma, PRNG &g)
Generate a random number following a Gaussian distribution using the best available algorithm.
Definition: sampling.h:325
real rand_gaussian_clt(real mean, real sigma, PRNG &g)
Generate a random number in a range following a Gaussian distribution by exploiting the Central Limit...
Definition: sampling.h:274
real rand_gaussian_boxmuller(real mean, real sigma, PRNG &g)
Generate a random number following a Gaussian distribution using the Box-Muller method.
Definition: sampling.h:236
real rand_diceroll(unsigned int faces, PRNG &g)
Dice roll random generator.
Definition: sampling.h:82
dual2 cos(dual2 x)
Compute the cosine of a second order dual number.
Definition: dual2_functions.h:82
real rand_cointoss(PRNG &g)
Coin toss random generator.
Definition: sampling.h:62
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition: constants.h:197
real rand_cauchy(real mu, real alpha, PRNG &g)
Generate a random number following a Cauchy distribution using the quantile (inverse) function method...
Definition: sampling.h:398
std::function< real(real, const vec< real > &)> stat_function
Function pointer to a probability distribution function where the first argument is the variable and ...
Definition: function.h:30
dual2 tan(dual2 x)
Compute the tangent of a second order dual number.
Definition: dual2_functions.h:94
real rand_gaussian_polar(real mean, real sigma, PRNG &g)
Generate a random number following a Gaussian distribution using Marsaglia's polar method.
Definition: sampling.h:199
real rand_trycatch(stat_function f, const vec< real > &theta, real x1, real x2, real y1, real y2, PRNG &g, unsigned int max_iter=STATISTICS_TRYANDCATCH_ITER)
Generate a pseudorandom value following any probability distribution function using the Try-and-Catch...
Definition: sampling.h:133
std::function< real(real)> real_function
Function pointer to a real function of real variable.
Definition: function.h:20
dual2 sin(dual2 x)
Compute the sine of a second order dual number.
Definition: dual2_functions.h:70
dual2 square(dual2 x)
Return the square of a second order dual number.
Definition: dual2_functions.h:23
constexpr real PI
The Pi mathematical constant.
Definition: constants.h:206
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54
constexpr real TAU
The Tau mathematical constant (Pi times 2)
Definition: constants.h:218
Pseudorandom number generator class.
A probability density function sampler which generates pseudorandom numbers following asymptotically ...
Definition: sampling.h:468
void fill(Vector &x, size_t N)
Fill a vector with sampled points.
Definition: sampling.h:499
void fill(Vector &x)
Fill a vector with sampled points.
Definition: sampling.h:508
pdf_sampler & operator>>(real &x)
Stream the next generated number.
Definition: sampling.h:516
std::vector< real > theta
The parameters of the target distribution.
Definition: sampling.h:474
PRNG & generator
A pseudorandom number generator.
Definition: sampling.h:477
static pdf_sampler rayleigh(real sigma, PRNG &generator)
Returns a Rayleigh distribution sampler.
Definition: sampling.h:547
static pdf_sampler cauchy(real mu, real alpha, PRNG &generator)
Returns a Cauchy distribution sampler.
Definition: sampling.h:541
real operator()()
Generate the next number.
Definition: sampling.h:493
pdf_sampler(pdf_sampling_function pdf, const std::vector< real > &theta, PRNG &generator)
Initialize the sampler with the given parameters.
Definition: sampling.h:481
pdf_sampling_function pdf
A p.d.f sampling function.
Definition: sampling.h:471
static pdf_sampler exponential(real lambda, PRNG &generator)
Returns an exponential distribution sampler.
Definition: sampling.h:535
static pdf_sampler gaussian(real mean, real sigma, PRNG &generator)
Returns a Gaussian distribution sampler.
Definition: sampling.h:529
static pdf_sampler laplace(real mu, real b, PRNG &generator)
Returns a Laplace distribution sampler.
Definition: sampling.h:559
static pdf_sampler pareto(real x_m, real alpha, PRNG &generator)
Returns a Pareto distribution sampler.
Definition: sampling.h:553
real next()
Generate the next number.
Definition: sampling.h:488
static pdf_sampler uniform(real a, real b, PRNG &generator)
Returns a uniform distribution sampler.
Definition: sampling.h:523