42 Computer simulation
Computers can help with more complex simulations.
While we can use dice, coins, counters in a jar, pieces of paper in a hat, or more sophisticated devices for running real simulations, these will be time consuming and impractical for obtaining accurate probability estimates. This is why research often involves computer simulation. By ‘simulation’ we mean imitating trials and events where the mathematical structure should be the same as in real life. We can use this to support theoretical calculations, to obtain results for probabilities where theoretical calculation is difficult (or impossible), or to predict probabilities when it would be time consuming to collect real data.
Let’s consider the Birthday Paradox and write a program for modelling it. We’ll start with the problem. Have a go at coming up with a method for answering it, but don’t spend more than 15 minutes as we don’t expect you to quickly find an accurate answer.
The Birthday Paradox
There are 23 people at a train platform, waiting for a train to come. What is the probability that at least 2 people in that group will share a birthday? |
Before we get into modelling the probability with a computer, let’s have a go at real simulation.
Simulation
Birth Month simulation – we’ll do birth month because it means we don’t need to find a deck of 365 cards!
- Take a set of 12 playing cards (numbered 1 – 10, then include a jack and a queen for 11 and 12). We’ll use these to represent the probability of being born in a particular month (and we’re approximating these probabilities as 1/12 each, even though there might be fewer people born in February).
- Draw 5 cards (replace the cards in the set of 12 each time you draw a card and reshuffle) and write down the sequence of 5 numbers. This represents one trial. If you drew the same number twice, write “success” next to the 5 numbers.
- Repeat the trial 9 more times (10 in total).
- How many times did you have a success? What’s the experimental probability you obtained for any group of 5 people having two people born in the same month.
Now let’s have a go at writing code to program the set of birth months.
In this case, Scratch allows us to simulate 100s of 1000s of trials in seconds, so we should be able to get a good estimate on the probability. You can take a look at the Scratch code and then ‘look inside’ to change the parameters and adapt it so that it estimates the probability of two people sharing a birthday if you have a group of:
- 10 people
- 20 people
- 30 people
- 23 people
You can increase the number of trials if you want to try and make your estimate more reliable. With enough trials, running the Scratch code each time would have yielded a slightly different result. How can we be sure that the simulation we ran is reliable? We can run multiple simulations – and in this case if the number of trials is high enough, the results might not vary much.
Calculating the exact probability
Although programming is often used to estimate probabilities that are too difficult (or even impossible) to calculate theoretically, algorithms that simulate probabilistic scenarios can also be used to validate or test a known result. You might already have some idea as to how you might calculate the theoretical probabilities for the birthday and birth month problems – the resource below gives an overview, as well as some more code that you could use to do it for you.
Take a look at Calculating the theoretic probability for the birthday paradox to understand the calculations behind the birthday paradox.
In the previous example, we knew how to model the probability of a person being born on any particular day. Can you imagine the probabilities that we’d need to take into account for the simulations described below?
Asteroid trackers from Spain have upgraded the chance that asteroid 1999 RQ36 could hit our planet, saying it now has a one-in-a-thousand chance of impacting the Earth in the year 2182. Previous estimates gave a 1 in 1,400 chance that this asteroid could strike Earth sometime between 2169 and 2199. Currently, however, NASA’s Near Earth Object website gives between a 1 in 3,850 and a 1 in 3,570 chance that 1999 RQ35 could potentially impact Earth on Sept. 24, 2182. To make everyone breathe a little easier, that’s a 99.97200000% chance the asteroid will completely miss the Earth.
Nancy Atkinson, “Researchers Say Asteroid Has 1 in 1,000 Chance of Hitting Earth in 2182”, Universe Today, 27 July 2010, http://www.universetoday.com/69640/researchers-say-asteroid-has-1-in-1000-chance-of-hitting-earth-in-2182/