Physics Tom

Birthday Monte Carlo

03 August, 2021Readtime: 9 mins

Every so often the birthday problem comes up, whether it be in conversation, at a party, in the media, or overhearing on the train, I have heard it many times and usually always it results in an interesting discussion. After hearing about it again quite recently and discussing it with someone who would not believe it was true (to be fair it is a veridical paradox, so not intuitive), I thought I would devote a post on it. Besides it is has many real world applications, including hash collisions - which is relevant if you are at all concerned with online security (but especially if you are a crypto enthusiast).

Just in case you are not aware of it, this Wikipedia page does a good job of describing it and various ways to analyse it, but the the basic question is: “Given a set of randomly chosen people, nn, what number do I need to have at least 50% probability that at least two share the same birthday?“. If you’ve never encountered this before you would probably think the number would have to be quite high, and in that case you’d be mistaken, since it is just n=23n = 23. That is of course, if you think 23 is a small number - I don’t know many 23 year olds who think they are old.

Only 23, but there are 365 days in a year (ignorning leap years of course), that must be wrong… that’s how the conversation normally goes. Depending on the mathematical ability of the audience it can be quite difficult to explain how this works out, but the maths is below to show how it works.

Let us denote the probability that at least two people in the group of size nn have the same birthday, as P(n)P(n). Conversely, we can also denote the probability that no two people in the group share the same birthday, as P(n)ˉ\bar{P(n)}. These two are then mutally exclusive (we cannot have both occuring, and if one occurs the other cannot), therefore we have:

P(n)=1P(n)ˉP(n) = 1 - \bar{P(n)}

We can leverage this relationship to make our lives much easier, since in turns out that computing P(n)ˉ\bar{P(n)} is much easier than P(n)P(n). Let’s go through this one by one to understand how we can work it out.

First, let’s start with only 1 person in the room, that is, n=1n=1. It is then obvious that we have:

P(1)=0P(1) = 0
P(1)ˉ=1\bar{P(1)} = 1

Now a second person walks in the room, n=2n=2. This person has a 1365\frac{1}{365} (<3<3%) chance that they share the same birthday. Therefore, we have:

P(2)=13650.003P(2) = \frac{1}{365} \sim 0.003
P(2)ˉ=3643650.997\bar{P(2)} = \frac{364}{365} \sim 0.997

OK, so still quite simple. Let us now add a third person. In order to have a match they need to have the same birthday as either person 1 or person 2, but this assumes that there was no match for P(1)P(1) or P(2)P(2), and this is where it becomes a bit more tricky, we have to count all these probabilities up and account for different paths, which of course is possible, but it is simpler to deal with no matches, as I will show.

P(3)=P(2)+(P(2)ˉ×2365)=1365+(364365×2365)0.008P(3) = P(2) + (\bar{P(2)} \times \frac{2}{365}) = \frac{1}{365} + (\frac{364}{365} \times \frac{2}{365}) \sim 0.008

and for the “no match” probability we have

P(3)ˉ=364365×3633650.992\bar{P(3)} = \frac{364}{365} \times \frac{363}{365} \sim 0.992

For the match probability, P(3)P(3), we had to account for the fact that there could have been a match with P(2)P(2), and in probability we represent this as an addition (++), and the second term then arises from the fact that it could have matched with person 1 (1365\frac{1}{365} chance) or with person 2 (again, 1365\frac{1}{365} chance) and we sum these to get 2365\frac{2}{365} which is then weighted by the probability of not matching with two people P(2)ˉ\bar{P(2)}. Still following?

However, using the not match probability, P(3)ˉ\bar{P(3)}, it was much simpler - it was just the product of (11365)(1 - \frac{1}{365}) and (12365)(1 - \frac{2}{365}). You start to see the pattern now.

P(n)ˉ=(364365)×(363365)×(362365)×....×(n1365)\bar{P(n)} = (\frac{364}{365}) \times (\frac{363}{365}) \times (\frac{362}{365}) \times .... \times (\frac{n-1}{365})

times by one to make this more obvious

P(n)ˉ=(365365)×(364365)×(363365)×(362365)×....×(n1365)\bar{P(n)} = (\frac{365}{365}) \times(\frac{364}{365}) \times (\frac{363}{365}) \times (\frac{362}{365}) \times .... \times (\frac{n-1}{365})

We can write this as

P(n)ˉ=[365×364×363×...×(n1)365n]\bar{P(n)} = \left[\frac{365 \times 364 \times 363 \times ... \times (n-1)}{365^{n}}\right]
P(n)ˉ=365!365n(365n)!\bar{P(n)} = \frac{365!}{365^{n}(365-n)!}

Where we use k!k! to denote factorial of kk, i.e. 4!=4×3×2×14! = 4 \times 3 \times 2 \times 1.

Therefore

P(n)=1[365!365n(365n)!]P(n) = 1 - \left[\frac{365!}{365^{n}(365-n)!}\right]

In the table below I chose some examples to see how it changes as nn increases. You can see it drops off fast. Two important things to notice here is, n=23n=23 is the magic number I told you about earlier (highlighted in bold), and secondly, it is near impossible to not find a match 365 people. Even at n=120n=120 you’ve got more chance of winning the Euromillions Jackpot (as of August 2021) Plottery7.15×109>P(n)ˉ2.44×1010P_{lottery} \sim 7.15 \times 10^{-9} \gt \bar{P(n)} \sim 2.44 \times 10^{-10}!

nn P(n)P(n) P(n)ˉ\bar{P(n)}
11 0.00.0 1.01.0
22 2.73973×1032.73973 \times 10^{-3} 9.97260×1019.97260 \times 10^{-1}
33 8.20417×1038.20417 \times 10^{-3} 9.91796×1019.91796 \times 10^{-1}
44 1.63559×1021.63559 \times 10^{-2} 9.83644×1019.83644 \times 10^{-1}
55 2.71356×1022.71356 \times 10^{-2} 9.72864×1019.72864 \times 10^{-1}
1010 1.16948×1011.16948 \times 10^{-1} 8.83052×1018.83052 \times 10^{-1}
1515 2.52901×1012.52901 \times 10^{-1} 7.47099×1017.47099 \times 10^{-1}
2020 4.11438×1014.11438 \times 10^{-1} 5.88562×1015.88562 \times 10^{-1}
2222 4.75695×1014.75695 \times 10^{-1} 5.24305×1015.24305 \times 10^{-1}
23\bold{23} 5.07297×101\bold{5.07297 \times 10^{-1}} 4.92703×101\bold{4.92703 \times 10^{-1}}
5050 9.70374×1019.70374 \times 10^{-1} 2.96264×1022.96264 \times 10^{-2}
100100 13.07249×10071 - 3.07249 \times 10^{-07} 3.07249×10073.07249 \times 10^{-07}
150150 12.45122×10161 - 2.45122 \times 10^{-16} 2.45122×10162.45122 \times 10^{-16}
364364 15.31059×101551 - 5.31059 \times 10^{-155} 5.31059×101555.31059 \times 10^{-155}
365365 11.45496×101571 - 1.45496 \times 10^{-157} 1.45496×101571.45496 \times 10^{-157}
366\geq366 1.01.0 0.00.0

In the below plot I show this on a linear y scale with a marker at n=23n=23, P(23)50P(23) \sim 50%. But to see the real detail for n>100n \gt 100 we need to plot on a logarithmic scale for y, see second plot below.

linear

log

We must remember that the question is, any pair, it is not the same question as saying with your specific birthday (see the “Same birthday as you” problem), this is of course a much higher number of people needed (n=253n = 253), and is often the first place of confusion with this problem.

OK, so this assumes 365 days in a year (ignoring leap years) and assumes uniform distribution of birthdays, which we know is not the case. The latter effect is an interesting thing to look into, and varies depending on culture and country but mostly more babies are born in the summer and more between Tuesdays and Fridays due to planned operations. Many people have looked at this, and it’s really cool to look at but we ignore all that in this case for simplicity. Some researchers have actually looked into these effects and they only seem to have neglible effects on nn, and a uniform distribution actually provides the highest nn. This statement actually is quite intuative since to minimise the chance of birthday collision we want to spread our distribution as best as possible, any other distribution would naturally increase the chance of a collision.


This is the point where everyone normally ends it and be done with the simple maths, but I think that is not enough. I will now take a different approach to the problem, an experimentalists view, if you will. Instead of analytically calculating the probabilities, can we perform an experiment (or series of experiments) to show this is actually true?

Just to clarify, I will not be inviting large groups of random people into my house and count them, I will simply write a small program to illustrate this effect (we will use python for simplicity, but if we want to run large scale simulations, C++ would be better for this). This will be a very simple Monte Carlo simulation (something we like to do a lot of in Particle and Nuclear Physics).

At the heart of every Monte Carlo Simulation is a random number generator, so let’s start with that.

def generate_birthday():
    """Generate an integer in [1, 365] uniform dist"""
    return randint(1, 365)

We are going to (incorrectly) assume there are no leap years and birthdays are uniformly distributed, which we know is not correct, but works as a first attempt and matches with the mathematical approach earlier.

Now we can run a simulation, using this generator. This is equivalent to us taking one sample. You can think of each call to this function as us asking people to come into a room until we have a match and then count the number of people in the room.

def count_until_match():
    """Count until we have a match!"""
    # at least one person always
    birthdays = [generate_birthday()]

    # keep adding people until we find a match
    while birthdays[-1] not in birthdays[:-1]:
        birthdays.append(generate_birthday())

    # only need the count, not all the birthdays
    return len(birthdays)

Of course, one sample is not going to work - we need lots of samples! The bigger the sample the more representative of our “true” distribution (thanks to the law of large numbers). Something like this:

def mc(nsamples=1000):
    return [count_until_match() for _ in range(nsamples)]

And that’s basically it! Running this for 100 experiments we already start to see the pattern, but not quite enough statistics. We get n=21n=21, so not quite right.

100

1,000 experiments is a bit better and we get the magic n=23n=23! Ta-da, no maths, just some brute force computation - to prove those disbelivers the maths is true!

1000

However, the distribution is a bit weak. Let’s try more events!

10,000 and 100,000 experiments start to give better results and smoother cumulative probability (green line). I know, I should show statistical error bars on my histogram here.

10000 100000

We can go to a million (takes a few minutes on single core) but still we don’t get many if any events above n=80n=80. Remember from our table the probability around n=100n=100 of no match, P(100)ˉ3×107\bar{P(100)} \sim 3 \times 10^{-7}! And even running 3.3 million experiments would maybe only yield us 1 count or so around n=100n=100, so clearly we have a problem around the tail.

1000000

It turns out that this is a common problem in physics too, most of the time we are interested in the very rare events and want to examine them in isolation. And in those cases there is no analytical solution, so we have to use MC methods. In those cases we have techiques such as reweighting and other variance reduction methods. Hopefully if I have time I will do a part II to show how we can apply such a technique to this problem. It would also be interesting to examine different distributions, include leap years, and look at smarter MC techniques in part II. For now I leave it here.

Final thought - while in this case MC was not really necessary, since the maths was easy, it is a very useful tool when you have no analytical way of solving a problem or equation and since compute power is cheap, just throw CPUs at the problem (or GPUs too!).

All the code to reproduce these plots can be found on this gist

The end.


Thomas Stainer

Written by Thomas Stainer who likes to develop software for applications mainly in maths and physics, but also to solve everyday problems. Check out my GitHub page here.