{"id":7537,"date":"2021-11-03T14:53:12","date_gmt":"2021-11-03T14:53:12","guid":{"rendered":"https:\/\/www.blopig.com\/blog\/?p=7537"},"modified":"2021-11-09T12:22:11","modified_gmt":"2021-11-09T12:22:11","slug":"monty-python","status":"publish","type":"post","link":"https:\/\/www.blopig.com\/blog\/2021\/11\/monty-python\/","title":{"rendered":"Monty Python"},"content":{"rendered":"\n<p>Every now and then I decide to overthink a problem I thought I understood and get confused \u2013 last week, it was the Monty Hall problem.&nbsp;<\/p>\n\n\n\n<p>For those unfamiliar with the thought experiment, the basic premise is that you are on a game show and are presented with three doors. Behind one of the doors is a car, while behind the other two are goats.&nbsp;<\/p>\n\n\n\n<p>With zero initial information, you make a guess as to which door you think the car is behind (we assume you have enough goats already). Before looking behind your chosen door, the host opens one of the remaining two doors and reveals a goat. The host then asks you if you would like to change your guess. What should you do?&nbsp;<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Most people instinctively think that whether you &#8216;stick&#8217; or &#8216;switch&#8217; should make no difference to your chances of winning &#8211; it should be 50-50 now that only two doors remain. However, probability says otherwise.&nbsp;<\/p>\n\n\n\n<p>I won&#8217;t cover the math here (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Monty_Hall_problem\" data-type=\"URL\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Monty_Hall_problem\" target=\"_blank\" rel=\"noreferrer noopener\">plenty of online resources do this already<\/a>), but I have written some simple code that proves the same result \u2013 choose the other door.&nbsp;The code can be executed and modified in Colab <a rel=\"noreferrer noopener\" href=\"https:\/\/colab.research.google.com\/gist\/lewis-chinery\/5b96065280fa36f5f0415e8b080c8b0c\/montyhall.ipynb\" target=\"_blank\">here<\/a>.<\/p>\n\n\n\n<div id=\"notebook\" class=\"border-box-sizing\">\n    <div class=\"container\" id=\"notebook-container\">\n\n<div class=\" highlight hl-python\"><pre><span><\/span><span class=\"kn\">from<\/span> <span class=\"nn\">random<\/span> <span class=\"kn\">import<\/span> <span class=\"n\">randrange<\/span>\n\n<span class=\"n\">number_of_guesses<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">10000<\/span>\n<span class=\"n\">doors<\/span> <span class=\"o\">=<\/span> <span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">1<\/span><span class=\"p\">,<\/span> <span class=\"mi\">2<\/span><span class=\"p\">]<\/span>\n<span class=\"n\">num_correct_guesses_stick<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span>\n<span class=\"n\">num_correct_guesses_switch<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span>\n\n<span class=\"k\">for<\/span> <span class=\"n\">guess_number<\/span> <span class=\"ow\">in<\/span> <span class=\"nb\">range<\/span><span class=\"p\">(<\/span><span class=\"n\">number_of_guesses<\/span><span class=\"p\">):<\/span>\n\n  <span class=\"c1\"># door that hides the car<\/span>\n  <span class=\"n\">correct_door<\/span> <span class=\"o\">=<\/span> <span class=\"n\">randrange<\/span><span class=\"p\">(<\/span><span class=\"mi\">3<\/span><span class=\"p\">)<\/span>\n\n  <span class=\"c1\"># door that the contestant guesses when there are 3 choices available<\/span>\n  <span class=\"n\">contestant_initial_guess<\/span> <span class=\"o\">=<\/span> <span class=\"n\">randrange<\/span><span class=\"p\">(<\/span><span class=\"mi\">3<\/span><span class=\"p\">)<\/span>\n\n  <span class=\"c1\"># doors that the contestant has not picked e.g. 0 and 2 if contestant picked 1<\/span>\n  <span class=\"n\">remaining_doors<\/span> <span class=\"o\">=<\/span> <span class=\"p\">[<\/span><span class=\"n\">door<\/span> <span class=\"k\">for<\/span> <span class=\"n\">door<\/span> <span class=\"ow\">in<\/span> <span class=\"n\">doors<\/span> <span class=\"k\">if<\/span> <span class=\"n\">door<\/span><span class=\"o\">!=<\/span> <span class=\"n\">contestant_initial_guess<\/span><span class=\"p\">]<\/span>\n\n  <span class=\"c1\"># door opened by the host, revealing a goat<\/span>\n  <span class=\"n\">remaining_goat_doors<\/span> <span class=\"o\">=<\/span> <span class=\"p\">[<\/span><span class=\"n\">door<\/span> <span class=\"k\">for<\/span> <span class=\"n\">door<\/span> <span class=\"ow\">in<\/span> <span class=\"n\">remaining_doors<\/span> <span class=\"k\">if<\/span> <span class=\"n\">door<\/span><span class=\"o\">!=<\/span> <span class=\"n\">correct_door<\/span><span class=\"p\">]<\/span>\n  <span class=\"n\">door_opened<\/span> <span class=\"o\">=<\/span> <span class=\"n\">remaining_goat_doors<\/span><span class=\"p\">[<\/span><span class=\"n\">randrange<\/span><span class=\"p\">(<\/span><span class=\"nb\">len<\/span><span class=\"p\">(<\/span><span class=\"n\">remaining_goat_doors<\/span><span class=\"p\">))]<\/span>\n\n  <span class=\"c1\"># door picked by contestant if they choose to switch<\/span>\n  <span class=\"n\">contestant_guess_switch<\/span> <span class=\"o\">=<\/span> <span class=\"p\">[<\/span><span class=\"n\">door<\/span> <span class=\"k\">for<\/span> <span class=\"n\">door<\/span> <span class=\"ow\">in<\/span> <span class=\"n\">doors<\/span> <span class=\"k\">if<\/span> <span class=\"n\">door<\/span> <span class=\"ow\">not<\/span> <span class=\"ow\">in<\/span> <span class=\"p\">[<\/span><span class=\"n\">contestant_initial_guess<\/span><span class=\"p\">,<\/span> <span class=\"n\">door_opened<\/span><span class=\"p\">]][<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span>\n\n  <span class=\"c1\"># record whether or not the initial or changed guess is correct<\/span>\n  <span class=\"k\">if<\/span> <span class=\"n\">contestant_initial_guess<\/span> <span class=\"o\">==<\/span> <span class=\"n\">correct_door<\/span><span class=\"p\">:<\/span>\n    <span class=\"n\">num_correct_guesses_stick<\/span> <span class=\"o\">+=<\/span> <span class=\"mi\">1<\/span>\n  <span class=\"k\">if<\/span> <span class=\"n\">contestant_guess_switch<\/span> <span class=\"o\">==<\/span> <span class=\"n\">correct_door<\/span><span class=\"p\">:<\/span>\n    <span class=\"n\">num_correct_guesses_switch<\/span> <span class=\"o\">+=<\/span> <span class=\"mi\">1<\/span>\n\n<span class=\"k\">print<\/span><span class=\"p\">(<\/span><span class=\"s2\">\"Success rate when stick with initial guess:  {:.2f}%\"<\/span><span class=\"o\">.<\/span><span class=\"n\">format<\/span><span class=\"p\">(<\/span><span class=\"mi\">100<\/span> <span class=\"o\">*<\/span> <span class=\"n\">num_correct_guesses_stick<\/span> <span class=\"o\">\/<\/span> <span class=\"n\">number_of_guesses<\/span><span class=\"p\">))<\/span>\n<span class=\"k\">print<\/span><span class=\"p\">(<\/span><span class=\"s2\">\"Success rate when switch guess after reveal: {:.2f}%\"<\/span><span class=\"o\">.<\/span><span class=\"n\">format<\/span><span class=\"p\">(<\/span><span class=\"mi\">100<\/span> <span class=\"o\">*<\/span> <span class=\"n\">num_correct_guesses_switch<\/span> <span class=\"o\">\/<\/span> <span class=\"n\">number_of_guesses<\/span><span class=\"p\">))<\/span>\n<\/pre><\/div>\n\n    <\/div>\n<\/div>\n\n\n<div class=\"output_wrapper\">\n<div class=\"output\">\n\n\n<div class=\"output_area\">\n\n    <div class=\"prompt\"><\/div>\n\n\n<div class=\"output_subarea output_stream output_stdout output_text\">\n<pre>Success rate when stick with initial guess:  32.63%\nSuccess rate when switch guess after reveal: 67.37%\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<p> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Every now and then I decide to overthink a problem I thought I understood and get confused \u2013 last week, it was the Monty Hall problem.&nbsp; For those unfamiliar with the thought experiment, the basic premise is that you are on a game show and are presented with three doors. Behind one of the doors [&hellip;]<\/p>\n","protected":false},"author":91,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","wikipediapreview_detectlinks":true,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"ngg_post_thumbnail":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[29,227,278],"tags":[],"ppma_author":[558],"class_list":["post-7537","post","type-post","status-publish","format-standard","hentry","category-code","category-python-code","category-statistics"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"authors":[{"term_id":558,"user_id":91,"is_guest":0,"slug":"lewis","display_name":"Lewis Chinery","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/29bddf38b6dd9db3c7161683ddd1a5fc6bad04b6f40e83334e5d43972380ed54?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/7537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/users\/91"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/comments?post=7537"}],"version-history":[{"count":5,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/7537\/revisions"}],"predecessor-version":[{"id":7566,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/7537\/revisions\/7566"}],"wp:attachment":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/media?parent=7537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/categories?post=7537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/tags?post=7537"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=7537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}