bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
Monte Carlo integration

In this first example we explore some of the basic elements of bitrl. In particular, we will see how to read a json file, how to use boost logging, various types exposed by the library. We will do so by using a simple Monte Carlo integration example.

Monte Carlo intergration

Frequently in applications we need to evaluate integrals for which no analytical solution exists. Numerical methods can help us overcome this. Monte Carlo is just one of these methods. The method works due to the law of large numbers. Compared to a standard numerical method, the method may not be especially efficient in one dimension, but it becomes increasingly efficient as the dimensionality of the integral grows.

Let's assume that we want to evaluate the integral

\[I=\int_a^b h(x) dx \]

If \(f\) is a polynomial or a trigonometric function, then this integral can be calculated in closed form. However, in many cases there may not be a closed for solution for \(I\). Numerical techniques, such as Gaussian quadrature or the the trapezoid rule can be employed in order to evaluate \(I\). Monte Carlo integration is yet another techinque for evaluating complex integrals that is notable for its simplicity and generality [1].

Let's begine by rewriting \(I\) as follows

\[I=\int_a^b \omega(x)f(x) dx \]

where \(\omega=h(x)(b-a)\) and \(f(x) = 1/(b-a)\) i.e. \(f\) is the probability density for a uniform random variable over \((a,b)\) [1]. Recall that the expectation for a continuous variable \(X\) is given by

\[E\left[X\right]=\int xf(x)dx\]

Hence,

\[I=E\left[\omega(X)\right]\]

This is the basic Monte Carlo integration method [1]. In order to evaluate the integral \(I\), we evaluate the following expression

\[\hat{I} = \frac{1}{n}\sum_{i=1}^{N}\omega(x_i)\]

where \(x \sim U(a,b)\). By the law of large numbers it follows, [1],

\[\hat{I}\rightarrow E\left[\omega(X)\right] = I \]

Notice that the law of large numbers provides us with probability convergence. Hence \(\hat{I}\) will converge in probability to \(I\). The standard error, \(\hat{se}\), for the estimate is [1]

\[\hat{se} = \frac{s}{\sqrt{n}} \]

where

\[s^2 = \frac{\sum_{i}^{N}(\omega(x_i) - \hat{I} )^2}{n - 1} \]

A \(1-\alpha\) confidence interval for the estimate is given from, [1],

\[\hat{I} \pm z_{\alpha/2}\hat{se} \]

The driver code

The driver code for this tutorial is shown below.

#include "bitrl/base/bitrl_config.h"
#include <fstream>
#include <iostream>
#include <random>
namespace intro_example_1
{
const std::string CONFIG = "config.json";
} // namespace intro_example_1
int main()
{
using namespace intro_example_1;
try
{
BITRL_LOG_INFO << "Running example...";
BITRL_LOG_INFO << "Reading configuration file...";
JSONFileReader json_reader(CONFIG);
json_reader.open();
const auto R = json_reader.template get_value<real_t>("R");
const auto N_POINTS = json_reader.template get_value<uint_t>("N_POINTS");
const auto SEED = json_reader.template get_value<uint_t>("SEED");
const auto X = json_reader.template get_value<real_t>("X");
const auto Y = json_reader.template get_value<real_t>("Y");
// create a circle
Circle c(R, {X, Y});
// simple object to control iterations
IterationCounter counter(N_POINTS);
// how many points we found in the Circle
auto points_inside_circle = 0;
// the box has side 2
const real_t SQUARE_SIDE = R * 2.0;
std::uniform_real_distribution dist(0.0, SQUARE_SIDE);
std::mt19937 gen(SEED);
BITRL_LOG_INFO << "Starting computation...";
while (counter.continue_iterations())
{
auto x = dist(gen);
auto y = dist(gen);
if (c.is_inside(x, y, 1.0e-4))
{
points_inside_circle += 1;
}
}
BITRL_LOG_INFO << "Finished computation...";
auto area = (static_cast<real_t>(points_inside_circle) / static_cast<real_t>(N_POINTS)) *
std::pow(SQUARE_SIDE, 2);
BITRL_LOG_INFO << "Circle area calculated with:" << N_POINTS << " is: " << area;
BITRL_LOG_INFO << "Circle area: " << c.area();
}
catch (std::exception &e)
{
BITRL_LOG_ERROR << e.what();
}
catch (...)
{
BITRL_LOG_ERROR << "Unknown exception occured";
}
return 0;
}
bitrl's logging: the logger of bitsim, bitsim/utils/logger.h, with bitrl's messages going to the logg...
#define BITRL_LOG_INFO
Definition bitrl_log.h:36
#define BITRL_LOG_ERROR
Definition bitrl_log.h:38
The Circle class. Models a common circle.
Definition circle.h:19
The IterationCounter class.
Definition iteration_counter.h:15
Definition json_file_reader.h:16
int main()
Definition discounted_returns.cpp:19
@ R
Definition fixed_wing_dynamics.h:39
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
Definition monte_carlo_integration.cpp:19
const std::string CONFIG
Definition monte_carlo_integration.cpp:27

Running the code above produces the following output

[2024-05-30 17:51:47.253901] [0x00007fe2bbb7b540] [info] Reading configuration file...
[2024-05-30 17:51:47.254017] [0x00007fe2bbb7b540] [info] Starting computation...
[2024-05-30 17:51:47.258199] [0x00007fe2bbb7b540] [info] Finished computation...
[2024-05-30 17:51:47.258222] [0x00007fe2bbb7b540] [info] Circle area calculated with:100000 is: 3.14716
[2024-05-30 17:51:47.258226] [0x00007fe2bbb7b540] [info] Circle area: 3.14159

References

  1. Larry Wasserman, All of Statistics. A Concise Course in Statistical Inference, Springer 2003.