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)
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)
116 for (
unsigned int i = 0; i <
S; ++i)
130 template<
unsigned int S>
142 return pow(x,
S + 1) - x - 1;
147 for (
unsigned int i = 0; i <
S; ++i)
165 PRNG&
g,
unsigned int N = 1000) {
169 for (
unsigned int i = 0; i < N; ++i) {
185 return (
N_inside /
static_cast<real>(N)) * (b - a) * (d - c);
203 PRNG&
g,
unsigned int N = 1000) {
207 for (
unsigned int i = 0; i < N; ++i) {
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];
253 return (
N_inside /
static_cast<real>(N)) * (b - a) * (d - c);
268 unsigned int N = 1000) {
272 for (
unsigned int i = 0; i < N; ++i) {
276 const real x_n = a + (b - a) * v[0];
302 unsigned int N = 1000) {
306 for (
unsigned int i = 0; i < N; ++i) {
329 PRNG&
gen,
unsigned int N = 1000) {
333 for (
unsigned int i = 0; i < N; ++i) {
351 unsigned int N = 1000) {
355 for (
unsigned int i = 0; i < N; ++i) {
373 typename Vector = std::vector<real>,
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
void resize(size_t n) const
Compatibility function to allow for allocation or resizing of dynamic vectors.
Definition vec.h:416
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:198
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
vec< real, 2 > vec2
A 2-dimensional vector with real elements.
Definition algebra_types.h:39
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
std::remove_reference_t< decltype(std::declval< Structure >()[0])> vector_element_t
Extract the type of a vector (or any indexable container) from its operator[].
Definition core_traits.h:134
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 root_bisect(RealFunction f, real a, real b, real tol=OPTIMIZATION_TOL, unsigned int max_iter=OPTIMIZATION_BISECTION_ITER)
Find the root of a univariate real function using bisection inside a compact interval [a,...
Definition roots.h:58
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
Pseudorandom number generator class.
Sampling from probability distributions.