Solving the Birthday Paradox in kdb q

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:

  1. Consider making a function to examine the case where there are N people.
  2. Generate lists of N random numbers between 0-365 representing their birthdays.
  3. Find lists that contain collisions.
  4. Find the number of collisions per possibilities examined.
  5. 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:

kdb chart showing the birthday paradox

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.

  1. No Comments