I’m not going to spend the time going into the background for the three door problem. Lets go straight to the fun! I like the alternative examples of using many doors, but I also have some fun variations I want to show. I haven’t heard of these before… but I have to assume someone has beaten me into making these because how could they have not. Anywho, these will show the problems then some randomly generated values to back up my guesses.
I’ll put the doors like this maybe:
Doors correct : total number of doors available
and
Before doors are closed -> after doors are closed
Here’s the example for the traditional game.
traditional <- function(keep){
prize = sample(c(1,2,3), 1)
guess = sample(c(1,2,3), 1)
if(keep == 1) {
ifelse(prize == guess, return(1), return(0))
} else {
ifelse(prize != guess, return(1), return(0))
}
return(prize)
}
cat("Non-switching win percentage: ", mean(replicate(1000, traditional(1))))
## Non-switching win percentage: 0.326
cat("\nSwitching win percentage: ", mean(replicate(1000, traditional(0))))
##
## Switching win percentage: 0.657
About what we expected. 2x the odds of winning if you switch. Tada! This could be a bit simpler though…
trad <- function() {
prize = sample(c(1,2,3), 1)
guess = sample(c(1,2,3), 1)
if(prize == guess) {
return(1) # means you would get correct if you stay
} else {
return(0) # means you would get correct if you switched
}
}
odds = mean(replicate(1000, trad())) # less computationally expensive?
cat("Non-switching win percentage: ", odds)
## Non-switching win percentage: 0.325
cat("\nSwitching win percentage: ", 1 - odds)
##
## Switching win percentage: 0.675
Adding some additional doors so we can see what this looks like.
bigger <- function() {
prize = sample(c(1:10), 1)
guess = sample(c(1:10), 1)
ifelse(prize == guess, return(1), return(0))
}
odds = mean(replicate(1000, bigger()))
cat("Non-switching win percentage: ", odds)
## Non-switching win percentage: 0.115
cat("\nSwitching win percentage: ", 1 - odds)
##
## Switching win percentage: 0.885
biggest <- function() {
prize = sample(c(1:100), 1)
guess = sample(c(1:100), 1)
ifelse(prize == guess, return(1), return(0))
}
odds = mean(replicate(1000, biggest()))
cat("Non-switching win percentage: ", odds)
## Non-switching win percentage: 0.018
cat("\nSwitching win percentage: ", 1 - odds)
##
## Switching win percentage: 0.982
Hopefully one gets the idea of how switching doors helps your chances of winning, and this is very apparent on larger scales. If you choose a door of an available 100,000 doors, the chances of you winning are minuscule. If someone opens up all but 1 other door which you did not guess… yea there’s a really good chance that 1/100,000 door isn’t the one you chose, but the other one which hadn’t been opened.
We’ve seen how to better understand this problem when the number of available doors to choose from, and get closed, increases… but what happens if we have multiple doors with prizes behind them?
Two of the ten doors have a prize. You select a door. Monty opens up 6 other doors, none of which have prizes behind them. Do you switch?
If you were initially correct, switching should have a 1/3 chance of winning. If initially incorrect, switching should have a 2/3 chance of winning. Together, this should combine to a 1/5*1/3 + 4/5*2/3 = 1/15 + 8/15 = 9/15 = 3/5.
alt1 <- function(switch) {
prize = sample(c(1:10), 2, replace=FALSE) #swithcing it up this time
guess = sample(c(1:10), 1)
if(switch == 0) {
if(guess %in% prize) {
return(1)
} else {
return(0)
}
} else {
# we got it correct, so two rando doors stay shut or we got it wrong so 1 other rando door stays shut
noprize = sample(setdiff(c(1:10), c(prize, guess)), 4 - length(unique(c(prize, guess))), replace=FALSE)
# selects new door
new_guess = sample(setdiff(c(prize, noprize), guess), 1)
if(new_guess %in% prize){
return(1)
} else {
return(0)
}
}
}
odds_switch = mean(replicate(10000, alt1(1)))
odds_keep = mean(replicate(10000, alt1(0)))
cat("Chance of winning staying the same: ", odds_keep)
## Chance of winning staying the same: 0.2044
cat("\nChance of winning after switching: ", odds_switch)
##
## Chance of winning after switching: 0.5965
Pretty solid
What happens now if two doors are correct, you guess one, but Monty then reveals 6 doors, one of which having a prize behind it. Obviously, you can’t choose the door which now has a prize revealed. How does this change the situation?
You have a 2/10 chance of being correct if you stay, this we know by now. But, what if you switch? If you were initially correct and decided to switch, obviously you’d lose. But if you were wrong initially, you should have a 50/50 chance of winning after the switch. Interestingly, 4/5*1/2 = 4/10, which is twice as likely as not switching. The Monty Hall problem is back, but in a weird new form.
# function assumes all guesses will switch
# you can just use alt1 for one that doesn't switch ig
alt2 <- function() {
prize = sample(c(1:10), 2, replace=FALSE)
guess = sample(c(1:10), 1)
us_prize = setdiff(prize, guess)
new = sample(setdiff(c(1:10), c(prize, guess)), 3 - length(us_prize), replace=FALSE)
new_guess = sample(c(new, sample(us_prize, length(us_prize) - 1)), 1)
if(new_guess %in% prize){
return(1)
} else {
return(0)
}
}
odds = mean(replicate(10000, alt2()))
cat("Chance of winning after switching: ", odds)
## Chance of winning after switching: 0.3928
Does the number of doors left open impact the problem? What if the number of doors with prizes behind them are higher?
Imagine 10 doors, but 5 have the prize. You open all by 8 doors, and of those 8, four have prizes, and four do not. Do you keep or stay? I think the odds should be equal
What if we had 3 prizes over 10 doors? You choose one, and then Monty opens 7 doors, but none show prizes. Well, that’s a guaranteed win no matter what you do. If Monty opens 7 doors and all three prizes are behind, then obviously you just lose. But if only one or two prizes are opened? *we assume Monty is opening all these doors with intention.
Original chance of winning is 3/10. After 7 more doors are opened, one of which being a prize, there’s two prizes remaining and three doors unopened. If you switch, there’s a 7/10 chance of having a guaranteed win, and a 3/10 chance of choosing the original correct and a 50/50 chance of picking the other correct one, or 3/20. Total odds 17/20, almost 3x better than staying the same.
Original chance of winning is 3/10. But after 7 doors open, only one prize door remains unopened. There was a 3/10 chance of guessing correctly first, then a 0/10 chance of guessing correct if you switched. There’s a 7/10 chance of guessing incorrectly first, but then a 50/50 chance of guessing correctly after switching. That’s 7/10*1/2 or 7/20, a little more than 2x as likely after switching.
You have two pennies, one HH other HT. You don’t know which one you picked, but you flip it and it comes back heads. What’s the chance the next flip lands tails?
fair = c(0,1) # coin 1
unfair = c(1,1) # coin 0
unfairflip <- function() {
coin = sample(c(0,1), 1)
if(coin == 1) {
if(sample(c(0,1), 1) == 0) {
# first flip was a tails, excluded from data
return(NA)
} else {
# after second flip, returns 1 if tails, 0 if tails
return(ifelse(sample(c(0,1), 1) == 0, 1, 0))
}
} else {
# coin is unfair, will always land heads
return(0)
}
}
odds = mean(replicate(10000, unfairflip()), na.rm=TRUE)
odds
## [1] 0.1670438
#cat("Odds of landind tails on next throw: ", )
This is equivalent to 1/6, or 0.16667, which is what we expect.