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)
import numpy as np import math import matplotlib.pyplot as plt
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
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) )
# 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()
Was this helpful?
0 / 0