TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Monte Carlo Integration More...
#include <cmath>
#include <cstdint>
#include <ctime>
#include <functional>
#include <iostream>
#include <random>
#include <vector>
Go to the source code of this file.
Namespaces | |
namespace | math |
for assert | |
namespace | monte_carlo |
Functions for the Monte Carlo Integration implementation. | |
Typedefs | |
using | math::monte_carlo::Function |
Functions | |
std::vector< double > | math::monte_carlo::generate_samples (const double &start_point, const Function &pdf, const uint32_t &num_samples, const uint32_t &discard=100000) |
short-hand for std::functions used in this implementation | |
double | math::monte_carlo::integral_monte_carlo (const double &start_point, const Function &function, const Function &pdf, const uint32_t &num_samples=1000000) |
Compute an approximation of an integral using Monte Carlo integration. | |
static void | test () |
Self-test implementations. | |
int | main () |
Main function. | |
In mathematics, Monte Carlo integration is a technique for numerical integration using random numbers. It is a particular Monte Carlo method that numerically computes a definite integral. While other algorithms usually evaluate the integrand at a regular grid, Monte Carlo randomly chooses points at which the integrand is evaluated. This method is particularly useful for higher-dimensional integrals.
This implementation supports arbitrary pdfs. These pdfs are sampled using the Metropolis-Hastings algorithm. This can be swapped out by every other sampling techniques for example the inverse method. Metropolis-Hastings was chosen because it is the most general and can also be extended for a higher dimensional sampling space.
Definition in file integral_approximation2.cpp.
#define _USE_MATH_DEFINES |
Definition at line 24 of file integral_approximation2.cpp.
using math::monte_carlo::Function |
Definition at line 46 of file integral_approximation2.cpp.
std::vector< double > math::monte_carlo::generate_samples | ( | const double & | start_point, |
const Function & | pdf, | ||
const uint32_t & | num_samples, | ||
const uint32_t & | discard = 100000 ) |
short-hand for std::functions used in this implementation
Generate samples according to some pdf
This function uses Metropolis-Hastings to generate random numbers. It generates a sequence of random numbers by using a markov chain. Therefore, we need to define a start_point and the number of samples we want to generate. Because the first samples generated by the markov chain may not be distributed according to the given pdf, one can specify how many samples should be discarded before storing samples.
start_point | The starting point of the markov chain |
The pdf to sample | |
num_samples | The number of samples to generate |
discard | How many samples should be discarded at the start |
Definition at line 64 of file integral_approximation2.cpp.
double math::monte_carlo::integral_monte_carlo | ( | const double & | start_point, |
const Function & | function, | ||
const Function & | pdf, | ||
const uint32_t & | num_samples = 1000000 ) |
Compute an approximation of an integral using Monte Carlo integration.
The integration domain [a,b] is given by the pdf. The pdf has to fulfill the following conditions: 1) for all x \in [a,b] : p(x) > 0 2) for all x \not\in [a,b] : p(x) = 0 3) \int_a^b p(x) dx = 1
start_point | The start point of the Markov Chain (see generate_samples) |
function | The function to integrate |
The pdf to sample | |
num_samples | The number of samples used to approximate the integral |
Definition at line 112 of file integral_approximation2.cpp.
int main | ( | void | ) |
Main function.
Definition at line 215 of file integral_approximation2.cpp.
|
static |
Self-test implementations.
Definition at line 133 of file integral_approximation2.cpp.