GitHub

@@ -551,9 +551,50 @@ plt.show()

551551552552

As $n$ increases, we can see that the probability density functions _concentrate_ on $0.4$, the true value of $\theta$.

553553554-

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.

555555556-

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.

557598558599

```{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

```

580621581-

```{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.

587623588-

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.

603625604626

```{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': []})

607633608634

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

```

Read the original on github.com ↗