Approximating Integrals using Numpy [ Riemann / Trapeze / Simpson’s / Adaptive /Monte Carlo Integration ] | by Jae Duk Seo | Towards Data Science

Approximating integrals is a very important task in computer science (even relating to neural networks). And today I wanted to implement different methods of approximating integrals.
Functions that we are going to Use
In total we are going to approximate the integral for four different functions
1. integral from -8 to 6     x dx = 18902. integral from -10 to 120 (x^2 + 100 - x^5) dx = -4976632440003. integral from -10 to 23   sqrt(x) dx = 2/3 (23 sqrt(23) + 10 i sqrt(10))≈73.5361 + 21.0819 i4. integral from 0 to pi     x sin(x^2) dx = sin^2(π^2/2)≈0.9513
And as seen above we can already see the results from analytical solutions.
Approximating Methods
Riemann integration
Trapeze integration
Simpson’s integration
Adaptive integration
Monte Carlo integration
As seen above we are going to use five different methods to estimate the integrals. Just as a side note, among the five methods four (except adaptive integral) have a hyper parameter as a number of samples. Increasing this value will result in better approximation of integral,
Function 1
While Adaptive integral got the most accurate integral value, other methods either under/over estimated. (of course as we increase number of samples it gets more accurate.)
Function 2
For this function adaptive integral method have under estimated the integral value for large amount.
Function 3
Except for the adaptive integral method overall other methods did quite well, for small number of data points.
Function 4
Interestingly similar results for sine function as well.
Interactive Code
To access the code for this post please click here.
Amazing Resources
Final Words
This was a good review as well as good programming practice. When I was playing around with the values, it became quite clear that Monte Carlo methods integration is a powerful method but it needs more time to converge properly.
If any errors are found, please email me at jae.duk.seo@gmail.com, if you wish to see the list of all of my writing please view my website here.
Reference
(2018). Maths.qmul.ac.uk. Retrieved 1 December 2018, from http://www.maths.qmul.ac.uk/~wj/MTH5110/notes/MAS235_lecturenotes1.pdfFourier Series GIF — Find & Share on GIPHY. (2018). GIPHY. Retrieved 1 December 2018, from https://giphy.com/gifs/fourier-series-HqZuL4RWgf2msMonte Carlo Integration In Python For Noobs. (2018). YouTube. Retrieved 1 December 2018, from https://www.youtube.com/watch?v=WAf0rqwAvggBatman Integration · LeiosOS . (2018). Leios.github.io. Retrieved 1 December 2018, from https://leios.github.io/Batman_MontecarloDhamdhere, K., Sundararajan, M., & Yan, Q. (2018). How Important Is a Neuron?. Arxiv.org. Retrieved 1 December 2018, from https://arxiv.org/abs/1805.12233
(2018). Maths.qmul.ac.uk. Retrieved 1 December 2018, from http://www.maths.qmul.ac.uk/~wj/MTH5110/notes/MAS235_lecturenotes1.pdf
Fourier Series GIF — Find & Share on GIPHY. (2018). GIPHY. Retrieved 1 December 2018, from https://giphy.com/gifs/fourier-series-HqZuL4RWgf2ms
Monte Carlo Integration In Python For Noobs. (2018). YouTube. Retrieved 1 December 2018, from https://www.youtube.com/watch?v=WAf0rqwAvgg
Batman Integration · LeiosOS . (2018). Leios.github.io. Retrieved 1 December 2018, from https://leios.github.io/Batman_Montecarlo
Dhamdhere, K., Sundararajan, M., & Yan, Q. (2018). How Important Is a Neuron?. Arxiv.org. Retrieved 1 December 2018, from https://arxiv.org/abs/1805.12233