> 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/03-bernoulli.md).

# Bernoulli

* Another simple RV model is the Bernoulli RV.
* You run an experiment with two possible outcomes $$Y$$ (yes) or $$N$$ (no).
* We have $$X=1$$ if outcome is $$Y$$ and $$X=0$$ if outcome is $$N$$.
* Define $$p\in \[0,1]$$ as probability of outcome $$Y$$ and thus probability of $$N$$ is $$(1-p)$$.
* Domain is thus $$\mathcal{D}={0,1}$$ and pdf is:

  $$
  \begin{aligned}
  f(x)=\left{\begin{array}{cc}(1-p) &\textrm{if };x=0\ p&\textrm{if };x=1\end{array}\right.
  \end{aligned}
  $$

  Pdf can be written in compact form:

  $$
  f(x)=p^{\mathbf{1}\[x=1]}(1-p)^{\mathbf{1}\[x=0]}.
  $$
* We say $$X$$ follows a Bernoulli model as $$X\sim \mathcal{B}(p)$$, where $$p$$ is the parameter.
* Mean and variance of this RV are:

  $$
  \begin{aligned}
  \mathbb{E}*X&=\sum*{x\in \mathcal{D}}x\cdot f(x)=0\cdot(1-p)+1\cdot p=p\\
  \mathbb{V}*X&=\sum*{x\in \mathcal{D}}(x-\mathbb{E}\_X)^2\cdot f(x)= p\cdot (1-p).
  \end{aligned}
  $$
* Bernoulli RV is used in sampling and forms basis of more sophisticated RVs.

<figure><img src="https://1753452229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FakKE106umUCSWFtTnl31%2Fuploads%2Fgit-blob-2dc0417dfa85504344a0a941a2085cc0711742b5%2Fch2_pdfcdfbernoulli.png?alt=media" alt="" width="489"><figcaption><p>Pdf and cdf for Bernoulli random variable with 𝑝 = 0.3.</p></figcaption></figure>

<details>

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

Pdf and cdf of a Bernoulli RV $$X\sim\mathcal{B}(p)$$ with $$p=0.3$$ (Victor Z and J E Umaña, UW-Madison, 2024). Python names: `p`, domain `D`, pdf `f`, cdf `F`, realizations `x_omega`.

**Note:** This notebook reproduces Example 2.3 (Section 2.1.2) of *Statistics for Chemical Engineers*: the Bernoulli RV B(0.3).

```python
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import bernoulli

p = 0.3
D = np.array([0, 1])
f = np.array([1 - p, p])        # f(x) = p^1[x=1] (1-p)^1[x=0]

x = np.linspace(-1, 2, 1000)
F = np.array([np.sum(f[D <= x_k]) for x_k in x])

np.random.seed(0)
S = 100
x_omega = bernoulli.rvs(p, size=S)

plt.figure(figsize=(10, 8))
plt.subplot(2, 2, (1, 2))
plt.plot(x_omega, 'ko', markerfacecolor='w', markersize=4)
plt.xlabel(r'Outcome $\omega$', fontsize=14)
plt.ylabel(r'$x_\omega$', fontsize=14)
plt.grid(True)
plt.yticks([-0.5, 0, 0.5, 1, 1.5])
plt.axis([0, S, -0.5, 1.5])

plt.subplot(2, 2, 3)
plt.vlines(D, 0, f, colors='k', linestyles='--')
plt.scatter(D, f, marker='o', facecolors='w', edgecolors='k', zorder=3)
plt.grid(True)
plt.xlabel('$x$', fontsize=14)
plt.ylabel('$f(x)$', fontsize=14)
plt.axis([-0.5, 1.5, -0.02, 1.02])

plt.subplot(2, 2, 4)
plt.step(x, F, 'k-', where='post')
plt.scatter(D, np.cumsum(f), marker='o', facecolors='w', edgecolors='k', zorder=3)
plt.grid(True)
plt.xlabel('$x$', fontsize=14)
plt.ylabel('$F(x)$', fontsize=14)
plt.axis([-0.5, 1.5, -0.02, 1.02])
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-357ce1c239b7e86648f7e78ce5afb2218712f930%2Fch2_compare_bernoulli-03-0.png?alt=media" alt="" width="520"><figcaption></figcaption></figure>

**Note:** The domain is {0, 1}, with f(1) = 0.3 and f(0) = 0.7; note how the observations land unevenly across the two values.

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

</details>

## Example Bernoulli

{% hint style="danger" icon="flask" %}

* Consider the previous machine status problem and the following questions:
  * Is it suitable to model the machine status as a Bernoulli RV?
  * If so, what would be its parameters?
  * What makes this different from a uniform RV?
* Define status as $$X=0$$ when it fails and $$X=1$$ when it works (domain is $$\mathcal{D}={0,1}$$).
* For a Bernoulli RV, we must have that $$\mathbb{P}(X=0)=1-p$$ and $$\mathbb{P}(X=1)=p$$.
* From our data we have:

  $$
  \begin{aligned}
  \hat{\mathbb{P}}(X=0)&=79/156=0.5064\\
  \hat{\mathbb{P}}(X=1)&=(156-79)/156=0.4936.
  \end{aligned}
  $$
* Machine status thus seems to behave as a Bernoulli RV $$\mathcal{B}(0.5)$$.
* In this example, uniform and Bernoulli models are identical.
* Uniform model is a generalization of Bernoulli model.
  {% endhint %}

#### Python implementation: Machine status as a Bernoulli RV

For a Bernoulli RV $$X\sim\mathcal{B}(p)$$, $$\mathbb{E}\_X=p$$, so the empirical mean $$\hat{\mathbb{E}}\_X$$ estimates $$p$$; here $$X=1$$ means the machine works.

```python
p_hat = np.mean(x_omega)                  # E_hat[X] = p_hat
print(f"p_hat = {p_hat:.4f}  ->  X ~ B({p_hat:.2f})")
print(f"E_X = p = {p_hat:.4f},  V_X = p(1-p) = {p_hat*(1-p_hat):.4f}")
```

```
p_hat = 0.4936  ->  X ~ B(0.49)
E_X = p = 0.4936,  V_X = p(1-p) = 0.2500
```

**Note:** As in the book, the machine failed in 79 of 156 weeks: the empirical probabilities (0.5064 and 0.4936) suggest that its status behaves as a uniform RV U(0,1).
