Birthday Paradox

Lecture 24, IMT DeCal, SP 19

Suraj Rampure

In [1]:
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
def fact(n):
    if n <= 1:
        return 1
    else:
        return n * fact(n-1)
In [3]:
def choose(n, k):
    return fact(n) / (fact(n-k) * fact(k))
In [4]:
def prob_shared(n):
    return 1 - choose(365, n) * fact(n) / (365**n)
In [5]:
upper_limit = 100

x = range(1, upper_limit + 1)
y = [prob_shared(xi) for xi in x]
plt.plot(x, y)
plt.plot([1, upper_limit], [0.5, 0.5])
plt.xlabel('n')
plt.ylabel('p(n)')
plt.show();
In [6]:
for i in range(1, 30):
    print(i, prob_shared(i))
1 0.0
2 0.002739726027397249
3 0.008204165884781345
4 0.016355912466550326
5 0.02713557369979358
6 0.040462483649111536
7 0.056235703095975365
8 0.07433529235166902
9 0.09462383388916673
10 0.11694817771107768
11 0.141141378321733
12 0.16702478883806438
13 0.19441027523242926
14 0.22310251200497289
15 0.25290131976368635
16 0.2836040052528499
17 0.31500766529656066
18 0.34691141787178925
19 0.37911852603153673
20 0.41143838358058005
21 0.4436883351652058
22 0.4756953076625501
23 0.5072972343239854
24 0.5383442579145288
25 0.5686997039694639
26 0.598240820135939
27 0.626859282263242
28 0.6544614723423994
29 0.680968537477777