Chapter 3.2 Exercise 10

In a ten-question true-false exam, find the probability that a student gets a grade of 70 percent or better by guessing. Answer the same question if the test has 30 questions, and if the test has 50 questions.

Using Functions

get_70_pct <- function(nq) {
  
  # number of successes to get at least 70%
  np <- ceiling(nq * .7)
  
  # true or false is 1/2 probability
  p <- dbinom(np, nq, .5)
  
  return(p)
  
}

# 10 question exam
(t1 <- get_70_pct(10))
## [1] 0.1171875
# 30 question exam
(t2 <- get_70_pct(30))
## [1] 0.01332457
# 50 question exam
(t3 <- get_70_pct(50))
## [1] 0.001999138

Manual Approach

# size <- 70
# pascals_t <- matrix(0, nrow = size, ncol = size)
# 
# for (i in 1:size) {
#   for (j in 1:size) {
#     if (j <= i) {
#       pascals_t[i,j] <- factorial(i) / (factorial(j) * factorial(i - j))
#     }
#   }
# }
# 
# pascals_t

# subscript out of bounds error


get_70_pct_v2 <- function(nq) {

  # number of successes to get at least 70%
  np <- ceiling(nq * .7)

  coef <- factorial(nq) / (factorial(np) * factorial(nq - np))
  
  p <- coef * (.5 ^ np) * (.5 ^ (nq - np))
  
  return(p)
}

# 10 questions
(t1_v2 <- get_70_pct_v2(10))
## [1] 0.1171875
# 30 questions
(t2_v2 <- get_70_pct_v2(30))
## [1] 0.01332457
# 50 questions
(t3_v2 <- get_70_pct_v2(50))
## [1] 0.001999138
# checks
t1 == t1_v2 & t2 == t2_v2 & t3 == t3_v2
## [1] FALSE
# since the above check is false let's try another
# total absolute difference
round(
abs(t1 - t1_v2) + abs(t2 - t2_v2) + abs(t3 - t3_v2)
,8)
## [1] 0