You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 8f0e37c
Browse filesBrowse the repository at this point in the historyBrowse files
- Use default matplotlib color cycle in comparison plots (drop
explicit 'k'/'r'); drop explicit color on the prior PDF
- Lowercase figure labels and titles (keep PDF and proper nouns)
- Use $p(k \mid \theta)$ as shorthand for the binomial PMF after
the first definition; replace $L(k\mid\theta)$ and $L(Y\mid\theta)$
with $p(\cdot \mid \theta)$ in the Bayesian section
- Remove premature $=\textrm{Beta}(\alpha+k,\beta+n-k)$ from the
Bayes' rule equation (was asserting the punchline of the exercise);
add the n-flip generalization after the (b) solution where it's
needed for part (c)
- Define indicator $\rho_{k,i}$ explicitly before using it in the LLN
discussion
- Reorder first-definition chain to
$p(k\mid\theta) := \mathbb{P}\{X=k\mid\theta\} = [formula]$
- Drop stray periods in "IID. sample"
- Tighten Bayesian section opener
- Lowercase "Posterior" mid-sentence in exercise prompt
Co-authored-by: Claude Opus 4.7 <[email protected]>
@@ -106,7 +106,7 @@ f_k^I = \frac{\textrm{number of samples of length n for which } \sum_{h=1}^n y_h
106
106
I}
107
107
$$
108
108
109
-
The probability $\mathbb{P}\{X = k \mid \theta\}$ answers the following question:
109
+
The probability $p(k \mid \theta)$ answers the following question:
110
110
111
111
* As $I$ becomes large, in what fraction of $I$ independent draws of $n$ coin flips should we anticipate $k$ heads to occur?
112
112
@@ -118,9 +118,9 @@ As usual, a law of large numbers justifies this answer.
118
118
1. Please write a Python class to compute $f_k^I$
119
119
120
120
2. 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$
122
122
123
-
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
124
124
```
125
125
126
126
```{solution-start} pm_ex1
@@ -208,12 +208,12 @@ for i in range(n_thetas):
208
208
```{code-cell} ipython3
209
209
fig, ax = plt.subplots(figsize=(8, 6))
210
210
ax.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$',
214
214
fontsize=16)
215
215
ax.set_xlabel(r'$\theta$', fontsize=15)
216
-
ax.set_ylabel('Fraction', fontsize=15)
216
+
ax.set_ylabel('fraction', fontsize=15)
217
217
ax.tick_params(labelsize=13)
218
218
ax.legend()
219
219
plt.show()
@@ -243,12 +243,12 @@ for i in range(n_ns):
243
243
```{code-cell} ipython3
244
244
fig, ax = plt.subplots(figsize=(8, 6))
245
245
ax.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$',
249
249
fontsize=16)
250
250
ax.set_xlabel(r'$n$', fontsize=15)
251
-
ax.set_ylabel('Fraction', fontsize=15)
251
+
ax.set_ylabel('fraction', fontsize=15)
252
252
ax.tick_params(labelsize=13)
253
253
ax.legend()
254
254
plt.show()
@@ -277,12 +277,12 @@ for i in range(n_Is):
277
277
```{code-cell} ipython3
278
278
fig, ax = plt.subplots(figsize=(8, 6))
279
279
ax.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$',
283
283
fontsize=16)
284
284
ax.set_xlabel(r'$I$', fontsize=15)
285
-
ax.set_ylabel('Fraction', fontsize=15)
285
+
ax.set_ylabel('fraction', fontsize=15)
286
286
ax.tick_params(labelsize=13)
287
287
ax.legend()
288
288
plt.show()
@@ -293,36 +293,34 @@ From the above graphs, we can see that **$I$, the number of independent sequence
293
293
When $I$ becomes larger, the difference between theoretical probability and frequentist estimate becomes smaller.
294
294
295
295
Also, 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)$.
297
297
298
298
The Law of Large Numbers is at work here.
299
299
300
-
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
301
303
302
304
$$
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)).
304
306
$$
305
307
306
308
So, by the LLN, the average of $\rho_{k,i}$ converges to:
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.
356
350
357
351
```{exercise}
358
352
:label: pm_ex2
@@ -369,7 +363,7 @@ So the posterior is also a beta distribution — a consequence of the beta prior
369
363
370
364
**f)** Please tell what question a Bayesian coverage interval answers.
371
365
372
-
**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$.
373
367
374
368
**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.
375
369
```
@@ -382,13 +376,13 @@ So the posterior is also a beta distribution — a consequence of the beta prior
382
376
**a)** The **likelihood function** for a single coin flip with outcome $Y \in \{0, 1\}$ is
383
377
384
378
$$
385
-
L(Y|\theta) = \theta^Y (1-\theta)^{1-Y}
379
+
p(Y \mid \theta) = \theta^Y (1-\theta)^{1-Y}
386
380
$$
387
381
388
382
**b)** By Bayes' Law, the posterior density for $\theta$ after observing a single flip $Y$ is
ax.set_xlabel('Number of Observations', fontsize=11)
532
+
ax.set_xlabel('number of observations', fontsize=11)
532
533
533
534
plt.show()
534
535
```
@@ -559,7 +560,7 @@ for i, n_obs in enumerate(n_obs_list[10:]):
559
560
ax.plot(θ_values, posterior.pdf(θ_values),
560
561
label=f'n = {n_obs:,}')
561
562
562
-
ax.set_title('PDF of Posterior Distributions', fontsize=15)
563
+
ax.set_title('PDF of posterior distributions', fontsize=15)
563
564
ax.set_xlabel(r"$\theta$", fontsize=15)
564
565
ax.set_xlim(0.3, 0.5)
565
566
@@ -580,18 +581,18 @@ std_list = [post.std() for post in bayes.posterior_list]
580
581
fig, ax = plt.subplots(1, 2, figsize=(14, 5))
581
582
582
583
ax[0].plot(mean_list)
583
-
ax[0].set_title('Mean of Posterior Distribution',
584
+
ax[0].set_title('mean of posterior distribution',
584
585
fontsize=13)
585
586
ax[0].set_xticks(np.arange(0, len(mean_list), 3))
586
587
ax[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)
588
589
589
590
ax[1].plot(std_list)
590
-
ax[1].set_title('Std Dev of Posterior Distribution',
591
+
ax[1].set_title('std dev of posterior distribution',
591
592
fontsize=13)
592
593
ax[1].set_xticks(np.arange(0, len(std_list), 3))
593
594
ax[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)
595
596
596
597
plt.show()
597
598
```
@@ -623,15 +624,15 @@ lower_bound = [post.ppf(0.05) for post in bayes.posterior_list]
623
624
624
625
fig, ax = plt.subplots(figsize=(10, 6))
625
626
ax.scatter(np.arange(len(upper_bound)),
626
-
upper_bound, label='95th Quantile')
627
+
upper_bound, label='95th quantile')
627
628
ax.scatter(np.arange(len(lower_bound)),
628
-
lower_bound, label='5th Quantile')
629
+
lower_bound, label='5th quantile')
629
630
630
631
ax.set_xticks(np.arange(0, len(upper_bound), 2))
631
632
ax.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)
635
636
636
637
ax.legend(fontsize=11)
637
638
plt.show()
@@ -656,7 +657,7 @@ So posterior and prior are both beta distributions, albeit ones with different p
656
657
657
658
When 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**.
658
659
659
-
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)$.
660
661
661
662
Typically, the functional form of the likelihood function determines the functional form of a **conjugate prior**.
0 commit comments