I was doing a Monte Carlo implementation of the Birthday Paradox in Python and I wanted to check if the results where the same as in the analytical implementation of the same problem (As they should be).
The question I'm answering is:
If 20 people are chosen at random, what is the probability that some of them share the same birthday?
To my surprise I get a systematically slightly higher value of the estimated probability using the Monte Carlo Method than the one I'm getting with the analytical solution.
I'm assuming a 365 days year and uniform probabilities for the birthdays.
the code for the Monte Carlo implementation is the following:
def MonteCarloBDay(num_people,num_simu): Bool = np.zeros(num_simu) for i in range(num_simu): test = np.random.choice(range(365),size=num_people, replace=True) Bool[i] = (len(set(test))!=num_people) # Check if we have num_people different birthdays --> If we do, then it means that no couple of people have the birthday in the same day return np.mean(Bool)
The code for the analytical implementation is the following:
def PropShareSameBirthday(n): NumPairs = n*(n-1)/2 ProbDiffBirthday_2People = 1 - 1/365 # We consider 366 days and uniform probabilities ProbNoCoupleHaseSameBirthday = ProbDiffBirthday_2People**NumPairs return 1-ProbNoCoupleHaseSameBirthday
The results I get from running the Monte Carlo 20 times with
num_people = 20 and num_simu = 1e5 are:
Results = [0.41207, 0.40994, 0.41335, 0.41142, 0.40799, 0.4107 , 0.41296, 0.41209, 0.41211, 0.41397, 0.41257, 0.4118 , 0.40946, 0.41218, 0.41281, 0.41194, 0.41123, 0.41268, 0.41195, 0.40982]
with a mean of 0.4116
The result I get from the analytical version is
AnaliticalProb = 0.4062.
When I run a t-test to check if the mean of the Results I got from Monte Carlo could be 0.4062 I get the following result:
Rejecting the Null Hypothesis with really high confidence.
I must have an error in one of the two implementations, but I can't seem to find where. Please let me know if you see some problem with my code or my reasoning.