Programming Snapshot – Probability

At a party with 23 guests, having two guests with the same birthday in more than 50 percent of cases may sound fairly unlikely to amateur mathematicians. Armed with statistical methods, party animal Mike Schilli sets out to prove this claim.

The problem depends on the exact wording. Nobody can expect to go to a party with 23 people and meet someone with the same date of birth with 50 percent probability. The unexpected result comes about by the fact that n guests are compared with each other (i.e., each with (n – 1) other guests). It is much more likely that two random guests will be born in the same month and on the same day (the year is not considered) than if you only compare your own birthday with that of (n – 1) guests [1].

Bottom Up

At a party with only two guests, what is the probability of both celebrating their birthday on the same day? Assuming a year to be 365 days for the sake of ease, without taking into account seasonal birth fluctuations or special cases such as twin parties, this occurs in one in 365 cases. Conversely, the probability that both guests have birthdays on different days is 364 in 365.

If another guest joins the pair, the probability that no one in the room is celebrating their birthday on the same day is the coincidence of two independent events: The first event, which we just calculated to occur with the probability 364/365, and a second event, where the added person does not share a birthday with the first or the second person and can thus celebrate a birthday on only 363 of 365 days.

A statistician determines the total probability of different birthdays for three guests by multiplying the probabilities of the two independent single events above, which comes to 364/365 x 363/365, or around 0.991795. The reverse event, namely the case where two or more people celebrate their birthdays on the same day, results in a probability of 1 – 0.991795, or 0.008205.

The sequence continues with guests number four, five, and so on. In each round, the number of remaining days, and thus the numerator of the fraction, is decremented by one; the result of which is multiplied by the probability of the previous round.

Listing 1[2] shows a lean Python implementation of the calculation. The output (Figure 1) shows that the 50 percent probability of a shared birthday between two guests was exceeded for the 23rd guest, showing a value of 50.73 percent. The script sets the number of days remaining in the calendar to 365 at the beginning and subtracts a value of 1 from it after each round, when a new guest with an unseen birthday arrives. The probability prob indicates the likelihood of no one in the room sharing their birthday with another person: For a room with only one person, this is obviously 1; for two, it is 0.9973.

In each iteration of the loop, Listing 1 multiplies the probability of the last round in prob by the new event's probability value and assigns the result back to prob. However, the probability of having no shared birthdays is not what we are looking for; instead, we want the opposite – the chance of a collision. Therefore, line 9 indicates the probability of the opposite event, or the chance of one or more people at the party sharing birthdays, or 1-prob.

Simulator

A simulation script (Listing 2) will show whether the formula for the computation was correct; in each round it assigns 23 guests in the guest_bdays list to a party, assigns each of them a random birthday from a list of 365 integer values, and then decides in the bday_match() function whether there are integer duplicates in guest_bdays. The randint() function from the random module outputs values between the extremes 1 and 365 (inclusive) for birthdays.

The for loop starting in line 13 iterates over a total of 10 test runs with 100,000 parties each. For each event showing a birthday pair, it increases the counter in matches by one. At the end of each run, the script prints the fraction of the number of parties with shared birthdays relative to the total number of parties; Figure 2 shows that the value settles at about 50.7 percent.

Figure 2: The 23-person simulation script settles at about 50 percent probability of a match.

The bday_match() function from line 5 expects a Python list with integers and checks if there are one or more duplicates. This test is efficient because it uses a hash function to squash previously seen values into a seen set; it can then quickly check whether the value is already in the set for each newly examined value. If you have ever had this task in a recruitment test, you will be aware that the compute time for the duplicate check using this procedure drops to O(n) for n list elements, while it would be O(n^2) for a less clever two-loop solution.

Black on White

How does the probability of a birthday collision develop with an increasing number of guests? Thanks to the matplotlib Python library, simply installed with

With the special file handle sys.stdin, Listing 3 reads the output lines of Listing 1 and uses the split() method in line 9 to split them at the colon, thus separating the number of guests and the probability. For the x and y values in the graph, it compiles the x and y lists by appending the latest value with the append() method to the respective list for each value pair. The plot() method then collectively accepts all x and y values and draws the graph that the savefig() method writes to a PNG image file in line 17. It can hardly be done with less effort, and the graph looks quite appealing.

./birthday-paradox | ./bd-plot

What happens at a party with 100 participants can also be determined with these scripts: The probability of two guests sharing a birthday bounces up to 99.99996928 percent; the chance that all guests have different birthdays at this mega party is more than 1 in 3 million.

Mike Schilli works as a software engineer in the San Francisco Bay area, California. Each month in his column, which has been running since 1997, he researches practical applications of various programming languages. If you go to mailto:mschilli@perlmeister.com he will gladly answer any questions.

Dynamic resource allocation and migration of virtual machines between hosts mean that VMware environments pose new monitoring challenges. A new version of the free OpenNMS network management tool now includes an option for monitoring VMware-based infrastructures.