Solving the Birthday Paradox in kdb q
December 10th, 2013 by Ryan Hamilton
In a previous post I looked at using the monte carlo method in kdb to find the outcome of rolling two dice. I also posed the question:
How many people do you need before the odds are good (greater than 50%) that at least two of them share a birthday?
In our kdb+ training courses I always advise breaking the problem down step by step, in this case:
- Consider making a function to examine the case where there are N people.
- Generate lists of N random numbers between 0-365 representing their birthdays.
- Find lists that contain collisions.
- Find the number of collisions per possibilities examined.
- Apply our function for finding the probability for N people to a list for many possibilities of N.
In kdb/q:
Plotting our data in using qStudio charting for kdb we get:
Therefore as you can see from either the q code or the graph, you need 23 people to ensure there’s a 50% chance that atleast 2 people in the room share a birthday. For more details see the wikipedia page. This still leaves us with the other problem of finding Pi using the monte carlo method in kdb.