Problème de Monty Hall
Changer ou ne pas changer de porte?
La plupart des personnes qui connaissent le problème de Monty Hall trouvent contre-intuitive l’idée que l’on doit toujours changer de porte dans ce jeu, vu que cela nous donnera ~66% de probabilité de succès à long terme (vs ~33% si on ne la change pas). Vous pouvez lire l’article de Wikipédia ci-dessus pour comprendre la logique du problème, mais on peut simplement simuler 300 tentatives (axe x) dans le jeu pour comparer les différences entre changer et ne pas changer de porte (c.-à-d., les conséquences pour notre probabilité de succès).
La fonction dans le code ci-dessous, mHall()
, est une simulation du jeu (aucun argument requis).
Code
mHall = function(){
options = c("money", "goat", "goat")
doors = sample(x = options, size = 3)
names(doors) = c("A", "B", "C")
firstChoice = readline(prompt = "Which door do you choose: A, B, or C? ")
if(doors[firstChoice] == "money"){
badDoors = doors[!names(doors) %in% firstChoice]
newOffer = sample(x = badDoors, size = 1)
secondChoice = readline(prompt = paste("I can tell you that door ", names(newOffer),
" has a ",
doors[names(newOffer)],
". Would you like to change your answer (y or n)? ",
sep = ""))
if(secondChoice == "y"){
newChoice = doors[!names(doors) %in% c(firstChoice, names(newOffer))]
return(paste("Here's what's behind door ", names(newChoice), ": ",
newChoice[[1]], sep = ""))
} else {
return(paste("Here's what's behind door ", firstChoice, ": ",
doors[[firstChoice]], sep = ""))
}
} else {
notChosen = doors[!names(doors) %in% firstChoice]
goodDoor = doors[names(which(notChosen == "money"))]
badDoor = doors[names(which(notChosen == "goat"))]
secondChoice = readline(prompt = paste("I can tell you that door ", names(badDoor),
" has a ", doors[names(badDoor)],
". Would you like to change your answer (y or n)? ",
sep = ""))
if(secondChoice == "y"){
newChoice = goodDoor
return(paste("Here's what's behind door ", names(newChoice), ": ",
newChoice[[1]], sep = ""))
} else {
return(paste("Here's what's behind door ", firstChoice, ": ",
doors[[firstChoice]], sep = ""))
}
}
return(doors[firstChoice])
}
Copyright © 2024 Guilherme Duarte Garcia