Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #----------------------------------------------------------------------
  2. # Monty Hall game SIMULATION
  3.  
  4. # This function generates 3 random doors where 2 are set equal to 0 (the goats) and
  5. # 1 is set equal to 1 (the car)
  6. generate_random_3_door <- function()
  7. {
  8. sampl = c(0,1)
  9. probvec = c(.5,.5)
  10. a <- sample(sampl,1,prob=probvec)
  11. b <- sample(sampl,1,prob=probvec)
  12. c <-sample(sampl,1,prob=probvec)
  13. if((a|b|c==1) & (a&b)!=1 & (b&c)!=1 & (c&a)!=1)
  14. {
  15. vect <- c(a,b,c)
  16. return(vect)
  17. }else
  18. {
  19. return(generate_random_3_doors())
  20. }
  21. }
  22.  
  23. # Library needed to plot the pie chart
  24. library(plotrix)
  25.  
  26. # This function simulates n games both changing and not changing door
  27. trialswsw <- function(n)
  28. {
  29. # We generate our sample matrix
  30. first <- c(1,0,0)
  31. doors <- matrix(first,ncol=3)
  32. for(j in 1:n){
  33. doors <- rbind(doors,generate_random_3_doors())
  34. }
  35. #View(doors)
  36.  
  37. # Choices matrix, choice 1: no change, choice 2: change
  38. choices <- matrix(ncol=2)
  39. for(i in 1:nrow(doors))
  40. {
  41. # Since there's no reason to pick a particular door, we always choose the first
  42. choice1 <- doors[i,1]
  43.  
  44. # Now Monty gets rid of one of the remaining wrong doors (either number 2 or 3)
  45. if((doors[i,2] == 0) & (doors[i,3] == 0))
  46. {
  47. doors[i,2] <- NA
  48. }
  49. else if(doors[i,2] == 0)
  50. {
  51. doors[i,2] <- NA
  52. }else if(doors[i,3] == 0)
  53. {
  54. doors[i,3] <- NA
  55. }
  56.  
  57. # We switch door, while being careful not to choose the open one! (NA) ;)
  58. if(is.na(doors[i,2])){
  59. choice2 <- doors[i,3]
  60. }else{
  61. choice2 <- doors[i,2]
  62. }
  63.  
  64. # This vector contains the choice
  65. choicevec = c(choice1,choice2)
  66.  
  67. # we add each vector (game) to the result matrix
  68. choices <- rbind(choices,choicevec)
  69. }
  70.  
  71. # A small adjustment, not relevant if n is big enough
  72. choices[1,]= c(1,1)
  73.  
  74. # We calculate relative frequencies
  75. freqrelchoice1 = sum(choices[,1])/nrow(choices)
  76. freqrelchoice2 = sum(choices[,2])/nrow(choices)
  77. data = c(freqrelchoice1,freqrelchoice2)
  78. labels = c("% win without changing","% win changing")
  79. names(data) = labels
  80.  
  81. # View(scelte)
  82. pie3D(data,labels=labels,explode=0.1,main="Monty Hall % chance of winning")
  83. return(data)
  84. }
  85.  
  86. # Let's try our function and go for the simulation
  87. generate_random_3_doors()
  88.  
  89. # we test our random doors generator
  90. trials(10000) # Please ignore this function which I have integrated in the trialswsw()
  91. trialswsw(10000) # We simulate 10000 games
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement