TransWikia.com

Sample Space of Longest Run

Data Science Asked by A.May on December 16, 2020

I am trying to find a way to calculate all possible combinations of a sequence that have a certain length of long run.

When answering questions regarding sequences of heads and tails, sometimes participants will consider a sample space of longest run.
For a sequence of five coin flips, this is easy enough to calculate using a brute-force method – writing out all 32 possible sequences and then categorising them based on their longest run.
So, for example, a sequence with the longest run of 5 has a probability of 1/32 as there is only one way to have a longest run of 5 with a sequence of length 5.

However, I now have a sequence of length 10. I want to find out exactly how many sequences out of the 1024 possible sequences that have a longest run of 2 or 3.
I am assuming that it doesn’t matter if the longest run is of heads or tails.

Is there code that could be used to calculate this?

So far I have:

x <- c(0, 1) # heads = 0, tails = 1

p = permutations(n = 2, r = 10, v = x, repeats.allowed = T)

p.df = as.data.frame(p)

To create a data frame of all possible combinations. Now I need to find the longest run of consecutive zeros or ones in each row and count how many rows have the same longest run.

One Answer

Aha! Figured it out!

Here is the code I have:

install.packages('gtools')
library(gtools)

x <- c(0, 1) # heads = 0, tails = 1

p = permutations(n = 2, r = 10, v = x, repeats.allowed = T)

p.df = as.data.frame(t(p))

q = apply(p.df, 2, function(x){
max(rle((x == 0))$lengths)
}
)

length(which(q == 10)) #where q == the length of the longest run
```

Answered by A.May on December 16, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP