Skip to content

Instantly share code, notes, and snippets.

@cdesante
Created August 17, 2012 18:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdesante/3381297 to your computer and use it in GitHub Desktop.
Save cdesante/3381297 to your computer and use it in GitHub Desktop.
The Birthday Problem
#The birthday Problem.
#Simple computation:
N <- 1:50
pshare <- 1- exp(-(N*(N-1)/2)/365)
cbind(N, pshare)
#just want one value:
bday <- function(X){
N <- X
pshare <- 1- exp(-(N*(N-1)/2)/365)
print(pshare)
}
bday(44)
#A nice plot of the results...
library(ggplot2)
qplot(N, pshare, geom="point", colour=pshare, xlab="\n Number of People",
ylab="Probability that two people share a birthday \n", main="The Birthday Problem \n") + geom_hline(y=.5, lty=2) +
geom_vline(x=23, lty=2) + scale_colour_gradient(limits=c(0, 1), low="lightblue", high="red") +
scale_x_continuous(limits=c(0, 50),breaks=c(0, 10, 20, 23, 30, 40, 50)
) + theme_bw() + opts(legend.position="none")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment