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


Want to share your content on R-bloggers? click here if you have a blog, or here if you don’t.

I find that when teaching statistics (and probability) it is often helpful to simulate data first in order to get an understanding of the problem. The Monty Hall problem recently came up in a class so I implemented a function to play the game.

The Monty Hall problem results from a game show, Let’s Make a Deal, hosted by Monty Hall. In this game, the player picks one of three doors. Behind one is a car, the other two are goats. After picking a door the player is shown the contents of one of the other two doors, which because the host knows the contents, is a goat. The question to the player: Do you switch your choice?

For more information, be sure to see the Wikipedia article.

Below we implement a function that will simulate a single play of this game. You can play interactively, or if you specify the pick and switch parameters this can be looped in order 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:

monty_hall()
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!

Let’s now simulate 1,000 games. We will use two vectors, mh_switch and mh_no_switch, to store the results after switching doors or not, respectively. For each iteration, the initial door pick is randomly selected.

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 probability of winning if we switch the door is:

mean(mh_switch)
[1] 0.671

The probability of winning if we do not switch the door is:

mean(mh_no_switch)
[1] 0.328

It should be noted that the theoretical probability of winning if you switch is 2/3, and is 1/3 if you don’t switch.

To leave a comment for the author, please follow the link and comment on their blog: Jason Bryer.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you’re looking to post or find an R/data-science job.


Want to share your content on R-bloggers? click here if you have a blog, or here if you don’t.

Continue reading: Simulating Monty Hall’s Problem

Understanding The Monty Hall Problem Through Simulation

The concept of probability and statistical analysis can often be nuanced and complex to grasp, particularly in an educational context. However, simulation can serve as a powerful pedagogical tool that allows for a more tangible understanding of such problems. A discussion recently arose in one of the classes in regard to such a scenario, propelling the development of a new function to play the famous Monty Hall problem game.

What is the Monty Hall problem?

Originating from a game show termed ‘Let’s Make a Deal’ hosted by Monty Hall, this particular problem puts the player in a position where he/she must select one among three doors. While a car is hidden behind one of these doors, the other two conceal goats. Once the player makes a pick, the host (who is aware of what is behind each door) reveals a goat hidden behind one of the remaining two doors. The player is then posed with the question: will they stick with their original choice or switch their choice to the remaining unopened door?

Simulating the Monty Hall problem

The function is programmed to simulate a round of this game. The pick and switch parameters allow for the facilitation of repetitive rounds to simulate multiple results.

After implementing this function, the user is able to play a single game or simulate 1000 games to study the results. Two vectors, known as mh_switch and mh_no_switch, are used to store the result of the games if the player decides to switch doors or not, respectively. For each repetition, the initial door pick is automatically selected.

Results and probability considerations

After simulating 1000 games, the function provided insightful results regarding the probability of winning based on the player’s strategy. If the player decided to switch their chosen door, they had a ~67.1% chance of winning. Conversely, if they decided to stick with their original chosen door, they had a ~32.8% chance of winning.

It is important to consider that the theoretical probability of winning if the door is switched is 2/3 (approximately 66.7%), and if one doesn’t switch, it decreases to 1/3 (approximately 33.3%).

Long-term Implications and Possible Future Developments

Through this simulation, we may enhance our understanding of probability and learn to make optimum choices in real-life situations that reflect similar scenarios as the Monty Hall problem. In a wider perspective, this procedure helps students and researchers gain a deeper understanding of statistical probability and the influence of decision making on outcomes.

One possible future development could be an extended function that simulates more complex versions of the Monty Hall problem, such as scenarios with additional doors, or multiple iterations of the game.

Actionable Advice

Teaching complex statistical concepts can be made more interactive and effective through such simulation-based approaches. Teachers and educators can develop similar tools for other statistical concepts, thereby facilitating a hands-on learning experience.

In terms of the Monty Hall problem, the advice, based on both simulation and theoretical probabilities, is generally to switch the chosen door. In scenarios where probability plays a key role, take the time to understand the likelihoods and adjust choices and decisions accordingly.

Read the original article