The problem of Monty Hall simulation R-Bloggers

The problem of Monty Hall simulation R-Bloggers

2 minutes, 39 seconds Read

[This article was first published on Jason Bryer, and kindly contributed to R-bloggers]. (You can report problems here about the content on this page)


Do you want to share your content on R-bloggers? Click here if you have a blog, or here If you don’t.

I think that when teaching statistics (and probability) it is often useful to first simulate data to understand the problem. Monty Hall’s problem recently came in a class, so I have implemented a function to play the game.

The problem of the Monty Hall stems from a game show, Let’s close a dealhosted by Monty Hall. In this game the player chooses one of the three doors. Behind one is a car, the other two are goats. After choosing a door, the player is shown the content of one of the other two doors, which because the host knows the content, is a goat. The question for the player: do you change your choice?

For more information, make sure you Wikipedia -article.

Below we implement a function that simulates a single game of this game. You can play interactively, or if you have the pick And switch Parameters This can be purchased to simulate the results.

monty_hall <- function(pick, switch) {
    interactive <- FALSE
    if(missing(pick)) {
        interactive <- TRUE
        cat('Pick your door:')
        pick <- LETTERS[menu(c('A', 'B', 'C'))]
    } else {
        if(!pick %in% LETTERS[1:3]) {
            stop('pick must be either A, B, or C')
        }
    }
    doors <- c('win', 'lose', 'lose')
    doors <- sample(doors) # Shuffle the doors
    names(doors) <- LETTERS[1:3]
    if(doors[pick] == 'win') {
        show <- sample(names(doors[!names(doors) %in% pick]), size = 1)
    } else {
        show <- doors[!names(doors) %in% pick] == 'lose'
        show <- names(which(show == TRUE))
    }
    if(missing(switch)) {
        interactive <- TRUE
        cat(paste0('Showing door ', show, '. Do you want to switch your choice?'))
        switch <- menu(c('yes', 'no')) == 1
    }
    if(switch) {
        pick <- names(doors)[!names(doors) %in% c(show, pick)]
    }
    win <- unname(doors[pick] == 'win')
    if(interactive) {
        if(win) {
            cat('You win!')
        } else {
            cat('Sorry, you lost.')
        }
        invisible(win)
    } else {
        return(win)
    }
}

We can play a single game:

Pick your door:
1: A
2: B
3: C

Selection: 2
Showing door A. Do you want to switch your choice?
1: yes
2: no

Selection: 1
You win!

Now let’s simulate 1,000 games. We will use two vectors, mh_switch And mh_no_switchTo save the results after changing doors or not respectively. The initial door choice is randomly selected for each iteration.

n_games <- 1000
mh_switch <- logical(n_games)
mh_no_switch <- logical(n_games)
for(i in 1:n_games) {
    pick <- sample(LETTERS[1:3], size = 1)
    mh_switch[i] <- monty_hall(pick = pick, switch = TRUE)
    mh_no_switch[i] <- monty_hall(pick = pick, switch = FALSE)
}

The chance to win when we change the door is:

The chance to win if we don’t switch is the door:

It should be noted that the theoretical opportunity to win if you switch is 2/3 and is 1/3 if you do not switch.


#problem #Monty #Hall #simulation #RBloggers

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *