@@ -19,7 +19,7 @@ kernelspec:
19192020This lecture illustrates two distinct interpretations of a **probability distribution**
212122-* A frequentist interpretation as **relative frequencies** anticipated to occur in a large IID. sample
22+* A frequentist interpretation as **relative frequencies** anticipated to occur in a large IID sample
23232424* A Bayesian interpretation as a **personal opinion** (about a parameter or list of parameters) after seeing a collection of observations
2525@@ -68,7 +68,7 @@ Consider the following classic example.
6868The random variable $X $ takes on possible values $k = 0, 1, 2, \ldots, n$ with probabilities
69697070$$
71-\mathbb{P}\{X = k \mid \theta\} =
71+p(k \mid \theta) := \mathbb{P}\{X = k \mid \theta\} =
7272\left(\frac{n!}{k! (n-k)!} \right) \theta^k (1-\theta)^{n-k}
7373$$
7474@@ -106,7 +106,7 @@ f_k^I = \frac{\textrm{number of samples of length n for which } \sum_{h=1}^n y_h
106106 I}
107107$$
108108109-The probability $\mathbb{P}\{X = k \mid \theta\}$ answers the following question:
109+The probability $p(k \mid \theta)$ answers the following question:
110110111111* As $I$ becomes large, in what fraction of $I$ independent draws of $n$ coin flips should we anticipate $k$ heads to occur?
112112@@ -118,9 +118,9 @@ As usual, a law of large numbers justifies this answer.
1181181. Please write a Python class to compute $f_k^I$
1191191201202. Please use your code to compute $f_k^I, k = 0, \ldots , n$ and compare them to
121- $\mathbb{P}\{X = k \mid \theta\}$ for various values of $\theta, n$ and $I$
121+ $p(k \mid \theta)$ for various values of $\theta, n$ and $I$
122122123-3. With the Law of Large Numbers in mind, use your code to describe the relationship between $f_k^I$ and $\mathbb{P}\{X = k \mid \theta\}$ as $I$ grows
123+3. With the Law of Large Numbers in mind, use your code to describe the relationship between $f_k^I$ and $p(k \mid \theta)$ as $I$ grows
124124```
125125126126```{solution-start} pm_ex1
@@ -208,12 +208,12 @@ for i in range(n_thetas):
208208```{code-cell} ipython3
209209fig, ax = plt.subplots(figsize=(8, 6))
210210ax.grid()
211-ax.plot(thetas, P, 'k-.', label='Theoretical')
212-ax.plot(thetas, f_kI, 'r--', label='Fraction')
213-ax.set_title(r'Comparison with different $\theta$',
211+ax.plot(thetas, P, '-.', label='theoretical')
212+ax.plot(thetas, f_kI, '--', label='fraction')
213+ax.set_title(r'comparison with different $\theta$',
214214 fontsize=16)
215215ax.set_xlabel(r'$\theta$', fontsize=15)
216-ax.set_ylabel('Fraction', fontsize=15)
216+ax.set_ylabel('fraction', fontsize=15)
217217ax.tick_params(labelsize=13)
218218ax.legend()
219219plt.show()
@@ -243,12 +243,12 @@ for i in range(n_ns):
243243```{code-cell} ipython3
244244fig, ax = plt.subplots(figsize=(8, 6))
245245ax.grid()
246-ax.plot(ns, P, 'k-.', label='Theoretical')
247-ax.plot(ns, f_kI, 'r--', label='Frequentist')
248-ax.set_title(r'Comparison with different $n$',
246+ax.plot(ns, P, '-.', label='theoretical')
247+ax.plot(ns, f_kI, '--', label='fraction')
248+ax.set_title(r'comparison with different $n$',
249249 fontsize=16)
250250ax.set_xlabel(r'$n$', fontsize=15)
251-ax.set_ylabel('Fraction', fontsize=15)
251+ax.set_ylabel('fraction', fontsize=15)
252252ax.tick_params(labelsize=13)
253253ax.legend()
254254plt.show()
@@ -277,12 +277,12 @@ for i in range(n_Is):
277277```{code-cell} ipython3
278278fig, ax = plt.subplots(figsize=(8, 6))
279279ax.grid()
280-ax.plot(Is, P, 'k-.', label='Theoretical')
281-ax.plot(Is, f_kI, 'r--', label='Fraction')
282-ax.set_title(r'Comparison with different $I$',
280+ax.plot(Is, P, '-.', label='theoretical')
281+ax.plot(Is, f_kI, '--', label='fraction')
282+ax.set_title(r'comparison with different $I$',
283283 fontsize=16)
284284ax.set_xlabel(r'$I$', fontsize=15)
285-ax.set_ylabel('Fraction', fontsize=15)
285+ax.set_ylabel('fraction', fontsize=15)
286286ax.tick_params(labelsize=13)
287287ax.legend()
288288plt.show()
@@ -293,36 +293,34 @@ From the above graphs, we can see that **$I$, the number of independent sequence
293293When $I$ becomes larger, the difference between theoretical probability and frequentist estimate becomes smaller.
294294295295Also, as long as $I$ is large enough, changing $\theta$ or $n$ does not substantially change the accuracy of the observed fraction
296-as an approximation of $\mathbb{P}\{X = k \mid \theta\}$.
296+as an approximation of $p(k \mid \theta)$.
297297298298The Law of Large Numbers is at work here.
299299300-For each draw of an independent sequence, $\mathbb{P}\{X_i = k \mid \theta\}$ is the same, so aggregating all draws forms an IID sequence of a binary random variable $\rho_{k,i},i=1,2,...I$, with a mean of $\mathbb{P}\{X = k \mid \theta\}$ and a variance of
300+For each independent sequence $i$, define the indicator $\rho_{k,i} = \mathbb{1}\{X_i = k\}$ — that is, $\rho_{k,i}$ equals 1 if the $i$-th sequence produces exactly $k$ heads and 0 otherwise.
301+302+The $\rho_{k,i}$ are IID across $i$, each with mean $p(k \mid \theta)$ and variance
301303302304$$
303-\mathbb{P}\{X = k \mid \theta\} \cdot (1-\mathbb{P}\{X = k \mid \theta\}).
305+p(k \mid \theta) \cdot (1-p(k \mid \theta)).
304306$$
305307306308So, by the LLN, the average of $\rho_{k,i}$ converges to:
307309308310$$
309-\mathbb{E}[\rho_{k,i}] = \mathbb{P}\{X = k \mid \theta\} = \left(\frac{n!}{k! (n-k)!} \right) \theta^k (1-\theta)^{n-k}
311+\mathbb{E}[\rho_{k,i}] = p(k \mid \theta) = \left(\frac{n!}{k! (n-k)!} \right) \theta^k (1-\theta)^{n-k}
310312$$
311313312314as $I$ goes to infinity.
313315314316315317## Bayesian Interpretation
316318317-We again use a binomial distribution.
318-319-But now we don't regard $\theta$ as being a fixed number.
320-321-Instead, we think of it as a **random variable**.
319+The likelihood remains binomial, but now we treat $\theta$ as a **random variable** rather than a fixed parameter.
322320323-$\theta$ is described by a probability distribution.
321+So $\theta$ is described by a probability distribution.
324322325-But now this probability distribution means something different than a relative frequency that we can anticipate to occur in a large IID. sample.
323+But now this probability distribution means something different than a relative frequency that we can anticipate to occur in a large IID sample.
326324327325Instead, the probability distribution of $\theta$ is now a summary of our views about likely values of $\theta$ either
328326@@ -340,19 +338,15 @@ the density of a **beta distribution** with parameters $\alpha, \beta$.
340338341339We can update this prior after observing data using Bayes' Law (see {doc}`Probability with Matrices <prob_matrix>` for an introduction).
342340343-For a sample of $n$ coin flips that yields $k$ heads, the **likelihood function** is the binomial probability
344-345-$$
346-L(k \mid \theta) = {n \choose k} \theta^k (1-\theta)^{n-k}
347-$$
341+For a sample of $n$ coin flips that yields $k$ heads, the **likelihood function** is the binomial PMF $p(k \mid \theta)$ introduced above.
348342349343Applying Bayes' Law with our beta prior, the **posterior density** is
350344351345$$
352-p(\theta \mid k) = \frac{L(k \mid \theta) \cdot p(\theta)}{\int_0^1 L(k \mid \theta) \cdot p(\theta) \, d\theta} = \textrm{Beta}(\alpha + k, \, \beta + n - k)
346+p(\theta \mid k) = \frac{p(k \mid \theta) \cdot p(\theta)}{\int_0^1 p(k \mid \theta) \cdot p(\theta) \, d\theta}
353347$$
354348355-So the posterior is also a beta distribution — a consequence of the beta prior being **conjugate** to the binomial likelihood.
349+The exercise below derives a closed form for the posterior.
356350357351```{exercise}
358352:label: pm_ex2
@@ -369,7 +363,7 @@ So the posterior is also a beta distribution — a consequence of the beta prior
369363370364**f)** Please tell what question a Bayesian coverage interval answers.
371365372-**g)** Please compute the Posterior probability that $\theta \in [.45, .55]$ for various values of sample size $n$.
366+**g)** Please compute the posterior probability that $\theta \in [.45, .55]$ for various values of sample size $n$.
373367374368**h)** Please use your Python class to study what happens to the posterior distribution as $n \rightarrow + \infty$, again assuming that the true value of $\theta = .4$, though it is unknown to the person doing the updating via Bayes' Law.
375369```
@@ -382,13 +376,13 @@ So the posterior is also a beta distribution — a consequence of the beta prior
382376**a)** The **likelihood function** for a single coin flip with outcome $Y \in \{0, 1\}$ is
383377384378$$
385-L(Y|\theta) = \theta^Y (1-\theta)^{1-Y}
379+p(Y \mid \theta) = \theta^Y (1-\theta)^{1-Y}
386380$$
387381388382**b)** By Bayes' Law, the posterior density for $\theta$ after observing a single flip $Y$ is
389383390384$$
391-p(\theta \mid Y) = \frac{L(Y \mid \theta) \cdot p(\theta)}{\int_{0}^{1} L(Y \mid \theta) \cdot p(\theta) \, d\theta}
385+p(\theta \mid Y) = \frac{p(Y \mid \theta) \cdot p(\theta)}{\int_{0}^{1} p(Y \mid \theta) \cdot p(\theta) \, d\theta}
392386$$
393387394388Substituting the likelihood from (a) and the beta prior density, this becomes
@@ -409,6 +403,14 @@ $$
409403\theta \mid Y \sim \textrm{Beta}(\alpha + Y, \, \beta + (1-Y))
410404$$
411405406+The same calculation with the binomial likelihood in place of the Bernoulli likelihood generalizes this result to $n$ flips with $k$ heads:
407+408+$$
409+\theta \mid k \sim \textrm{Beta}(\alpha + k, \, \beta + n - k)
410+$$
411+412+This is the formula we use in the remaining parts of the exercise.
413+412414**c)**
413415414416```{code-cell} ipython3
@@ -467,15 +469,14 @@ bayes.form_posterior_series(n_obs_list)
467469fig, ax = plt.subplots(figsize=(10, 6))
468470469471ax.plot(θ_values, bayes.prior.pdf(θ_values),
470- label='n = 0 (prior)', color='k',
471- linestyle='--')
472+ label='n = 0 (prior)', linestyle='--')
472473473474for i, n_obs in enumerate(n_obs_list[:10]):
474475 posterior = bayes.posterior_list[i]
475476 ax.plot(θ_values, posterior.pdf(θ_values),
476477 label=f'n = {n_obs}')
477478478-ax.set_title('PDF of Posterior Distributions',
479+ax.set_title('PDF of posterior distributions',
479480 fontsize=15)
480481ax.set_xlabel(r"$\theta$", fontsize=15)
481482@@ -522,13 +523,13 @@ posterior_prob_list = [
522523fig, ax = plt.subplots(figsize=(8, 5))
523524ax.plot(posterior_prob_list)
524525ax.set_title(
525- r'Posterior Probability that $\theta$'
526- f' Ranges from {left_value:.2f}'
526+ r'posterior probability that $\theta$'
527+ f' ranges from {left_value:.2f}'
527528 f' to {right_value:.2f}',
528529 fontsize=13)
529530ax.set_xticks(np.arange(0, len(posterior_prob_list), 3))
530531ax.set_xticklabels(n_obs_list[::3])
531-ax.set_xlabel('Number of Observations', fontsize=11)
532+ax.set_xlabel('number of observations', fontsize=11)
532533533534plt.show()
534535```
@@ -559,7 +560,7 @@ for i, n_obs in enumerate(n_obs_list[10:]):
559560 ax.plot(θ_values, posterior.pdf(θ_values),
560561 label=f'n = {n_obs:,}')
561562562-ax.set_title('PDF of Posterior Distributions', fontsize=15)
563+ax.set_title('PDF of posterior distributions', fontsize=15)
563564ax.set_xlabel(r"$\theta$", fontsize=15)
564565ax.set_xlim(0.3, 0.5)
565566@@ -580,18 +581,18 @@ std_list = [post.std() for post in bayes.posterior_list]
580581fig, ax = plt.subplots(1, 2, figsize=(14, 5))
581582582583ax[0].plot(mean_list)
583-ax[0].set_title('Mean of Posterior Distribution',
584+ax[0].set_title('mean of posterior distribution',
584585 fontsize=13)
585586ax[0].set_xticks(np.arange(0, len(mean_list), 3))
586587ax[0].set_xticklabels(n_obs_list[::3])
587-ax[0].set_xlabel('Number of Observations', fontsize=11)
588+ax[0].set_xlabel('number of observations', fontsize=11)
588589589590ax[1].plot(std_list)
590-ax[1].set_title('Std Dev of Posterior Distribution',
591+ax[1].set_title('std dev of posterior distribution',
591592 fontsize=13)
592593ax[1].set_xticks(np.arange(0, len(std_list), 3))
593594ax[1].set_xticklabels(n_obs_list[::3])
594-ax[1].set_xlabel('Number of Observations', fontsize=11)
595+ax[1].set_xlabel('number of observations', fontsize=11)
595596596597plt.show()
597598```
@@ -623,15 +624,15 @@ lower_bound = [post.ppf(0.05) for post in bayes.posterior_list]
623624624625fig, ax = plt.subplots(figsize=(10, 6))
625626ax.scatter(np.arange(len(upper_bound)),
626- upper_bound, label='95th Quantile')
627+ upper_bound, label='95th quantile')
627628ax.scatter(np.arange(len(lower_bound)),
628- lower_bound, label='5th Quantile')
629+ lower_bound, label='5th quantile')
629630630631ax.set_xticks(np.arange(0, len(upper_bound), 2))
631632ax.set_xticklabels(n_obs_list[::2])
632-ax.set_xlabel('Number of Observations', fontsize=12)
633-ax.set_title('Bayesian Coverage Intervals of '
634- 'Posterior Distributions', fontsize=15)
633+ax.set_xlabel('number of observations', fontsize=12)
634+ax.set_title('Bayesian coverage intervals of '
635+ 'posterior distributions', fontsize=15)
635636636637ax.legend(fontsize=11)
637638plt.show()
@@ -656,7 +657,7 @@ So posterior and prior are both beta distributions, albeit ones with different p
656657657658When a likelihood function and prior fit together like hand and glove in this way, we can say that the prior and posterior are **conjugate distributions**.
658659659-In this situation, we also sometimes say that we have **conjugate prior** for the likelihood function $\mathbb{P}\{X \mid \theta\}$.
660+In this situation, we also sometimes say that we have **conjugate prior** for the likelihood function $p(k \mid \theta)$.
660661661662Typically, the functional form of the likelihood function determines the functional form of a **conjugate prior**.
662663