https://github.com/cuadernin/integralsimulation
Computes the value of a definite integral using Monte Carlo simulation.
https://github.com/cuadernin/integralsimulation
integral monte-carlo monte-carlo-simulation simulation
Last synced: 6 months ago
JSON representation
Computes the value of a definite integral using Monte Carlo simulation.
- Host: GitHub
- URL: https://github.com/cuadernin/integralsimulation
- Owner: Cuadernin
- Created: 2021-02-04T03:46:14.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-06-19T03:57:33.000Z (over 4 years ago)
- Last Synced: 2024-05-17T16:59:19.998Z (over 1 year ago)
- Topics: integral, monte-carlo, monte-carlo-simulation, simulation
- Language: Python
- Homepage:
- Size: 11.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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.