Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Sometimes a value cannot be found so easily, this is the case for probabilities for example. When it comes to find the chances, say of a football team winning a certain match, you cannot use traditional probability approach, which is:

or, I should say, you could use it, but it can be misleading. For instance, the probability of winning for each team should be 50% even if the two teams are a Premier League team and a local team… is there anyone who believes this?
A better approach in these cases is simulating the entire process (i.e. the game) a big number of time. The bigger the number of simulations, the more precise the parameter(s) estimate. In case of a football match for example, you could just model all the players and their characteristics and simulate thousands of games. Even in this case you do not have a 100% confidence that your estimate is correct, since the initial parameters in the simulations, of course, affect the outcome and therefore a wrong initial parameter could lead to a wrong estimate. However, you can get interesting results using this approach, especially if you apply it to games of chance.

Another use of this method is the approximations of parameters such as pi, areas, and other values which are not easily computable in another way or that for some reason you do not want to compute in another way.

We can use R and Monte Carlo method to approximate the value of pi, and this is exactly what this post aims to.
By the way, approximating values using Monte Carlo method is one of my favourite techniques.

Perhaps in other posts I will write about Monte Carlo methods and games of chance.

The approach to calculate pi using Monte Carlo is pretty straightforward:
Say you have a circumference of radius 1, you can fit a square around it and easily calculate the area of the square.
Now, imagine to fill the square with thousands of dots. (You can do the same with a semi-circumference and it will be easier to code that, therefore we will opt for this solution).

The more dots you use, the more precise the approximation of pi.

Some of them will be inside the circumference and some of them will be outside of it. By calculating the proportion of inside dots and outside dots you can find the ratio of the circumference area to the square area. Now you can approximate the circumference area and reversing the formula for the area, obtain an approximation of pi.

In this example we use 100.000 points.

The output should be something like:
“The area of the semi-circumference: 1.571174; Our approximation of pi: 3.142348; We missed of -0.0240434229862333 percent”

In this case we made an approximation error of -0.02 percent with respect to the built-in value of pi. Not bad huh?