# Negative Binomial

A negative binomial experiment is a statistical experiment that has the following properties:

* The experiment consists of $$n$$ repeated trials.
* The trials are independent.
* The outcome of each trial is either *success* ($$s$$) or *failure* ($$f$$).
* $$P(s)$$ is the same for every trial.
* The experiment continues until $$x$$ successes are observed.&#x20;

If $$X$$ is the number of experiments until the $$x^{th}$$ success occurs, then $$X$$ is a discrete random variable called a *negative binomial*.&#x20;

#### [ Negative Binomial Distribution](https://en.wikipedia.org/wiki/Negative_binomial_distribution)

Consider the following probability mass function:

$$b \cdot (x, n, p) = \binom{n-1}{x-1} \cdot p^x \cdot q^{(n-x)}$$

The function above is *negative binomial* and has the following properties:

* The number of successes to be observed is $$x$$.
* The total number of trials is $$n$$.
* The probability of success of $$1$$ trial is $$p$$.
* The probability of failure of $$1$$ trial , where $$q=1-p$$.
* &#x20;is the *negative binomial probability*, meaning the probability of having $$x-1$$ successes after $$n-1$$ trials and having $$x$$ successes after $$n$$ trials.&#x20;

**Note:** Recall that $$\binom{n}{x}=\frac{n!}{x!(n-x)!}$$. For further review, see the [Combinations and Permutations Tutorial](https://www.hackerrank.com/challenges/s10-mcq-5/tutorial).&#x20;

#### [Geometric Distribution](https://en.wikipedia.org/wiki/Geometric_distribution)

The *geometric distribution* is a special case of the negative binomial distribution that deals with the number of Bernoulli trials required to get a success (i.e., counting the number of failures before the first success). Recall that $$X$$ is the number of successes in  independent Bernoulli trials, so for each $$i$$ (where $$1\leq i \leq n$$):

$$X\_i = \begin{cases} 1, & \text{if the } i^{th}\text{ trail is a success} \ 0, & \text{otherwise } \end{cases}$$

The geometric distribution is a negative binomial distribution where the number of successes is $$1$$. We express this with the following formula:

$$g(n, p) = q^{(n-1)} \cdot p$$

#### Example

Bob is a high school basketball player. He is a $$70%$$ free throw shooter, meaning his probability of making a free throw is $$0.70$$. What is the probability that Bob makes his first free throw on his fifth shot?&#x20;

For this experiment, $$n=5$$, $$p=0.7$$ and $$q=0.3$$. So, $$\newline g(n=5, p=0.7) = 0.3^4 0.7 = 0.00567$$

```
# The probability that a machine produces a defective product 
# is 1/3. What is the probability that the 1st defect is found 
# during the 5th inspection?
#
# Answer 0.066
print(round((((1-ratio)**4)*ratio), 3))

# What is the probability that the 1st defect is found during 
# the first 5th inspections?
def geo_dist(i, r):
    return ((1-r)**i)*r

r = 1/3
t = 5
print(round(sum([geo_dist(i, r) for i in range(0, t)]), 3))

```
