Monte Carlo calculates outcomes based on a randomised set of input data. It won’t pick the winning lottery tickets but it can help your decision making process.

From Manhattan to Monte Carlo

The Monte Carlo method helps us to predict potential outcomes. It can’t explicitly predict the future and sadly, it won’t pick the winning tickets for next week’s lottery. However, it can create a range of possible outcomes based on a randomised set of input data. It will also determine the relative likelihood of each of these outcomes. This helps us to make informed decisions and to plan our actions accordingly.

Its name comes from the famous gambling resort in Monaco. A principality on the Côte d’Azur or French Riviera as we know it. The Italian scientist, Enrico Fermi used the Monte Carlo method in the 1930s for his work on nuclear physics. It was later used in the Manhattan Project near the end of the Second World War. An initiative that Franklin D. Roosevelt put into place following advice from Einstein and other eminent physicists of the time.

Today, the use of Monte Carlo simulations reaches many different areas. Examples are finance, engineering, medicine, manufacturing and of course software development. We can use Monte Carlo to increase our confidence of meeting a delivery schedule or to analyse our development estimates. We can calculate potential revenue and show the likelihood of winning new business.

Why do we need it?

As humans, we have an insatiable appetite for knowledge. A timeless longing to learn about the world around us. This craving has turned us into a civilisation of tinkerers. Dreaming up increasingly elaborate experiments that help us to understand life, the universe and everything.

Our thirst for information is unquenchable. Radio telescopes allow us to see distant galaxies as they were billions of years ago. Particle accelerators enable us to detect subatomic particles – the building blocks of the universe. The equipment we need to help us is becoming increasingly complex and expensive. It is estimated that the total cost of finding the Higgs Boson is something in excess of 13 billion euros. CERN’s yearly operating costs alone exceed €800m.

As computing power increases, we are able to run simulations that complement our experiments. These simulations help us to predict possible outcomes given a set of varying input data. Using this information, we can focus our efforts by configuring our experiments accordingly. This saves time and reduces the associated costs.

A practical example

Let’s look at a how we can apply the Monte Carlo method to estimate the value of pi. First, take a square and draw the largest possible circle that fits inside it. Let’s give our circle a radius of r. This means that the length of each of the square’s sides is 2r; as the diagram shows.

Estimating Pi with Monte Carlo - Matthew Weaver

So we can see that the value of Pi is 4 times the area of the circle divided by the area of the square. To estimate Pi, we can plot a set of random points that lie inside the square. We then count the points that are inside the circle and divide by the total number of points we have plotted.  Multiply this by 4 and we have our estimate. For the diagram,  this gives us 10/13*4 which is 3.08. Of course this doesn’t come close to the actual value of pi; to do that, we need to plot more points. This is where a computer comes in handy. 

Practising with Python

The following demo is written using Python 3.5. You can see the code and run it here. This will repeatedly print estimated values for Pi until you stop the application.

There is also a version that produces a graphical output. For this you will need access to the numpy and matplotlib libraries. You can find the code for this version on GitHub. The image below shows the output from running the application 3 times.

Estimating Pi with Monte Carlo - Matthew Weaver

Next time you are working with project schedules and estimates, think about how Monte Carlo simulations can help you. A spreadsheet and a little creative thinking is all you need to get going.

Link to original article can be found here.