## The model

Suppose weâ€™re interested in counting the number of successes in \(n\) independent trials of a success/failure random experiment, where the probability of success is \(p \in (0,1)\). Some real world examples of this are:

- counting the number of heads in \(n\) coin tosses (here \(p = 1/2\) for a fair coin)
- counting the number of times 3 comes up in \(n\) die rolls (\(p = 1/6\))
- counting the number of defective components in a manufacturing process that produces \(n\) components in a day (\(p = 1/1000\) in an earlier example of this type)

## The distribution

Each trial of the experiment can be modeled as independent Bernoulli random variables \(X_1,\ldots,X_n\) where \[P(X_i = 1) = p, \quad P(X_i = 0) = 1-p\] for each \(i = 1,\ldots,n\). Then the number of succeses is \(X = X_1 + \cdots + X_n\).

The random variable \(X\) is called a **binomial** random variable. Its distribution is given by \[P(X = k) = \binom{n}{k} p^k(1-p)^{n-k}, \quad k=0,\ldots,n.\] We write \[X \sim \mathrm{Bin}(n,p)\] to denote a binomial random variable with parameters \(n\) and \(p\).

## Visualization

The following gives a visualization of the probabilities associated with \(X \sim \mathrm{Bin}(n,p)\) random variable with some specific choices for \(n\) and \(p\). Along the \(x\)-axis are the possible values \(X\) can take, and along the \(y\)-axis are the associated probabilities.

```
n <- 20
p <- 1/3
barplot(dbinom(0:n,n,p), names = 0:n)
```

Here is another with different choices for \(n\) and \(p\).

```
n <- 8
p <- 1/2
barplot(dbinom(0:n,n,p), names = 0:n)
```

## Computation

We can use the R function `pbinom`

to do computation of probabilities like \[P(X \leq m) = P(X = 0) + P(X = 1) + \cdots P(X = m)\] which would otherwise be tedious.

For example, suppose we flip a biased coin, with head probability 1/3, twenty times. The following command lets us compute \(P(X \leq 10)\) the probability of getting at most 10 heads.

```
n <- 20
p <- 1/3
pbinom(10, 20, 1/3)
```

`## [1] 0.9623634`