Monty Hall problem: Difference between revisions

No edit summary
Line 372:
Switching allows you to win 66529 out of 100000 times.</pre>
 
# Since R is a vector based language that penalizes for loops, we will avoid
=={{header|R}}==
# for-loops, instead using "apply" statement variants (like "map" in other
# functional languages).
set.seed(19771025) # set the seed to set the same results as this code
N <- 10000 # trials
true_answers <- sample(1:3, N, replace=TRUE)
# We can assme that the contestant always choose door 1 without any loss of
# generality, by equivalence. That is, we can always relabel the doors
# to make the user-chosen door into door 1.
# Thus, the host opens door '2' unless door 2 has the prize, in which case
# the host opens door 3.
host_opens <- 2 + (true_answers == 2)
other_door <- 2 + (true_answers != 2)
## if always switch
summary( other_door == true_answers )
## if we never switch
summary( true_answers == 1)
## if we randomly switch
random_switch <- other_door
random_switch[runif(N) >= .5] <- 1
summary(random_switch == true_answers)
 
 
 
## To go with the exact parameters of the Rosetta challenge, complicating matters....
## Note that the player may initially choose any of the three doors (not just Door 1),
## that the host opens a different door revealing a goat (not necessarily Door 3), and
## that he gives the player a second choice between the two remaining unopened doors.
N <- 10000 #trials
true_answers <- sample(1:3, N, replace=TRUE)
user_choice <- sample(1:3, N, replace=TRUE)
## the host_choice is more complicated
host_chooser <- function(user_prize) {
# this could be cleaner
bad_choices <- unique(user_prize)
# in R, the x[-vector] form implies, choose the indices in x not in vector
choices <- c(1:3)[-bad_choices]
# if the first arg to sample is an int, it treats it as the number of choices
if (length(choices) == 1) { return(choices)}
else { return(sample(choices,1))}
}
host_choice <- apply( X=cbind(true_answers,user_choice), FUN=host_chooser,MARGIN=1)
not_door <- function(x){ return( (1:3)[-x]) } # we could also define this
# directly at the FUN argument following
other_door <- apply( X = cbind(user_choice,host_choice), FUN=not_door, MARGIN=1)
## if always switch
summary( other_door == true_answers )
## if we never switch
summary( true_answers == user_choice)
## if we randomly switch
random_switch <- user_choice
change <- runif(N) >= .5
random_switch[change] <- other_door[change]
summary(random_switch == true_answers)
 
## AUTHOR: Gregg Lind <gregg.lind @ gmail.com>
## Date: 9/13/2008
## License: Public Domain, attribution politely requested
## Purpose: Two variations on the Monty Hall problem written in R
 
<pre>
Results:
 
> ## if always switch
> summary( other_door == true_answers )
Mode FALSE TRUE
logical 3298 6702
> ## if we never switch
> summary( true_answers == 1)
Mode FALSE TRUE
logical 6702 3298
> ## if we randomly switch
> summary(random_switch == true_answers)
Mode FALSE TRUE
logical 5028 4972
 
 
> ## if always switch
> summary( other_door == true_answers )
Mode FALSE TRUE
logical 3295 6705
> ## if we never switch
> summary( true_answers == user_choice)
Mode FALSE TRUE
logical 6705 3295
> ## if we randomly switch
> summary(random_switch == true_answers)
Mode FALSE TRUE
logical 4986 5014
 
</pre>
 
=={{header|Scheme}}==
Anonymous user