/IntegralSimulation

Computes the value of a definite integral using Monte Carlo simulation.

Primary LanguagePython

Integral Simulation

Computes the value of a definite integral using Monte Carlo simulation in Python.

Problem to solve ✔

If X is a continuous random variable with density function fx and g is a continuous function, the expectation of g(X) is given by:

If X is uniformly distributed in [0, 1], then

This fact suggests that we can use simulation to estimate the previous integral, estimating the value of E(g(X)). If X1, X2, X3, ..., Xn are independent and uniformly distributed in [0, 1], then the g(Xi) are also independent. Therefore, if we define the mean of g(X) as:

and we assume that g(X) has mean μ and variance σ², then:

These two equalities suggest that to estimate μ, we can generate a sequence of numbers u1, u2, u3, ..., a uniformly distributed in [0, 1], independent and calculate (g(u1)+g(u2)+g(u3)+...+g(un))/n. Note that the variance of the mean g(X) can be made as small as you like by taking it large enough.

Input📋

A menu of options is displayed where you must choose the type of interval where the integral will be calculated, that is:

  • 1 == [a, b]
  • 2 == [0, 00]
  • 3 == [-00 , 00]

Next, you must write the function in terms of x and the limits of the interval.

exp(-x**2)

It means

Output 📦

  • Simulated value
  • Real value
  • Percentage error

Note

You can do the exact same thing for a multiple integral.