{"id":5367,"date":"2016-01-27T15:42:16","date_gmt":"2016-01-27T20:42:16","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=5367"},"modified":"2016-10-05T12:42:46","modified_gmt":"2016-10-05T17:42:46","slug":"birthday-paradox-in-python","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2016\/01\/27\/birthday-paradox-in-python\/","title":{"rendered":"Birthday paradox in python"},"content":{"rendered":"<p>I love things like this.  Earlier, we produced a working example of the <a href=http:\/\/appcrawler.com\/wordpress\/2014\/02\/25\/monty-hall-simulation-in-python\/ target=\"blank\">Monte Hall<\/a> problem.  In this post, we show something similar for the <a href=https:\/\/en.wikipedia.org\/wiki\/Birthday_problem target=\"blank\">birthday paradox<\/a>&#8230;<\/p>\n<pre>\r\nc:\\Python27>type c:\\Users\\showard\\bday.py\r\nfrom random import randint\r\n\r\ncnt = 0\r\n\r\nfor k in range(2000):\r\n  persons = dict()\r\n  for i in range(23):\r\n    persons[i] = randint(1,365)\r\n\r\n  b = False\r\n  for i in persons:\r\n    for j in persons:\r\n      if persons[j] == persons[i] and i != j:\r\n        cnt += 1\r\n        b = True\r\n        break\r\n    if b == True:\r\n      break\r\n\r\nprint cnt * 100 \/ 2000\r\nc:\\Python27>python.exe c:\\Users\\showard\\bday.py\r\n49\r\n\r\nc:\\Python27>\r\n<\/pre>\n<p>As you can see, we find a match 49% of the time in which at least one pair of persons shares the same birthday in the year, even though we only used 23 samples in each run.  This will vary right around 50% for 23 samples.<\/p>\n<p>Another way of doing this is by taking the product of each probability, as shown in the wikipedia link above.<\/p>\n<pre>\r\n>>> total = 1\r\n>>> for i in range(365,342,-1):\r\n...   total = total * (float(i) \/ 365)\r\n...   print round(total,2),i\r\n...\r\n1.0 365\r\n1.0 364\r\n0.99 363\r\n0.98 362\r\n0.97 361\r\n0.96 360\r\n0.94 359\r\n0.93 358\r\n0.91 357\r\n0.88 356\r\n0.86 355\r\n0.83 354\r\n0.81 353\r\n0.78 352\r\n0.75 351\r\n0.72 350\r\n0.68 349\r\n0.65 348\r\n0.62 347\r\n0.59 346\r\n0.56 345\r\n0.52 344\r\n0.49 343\r\n>>>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I love things like this. Earlier, we produced a working example of the Monte Hall problem. In this post, we show something similar for the birthday paradox&#8230; c:\\Python27>type c:\\Users\\showard\\bday.py from random import randint cnt = 0 for k in range(2000):&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2016\/01\/27\/birthday-paradox-in-python\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[26,4],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/5367"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=5367"}],"version-history":[{"count":11,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/5367\/revisions"}],"predecessor-version":[{"id":5834,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/5367\/revisions\/5834"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=5367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=5367"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=5367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}