\HeaderA{birthday}{Probability of coincidences}{birthday}
\aliasA{pbirthday}{birthday}{pbirthday}
\aliasA{qbirthday}{birthday}{qbirthday}
\keyword{distribution}{birthday}
\begin{Description}\relax
Computes approximate answers to a generalised \emph{birthday paradox}
problem. \code{pbirthday} computes the probability of a coincidence
and \code{qbirthday} computes the number of observations needed to
have a specified probability of coincidence.
\end{Description}
\begin{Usage}
\begin{verbatim}
qbirthday(prob = 0.5, classes = 365, coincident = 2)
pbirthday(n, classes = 365, coincident = 2)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{classes}] How many distinct categories the people could fall into
\item[\code{prob}] The desired probability of coincidence
\item[\code{n}] The number of people
\item[\code{coincident}] The number of people to fall in the same category
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
The birthday paradox is that a very small number of people, 23,
suffices to have  a 50-50 chance that two of them have the same
birthday.  This function generalises the calculation to probabilities
other than 0.5, numbers of coincident events other than 2, and numbers
of classes other than 365.

This formula is approximate, as the example below shows.  For
\code{coincident=2} the exact computation is straightforward and may be
preferable.
\end{Details}
\begin{Value}
\begin{ldescription}
\item[\code{qbirthday}] Number of people needed for a probability \code{prob} that \code{k} of
them have the same one out of \code{classes} equiprobable labels.

\item[\code{pbirthday}] Probability of the specified coincidence
\end{ldescription}
\end{Value}
\begin{References}\relax
Diaconis, P. and Mosteller F. (1989)
Methods for studying coincidences.
J. American Statistical Association, \bold{84}, 853-861.
\end{References}
\begin{Examples}
\begin{ExampleCode}
require(graphics)

 ## the standard version
qbirthday()
 ## same 4-digit PIN number
qbirthday(classes=10^4)
 ## 0.9 probability of three coincident birthdays
qbirthday(coincident=3, prob=0.9)
## Chance of 4 coincident birthdays in 150 people
pbirthday(150,coincident=4)
## 100 coincident birthdays in 1000 people: *very* rare:
pbirthday(1000, coincident=100)

## Accuracy compared to exact calculation
x1<-  sapply(10:100, pbirthday)
x2<- 1-sapply(10:100, function(n)prod((365:(365-n+1))/rep(365,n)))
par(mfrow=c(2,2))
plot(x1, x2, xlab="approximate", ylab="exact")
abline(0,1)
plot(x1, x1-x2, xlab="approximate", ylab="error")
abline(h=0)
plot(x1, x2, log="xy", xlab="approximate", ylab="exact")
abline(0,1)
plot(1-x1, 1-x2, log="xy", xlab="approximate", ylab="exact")
abline(0,1)
\end{ExampleCode}
\end{Examples}

