A coin with probability of heads $p \in (0, 1) $ is tossed $n$ times. What is the joint probability distribution of the number of runs and the longest run?
Schilling (1990) discusses the distribution of the longest run but I haven't found a good source for the joint distribution of the longest run and the number of runs. I also found this post at Mathematics Stack Exchange.
As an illustration, I've randomly generated 10 coin tosses with a fair coin ($p=0.5$) and got the following sequence: $\text{TTHTHHTTHT}$. The longest run is 2 and the number of runs is 7.
Here is a simulation with 5000 repeats using $n=100$ and $p=0.5$:
Here is the R code I used for the simulations and the graphic:
library(ggplot2)
library(grid)
res.frame <- data.frame(
noruns = numeric(0)
, longestrun = numeric(0)
)
n_sims <- 5000
set.seed(142857)
for (i in seq_len(n_sims)) {
x <- rbinom(100, 1, 0.5)
y <- rle(x)
res.frame[i, "noruns"] <- length(y$lengths[y$length])
res.frame[i, "longestrun"] <- max(y$lengths)
}
Plot the data
theme_set(theme_bw())
p <- ggplot(res.frame, aes(x = noruns, y = longestrun)) +
geom_point(position = position_jitter(width = 0.2, height = 0.27), size = 0.5) +
labs(
x = "Number of runs"
, y = "Longest run"
) +
scale_y_continuous(breaks = seq(2, 20, 2)) +
scale_x_continuous(breaks = seq(20, 100, 5)) +
theme(
plot.title=element_text(size=30),
axis.title.y=element_text(colour = "black", size = 17, hjust = 0.5, margin = margin(0,12,0,0)),
axis.title.x=element_text(colour = "black", size = 17, margin = margin(10,0,0,0)),
axis.text.x=element_text(size=17, angle=0, hjust=0.5, vjust=1),
axis.text.y=element_text(size=17),
legend.position="none",
plot.margin=unit(c(0.2, 0.2, 0.7, 0.2),"cm"),
strip.text.x = element_text(size = 20),
panel.grid.minor=element_blank(),
panel.grid.major=element_blank(),
strip.background=element_rect(fill="white")
)
p
