Skip to content

[prob_dist] Update Bernoulli distribution section #406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 18 additions & 49 deletions lectures/prob_dist.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,74 +155,43 @@ Check that your answers agree with `u.mean()` and `u.var()`.

#### Bernoulli distribution

Another useful (and more interesting) distribution is the Bernoulli distribution
Another useful distribution is the Bernoulli distribution on $S = \{0,1\}$, which has PMF:

We can import the uniform distribution on $S = \{1, \ldots, n\}$ from SciPy like so:

```{code-cell} ipython3
n = 10
u = scipy.stats.randint(1, n+1)
```
$$
p(x_i)=
\begin{cases}
p & \text{if $x_i = 1$}\\
1-p & \text{if $x_i = 0$}
\end{cases}
$$

Here $x_i \in S$ is the outcome of the random variable.

Here's the mean and variance
We can import the Bernoulli distribution on $S = \{0,1\}$ from SciPy like so:

```{code-cell} ipython3
u.mean(), u.var()
p = 0.4
u = scipy.stats.bernoulli(p)
```

The formula for the mean is $(n+1)/2$, and the formula for the variance is $(n^2 - 1)/12$.


Now let's evaluate the PMF
Here's the mean and variance:

```{code-cell} ipython3
u.pmf(1)
u.mean(), u.var()
```

```{code-cell} ipython3
u.pmf(2)
```
The formula for the mean is $p$, and the formula for the variance is $p(1-p)$.


Here's a plot of the probability mass function:
Now let's evaluate the PMF:

```{code-cell} ipython3
fig, ax = plt.subplots()
S = np.arange(1, n+1)
ax.plot(S, u.pmf(S), linestyle='', marker='o', alpha=0.8, ms=4)
ax.vlines(S, 0, u.pmf(S), lw=0.2)
ax.set_xticks(S)
plt.show()
```


Here's a plot of the CDF:

```{code-cell} ipython3
fig, ax = plt.subplots()
S = np.arange(1, n+1)
ax.step(S, u.cdf(S))
ax.vlines(S, 0, u.cdf(S), lw=0.2)
ax.set_xticks(S)
plt.show()
```


The CDF jumps up by $p(x_i)$ and $x_i$.


```{exercise}
:label: prob_ex2

Calculate the mean and variance for this parameterization (i.e., $n=10$)
directly from the PMF, using the expressions given above.

Check that your answers agree with `u.mean()` and `u.var()`.
u.pmf(0)
u.pmf(1)
```



#### Binomial distribution

Another useful (and more interesting) distribution is the **binomial distribution** on $S=\{0, \ldots, n\}$, which has PMF
Expand Down