From 0be5f5bd5034385e2769c1d4e5b771e1bd6dafda Mon Sep 17 00:00:00 2001 From: Longye Tian Date: Wed, 20 Mar 2024 22:05:20 +1100 Subject: [PATCH 1/2] [prob_dist] Update Bernoulli distribution section This pull request resolves issue #403. --- lectures/prob_dist.md | 65 +++++++++++-------------------------------- 1 file changed, 16 insertions(+), 49 deletions(-) diff --git a/lectures/prob_dist.md b/lectures/prob_dist.md index 3fb61a39..ba3bc7b7 100644 --- a/lectures/prob_dist.md +++ b/lectures/prob_dist.md @@ -155,73 +155,40 @@ 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: +$$ +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. -We can import the uniform distribution on $S = \{1, \ldots, n\}$ from SciPy like so: +We can import the Bernoulli distribution on $S = \{0,1\}$ from SciPy like so: ```{code-cell} ipython3 -n = 10 -u = scipy.stats.randint(1, n+1) +p = 0.4 +u = scipy.stats.bernoulli(p) ``` -Here's the mean and variance +Here's the mean and variance: ```{code-cell} ipython3 u.mean(), u.var() ``` -The formula for the mean is $(n+1)/2$, and the formula for the variance is $(n^2 - 1)/12$. +The formula for the mean is $p$, and the formula for the variance is $p(1-p)$. -Now let's evaluate the PMF +Now let's evaluate the PMF: ```{code-cell} ipython3 +u.pmf(0) u.pmf(1) ``` -```{code-cell} ipython3 -u.pmf(2) -``` - - -Here's a plot of the probability mass function: - -```{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()`. -``` - - #### Binomial distribution From c2c1c8e2c8b19078833f74fb3bc360bac17548a8 Mon Sep 17 00:00:00 2001 From: Longye Tian Date: Wed, 20 Mar 2024 22:30:46 +1100 Subject: [PATCH 2/2] Update prob_dist.md --- lectures/prob_dist.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lectures/prob_dist.md b/lectures/prob_dist.md index ba3bc7b7..d56ec24d 100644 --- a/lectures/prob_dist.md +++ b/lectures/prob_dist.md @@ -156,6 +156,7 @@ Check that your answers agree with `u.mean()` and `u.var()`. #### Bernoulli distribution Another useful distribution is the Bernoulli distribution on $S = \{0,1\}$, which has PMF: + $$ p(x_i)= \begin{cases} @@ -163,6 +164,7 @@ 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. We can import the Bernoulli distribution on $S = \{0,1\}$ from SciPy like so: