How to estimate PI when we only have R and the formula for the surface of a circle (Surface = PI * r * r)?

The estimation of this number has been one of the greatest challenge in the history of mathematics. PI is the ratio between a circle's circumference and diameter. The first estimation I have heard about has been done by an Egyptian 3700 years ago. His estimation was around 3.16. Which is fairly accurate for a 3700-year-old estimation.

In this post I propose an easy way to estimate PI. Though it is far from being the most efficient estimation ever made, it is a good opportunity to introduce Monte Carlo simulation in the blog.

Monte Carlo simulations are methods to estimate results by repeating a random process. The limit of this method is the source of randomness in the results. By construction of these methods, it cannot be mathematically proved, but only "confidence interval" results. Besides, most of these methods are very slow if we want to obtain a fairly accurate result. The optimization of Monte Carlo is an amazing area which is really useful in many disciplines. 

Again, the method I program here, is far from being efficient, it is only a basic of the Monte Carlo theory and I think one of the easiest illustrations.

Let's explain how it works.

As we know, if r is the radius of a circle, then the surface of this circle is PI * r * r. Therefore, if we know the radius of a circle, and if we can estimate the surface of this circle, we can estimate PI. How can we estimate the surface of a circle with a random method. The answer is found in the square! Yes, in the square. Especially in the following figure. 


A circle in a square is our solution. Indeed, this is very easy to know the surface of a square. It is the square of the length of one of its sides. Do you see where this is leading us? What if we were able to estimate the rate between the surface of the circle and the surface of the square? Then we would know the surface of the circle.

Indeed, 
surfaceCircle = (surfaceCircle / surfaceSquare) * surfaceSquare. 



Finally, we just need to estimate (surfaceCircle/surfaceSquare). And this is where Monte Carlo is useful. If we uniformly distribute k random variables in the square and count how many of them are in the circle, then we have a proxy of the quantity surfaceCircle/surfaceSquare.

We would obtain something like the next graph. Then the proportion between the number of blue spots and the total number of spots is equal to surfaceCircle/surfaceSquare.


I have done this experience with R. The next plot is the estimation of PI we can obtain with this method. The red line is the real value of PI. The black line is my estimator of PI according to the number of simulated random variables. As we can see, the accuracy tends to increase with the size of our sample. After 10,000 simulations, we obtain an estimation of PI of 3.1444 which is not so good. Indeed, you can already find in the Old Testament a better estimation of PI!



Even though it is a weak estimation, Monte Carlo simulation is a very powerful method. Indeed, if the circle is now a well known mathematical object, all the mathematical concepts are not as well understood. The advantage of Monte Carlo simulation is that we do not need any analytic study of the object. This is why Monte Carlo is used in many applied areas.

 The code (R):

simulation = function(long){
  c = rep(0,long)
  numberIn = 0
  for(i in 1:long){
    x = runif(2,-1,1)
    if(sqrt(x[1]*x[1] + x[2]*x[2]) <= 1){
      numberIn = numberIn + 1
    }
    prop = numberIn / i
    piHat = prop *4
    c[i] = piHat
  }
  return(c)
}

size = 1000
res = simulation(size)
ini = 1
plot(res[ini:size], type = 'l')
lines(rep(pi, size)[ini:size], col = 'red')
0

Add a comment

The financial market is not only made of stock options. Other financial products enable market actors to target specific aims. For example, an oil buyer like a flight company may want to cover the risk of increase in the price of oil. In this case it is possible to buy on the financial market what is known as a "Call" or a "Call Option".

A Call Option is a contract between two counterparties (the flight company and a financial actor). The buyer of the Call has the opportunity but not the obligation to buy a certain  quantity of a certain product (called the underlying) at a certain date (the maturity) for a certain price (the strike).

I found a golden website. The blog of Esteban Moro. He uses R to work on networks. In particular he has done a really nice code to make some great videos of networks. This post is purely a copy of his code. I just changed a few arguments to change colors and to do my own network.

To create the network, I used the  Barabási-Albert algorithm that you can find at the end of the post on the different algorithms for networks. Igraph is the library which has been used.
3

As you have certainly seen now, I like working on artificial neural networks. I have written a few posts about models with neural networks (Models to generate networks, Want to win to Guess Who and Study of spatial segregation).

Unfortunately, I missed so far a nice and pleasant aspect of networks : its graphical approach. Indeed, plots of neural networks are often really nice and really useful to understand the network.

Sometimes such a graph can point out some characteristics of the network.
1

I already talked about networks a few times in this blog. In particular, I had this approach to explain spatial segregation in a city or to solve the Guess Who? problem. However, one of the question is how to generate a good network. Indeed, I aim to study strategy to split a network, but I need first to work with a realistic neural network. I could have downloaded data of a network, but I'd rather study the different models proposed to generate neural networks.

The function apply() is certainly one of the most useful function. I was scared of it during a while and refused to use it. But it makes the code so much faster to write and so efficient that we can't afford not using it. If you are like me, that you refuse to use apply because it is scary, read the following lines, it will help you. You want to know how to use apply() in general, with a home-made function or with several parameters ? Then, go to see the following examples.
1

Have you ever played the board game "Guess who?". For those who have not experienced childhood (because it might be the only reason to ignore this board game), this is a game consisting in trying to guess who the opponent player is thinking of among a list of characters - we will call the one he chooses the "chosen character". These characters have several characteristics such as gender, having brown hair or wearing glasses.

If you want to choose randomly your next holidays destination, you are likely to process in a way which is certainly biased. Especially if you choose randomly the latitude and the longitude. A bit like they do in this lovely advertising (For those of you who do not speak French, this is about a couple who have won the national gamble prize and have to decide their next travel. The husband randomly picks Australia and the wife is complaining : "Not again!").
4

My previous post is about a method to simulate a Brownian motion. A friend of mine emailed me yesterday to tell me that this is useless if we do not know how to simulate a normally distributed variable.

My first remark is: use the rnorm() function if the quality of your simulation is not too important (Later, I'll try to explain you why the R "default random generation" functions are not perfect). However, it may be fun to generate a normal distribution from a simple uniform distribution.

The Brownian motion is certainly the most famous stochastic process (a random variable evolving in the time). It has been the first way to model a stock option price (Louis Bachelier's thesis in 1900).

The reason why is easy to understand, a Brownian motion is graphically very similar to the historical price of a stock option.
1

The merge of two insurance companies enables to curb the probability of ruin by sharing the risk and the capital of the two companies.

For example, we can consider two insurance companies, A and B. A is a well known insurance company with a big capital and is dealing with a risk with a low variance. We will assume that the global risk of all its customers follow a chi-square distribution with one degree of freedom.
Blog Archive
Translate
Translate
Loading