Showing posts with label Probability. Show all posts
Showing posts with label Probability. Show all posts

Monday, July 20, 2009

Probability exercise: negative binomial distribution

What is the probability you get the 4th cross before the 3rd head, flipping a coin?

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.

Sunday, July 19, 2009

A probability exercise on the Bernoulli distribution

What is the probability, flipping a coin 8 times, to obtain the sequence HHTTTHTT? (H = head; T= tail)

The theory teaches us that to solve this question, we can simply use the following formula:

$$f(x)=P(X=x)=B(n,p)=\begin{pmatrix}n\\ x \end{pmatrix} \cdot p^x \cdot q^{n-x}=\frac{n!}{x!(n-x)!}$$

To solve a problem like this, we can use in R the function dbinom(x, n, p). The coin flipping follow a binomial distribution, in which every event can be H or T. Suppose that T is the number of successes x (in this case x = 5), while n is the number independet experiments (in this case n = 8). The probability of success is p = 0.5. Put these data into R and get the answer:


dbinom(5, 8, 0.5)
[1] 0.21875


The probability of obtaining that particular sequence is equal to 21,875%.
What probability we would have obtained if we had chosen H as the success (ie by imposing x = 3)?