https://gist.github.com/sallos-cyber/92bc3666b6228925c5798dc296171ab3#file-birthdayexample-ipynb
How large is the probability that there are at least two people in a group of k people whose birthday is on the same day? Thus, the event we are interested in: At least one birthday match. So, P(A) = 1- P(A^c). P(A^c) = (365!/((365-k)!) / *365^n) For several values of k (number of people) we compute P(A) = 1 – P(A^c)
In [29]:
import numpy as np
import math
import matplotlib.pyplot as plt
In [30]:
def computeBirthdayProb(k):
if k == 0 or k==1:
return 0
variation=np.arange(365,365-k,-1)
#this is neither efficient nor elegant
variation = variation / 365
prob_no_match=1
for i in variation:
prob_no_match = prob_no_match*i
return 1-prob_no_match
In [31]:
number_of_people = 50
number_of_people_list = list(range(1,number_of_people+1))
dist = []
for k in number_of_people_list:
dist.append(computeBirthdayProb(k) )
In [32]:
# make a nice figure
fig,ax1 = plt.subplots(figsize=(6,6))
ax1.set_ylabel("probability of match", color="blue")
ax1.set_xlabel("number of people")
ax1.plot(number_of_people_list, dist,color='blue')
ax1.set_title('probability of birthday match according to number of people')
ax1.set(ylim=(0, 1))
plt.interactive(True)
plt.show()
In [ ]:
In [ ]:
Was this helpful?
2 / 0