Skip to content

Commit 1f15346

Browse files
jstacclaude
andauthored
Make the posterior-concentration argument visible and concrete (#917)
* Make the posterior-concentration argument visible and concrete Two fixes to the "Why the posterior concentrates" section of prob_meaning: - The opening referred to "the patterns above", but those plots lived inside the pm_ex3 solution dropdown (collapsed by default). Move the mean/std figure out of the solution into this always-visible section and reword the opening so it stands on its own. - The section gave the generic beta mean/variance in terms of (alpha, beta) — which are the *prior* parameters, so it actually displayed the prior's moments, not the posterior's. Substitute the posterior parameters (alpha+k, beta+n-k) and take the limit explicitly: the mean -> 0.4 since k/n -> 0.4, and the variance ~ theta(1-theta)/n -> 0. Now the stated convergence is shown, not merely asserted. Verified with jupytext export + headless execution. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * Add pointer, ground the analysis section, box-and-whisker coverage plot - Add a one-line pointer from the pm_ex3 part-f solution to the "Why the posterior concentrates" section. - Open that section with a back-reference to the solution of Exercise 3 ("In the solution to pm_ex3 we watched ..."), since the concentration it discusses is shown in a solution box that is collapsed by default. - Introduce the coverage-interval plot with a lead-in sentence (it previously followed the mean/std figure with none). - Replace the 5th/95th-quantile scatter with a box-and-whisker plot (median, IQR, 5-95% whiskers) built from the analytical posterior quantiles, with the true theta marked. Verified with jupytext export + headless execution. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 398a404 commit 1f15346

1 file changed

Lines changed: 57 additions & 35 deletions

File tree

‎lectures/prob_meaning.md‎

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,50 @@ plt.show()
551551
552552
As $n$ increases, we can see that the probability density functions _concentrate_ on $0.4$, the true value of $\theta$.
553553
554-
Here the posterior mean converges to $0.4$ while the posterior standard deviation converges to $0$ from above.
554+
The next section explains *why* this concentration occurs and how fast it happens.
555555
556-
To show this, we compute the mean and standard deviation of the posterior distributions.
556+
```{solution-end}
557+
```
558+
559+
### Why the posterior concentrates
560+
561+
In the solution to {ref}`pm_ex3` we watched the posterior distribution concentrate ever more tightly around the true value $\theta = 0.4$ as the sample grew. Why does this happen?
562+
563+
The answer is encoded in the posterior we derived.
564+
565+
Recall that after observing $k$ heads in $n$ flips, the posterior is $\textrm{Beta}(\alpha + k, \, \beta + n - k)$.
566+
567+
A beta distribution with parameters $a$ and $b$ has
568+
569+
* mean $\dfrac{a}{a + b}$,
570+
571+
* variance $\dfrac{a\, b}{(a + b)^2\, (a + b + 1)}$.
572+
573+
Substituting the *posterior* parameters $a = \alpha + k$ and $b = \beta + n - k$, so that $a + b = \alpha + \beta + n$, gives
574+
575+
$$
576+
\mathbb{E}[\theta \mid k] = \frac{\alpha + k}{\alpha + \beta + n},
577+
\qquad
578+
\operatorname{Var}[\theta \mid k] = \frac{(\alpha + k)(\beta + n - k)}{(\alpha + \beta + n)^2\, (\alpha + \beta + n + 1)} .
579+
$$
580+
581+
As $n$ grows, the fixed prior counts $\alpha$ and $\beta$ become negligible beside the data.
582+
583+
Since the data are generated with $\theta = 0.4$, the Law of Large Numbers gives $k/n \to 0.4$ (see {ref}`pm_ex1`), so the posterior mean
584+
585+
$$
586+
\frac{\alpha + k}{\alpha + \beta + n} \;\approx\; \frac{k}{n} \;\to\; 0.4 .
587+
$$
588+
589+
In the variance, the numerator grows like $n^2$ while the denominator grows like $n^3$, so
590+
591+
$$
592+
\operatorname{Var}[\theta \mid k] \;\approx\; \frac{\theta(1 - \theta)}{n} \;\longrightarrow\; 0 .
593+
$$
594+
595+
The posterior mean therefore homes in on the truth while its spread vanishes at rate $1/n$.
596+
597+
The next figure confirms both claims: the posterior mean settles on $0.4$ and the standard deviation decays toward zero.
557598
558599
```{code-cell} ipython3
559600
mean_list = [post.mean() for post in posterior_list]
@@ -578,45 +619,26 @@ ax[1].set_xlabel('number of observations', fontsize=11)
578619
plt.show()
579620
```
580621
581-
```{solution-end}
582-
```
583-
584-
### Why the posterior concentrates
585-
586-
How shall we interpret the patterns above?
622+
We can also display the Bayesian coverage intervals directly.
587623
588-
The answer is encoded in the Bayesian updating formula derived above.
589-
590-
Recall that after observing $k$ heads in $n$ flips, the posterior is $\textrm{Beta}(\alpha + k, \, \beta + n - k)$.
591-
592-
A beta distribution with parameters $\alpha$ and $\beta$ has
593-
594-
* mean $\frac{\alpha}{\alpha + \beta}$
595-
596-
* variance $\frac{\alpha \beta}{(\alpha + \beta)^2 (\alpha + \beta + 1)}$
597-
598-
Here $\alpha + k$ can be viewed as the number of successes (prior pseudo-count plus observed heads) and $\beta + n - k$ as the number of failures.
599-
600-
Since the data are generated with $\theta = 0.4$, the Law of Large Numbers tells us that, as $n$ grows, $k/n \to 0.4$ (see {ref}`pm_ex1`).
601-
602-
Consequently, the posterior mean converges to $0.4$ and the posterior variance shrinks to zero.
624+
The box-and-whisker plot below summarizes each posterior by its median (central line), interquartile range (box), and $5$th–$95$th percentile range (whiskers), with the true value $\theta = 0.4$ marked.
603625
604626
```{code-cell} ipython3
605-
upper_bound = [post.ppf(0.95) for post in posterior_list]
606-
lower_bound = [post.ppf(0.05) for post in posterior_list]
627+
quantiles = [0.05, 0.25, 0.5, 0.75, 0.95]
628+
box_stats = []
629+
for post in posterior_list:
630+
lo, q1, med, q3, hi = post.ppf(quantiles)
631+
box_stats.append({'med': med, 'q1': q1, 'q3': q3,
632+
'whislo': lo, 'whishi': hi, 'fliers': []})
607633
608634
fig, ax = plt.subplots(figsize=(10, 6))
609-
ax.scatter(np.arange(len(upper_bound)),
610-
upper_bound, label='95th quantile')
611-
ax.scatter(np.arange(len(lower_bound)),
612-
lower_bound, label='5th quantile')
613-
614-
ax.set_xticks(np.arange(0, len(upper_bound), 2))
615-
ax.set_xticklabels(n_obs_list[::2])
635+
ax.bxp(box_stats, positions=np.arange(len(box_stats)), showfliers=False)
636+
ax.axhline(0.4, color='C1', linestyle='--', label=r'true $\theta = 0.4$')
637+
ax.set_xticks(np.arange(len(box_stats)))
638+
ax.set_xticklabels(n_obs_list, rotation=45)
616639
ax.set_xlabel('number of observations', fontsize=12)
617-
ax.set_title('Bayesian coverage intervals of '
618-
'posterior distributions', fontsize=15)
619-
640+
ax.set_ylabel(r'$\theta$', fontsize=12)
641+
ax.set_title('posterior coverage intervals as $n$ grows', fontsize=15)
620642
ax.legend(fontsize=11)
621643
plt.show()
622644
```

0 commit comments

Comments
 (0)