daily pastebin goal
54%
SHARE
TWEET

"The Probabilistic Quiz" blog post for vijesh.me

vijesh Feb 5th, 2015 190 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Proceed with caution, clumsy code ahead! But rest assured, it works.
  2. # You're more than welcome to tweak this code to play around with the parameters.
  3. library(ggplot2)
  4. num_sim_trials <- 1000
  5. num_quiz_trials <- 3
  6. num_questions <- 9
  7. num_options <- 4
  8.  
  9. num_correct = c()
  10. for(i in 1:num_sim_trials) {
  11.         correct_answers <- sample(1:num_options, num_questions, replace = T)
  12.         available_selections <- list()
  13.         for(q in 1:num_questions) {
  14.                 available_selections[[q]] = 1:num_options
  15.         }
  16.        
  17.         for(j in 1:num_quiz_trials) {
  18.                 chosen_answers = c()
  19.                 for(q in 1:num_questions) {
  20.                         if (length(available_selections[[q]]) > 1) {
  21.                                 chosen_answer = sample(available_selections[[q]], 1)
  22.                         } else {
  23.                                 chosen_answer = available_selections[[q]]
  24.                         }
  25.                        
  26.                         if(chosen_answer == correct_answers[q]) {
  27.                                 available_selections[[q]] = c(chosen_answer)
  28.                         } else {
  29.                                 available_selections[[q]] = available_selections[[q]][ available_selections[[q]] != chosen_answer ]
  30.                         }
  31.                         chosen_answers = c(chosen_answers, chosen_answer)
  32.                 }
  33.         }
  34.         num_correct = c(num_correct, sum(correct_answers == chosen_answers))
  35. }
  36. df = data.frame(correct = num_correct)
  37. g = ggplot(df) + aes(x = correct)
  38. g = g + geom_histogram(binwidth = 0.5)
  39. g = g + xlab(paste("Number of correct answers for", num_questions, "questions and", num_quiz_trials, "attempts."))
  40. g = g + ggtitle(paste("Histogram of correct answers for", num_sim_trials, "trials"))
  41. print(g)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top