6 #ifndef THEORETICA_MONTECARLO_H
7 #define THEORETICA_MONTECARLO_H
12 #include "../algebra/algebra_types.h"
27 PRNG& g,
unsigned int N = 1000) {
31 for (
unsigned int i = 0; i < N; ++i)
34 return (b - a) * sum_y /
static_cast<real>(N);
43 template<
unsigned int S>
46 PRNG& g,
unsigned int N = 1000) {
51 for (
unsigned int i = 0; i < N; ++i) {
54 for (
unsigned int k = 0; k < S; ++k)
62 for (
unsigned int i = 0; i < S; ++i)
63 volume *= (extremes[i][1] - extremes[i][0]);
65 return volume * sum_y /
static_cast<real>(N);
79 unsigned int N = 1000) {
83 for (
unsigned int i = 0; i < N; ++i)
86 return (b - a) * sum_y /
static_cast<real>(N);
97 template<
unsigned int S>
104 for (
unsigned int i = 0; i < N; ++i) {
107 for (
unsigned int k = 0; k < S; ++k)
108 v[k] = extremes[k][0]
110 * (extremes[k][1] - extremes[k][0]));
116 for (
unsigned int i = 0; i < S; ++i)
117 volume *= (extremes[i][1] - extremes[i][0]);
119 return volume * sum_y /
static_cast<real>(N);
130 template<
unsigned int S>
133 unsigned int N = 1000,
real alpha = 0) {
142 return pow(x, S + 1) - x - 1;
147 for (
unsigned int i = 0; i < S; ++i)
148 v[i] =
pow(alpha, i + 1);
165 PRNG& g,
unsigned int N = 1000) {
169 for (
unsigned int i = 0; i < N; ++i) {
174 const real f_x = f(x_n);
185 return (N_inside /
static_cast<real>(N)) * (b - a) * (d - c);
203 PRNG& g,
unsigned int N = 1000) {
205 unsigned int N_inside = 0;
207 for (
unsigned int i = 0; i < N; ++i) {
216 return (N_inside /
static_cast<real>(N)) * (b - a) * f_max;
231 unsigned int N = 1000) {
235 for (
unsigned int i = 0; i < N; ++i) {
239 const real x_n = a + (b - a) * v[0];
240 const real y_n = c + (d - c) * v[1];
242 const real f_x = f(x_n);
253 return (N_inside /
static_cast<real>(N)) * (b - a) * (d - c);
268 unsigned int N = 1000) {
270 unsigned int N_inside = 0;
272 for (
unsigned int i = 0; i < N; ++i) {
276 const real x_n = a + (b - a) * v[0];
277 const real y_n = v[1] * f_max;
283 return (N_inside /
static_cast<real>(N)) * (b - a) * f_max;
302 unsigned int N = 1000) {
304 unsigned int N_inside = 0;
306 for (
unsigned int i = 0; i < N; ++i) {
316 return (N_inside /
static_cast<real>(N)) * (b - a) * (d - c) * f_max;
329 PRNG& gen,
unsigned int N = 1000) {
333 for (
unsigned int i = 0; i < N; ++i) {
335 sum_y += f(z) / g(z);
338 return sum_y /
static_cast<real>(N);
351 unsigned int N = 1000) {
355 for (
unsigned int i = 0; i < N; ++i) {
357 sum_y += f(z) / g(z);
360 return sum_y /
static_cast<real>(N);
373 typename Vector = std::vector<real>,
374 typename Function = std::function<
real(vec<real>)>
376 Vector
sample_mc(Function f, std::vector<pdf_sampler>& rv,
unsigned int N) {
384 for (
unsigned int i = 0; i < N; ++i) {
386 for (
unsigned int j = 0; j < rv.size(); ++j)
A pseudorandom number generator.
Definition: prng.h:18
A statically allocated N-dimensional vector with elements of the given type.
Definition: vec.h:88
void resize(size_t n) const
Compatibility function to allow for allocation or resizing of dynamic vectors.
Definition: vec.h:412
Main namespace of the library which contains all functions and objects.
Definition: algebra.h:27
double real
A real number, defined as a floating point type.
Definition: constants.h:188
real integral_crude(real_function f, real a, real b, PRNG &g, unsigned int N=1000)
Approximate an integral by using Crude Monte Carlo integration.
Definition: montecarlo.h:24
real integral_quasi_impsamp(real_function f, real_function g, real_function Ginv, unsigned int N=1000)
Approximate an integral by using Crude Quasi-Monte Carlo integration with importance sampling,...
Definition: montecarlo.h:349
real integral_quasi_crude(real_function f, real a, real b, unsigned int N=1000)
Approximate an integral by using Crude Quasi-Monte Carlo integration by sampling from the Weyl sequen...
Definition: montecarlo.h:76
real integral_hom_2d(real(*f)(real, real), real a, real b, real c, real d, real f_max, PRNG &g, unsigned int N=1000)
Use the Hit-or-Miss Monte Carlo method to approximate a double integral.
Definition: montecarlo.h:297
real integral_quasi_hom(real_function f, real a, real b, real c, real d, unsigned int N=1000)
Approximate an integral by using Hit-or-miss Quasi-Monte Carlo integration, sampling points from the ...
Definition: montecarlo.h:228
real integral_hom(real_function f, real a, real b, real c, real d, PRNG &g, unsigned int N=1000)
Approximate an integral by using Hit-or-miss Monte Carlo integration.
Definition: montecarlo.h:162
real integral_impsamp(real_function f, real_function g, real_function Ginv, PRNG &gen, unsigned int N=1000)
Approximate an integral by using Crude Monte Carlo integration with importance sampling.
Definition: montecarlo.h:327
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
Vector sample_mc(Function f, std::vector< pdf_sampler > &rv, unsigned int N)
Generate a Monte Carlo sample of values of a given function of arbitrary variables following the give...
Definition: montecarlo.h:376
real qrand_weyl(unsigned int n, real alpha=INVPHI)
Weyl quasi-random sequence.
Definition: quasirandom.h:24
vec2 qrand_weyl2(unsigned int n, real alpha=0.7548776662466927)
Weyl quasi-random sequence in 2 dimensions.
Definition: quasirandom.h:70
std::function< real(real)> real_function
Function pointer to a real function of real variable.
Definition: function.h:20
dual2 pow(dual2 x, int n)
Compute the n-th power of a second order dual number.
Definition: dual2_functions.h:41
real root_bisection(RealFunction f, real a, real b, real tolerance=OPTIMIZATION_TOL)
Approximate a root of an arbitrary function using bisection inside a compact interval [a,...
Definition: roots.h:56
Pseudorandom number generator class.
Sampling from probability distributions.