Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #----------------------------------------------------------------------
- # Monty Hall game SIMULATION
- # This function generates 3 random doors where 2 are set equal to 0 (the goats) and
- # 1 is set equal to 1 (the car)
- generate_random_3_door <- function()
- {
- sampl = c(0,1)
- probvec = c(.5,.5)
- a <- sample(sampl,1,prob=probvec)
- b <- sample(sampl,1,prob=probvec)
- c <-sample(sampl,1,prob=probvec)
- if((a|b|c==1) & (a&b)!=1 & (b&c)!=1 & (c&a)!=1)
- {
- vect <- c(a,b,c)
- return(vect)
- }else
- {
- return(generate_random_3_doors())
- }
- }
- # Library needed to plot the pie chart
- library(plotrix)
- # This function simulates n games both changing and not changing door
- trialswsw <- function(n)
- {
- # We generate our sample matrix
- first <- c(1,0,0)
- doors <- matrix(first,ncol=3)
- for(j in 1:n){
- doors <- rbind(doors,generate_random_3_doors())
- }
- #View(doors)
- # Choices matrix, choice 1: no change, choice 2: change
- choices <- matrix(ncol=2)
- for(i in 1:nrow(doors))
- {
- # Since there's no reason to pick a particular door, we always choose the first
- choice1 <- doors[i,1]
- # Now Monty gets rid of one of the remaining wrong doors (either number 2 or 3)
- if((doors[i,2] == 0) & (doors[i,3] == 0))
- {
- doors[i,2] <- NA
- }
- else if(doors[i,2] == 0)
- {
- doors[i,2] <- NA
- }else if(doors[i,3] == 0)
- {
- doors[i,3] <- NA
- }
- # We switch door, while being careful not to choose the open one! (NA) ;)
- if(is.na(doors[i,2])){
- choice2 <- doors[i,3]
- }else{
- choice2 <- doors[i,2]
- }
- # This vector contains the choice
- choicevec = c(choice1,choice2)
- # we add each vector (game) to the result matrix
- choices <- rbind(choices,choicevec)
- }
- # A small adjustment, not relevant if n is big enough
- choices[1,]= c(1,1)
- # We calculate relative frequencies
- freqrelchoice1 = sum(choices[,1])/nrow(choices)
- freqrelchoice2 = sum(choices[,2])/nrow(choices)
- data = c(freqrelchoice1,freqrelchoice2)
- labels = c("% win without changing","% win changing")
- names(data) = labels
- # View(scelte)
- pie3D(data,labels=labels,explode=0.1,main="Monty Hall % chance of winning")
- return(data)
- }
- # Let's try our function and go for the simulation
- generate_random_3_doors()
- # we test our random doors generator
- trials(10000) # Please ignore this function which I have integrated in the trialswsw()
- trialswsw(10000) # We simulate 10000 games
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement