# Confusion related to the calculation of value of $\pi$

I was referring to this lecture Markov Chain Monte Carlo. However, I didn't get how the $\pi$ value was calculated. Here is a screenshot

As far as I know the integral gives the area under the curve. So I didn't get how the double integral is giving the value of $\pi$ using $P(x,y)$.

Also how did it convert to mean in Octave. I am just a beginner. So please some guidance is required

-

You are familiar with $\int_a^b f(x)\,dx$, which is an integral over an interval. One can also define "double" integrals, that is, integrals over a region of the plane. And triple integrals, and so on.

Here they are finding the double integral of a certain product over the unit square. The second term $P(x,y)$ is explained in your post. The first term is $I((x^2+y^2)\lt 1))$. This is an indicator function, which by definition is $1$ where $x^2+y^2\lt 1$, and $0$ elsewhere.

So the product that we are integrating is $1$ inside the quarter-circle, and $0$ outside. The integral over the square of the product is therefore the area of the quarter-circle.

I expect you understand the rest. But for completeness, effectively the program throws a dart repeatedly at the $1\times 1$ square, and records a $1$ for every time the dart lands in the quarter circle. If the number $N$ of dart throws is very large, and $H$ is the number of hits, one would expect $\dfrac{H}{N}$ to be close to the ratio of the area of the quarter-circle to the area of the square, that is, to $\dfrac{\pi}{4}$.

Alternately, one could use older notation, get rid of the indicator function, and integrate over the quarter-circle. But the indicator function version is closer in spirit to the dart throwing model, in the sense that, like in the dart throwing, we record a $1$ whenever we land inside the quarter-circle.

Remark: Although this is an inefficient way of approximating $\pi$, the general idea is extremely useful. It can be used to approximate the volume of extremely complicated multidimensional regions. Tou might look into Monte Carlo Methods.

-
I am a bit confused. What does $\int \int I(x^2 + y^2) <1) P(x,y) dxdy$ does here. I know simple integration calculate area.However here the function is a bit confusion – user34790 Oct 10 '12 at 20:38
@My question is how come we are having that mean in the octave function. If P(x,y) had been probability distribution so that sum of all P(xi,yi) = 1 then that is fine it is the average. But here P(x,y) = 1 if 0<x<1 and 0<y<1 so sum P(xi,yi) > 1. – user34790 Oct 10 '12 at 20:53
The density function is indeed $1$ on the unit square and $0$ elsewhere. So the probability of lanfing in a certain part $C$ of the square is the integral of the indicator function of $C$ times the density. Or, in more old-fashioned language, the integral over $C$ of the density function. Indicator functions have become very popular in probability. They used to be confined to the non-elementary parts of the subject. – André Nicolas Oct 10 '12 at 21:05

Actually you are counting the number of $\color{blue}{\text{dots}}$, that fall into to $\color{red}{\text{quarter circle}}$ divided by the total number of $\color{blue}{\text{dots}}$, which has area $\frac \pi4$.

You can use the "mean", since for a large number of $\color{blue}{\text{dots}}$, it approximates the integral.

EDIT If you would use $\color{blue}{\text{water }}$instead of $\color{blue}{\text{dots}}$, something like this was already written in the bible:

And he made a molten sea, ten cubits from the one brim to the other: it was round all about, and his height was five cubits: and a line of thirty cubits did compass it about. (I Kings 7, 23; II Chronicles 4, 2.)

-