4#ifndef THEORETICA_SAMPLING_H
5#define THEORETICA_SAMPLING_H
8#include "../core/function.h"
40 return a + (b - a) * x;
50 if(theta.size() != 2) {
65 return (
g() % 2 == 0) ? 1 : -1;
100 if(theta.size() != 1) {
143 unsigned int iter = 0;
175 for (
unsigned int i = 0; i <
max_tries; ++i) {
228 return mean +
sigma * x *
s;
278 constexpr unsigned int N = 12;
281 for (
unsigned int i = 0; i < N; ++i)
288 return mean + (
s /
static_cast<real>(N)) *
sigma * 6;
310 PRNG&
g,
unsigned int N) {
313 for (
unsigned int i = 0; i < N; ++i)
337 if(theta.size() != 2) {
365 if(theta.size() != 1) {
388 if(theta.size() != 1) {
411 if(theta.size() != 2) {
433 if(theta.size() != 2) {
456 if(theta.size() != 2) {
484 const std::vector<real>&
theta,
499 template<
typename Vector>
514 for (
size_t i = 0; i < N; ++i)
520 template<
typename Vector>
593 for(
unsigned int i = 0; i <
depth; i++) {
A pseudorandom number generator.
Definition prng.h:19
A statically allocated N-dimensional vector with elements of the given type.
Definition vec.h:92
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compilation op...
Definition error.h:219
Vector & vec_error(Vector &v)
Overwrite the given vector with the error vector with NaN values.
Definition algebra.h:58
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:422
double real
A real number, defined as a floating point type.
Definition constants.h:207
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:342
dual2 ln(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition dual2_functions.h:195
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:242
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:444
real rand_exponential(real lambda, PRNG &g)
Generate a random number following an exponential distribution using the quantile (inverse) function ...
Definition sampling.h:348
Vector make_error()
Create a vector representing an error state, with all NaN values.
Definition algebra.h:103
real rand_rayleigh(real sigma, PRNG &g)
Generate a random number following a Rayleigh distribution using the quantile (inverse) function meth...
Definition sampling.h:376
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:587
constexpr unsigned int STATISTICS_TRYANDCATCH_ITER
Maximum number of failed iterations for the Try-and-Catch algorithm.
Definition constants.h:324
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:170
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:326
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:74
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:275
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:237
real rand_diceroll(unsigned int faces, PRNG &g)
Dice roll random generator.
Definition sampling.h:83
dual2 cos(dual2 x)
Compute the cosine of a second order dual number.
Definition dual2_functions.h:86
real rand_cointoss(PRNG &g)
Coin toss random generator.
Definition sampling.h:63
@ InvalidArgument
Invalid argument.
@ NoConvergence
Algorithm did not converge.
@ DivByZero
Division by zero.
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:216
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:399
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:26
dual2 tan(dual2 x)
Compute the tangent of a second order dual number.
Definition dual2_functions.h:100
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:200
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:134
std::function< real(real)> real_function
Function pointer to a real function of real variable.
Definition function.h:20
complex< T > powf(complex< T > z, real p)
Compute a complex number raised to a real power.
Definition complex_analysis.h:65
dual2 sin(dual2 x)
Compute the sine of a second order dual number.
Definition dual2_functions.h:72
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:225
constexpr real TAU
The Tau mathematical constant (Pi times 2)
Definition constants.h:237
real rand_uniform(real a, real b, PRNG &g, uint64_t prec=PSEUDORANDOM_PREC)
Generate a pseudorandom real number in [a, b] using a preexisting generator.
Definition sampling.h:34
constexpr uint64_t PSEUDORANDOM_PREC
Default precision for random number generation using rand_uniform()
Definition constants.h:339
Pseudorandom number generation.
A probability density function sampler which generates pseudorandom numbers following asymptotically ...
Definition sampling.h:469
void fill(Vector &x, size_t N)
Fill a vector with sampled points.
Definition sampling.h:500
void fill(Vector &x)
Fill a vector with sampled points.
Definition sampling.h:521
pdf_sampler & operator>>(real &x)
Stream the next generated number.
Definition sampling.h:529
std::vector< real > theta
The parameters of the target distribution.
Definition sampling.h:475
PRNG & generator
A pseudorandom number generator.
Definition sampling.h:478
static pdf_sampler rayleigh(real sigma, PRNG &generator)
Returns a Rayleigh distribution sampler.
Definition sampling.h:560
static pdf_sampler cauchy(real mu, real alpha, PRNG &generator)
Returns a Cauchy distribution sampler.
Definition sampling.h:554
real operator()()
Generate the next number.
Definition sampling.h:494
pdf_sampler(pdf_sampling_function pdf, const std::vector< real > &theta, PRNG &generator)
Initialize the sampler with the given parameters.
Definition sampling.h:482
pdf_sampling_function pdf
A p.d.f sampling function.
Definition sampling.h:472
static pdf_sampler exponential(real lambda, PRNG &generator)
Returns an exponential distribution sampler.
Definition sampling.h:548
static pdf_sampler gaussian(real mean, real sigma, PRNG &generator)
Returns a Gaussian distribution sampler.
Definition sampling.h:542
static pdf_sampler laplace(real mu, real b, PRNG &generator)
Returns a Laplace distribution sampler.
Definition sampling.h:572
static pdf_sampler pareto(real x_m, real alpha, PRNG &generator)
Returns a Pareto distribution sampler.
Definition sampling.h:566
real next()
Generate the next number.
Definition sampling.h:489
static pdf_sampler uniform(real a, real b, PRNG &generator)
Returns a uniform distribution sampler.
Definition sampling.h:536