Get startedGet started for free

Simulate sign errors: changing probabilities

In the previous exercise, each simulation had a fixed probability of a sign error at each step.

In the original question as posed, the probability of a sign error can be different at each step; the only requirement is that each probability is less than 0.50.

Here, let us simulate a math problem with 2 steps in which the probability of a switch on each step is 0.49 and 0.01, meaning that there is a high probability of becoming incorrect on the first step and a low probability of switching back.

This exercise is part of the course

Probability Puzzles in R

View Course

Exercise instructions

  • Use the sapply function with the rbinom function to simulate the switches within one problem, with switch probabilities of 0.49 and 0.01 for the first and second steps, respectively.
  • Determine the total number of switches that occurred.
  • Determine whether the answer to the math problem would be correct or not.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

set.seed(1)
counter <- 0

for(i in 1:10000){
  # Simulate switches
  each_switch <- sapply(___)
  # Count switches
  num_switches <- 
  # Check solution
  if(___){
    counter <- counter + 1
  }
}

print(counter/10000)
Edit and Run Code