Reputation: 160
I'm coding the birthday problem. My code:
> k = 100
> for (n in 1:100) {
+ prob = 1 - (0:(n-1))/365
+ k[n] = 1 - prod(prob) }
> plot(k)
I need to find the n for which its ~50% likely that two people share the same birthday. Any help? I tried googling but I can only find information about popular distributions.
Upvotes: 1
Views: 203
Reputation: 551
Try the following from the stats
package...
approx(k,1:100, xout = .5)
The above uses linear interpolation between the closest two provided points, which I may get jumped on because it is inaccurate...
But if we're doing the birthday problem I'm guessing we're learning about coding and not launching a rocket, so maybe its close enough?
Upvotes: 1
Reputation: 263342
The "Birthday problem" already has two R functions designed to solve various forms of it:
birthday {stats} R Documentation
Probability of coincidences
Description
Computes answers to a generalised birthday paradox problem. pbirthday computes the
probability of a coincidence and qbirthday computes the smallest number of observations
needed to have at least a specified probability of coincidence.
Usage
qbirthday(prob = 0.5, classes = 365, coincident = 2)
pbirthday(n, classes = 365, coincident = 2)
So the defaults for `qbirthday::
> qbirthday()
[1] 23
Upvotes: 4
Reputation: 898
You've already solved the problem. You can find it "by eye", just type "k" and start counting. Or you can have R find it for you. The first day where the probability is greater than or equal to .5 is day number:
#TRUE is the maximum, pick the first of the ties:
which.max(k >= .5)
We can find the day with the minimum absolute difference from probability .5:
which.min(abs(k - .5))
Upvotes: 1