11 March 2010 View Comments

Birthday Probability Problem


In a group of at least 23 randomly chosen people, there is more than 50% probability that some pair of them will have the same birthday. Such a result is counter-intuitive to many. In probability theory, thi is known as the birthday problem, or birthday paradox. Here is how we calculate this.

Solving the Birthday Paradox with Matlab

Using Matlab to solve this problem might be a little easier than you would expect (it was certainly easier than I expected). My approach was to first write a functon that would simulate the percentage for a given number of people. I saved this function as an m-file. You can see the code below.

[cc lang="matlab"]
function result = birthdayMatch(numPeople)

total = 0;

%do the simulation for 20,000 iterations
for x = 1:20000

%create a random vector of birthdays for the specified number of people
birthdayVector = ceil(rand(1,numPeople)*365);

%the mode command returns the number (a) that occurs the most
%if there are two numbers that occur with the same frequency,
%the lowest number is returned
%the second return argument (b) is the number of times (a) occurs
[a,b] = mode(birthdayVector);

%if there is a birthday match
if b > 1
total = total + 1;
end

end

result = total/x; %get the average percent of a matching birthday
[/cc]

So now we have a function that will perform 20,000 iterations of the event, we can use this function to solve the above problem

At the matlab command prompt, we can enter the following:

[cc lang="matlab"]
%the following commands might take a couple of seconds to execute
%so be patient!

%get the percent chance of a birthday match with 5 people
fivePeople = birthdayMatch(5)

%get the percent chance fo a birthday match with 23 people
twentythreePeople = birthdayMatch(23)

%get the percent chance fo a birthday match with 57 people
fiftysevenPeople= birthdayMatch(57)
[/cc]

The Answer!

The theoretical answer for this problem is 23 people for 50% chance and 57 people for 99% chance.

The simulations basically confirm that the theoretical answer is indeed correct, but the answers might be off by a little bit. If you want more precise answers, you can increase the number of iterations used. Of course, this will cause the computation time to increase as well.

Reference: YouTube, BlinkDagger.com

Tags: ,
  • well, it is a great site.many are look here for help there many asking.
blog comments powered by Disqus