I'm trying to find the average of the longest run in 10,000 simulations when flipping a coin 30 times. I need to simulate in R, the experiment described above 10,000 times and each time note the length of the longest run.
Here's my code so far:
coin <- sample(c("H", "T"), 10000, replace = TRUE)
table(coin)
head(coin, n = 30)
rle(c("H", "T", "T", "H", "H", "H", "H", "H", "T", "H"))
coin.rle <- rle(coin)
str(coin.rle)
All of the coin flips are independent of one another (i.e., the outcome of one flip does not influence another flip). Because of this, we can flip all of the coins for all the simulations at once and then format in such a way that will make it simpler to summarize each 30 flip trial. Here is how I would approach this.
# do all of the flips at once, this is okay because each flip
# is independent
coin_flips <- sample(c("heads", "tails"), 30 * 10000, replace = TRUE)
# put them into a 10000 by 30 matrix, each row
# indicates one 'simulation'
coin_matrix <- matrix(coin_flips, ncol = 30, nrow = 10000)
# we now want to iterate through each row using apply,
# to do so we need to make a function to apply to each
# row. This gets us the longest run over a single
# simulation
get_long_run <- function(x) {
max(rle(x)$length)
}
# apply this function to each row
longest_runs <- apply(coin_matrix, 1, get_long_run)
# get the number of simulations that had a max run >= 7. Divide this
# by the number of simulations to get the probability of this occuring.
sum(longest_runs >= 7)/nrow(coin_matrix)
You should get something between 18-19%, but this will vary a little bit for each time you try this simulation.