1. LON-CAPA Logo
  2. Help
  3. Log In
 

Browsing resource, all submissions are temporary.


Checking of Code Related to the Coin Flip Problem

Several techniques of checking a code have been introduced in this course. In this exercise, you will learn one more technique: using a statistical test to check a code. This technique was used in the creation of Week 9's problem on the coin flip simulation.

Recall that one of the questions in the coin flip problem is to find the probability of getting at least 7 consecutive heads or tails in 100 coin flips. As mentioned in the problem, the question was inspired by Statistics Professor Deborah Nolan's class activity described in the podcast Stochasticity. I thought it would be fun to simulate the coin flips. To set up the problem, I needed to do a number of simulations of the coin flip experiments and copied the results to Lon Capa.

The most important step of the simulation is to create the function max_streak(flips), which counts the maximum number of consecutive 0s and 1s in the integer vector flips. At the time, I did not know the function rle(). The first version of the function I wrote was shown below.

max_streak1 <- function(flips) {
  n <- length(flips) 
  # Initialize max_count and count to 1
  max_count <- 1L
  count <- 1L
  for (i in 2:n) { 
    if (flips[i]==flips[i-1]) {
      count <- count + 1L
    } else {
      # A streak is broken. Update max_count and reset count to 1
      max_count <- max(max_count,count) 
      count <- 1L
    }
  }
  # Return max_count
  max_count
}

The function was tested with a number of different integer vectors flips and it returned the expected results. To test it further, it was used to perform the 100 coin flips experiment 100,000 times using the following code.

RNGversion("3.5.0") 
set.seed(1703699)
maxStreaks1 <- replicate(1e5, max_streak1(sample(c(0,1),100,replace=TRUE)))

Copy and paste the function max_streak1() and the code above and execute it. It may take more than 10 seconds to run, depending on your computer speed. It is slow because the function uses a for-loop and it is called 100,000 times.

a. Use the same method as in Week 9's coin flip problem to compute the approximate probability of at least 7 consecutive heads or tails occurring in 100 coin flips. Enter all 5 digits.

P(≥7) ≈

 Tries 0/3

How do we know whether or not the result in (a) is correct or even reasonable? The first thought I had was to compare it to the analytic solution. As mentioned in the coin flip problem, the analytic solution involves the computation of a recurrence formula. As a result, the analytic solution also requires a code to compute. I wrote a code to compute the analytic solution and get P(≥ 7) = 0.542336855499. The value in (a) is close to the analytic calculation. We can perform a z-test to see if the deviation is statistically significant. The following null and alternative hypothesis are formulated for the test.

Null hypothesis H0: The value calculated in (a) is consistent with the analytic value 0.542336855499. The deviation is caused by chance variation.

Alternative hypothesis HA: The value calculated in (a) is inconsistent with the analytic value 0.542336855499. The deviation between the value calculated in (a) and the analytic value 0.542336855499 is too large to be caused by chance variation.

Set up a box model assuming H0: consider a box with trillions of tickets inside, 54.2336855499% of which are written '1' and the rest are written '0'. A ticket with '1' represents a value in maxStreaks1 ≥ 7 and that with '0' represents a value in maxStreaks1 < 7.

The result stored in the logical vector (maxStreak >= 7) is equivalent to drawing 100,000 tickets with replacement with '0' representing FALSE and '1' representing TRUE. [For those who have learned the binomial distribution in the formal way, it simply means that the probability of maxStreaks1 ≥ 7 follows a binomial distribution with n=100,000 and p=0.542336855499.]

b. Assume H0 is true, the expected value of mean(maxStreaks1 >= 7) is (enter at least 6 decimal places) .

 Tries 0/3

c. Calculate the standard error of mean(maxStreaks1 >= 7). Give your answer to 6 decimal places.

SE =

 Tries 0/3

d. Calculate the Z score of the observed value in (a) and the corresponding p-value. Give your answers to 3 significant figures. Recall that you can use the e-notation in your answer. For example, you can enter 4.56e-7 for the number 4.56×10-7.

Z = ,     p-value = .

 Tries 0/5

The tiny p-value means that the null is rejected at a very high significance level. We have to conclude that the simulation result in (a) is inconsistent with the analytic solution. There are three possibilities:

1. The function max_streak1() is wrong.

2. The analytic value 0.542336855499 is wrong.

3. Both the function max_streak1() and the analytic value 0.542336855499 are wrong.

It turns out that the coin flip problem can also be solved analytically using a technique called the discrete time Markov chain theory. The calculation involves multiplying a 7×7 matrix 99 times. I wrote a code to do the calculation and got the same analytic value 0.542336855499. That two completely different methods and pieces of code give the same result is a strong indication that the analytic value is correct. It is time to test the function max_streak1() more carefully.

By the way, you might wonder why you were asked to simulate the experiment 100,000 times instead of 10,000 times. The reason is that the difference between the analytical value and the simulation result is not very big and it requires a large sample size to detect the small difference.

e. Run the 4 tests listed on Week 9's coin flip problem on the function max_streak1(). Which tests does max_streak1() pass? (Select all that apply)
The first test: max_streak1(flips24) returns 4.
The second test: max_streak1(flips9) returns 1.
The third test: max_streak1(flips16) returns 10.
The fourth test: max_streak1(x) always returns 20.

 Tries 0/5

The rest of the problem will show up after you finish part (e).