Created
October 23, 2013 22:48
-
-
Save leepro/7128223 to your computer and use it in GitHub Desktop.
Birthday Paradox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%pylab inline --no-import-all | |
import matplotlib.pyplot as plt | |
def prob_samebirthday(n): | |
prob_days = [ (1.0-(i/365.0)) for i in xrange(1,n+1) ] | |
return 100.0*(1.0-reduce(lambda x,y: x*y, prob_days)) | |
def draw(): | |
plt.figure() | |
plt.grid(True) | |
plt.xlabel("people") | |
plt.ylabel("probability") | |
plt.title("Birthday Paradox") | |
y = [] | |
x = xrange(1,101) | |
y = [ prob_not_sameday(i) for i in x ] | |
plt.plot(x, y, 'r', linewidth=3) | |
plt.plot([21, 21], [0, y[21]], color='blue', linestyle='--', linewidth=2) | |
plt.plot([0, 21], [y[21], y[21]], color='blue', linestyle='--', linewidth=2) | |
show() | |
draw() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment