[R] runs of heads when flipping a coin

Moshe Olshansky m_olshansky at yahoo.com
Fri Oct 10 04:45:11 CEST 2008


First of all, we must define what is a run of length r: is it a tail, then EXACTLY r heads and a tail again or is it AT LEAST r heads.
Let's assume that we are looking for a run of EXACTLY r heads (and we toss the coin n times).
Let X[1],X[2],...,X[n-r+1] be random variables such that Xi = 1 if there is a run of r heads starting at place i and 0 otherwise. Then the number of runs of length r is just sum(X), so the expected number of runs of length r is sum(E(X)) = sum(P(X[i]=1)).
Now, for i=2,3,...,n-r P(X[i] = 1) = (1-h)*h^r*(1-h) and also P(X[1] = 1) = h^r*(1-h) and P(X[n-r+1] = 1) = (1-h)*h^r, so that the expected number of runs of length r is
(1-h)*h^r*(2 + (n-r-1)*(1-h))

As to the distribution of the number of such runs, this is a much more difficult question (as mentioned by some other people).


--- On Fri, 10/10/08, Harvey <modelers at gmail.com> wrote:

> From: Harvey <modelers at gmail.com>
> Subject: [R] runs of heads when flipping a coin
> To: r-help at r-project.org
> Received: Friday, 10 October, 2008, 4:16 AM
> Can someone recommend a method to answer the following type
> of question:
> 
> Suppose I have a coin with a probability hhh of coming up
> heads (and 1-hhh
> of coming up tails)
> I plan on flipping the coin nnn times (for example, nnn =
> 500)
> What is the expected probability or frequency of a run of
> rrr heads* during
> the nnn=500 coin flips?
> Moreover, I would probably (excuse the pun) want the answer
> for a range of
> rrr values, for example rrr = 0:50
> 
> Of course I am more interested in an analytical solution
> than a monte carlo
> simulation solution.
> 
> Thanks in advance,
> 
> Harvey
> 
> * OR "a run of rrr heads or more ..."
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained,
> reproducible code.



More information about the R-help mailing list