andreister

George Lucas Movie Experiment

Sep 5th, 2012
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.26 KB | None | 0 0
  1. #
  2. #see http://stats.stackexchange.com/a/35707/9013 for more details
  3. #
  4.  
  5. #total viewers of both "Episode A" and "Episode B" movies
  6. totalViewers <- 20000  
  7.                
  8. #movie visit simulations, each item representing the length of all "Episode A" viewer chains (each chain gets interrupted by an "Episode B" viewer at some point, we sum up the total length of all "Episode A" chains)
  9. simulationCount <- 3000                
  10. simulations <- vector(length=simulationCount)      
  11.  
  12. #run the simulator
  13. for(i in 1:simulationCount)                
  14. {
  15.     #simulate a movie visit: generate a vector of {-1,1},  "1" means the viewer watched "Episode A" and "-1" means "Episode B" (50% probability for each)
  16.     movieVisit <- 2*rbinom(totalViewers, 1, 0.5) - 1   
  17.  
  18.     #calculate the "Episode A" viewer chains (each positive number in the vector is the length of the "Episode A" viewers chain until it's interrupted by an "Episode B" viewer)
  19.     episodeAViewers <- cumsum(movieVisit)          
  20.  
  21.     #remember the total length of all "Episode A" viewer chains for this simulation
  22.     simulations[i] <- sum(episodeAViewers>=0)/totalViewers     
  23. }
  24.  
  25. #plot the histogram
  26. hist(simulations, freq=FALSE, xlab="Proportion of time one movie is the leader", main="George Lucas experiment")
  27. curve(dbeta(x, 1/2, 1/2), 0, 1, col=2, add=TRUE)
Add Comment
Please, Sign In to add comment