Last active
September 21, 2015 05:20
-
-
Save actuaryactually/0048bc54c516f812ed6a to your computer and use it in GitHub Desktop.
Game Theory Switching Strategy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#=============================================================================================== | |
#Monty Hall Problem - Game Theory around Choice | |
#Three options available. Two options have no prize, third option delivers a prize. You select one, and | |
#then the host of the game removes one of the losing options. You are then offered the chance to reselect | |
#For this code, I assume that you always switch from your first choice. It then examines the number of times | |
#you win based on this "always-switching" strategy | |
#=============================================================================================== | |
#1. decide how many games you'll play | |
no.games<-100 | |
#2. establish winning stake | |
prize<-100 | |
#3. create all possible outcomes table | |
possible.outcome<-rbind(c(prize,0,0), c(0,prize,0), c(0,0,prize)) | |
#4. create appropriate sized vector to store results: | |
results<-rep(NA,no.games) | |
#5. start loop | |
for(i in 1:no.games) { | |
#5.1 pick a row, which will be deemed as the true outcomes provided by the dealer for the current game | |
(selected.row<-sample(1:3,1,replace=FALSE)) | |
#5.2 current games therefore, is: | |
(current.game<-possible.outcome[selected.row,]) | |
#5.3 player's first guess: | |
(initial.pick.index<-sample(1:3,1,replace=FALSE)) | |
#5.4 establish indices of the values not selected; let's call this "check.options": | |
ifelse(initial.pick.index==1,check.options<-c(2,3), | |
ifelse(initial.pick.index==2,check.options<-c(1,3), | |
check.options<-c(1,2))) | |
#check.options | |
#5.5 use check options vector to decide which box the dealer should leave available: | |
ifelse(current.game[check.options[1]]==0, | |
final.options<-c(check.options[2],initial.pick.index), | |
final.options<-c(check.options[1],initial.pick.index)) | |
#(final.options<-sort(final.options)) | |
#5.6 decide which box to keep based on "strategy": | |
ifelse(final.options[1]==initial.pick.index, | |
final.choice<-final.options[2], | |
final.choice<-final.options[1]) | |
#5.7 assess outcome: | |
outcome<-current.game[final.choice] | |
#5.8 record outcome for this game to the relevant position in our vector: | |
results[i]<-outcome | |
#5.9 play again | |
} | |
#6. analyse matrix output across all games | |
plot(1:no.games,results,col="blue",main = "Monty Hall Simulation\nOutcome by Trial",ylab="Prize",xlab="Trial") | |
table(results) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment