> For the complete documentation index, see [llms.txt](https://zavalab.gitbook.io/statschemebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zavalab.gitbook.io/statschemebook/chapter-2-univariate-random-variables/05-binomial.md).

# Binomial

* In Bernoulli setting, a trial has possible outcomes $$Y$$ (yes) or $$N$$ (no).
* Imagine we perform a *sequence* of $$n$$ Bernoulli trials and record number of times that outcomes are of type $$Y$$. Each trial is *independent* of the others.
* Binomial RV $$X$$ counts number of trials of type $$Y$$ in our $$n$$ trials.
* Domain is $$\mathcal{D}={0,1,...,n}$$ and probability that number of $$Y$$ trials is $$x$$ is:

  $$
  \begin{aligned}
  f(x)=\left(\begin{array}{c}n\ x\end{array}\right)\cdot p^x\cdot (1-p)^{(n-x)},\quad x\in \mathcal{D}.
  \end{aligned}
  $$
* We denote Binomial RV as $$X\sim \textrm{Bi}(n,p)$$.
* Expected value and variance are:

  $$
  \begin{aligned}
  \mathbb{E}\_X&=n\cdot p,\qquad \mathbb{V}\_X=n\cdot p\cdot(1-p).
  \end{aligned}
  $$
* Binomial RV is a generalization of Bernoulli RV; $$\textrm{Bi}(1,p)=\mathcal{B}(p)$$.
* Consider sequence of independent Bernoulli RVs $$X\_1,X\_2,...,X\_n$$ (each with probability $$p$$), then $$X=\sum\_{i=1}^nX\_i$$ is a binomial RV $$X\sim \textrm{Bi}(n,p)$$.
* Binomial RV is limiting case of a hypergeometric RV in the sense that:

  $$
  \lim\_{N\_t\to\infty} \mathcal{H}(N\_y,N\_t,n,p)=\textrm{Bi}(n,p).
  $$

  i.e., binomial setting is equivalent to hypergeometric setting when size of the population is large.
* In hypergeometric setting, we choose $$n$$ items at once while in binomial setting we choose $$n$$ items by choosing one item at a time (resetting system each time).
* Intuitively, when population $$N\_t$$ is large, size of the sample does not matter as much and picking $$n$$ items at once or as an independent sequence is equivalent.

<figure><img src="https://1753452229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FakKE106umUCSWFtTnl31%2Fuploads%2Fgit-blob-1183b2dae8a0a5737e9139335871a2ec5ac556c7%2Fch2_pdfcdfbinomial.png?alt=media" alt="" width="489"><figcaption><p>Pdf and cdf for binomial RV with 𝑛 = 10 and 𝑝 = 0.3 (top). Binomial pdf for different 𝑝 (bottom-left). Convergence of hypergeometric pdf to binomial pdf for 𝑁ₜ = 1000 (bottom-right).</p></figcaption></figure>

<details>

<summary>Python: reproduce this figure (ch2_compare_binomial.ipynb)</summary>

Pdf and cdf of a binomial RV $$X\sim\mathrm{Bi}(n,p)$$, and convergence of $$\mathcal{H}(N\_y,N\_t,n,p)$$ to $$\mathrm{Bi}(n,p)$$ for large $$N\_t$$ (Victor Z and J E Umaña, UW-Madison, 2024). Python names: `n`, `p`, domain `D`, pdf `f`, cdf `F`, population `N_t`, `N_y`.

**Note:** This notebook reproduces Example 2.7 (Section 2.1.4) of *Statistics for Chemical Engineers*: the binomial RV, and its relation to the hypergeometric RV.

```python
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import binom, hypergeom

n, p = 10, 0.3
D = np.arange(0, n + 1)
f = binom.pmf(D, n, p)     # C(n,x) p^x (1-p)^(n-x)
F = binom.cdf(D, n, p)

plt.figure(figsize=(10, 8))
plt.subplot(221)
plt.vlines(D, 0, f, colors='k', linestyles='--')
plt.scatter(D, f, marker='o', facecolors='w', edgecolors='black', zorder=3)
plt.grid(True)
plt.xlabel('$x$', fontsize=14)
plt.ylabel('$f(x)$', fontsize=14)

plt.subplot(222)
plt.step(D, F, where='post', color='black')
plt.scatter(D, F, marker='o', facecolors='w', edgecolors='black', zorder=3)
plt.grid(True)
plt.xlabel('$x$', fontsize=14)
plt.ylabel('$F(x)$', fontsize=14)
plt.axis([-1, 10, 0, 1.02])

plt.subplot(223)
for p_k, mk in zip([0.95, 0.75, 0.5], ["o-", "x-", "d-"]):
    plt.plot(D, binom.pmf(D, n, p_k), mk, color='k', markerfacecolor='w', label=f'$p={p_k}$')
plt.grid(True)
plt.xlabel('$x$', fontsize=14)
plt.ylabel('$f(x)$', fontsize=14)
plt.axis([0, 10, 0, 0.62])
plt.legend(loc='upper left', fontsize=10)

# hypergeometric H(N_y, N_t, n, p) approaches Bi(n, p) when N_t is large
plt.subplot(224)
p_H, N_t = 0.25, 1000
N_y = round(p_H * N_t)
D_H = np.arange(max(n + N_y - N_t, 0), min(n, N_y) + 1)
plt.plot(D_H, hypergeom.pmf(D_H, N_t, N_y, n), 'k--', linewidth=0.5, label=r'$\mathcal{H}(N_y,N_t=1000,n,p)$')
plt.plot(D, binom.pmf(D, n, p_H), 'ko', markerfacecolor='w', label=r'$\mathrm{Bi}(n,p)$')
plt.grid(True)
plt.xlabel('$x$', fontsize=14)
plt.ylabel('$f(x)$', fontsize=14)
plt.axis([0, 10, 0, 0.3])
plt.legend(loc='upper right', fontsize=10)
plt.tight_layout()
plt.show()
```

<figure><img src="https://1753452229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FakKE106umUCSWFtTnl31%2Fuploads%2Fgit-blob-86ff93f2afb22ea276382c49c15d8091d2700c94%2Fch2_compare_binomial-03-0.png?alt=media" alt="" width="520"><figcaption></figcaption></figure>

**Note:** As in the book, the hypergeometric pdf for a large population (N\_t = 1000) is nearly identical to the binomial pdf: the hypergeometric RV converges to a binomial RV.

Open in Colab: [`ch2_compare_binomial.ipynb`](https://colab.research.google.com/github/vzavala/StatsBook/blob/master/Python/ch2_compare_binomial.ipynb) · MATLAB live script: [`ch2_compare_binomial.mlx`](https://github.com/vzavala/StatsBook/blob/master/Matlab-Live/ch2_compare_binomial.mlx)

</details>

## Example Binomial

{% hint style="danger" icon="flask" %}
Consider machine status problem; recall we have established that status is $$\mathcal{B}(0.5)$$.

* What is probability of finding 2 failures in a sequence of 10 times?
* What is probability of finding 10 failures in a sequence of 10 times?
* What is average number of failures in a sequence of 10 times?
  {% endhint %}

<figure><img src="https://1753452229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FakKE106umUCSWFtTnl31%2Fuploads%2Fgit-blob-11745896c090b261e8a11e1187ebb377ae86cb53%2Fch2_unifprocess.png?alt=media" alt="" width="489"><figcaption><p>Failures (dark dots) recorded over three years.</p></figcaption></figure>

{% hint style="danger" icon="flask" %}
Status is a Bernoulli RV with $$p=0.5$$ and thus the number of failures in an $$n$$-sequence is $$\textrm{Bi}(n,p)$$.

* Probability of finding 2 failures in a sequence of $$n=10$$ times is:

  $$
  \mathbb{P}(X=2)=f(2)=0.0439.
  $$
* Probability of finding 10 failures is:

  $$
  \mathbb{P}(X=10)=f(10)=9.76\times 10^{-4}
  $$
* Average number of failures is:

  $$
  \mathbb{E}\_X=n\cdot p=10\cdot 0.5=5
  $$
  {% endhint %}

#### Python implementation

{% hint style="success" %}
Notebook [`ch2_example_binomial.ipynb`](https://colab.research.google.com/github/vzavala/StatsBook/blob/master/Python/ch2_example_binomial.ipynb) — open it in Colab and run the cells alongside this section.

MATLAB live script: [`ch2_example_binomial.mlx`](https://github.com/vzavala/StatsBook/blob/master/Matlab-Live/ch2_example_binomial.mlx)
{% endhint %}

Failures in a sequence of $$n=10$$ weeks (Victor Z and J E Umaña, UW-Madison, 2024). The weekly status is $$\mathcal{B}(p)$$ with $$p=0.5$$, so the number of failures in $$n$$ weeks is $$X\sim\mathrm{Bi}(n,p)$$ with pdf $$f(x)=\binom{n}{x}p^x(1-p)^{n-x}$$. Python names: `n`, `p`, pdf `f(x)`, `E_X`.

**Note:** This notebook reproduces Example 2.8 (Section 2.1.4) of *Statistics for Chemical Engineers*: number of failures in a sequence of n weeks, with p = 0.5.

```python
from scipy.stats import binom

n, p = 10, 0.5

def f(x):
    """Binomial pdf f(x) = C(n, x) p^x (1-p)^(n-x)."""
    return binom.pmf(x, n, p)

print(f"P(X=2)  = f(2)  = {f(2):.4f}")
print(f"P(X=10) = f(10) = {f(10):.3g}")
E_X = n * p
print(f"E_X = n p = {E_X:g}")
```

```
P(X=2)  = f(2)  = 0.0439
P(X=10) = f(10) = 0.000977
E_X = n p = 5
```

**Note:** As in the book, the machine is equally likely to be in failure or normal mode, so it is unlikely to be in either mode all n times: P(X = 0) = P(X = 10) = 9.76e-4.
