Monte Carlo Integration

Overview

It is well known that monte carlo simulations can be used to calculate integrals
{% \mathbb{E}(g(x)) = \displaystyle \int_{a}^{b} g(x)f(x)dx %}
when f(x) is taken to be uniform over the interval [a,b]
{% \mathbb{E}(g(x)) = \displaystyle \int_{a}^{b} g(x)/(b-a)dx = \frac{1}{b-a} \displaystyle \int_{a}^{b} g(x)dx %}
{% \displaystyle \int_{a}^{b} g(x)dx = (b-a) \times \mathbb{E}(g(x)) %}
The following example demonstrates calculating the area of a circle with radius 1 (and hence calculating PI).


	let mt = await import('/lib/numeric/integration/v1.0.0/monte-carlo.mjs');
	
	let integral = mt.integral({
	iterations:10000,
	ranges:[[-1,1],[-1,1]],
	accept:function(x, y){
	return x*x + y*y <= 1;
	}
	})
	
Try it!