I was looking for an algorithm to create a PI estimator, and I ran across this: https://stackoverflow.com/questions/36659034/trying-to-create-a-pi-estimator-in-r
Briefly, the steps are:
(1) set j equal to 0.
(2) start a for loop with counter i that repeats N times.
(3) Inside the for loop, generate two random uniform numbers x and y between -1 and +1 using runif().
(4) If x^2 + y^2 < 1 then add one to j.
(5) End the for loop.
(6) Return the estimate of π which is 4×j/N
Can someone please explain me the logic behind the sampling of uniform distribution? And why these steps actually gives estimate to PI?
Thank you!
This is a monte carlo integration algorithm that finds the area of a circle of radius 1 inside a square with side 2. You choose a random point in the square and see whether it is inside the circle. The proportion of points inside the circle will approximate the area of the circle ($\pi$) over the area of the square, $4$.