{"id":116,"date":"2012-11-16T19:27:21","date_gmt":"2012-11-16T19:27:21","guid":{"rendered":"http:\/\/www.clayford.net\/statistics\/?p=116"},"modified":"2023-08-20T11:23:57","modified_gmt":"2023-08-20T15:23:57","slug":"simulating-the-monty-hall-problem","status":"publish","type":"post","link":"https:\/\/www.clayford.net\/statistics\/simulating-the-monty-hall-problem\/","title":{"rendered":"Simulating the Monty Hall Problem"},"content":{"rendered":"<p>Back in 1990, the following question was posed to\u00a0<em>PARADE magazine <\/em>columnist,\u00a0Marilyn Vos Savant (once holder of the\u00a0Guinness\u00a0World Record for highest IQ):<\/p>\n<blockquote><p>Suppose you&#8217;re on a game show, and you&#8217;re given the choice of three doors. Behind one door is a car, behind the others, goats. You pick a door, say #1, and the host, who knows what&#8217;s behind the doors, opens another door, say #3, which has a goat. He says to you, &#8220;Do you want to pick door #2?&#8221; Is it to your advantage to switch your choice of doors?<\/p><\/blockquote>\n<p>Marilyn&#8217;s response: &#8220;Yes; you should switch. The first door has a 1\/3 chance of winning, but the second door has a 2\/3 chance.&#8221; And thus began a firestorm of controversy. Over a thousand people with PhDs (many of them statisticians and mathematicians) wrote in to her column to tell her she was wrong. <a href=\"http:\/\/en.wikipedia.org\/wiki\/Monty_Hall_problem\">Wikipedia has a nice summary<\/a> of how it went down. And\u00a0Marylin&#8217;s own site <a href=\"http:\/\/marilynvossavant.com\/game-show-problem\/\">has a nice sample<\/a> of some of the thousands of letters she received. In the end, though, she was right.<\/p>\n<p>If you want to know why she was right, visit the links above. You won&#8217;t believe how many ways there are to analyze this seemingly simple problem in probability. What I want to do in this post is simulate it using R. If what she said was right, I should win 2\/3 of the time.<\/p>\n<p>So here&#8217;s what we do. First define a variable to store the number of simulations and a vector to store the results of each simulation.<\/p>\n<pre># number of simulations\r\nn <- 10000<\/pre>\n<pre># allocate memory to store results\r\nx <- vector(mode = \"integer\", length = n)<\/pre>\n<p>Now we do the simulation. Picking the first door can be thought of as a single binomial trial with p = 1\/3. We either pick the car (1) or we don't (0). We have a 2\/3 chance of not picking the car. After we pick our door, we know another door will be opened to reveal a goat. That happens every time. That means we'll either switch to a goat or to a car. Hence the \"if\" statement: if we picked the car (1), switch to goat (0); else switch to car(1).<\/p>\n<pre># simulation\r\nfor (j in 1:n){\r\n # pick a door; either goat=0 (p=2\/3) or car=1 (p=1\/3)\r\n x[j] <- rbinom(1,1,prob=(1\/3)) \r\n # switch; if 1, switch to 0, and vice versa\r\n if (x[j] == 1) {\r\n x[j] <- 0\r\n } else x[j] <- 1\r\n}<\/pre>\n<p>When the simulation is done, calculate the percentage of times you switched to a car (1):<\/p>\n<pre>sum(x)\/n # percentage of time you get car<\/pre>\n<p>When I ran this I got 0.6667, which is in excellent agreement with the theoretical answer of 2\/3. Yay winning cars on game shows!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Back in 1990, the following question was posed to\u00a0PARADE magazine columnist,\u00a0Marilyn Vos Savant (once holder of the\u00a0Guinness\u00a0World Record for highest&#8230; <a class=\"read-more\" href=\"https:\/\/www.clayford.net\/statistics\/simulating-the-monty-hall-problem\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,13],"tags":[],"class_list":["post-116","post","type-post","status-publish","format-standard","hentry","category-probability","category-using-r"],"_links":{"self":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/comments?post=116"}],"version-history":[{"count":3,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":898,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/116\/revisions\/898"}],"wp:attachment":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/media?parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/categories?post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/tags?post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}