(This article was first published on

What is the probability you get the 4th cross before the 3rd head, flipping a coin?**Statistic on aiR**, and kindly contributed to R-bloggers)The mathematical formula for solving this exercise, which follows a

**negative binomial distribution**, is:

$$f(x)=P(X=x)=\begin{pmatrix} x+y-1\\ y-1 \end{pmatrix} \cdot p^x \cdot (1-p)^y$$

To solve a problem like this, the number of experiments is not significant, since it is sufficient to know the probability of individual shots that are done and you want to combine, to obtain the required probability. To solve this problem in R, we can use the function

`dnbinom(x, y, p)`

. This distribution allows to calculate the probability that a number of *failures x*occurs before

*y-th success*, in a

**sequence of Bernoulli trials**, for which the probability of individual success is

*p*.

dnbinom(4,3,0.5)

[1] 0.1171875

The probability of obtaining the fourth cross before the third head (and then after two head) is equal to 11,72%.

It may seem a strange result, but to convince us about the accuracy of this function of R, let us consider this other problem: what are the chances of leaving the first, second, third, ... the 25th head before the second cross?

We can obtain a histogram in R, which shows what is required, in this manner:

barplot(dnbinom(1:25,2,0.5), col="grey", names.arg=1:25)

Observe that the probability to get the first head before the second cross is 0.25, which is the product

`0.5 x 0.5`

(probability to get H after T). As the number of flips have been going on, the probability falls more and more.
To

**leave a comment**for the author, please follow the link and comment on his blog:**Statistic on aiR**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...