#
#
###### The Two Envelopes problem.
#
# You are presented with two identical envelopes. I tell you there is money
# in both envelopes; one envelope has more money than the other.
# You must pick one of them.
# Once you pick, and before you open it, I flip a coin.
# If the coin is heads I put twice the amount I know is in your envelope
# into the other envelope.
# If the coin is tails, I put half the amount I know is in your envelope
# into the other envelope.
# Next, I ask you if you want to switch to the other envelope.
# Would you switch? Would switching make any difference across multiple
# trials (i.e. long run) in terms of the total amount of money won?
values <- c(20, 10)
first <- sample(values, 1) # Your first pick.
flip <- sample(c("head","tail"), 1)
if(flip == "head"){other <- first*2}
if(flip == "tail"){other <- first/2}
switch <- sample(c("y","n"), 1) # Do you switch? (Here we randomly decide).
if(switch == "y"){result <- other}
if(switch == "n"){result <- first}
switch; result
rm(first, flip, other, result, switch, values)
#### Checking 1000 iterated simulations of the scenario with 3 strategies:
# No strategy employed; decision to switch is
# Random:
n <- 1000
random <- data.frame(matrix(rep(NA,2*n), ncol = 2))
names(random) <- c("decision","result")
for (i in 1:n){
values <- c(20,10)
first <- sample(values, 1)
flip <- sample(c("head","tail"), 1)
if(flip == "head"){other <- first*2}
if(flip == "tail"){other <- first/2}
random[i,1] <- sample(c("y","n"), 1)
if(random[i,1] == "y"){random[i,2] <- other}
if(random[i,1] == "n"){random[i,2] <- first}
}; rm(n, i, values, first, flip, other)
# Next, employ the Never switch strategy:
n <- 1000
never <- data.frame(matrix(rep(NA,2*n), ncol = 2))
names(never) <- c("decision","result")
for (i in 1:n){
values <- c(20,10)
first <- sample(values, 1)
flip <- sample(c("head","tail"), 1)
if(flip == "head"){other <- first*2}
if(flip == "tail"){other <- first/2}
never[i,1] <- "n"
if(never[i,1] == "y"){never[i,2] <- other}
if(never[i,1] == "n"){never[i,2] <- first}
}; rm(n, i, values, first, flip, other)
# Next, employ the Always switch strategy:
n <- 1000
always <- data.frame(matrix(rep(NA,2*n), ncol = 2))
names(always) <- c("decision","result")
for (i in 1:n){
values <- c(20,10)
first <- sample(values, 1)
flip <- sample(c("head","tail"), 1)
if(flip == "head"){other <- first*2}
if(flip == "tail"){other <- first/2}
always[i,1] <- "y"
if(always[i,1] == "y"){always[i,2] <- other}
if(always[i,1] == "n"){always[i,2] <- first}
}; rm(n, i, values, first, flip, other)
# Now take a look at the 'winnings' to compare the strategies.
sum(random[,2])
sum(never[,2])
sum(always[,2])
rm(always, never, random)
ls()
# End script.