User Tools

Site Tools


classblogs:ba2011:monty_hall_problem_simulation

Monty Hall problem simulation

Quinn conducted a simulation in R to investigate the Monty Hall problem. Thanks, Quinn. This is a very elegant way to show the problem intuitively. I attached his codes here. If you are interested, feel free to modify the codes to conduct your own simulation. You are welcome to post your codes here, too.

#Monty Hall Simulation
#	 Quinn Lathrop
#   	8/25/2011
 
Monty.Hall<- function(reps){
 
	big.rep <- reps
 
	results <- matrix(0, big.rep, 2)
	colnames(results) <- c("Stayed", "Switched")
 
	for(br in 1: big.rep){
 
		doors<-c(0,0,0)				#start with three doors
 
		doors[sample(1:3,1)] <- 1 	#this denotes which door is the winner
 
		picked <- sample(1:3,1)		#this is the contestent's pick
		not.picked <-doors[-picked]
 
		if(sum(not.picked)==1){
							#If the contestent's pick is wrong,
			results[br, 2] <- 1   	#the host will open the one remaing door
							#that is also wrong. It is winning to switch.
		} else {				#If the contestent's pick is right, 
							#the host will open either remaing door.
			results[br, 1] <- 1		#It is winning to stay.	
		}
	}
 
return(colMeans(results))
}	
You could leave a comment if you were logged in.
classblogs/ba2011/monty_hall_problem_simulation.txt · Last modified: by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki