# The Binomial Distribution

This is the first of a series of articles I’ll be posting here about some of the most common probability distributions.

## Bernoulli random variable

It is a variable that has 2 possible outcomes: “success”, or “failure”. Success occurs with probability $$p$$ and failure with probability $$q=1-p$$

The distribution of heads and tails in coin tossing is an example of a Bernoulli distribution with $$p=q=1/2$$. The Bernoulli distribution is the simplest discrete distribution, and it the building block for other more complicated discrete distributions. The distributions of a number of variate types defined based on sequences of independent Bernoulli trials that are curtailed in some way are summarized in the following list:

• binomial distribution: number of successes in n trials
• geometric distribution: number of failures before the first success
• negative binomial distribution: number of failures before the xth success

## Binomial probability distribution

Suppose that n independent Bernoulli trials each one having probability of success p are to be performed. Let X be the number of successes among the n trials.

We say that X follows the binomial probability distribution with parameters $B(n,p)$

In plain english, the binomial distribution describes the outcome of n independent trials in an experiment. Each trial is assumed to have only two outcomes, either success or failure.

• Probability mass function of X:

where $\binom{n}{x}=\frac{n!}{(n-x)!x!}$

### Binomial Distribution Properties

• Mean: $$\mu = np$$
• Variance: $$\sigma^2=npq$$
• Standard deviation: $$\sigma =\sqrt{npq}$$
• Skewness: $$\alpha_3=\frac{q-p}{\sqrt{npq}}$$
• Kurtosis: $$\alpha_4= 3 + \frac{1-6pq}{npq}$$
• Moment generating function: $$M(t) =(q+pe^t)^n$$
• Characteristic function: $$\phi(\omega)=(q+pe^{i\omega})^n$$

Note: The special case of a binomial distribution with $$n=1$$ is also called the Bernoulli distribution.

### Binomial Distribution in R

• Generate Binomial random numbers:

In R we use the function rbinom(e,n,p) to generate binomial random numbers.

Parameters:

e: number of experiments you want to simulate

n: number of independent Bernoulli trials in each experiment

p: define the probability of success

Example1: A fair coin is tossed. Let the variable x take values 1 and 0 according to as the toss results in “Head”” or “Tail”. Then, X is a Bernoulli variable with parameter p=1/2. Here, X denotes the number of heads obtained in the toss. Probability of success 1/2 and the probability of failure 1/2

Example2: Run the above experiment 10 times and calculated the expected number of heads. Well, we don’t really need to do the simulation as we already know the E[X] for a binomial distribution is np. So the expected number of heads in 10 experiments is:

let’s run the simulation and calculate the average:

Close enough. To understand why we got 6 instead 5 we need to understand “Law of Large Numbers”. This law explains that in the long run it becomes extremely likely that the proportion of success, X/n, will be as close as you like to the probability of success in a single trial, p.

Let’s run again the same example but 10000 times

let’s run the simulation and calculate the average:

So, 0.4959 is very close to our original p=0.5

Example3: Generate 100 samples of binomial(20,.5). Print the first 3 experiments

The interpretation of the above result is:

• In our first experiment we got 13 heads out 20

• In our second experiment we got 9 heads out 20

• In our third experiment we got 9 heads out 20

• Using probability distribution function pbinom() density function dbinom()

Let’s illustrate the above functions with an example:

Example4: What’s the probability of getting exactly 2 heads in 6 tosses of a fair coin?

Let’s do it the hard way: manually

The probability of getting exactly 2 heads in 6 tosses is: 0.23461

Using dbinom() is much simpler: dbinom() calculates the exact probability of success for every x.

Alternatively, we can use the cumulative probability function for binomial distribution pbinom()

Example5: Find the probability that in five tosses of a fair die, a 3 will appear

Let X the number of times a 3 appear in five tosses of a fair die

a) twice: $$P(X=2)$$

b) at most once: $$P(X\leqslant{1})$$

c) at least two times: $$P(X\geqslant{2})=1 -P(X\lt{2})$$

Example6: if 20% of the bolts produced by a machine are defective, determine the probability that out of 4 bolts chosen at random:

a) 1 defective: $$P(X=1)$$

b) 0 defectives $$P(X=0)$$

c) Less than 2 defective: $$P(X \lt 2) = P(X\leqslant{1})$$

Blogs I follow...