# [R] runs of heads when flipping a coin

jgarcia at ija.csic.es jgarcia at ija.csic.es
Thu Oct 9 20:18:11 CEST 2008

```It seems to me that you are asking for:

> hhh <- 0.1
> sum(dbinom(x=0:50,size=500,prob=hhh))
[1] 0.5375688

> You can use 'sample' and 'rle':
>
>> x <- sample(c("H","T"), 500, replace=TRUE, prob=c(.95, .05))
>> as.data.frame(unclass(rle(x)))
>    lengths values
> 1       12      H
> 2        1      T
> 3       10      H
> 4        1      T
> 5        5      H
> 6        2      T
> 7       26      H
> 8        1      T
> 9       17      H
> 10       1      T
> 11      54      H
> 12       1      T
> 13      23      H
> 14       1      T
> 15       7      H
> 16       1      T
> 17       2      H
>
>
> On Thu, Oct 9, 2008 at 1:16 PM, Harvey <modelers at gmail.com> wrote:
>> 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.
>>
>>
>> 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
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem that you are trying to solve?
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help