{"id":1808,"date":"2021-05-15T15:32:29","date_gmt":"2021-05-15T15:32:29","guid":{"rendered":"http:\/\/192.168.31.181\/muthu\/?p=1808"},"modified":"2021-05-15T15:32:29","modified_gmt":"2021-05-15T15:32:29","slug":"birthday-problem-and-monte-carlo-simulation","status":"publish","type":"post","link":"http:\/\/write.muthu.co\/birthday-problem-and-monte-carlo-simulation\/","title":{"rendered":"Birthday Problem and Monte Carlo Simulation"},"content":{"rendered":"\n

If you get on a plane that can carry 100 or more passengers at a time, there is a 99% chance that one of the passengers shares the same birthday as yours. If you get into a bus with a capacity of 50 passengers, you have a 97% chance of finding someone who shares your birth date! Surprising isn’t it? This is exactly what the birthday paradox or the birthday problem is.<\/p>\n\n\n\n

In this post, I will try to solve the birthday problem first the analytical way and then using the Monte Carlo simulation. <\/p>\n\n\n\n

The Analytical Solution<\/h2>\n\n\n\n

The problem is to compute the probability of finding at least two people with the same birthday in a group of N <\/em> people. Suppose we have a group of 10 people, the probability of two or more people sharing a birth date is equivalent to finding the probability of everyone having a different birthday and subtracting it from 1, using the idea, P<\/em>(A<\/em>) = 1 \u2212\u00a0P<\/em>(A<\/em>\u2032), where A<\/em>\u2032 represents “everyone having different birthdays”<\/p>\n\n\n\n

We will ignore the leap year for simplicity’s sake. If a person is born on a particular day, there are 365 ways of picking a date. Once the first date is picked, the 2nd person will be left with 364 days, the next one 363, and so on. So the number of ways everyone can have a different birthday is equivalent to 365x364x363x362.. 365-(N + 1), where N represents the number of people. In terms of probabilities, the person has 365 options to choose from so his\/her probability will be 365\/365, if the next person has a different birthday, his\/her probability will be 364\/365 and the list goes on. This leads us to the total probability as below.<\/p>\n\n\n\n

\\(P(A{`}) = \\frac{365}{365}\\times \\frac{364}{365}\\times \\frac{363}{365}\\cdots \\frac{365-(n+1))}{365} \\)<\/p>\n\n\n\n

so the probabability of two or more people sharing the same birthday is given by <\/p>\n\n\n\n

\\(P(A) = 1- \\frac{365}{365}\\times \\frac{364}{365}\\times \\frac{363}{365}\\cdots \\frac{365-(n+1))}{365} \\)<\/p>\n\n\n\n

which is equivalent to,<\/p>\n\n\n\n

\\(P(A) = 1- \\frac{365\\times364\\times363\\times..(365-(n+1))}{365^{n}} \\)<\/p>\n\n\n\n

Now that we have the formula, let’s check the probability with different values of N using a simple python code:<\/p>\n\n\n\n

<\/p>\n\n\n\n

import numpy as np\nimport pandas as pd\n\nresults = []\nfor number_of_people in np.arange(5,130,5):\n  probability = 1\n  for count in range(1, number_of_people+1):\n    probability *= (365-count+1)\/365\n  results.append({\n      'n': number_of_people,\n      'p(n)': (1-probability)\n  })\nprint(pd.DataFrame(results))<\/code><\/pre>\n\n\n\n

The results are printed as below<\/p>\n\n\n\n

N<\/th>P(N)<\/th><\/tr><\/thead>
5<\/td>0.027136<\/td><\/tr>
10<\/td>0.116948<\/td><\/tr>
15<\/td>0.252901<\/td><\/tr>
20<\/td>0.411438<\/td><\/tr>
25<\/td>0.568700<\/td><\/tr>
30<\/td>0.706316<\/td><\/tr>
35<\/td>0.814383<\/td><\/tr>
40<\/td>0.891232<\/td><\/tr>
45<\/td>0.940976<\/td><\/tr>
50<\/td>0.970374<\/td><\/tr>
55<\/td>0.986262<\/td><\/tr>
60<\/td>0.994123<\/td><\/tr>
65<\/td>0.997683<\/td><\/tr>
70<\/td>0.999160<\/td><\/tr>
75<\/td>0.999720<\/td><\/tr>
80<\/td>0.999914<\/td><\/tr>
85<\/td>0.999976<\/td><\/tr>
90<\/td>0.999994<\/td><\/tr>
95<\/td>0.999999<\/td><\/tr>
100<\/td>1.000000<\/td><\/tr>
105<\/td>1.000000<\/td><\/tr>
110<\/td>1.000000<\/td><\/tr>
115<\/td>1.000000<\/td><\/tr>
120<\/td>1.000000<\/td><\/tr>
125<\/td>1.000000<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n\n\n

As you can see in the above table, surprisingly, the probability of you sharing a birthday with someone else in a group of 100 and more people is nearly a 100%. The growth of this probability distribution can be seen in the below graph.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

Monte Carlo Simulation<\/strong><\/h2>\n\n\n\n

Monte Carlo simulation is a way to randomly simulate a large number of events and arrive at the probability of an outcome using the results of the experiments. The credit for the invention of Monte Carlo simulation is given to Stanislaw Ulam, a Polish-born mathematician who worked for John von Neumann. One day while trying to find the probability of winning a game of solitaire with pure combinatorial calculations, he wondered if he could just play the game of solitaire a large number of times and arrive at an opinion based on his observations. But it’s nearly impossible to play the game a million times, that is when he called up John, his colleague and they decided to run this simulation on a computer. This way of using a simulation is called the Monte Carlo simulation named after a Casino in Monaco where Ulam’s uncle would borrow money from relatives to gamble.<\/p>\n\n\n\n

Let’s look at a simple simulation before jumping into the birthday problem. Given 3 fair coins, the probability of getting 3 heads (HHH) in a row is 1\/8, this is something we already know. Can we arrive at the same value using Monte Carlo Simulation? Let’s give it a try. The code below simulates the tossing of three coins and stores the probabilities in an array. We plot the probabilities towards the end of our experiment.<\/p>\n\n\n\n

import matplotlib.pyplot as plt\nimport numpy as np\nimport random\n\n# Assuming 0 to be Heads, 1 to be Tails\ndef toss_three_coins():\n  coin1 = random.choice([0,1])\n  coin2 = random.choice([0,1])\n  coin3 = random.choice([0,1])\n\n  if coin1 == coin2 and  coin2 == coin3 and coin1 == 0:\n    # this is equivalent to HHH\n    return 'HHH'\n  else:\n    return '' # empty as I am only interested in HHH\n\nnumber_of_times_HHH = 0\ntotal_number_of_experiments = 0\nprobabilities = []\nfor experiment_index in range(0,100): # increase this value to run more experiments\n  result = toss_three_coins()\n  if result == 'HHH':\n    number_of_times_HHH+=1 # increment HHH occurence.\n\n  total_number_of_experiments += 1 # increment count\n\n  probability = number_of_times_HHH\/total_number_of_experiments\n  probabilities.append(probability)\n\nplt.plot(probabilities)\nplt.show()<\/code><\/pre>\n\n\n\n

The outputs are as shown below:<\/p>\n\n\n\n

\n
\n
\"\"<\/figure>\n<\/div>\n\n\n\n
\n
\"\"<\/figure>\n<\/div>\n<\/div>\n\n\n\n
\n
\n
\"\"<\/figure>\n<\/div>\n\n\n\n
\n
\"\"<\/figure>\n<\/div>\n<\/div>\n\n\n\n

As can be observed in the above charts, as we keep running the simulation, the probabilities are converging to the theoretical value of 0.125 (1\/8). S, now that we know how Monte Carlo Simulation works, let’s apply it to our birthday problem.<\/p>\n\n\n\n

Birthday Problem Monte Carlo Simulation<\/h2>\n\n\n\n

It’s a lot simpler to simulate this problem over arriving at it analytically. <\/p>\n\n\n\n

  • First, choose the group size as N. <\/li>
  • Then randomly assign a date to each person in that group. Instead of assigning a date in terms of day and month, we can assign a number from 1 to 365. <\/li>
  • Check if there are any duplicate dates in the group.<\/li>
  • Divide the count of occurrences with the total number of experiments performed. <\/li><\/ul>\n\n\n\n

    From the table above that we derived analytically, we can see that at 30 people, the probability of having a shared birthday is nearly 70%. So, for our experiment, let’s run the simulation with N=30. <\/p>\n\n\n\n

    import matplotlib.pyplot as plt\nimport numpy as np\n\nnp.random.seed(10)\n\ndef contains_duplicates(X):\n    return len(np.unique(X)) != len(X)\n\n\nshared_birthday = 0\ntotal_number_of_experiments = 0\nprobabilities = []\nnumber_of_people = 30\nfor experiment_index in range(0,100):\n\n  # assign dates to people\n  dates = np.random.choice(range(1,366), size=(number_of_people))\n\n  # check if there are duplicates\n  if contains_duplicates(dates):\n    shared_birthday+=1 # increment shared birthday\n\n  total_number_of_experiments += 1 # increment count\n\n  probability = shared_birthday\/total_number_of_experiments\n  probabilities.append(probability)\n\nplt.plot(probabilities)\nplt.title(\"Output of 100 Experiments, converging to \" + str(probabilities[-1]))\nplt.show()<\/code><\/pre>\n\n\n\n

    The outcomes are shown below<\/p>\n\n\n\n

    \n
    \n
    \"\"<\/figure>\n<\/div>\n\n\n\n
    \n
    \"\"<\/figure>\n<\/div>\n<\/div>\n\n\n\n
    \n
    \n
    \"\"<\/figure>\n<\/div>\n\n\n\n
    \n
    \"\"<\/figure>\n<\/div>\n<\/div>\n\n\n\n

    As can be seen in the charts above, as more and more experiments are performed, the probabilities are slowly converging to our theoretical value of 0.70 for 30 people.<\/p>\n\n\n\n

    Key Ideas<\/h2>\n\n\n\n
    • The Birthday problem or Birthday paradox states that, in a set of n randomly chosen people, some will have the same birthday. In a group of 23 people, the probability of a shared birthday exceeds 50%, while a group of 70 has a 99.9% chance of a shared birthday.<\/li>
    • We can use conditional probability to arrive at the above-mentioned probabilities.<\/li>
    • Monte Carlo simulation is a way to randomly simulate a large number of events and arrive at the probability of an outcome using the results of the simulated experiments.<\/li>
    • Monte Carlo simulation must perform a large number of experiments, over a million usually to find where the probability is converging. The value of convergence is usually very close to the real probability.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"

      If you get on a plane that can carry 100 or more passengers at a time, there is a 99% chance that one of the passengers shares the same birthday as yours. If you get into a bus with a capacity of 50 passengers, you have a 97% chance of finding someone who shares your […]<\/p>\n","protected":false},"author":1,"featured_media":1822,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,37,31],"tags":[],"_links":{"self":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/1808"}],"collection":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/comments?post=1808"}],"version-history":[{"count":11,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/1808\/revisions"}],"predecessor-version":[{"id":1831,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/1808\/revisions\/1831"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/media\/1822"}],"wp:attachment":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/media?parent=1808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/categories?post=1808"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/tags?post=1808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}