IE437 · Data-Driven Decision Making and Control — Lecture 2

Fundamentals on Bayesian Statistics

From coin tossing to Bayesian regression
Jinkyoo Park · KAIST
Model → update → predict, one example at a time
01 · FOUNDATIONS

1 · Introduction and Bayes’ rule

Start with the original coin question: what is its probability of heads? Compare statistical viewpoints, then learn the update rule.
01 · FOUNDATIONS1 · Introduction and Bayes’ ruleLecture 2

Where we are — the coefficients must be learned

Previous step This chapter's question
Lecture 1 optimized a specified model. Now represent an unknown parameter using a distribution.

Keep the likelihood, posterior and prediction target separate. The temperature example will distinguish an unknown mean from measurement noise.

Update a temperature mean and distinguish parameter uncertainty from a new noisy reading.
Lecture 2 · Fundamentals on Bayesian Statistics3 / 96
01 · FOUNDATIONS1 · Introduction and Bayes’ ruleLecture 2

Statistics works backwards from data to a model

Modelθ\theta: probability of heads
DataD=(H,H,T)D=(H,H,T): observed tosses

Given θ\theta, the model tells us which data are plausible. Given data, inference asks which values of θ\theta remain plausible. This alone is not a claim of causal identification.

Observation uncertainty

Yi∣θ∼Bernoulli(θ).Y_i\mid\theta\sim\mathrm{Bernoulli}(\theta).

Even if θ\theta were known, the next toss would remain random.

Parameter uncertainty

We do not know θ\theta. A Bayesian distribution over θ\theta represents our uncertainty about it; it does not require the physical coin to change on every toss.

Source alignment: original Lecture 2 PDF 3.

Lecture 2 · Fundamentals on Bayesian Statistics4 / 96
01 · FOUNDATIONS1 · Introduction and Bayes’ ruleLecture 2

Two approaches, both able to quantify uncertainty

Frequentist

θ\theta is fixed; the data is what is random. So a probability describes the datasets you might have drawn — the long run of repeated, identical experiments.

Estimate by relative frequency, θ^=S/n\hat\theta = S/n. Running the experiment with more and more tosses:

1356513129313614231234000.330.830.380.410.50\begin{array}{ccccc} \tfrac{1}{3} & \tfrac{5}{6} & \tfrac{5}{13} & \tfrac{129}{313} & \tfrac{61423}{123400}\\[3pt] \dm{0.33} & \dm{0.83} & \dm{0.38} & \dm{0.41} & \dm{0.50} \end{array}

The difficulty. Small nn swings wildly — that is high variance, not bias; S/nS/n is centred on θ\theta at every nn. And "identical flip" is an idealisation no real experiment quite meets.

Bayesian

The data is fixed — you observed it. θ\theta is what is uncertain, so a probability describes θ\theta itself: a degree of belief.

Specify a prior p(θ)p(\theta), condition on the observed DD, and read off the posterior:

p(θ)  ⟶  p(θ∣D)p(\theta) \;\longrightarrow\; p(\theta \mid D)

The difficulty. The prior is a modelling choice, and choosing it honestly is real work. Check what it implies and compare reasonable alternatives.

The distinction is what the probability statement describes, not whether uncertainty matters.

Source alignment: original Lecture 2 PDF 4–7. The estimate sequence is the source's own (PDF 6), as is the pairing of one difficulty against the other. Two deliberate departures: the source says small nn makes the estimate biased, which is wrong — S/nS/n is unbiased for every nn, and what small nn buys is variance; and the sampling-variance formula an earlier draft used to make that point has been dropped, since the binomial is not introduced until "One toss, a sequence, and a count". Experiment 1 demonstrates the scatter instead, and the two 95% statements are contrasted later on their own slide.

Lecture 2 · Fundamentals on Bayesian Statistics5 / 96
01 · FOUNDATIONS1 · Introduction and Bayes’ ruleLecture 2

The lecture route — keep each example together

Bring: Conditional probability, sums and a Gaussian distribution.

First pass What to do
Follow the idea Specify → update → predict → check; repeat for coins, counts and regression
Work without the solution Update a temperature mean and distinguish parameter uncertainty from a new noisy reading.
Return later Complete-square derivations and additional conjugate calculations are in the appendix.
For the temperature thread: predict → calculate → reveal and check → change one condition. Complete the core calculation before reading the research extensions.
Lecture 2 · Fundamentals on Bayesian Statistics6 / 96
01 · FOUNDATIONS1 · Introduction and Bayes’ ruleLecture 2

Bayes' rule — name each part before using it

Q1 Why a distribution?Q2 What does the coin teach us?Q3 How does the model change?Q4 How do we learn a regression?
p(θ∣data)⏟posterior  =  p(data∣θ)⏞likelihood    p(θ)⏞priorp(data)⏟evidence\underbrace{p(\theta\mid\mathrm{data})}_{\hl{\text{posterior}}} \;=\; \frac{\overbrace{p(\mathrm{data}\mid\theta)}^{\textcolor{#16A34A}{\text{likelihood}}}\;\;\overbrace{p(\theta)}^{\textcolor{#D97706}{\text{prior}}}} {\underbrace{p(\mathrm{data})}_{\text{evidence}}}

The evidence p(data)=∫p(data∣θ) p(θ) dθp(\mathrm{data})=\int p(\mathrm{data}\mid\theta)\,p(\theta)\,\mathrm{d}\theta is the probability of the data over every candidate at once. It does not depend on θ\theta, so it only rescales the curve — which is why posterior ∝\propto likelihood ×\times prior is usually all you write.

Prior in, posterior out. The likelihood tells us how to reweight the candidates.

Source alignment: original Lecture 2 PDF 8–9.

Lecture 2 · Fundamentals on Bayesian Statistics7 / 96
01 · FOUNDATIONS1 · Introduction and Bayes’ ruleLecture 2

Why Bayes' rule works

The same joint probability can be factored in two orders:

p(θ,D)=p(D∣θ)p(θ)=p(θ∣D)p(D).p(\theta,D)=p(D\mid\theta)p(\theta)=p(\theta\mid D)p(D).

Divide by the evidence p(D)>0p(D)>0:

p(θ∣D)=p(D∣θ)p(θ)p(D).p(\theta\mid D)=\frac{p(D\mid\theta)p(\theta)}{p(D)}.
Multiply prior by likelihood, then normalise so the posterior sums or integrates to 1.

Source alignment: original Lecture 2 PDF 8.

Lecture 2 · Fundamentals on Bayesian Statistics8 / 96
01 · FOUNDATIONS1 · Introduction and Bayes’ ruleLecture 2

Two candidate coins — Bayes' rule with numbersan illustrative calculation before the continuous case

A coin is either fair (θ=0.5\theta=0.5) or head-biased (θ=0.8\theta=0.8), with equal prior probabilities. We observe two heads, conditionally independently.

Candidate Prior Likelihood of HH Product Posterior
θ=0.5\theta=0.5 0.50.5 0.52=0.250.5^2=0.25 0.1250.125 0.125/0.445=0.2810.125/0.445=0.281
θ=0.8\theta=0.8 0.50.5 0.82=0.640.8^2=0.64 0.3200.320 0.320/0.445=0.7190.320/0.445=0.719

The evidence is p(HH)=0.125+0.320=0.445p(HH)=0.125+0.320=0.445. Dividing by it makes the posterior probabilities add to 1.

Two heads favour the biased coin, but do not prove which coin we have.

Source alignment: original Lecture 2 PDF 8–10.

Lecture 2 · Fundamentals on Bayesian Statistics9 / 96
01 · FOUNDATIONS1 · Introduction and Bayes’ ruleLecture 2

One formula, two readings — probability and likelihoodthe same expression, according to which argument you hold fixed

For the observed sequence D=(H,H,T)D=(H,H,T), conditional independence gives

p(D∣θ)=θ⋅θ⋅(1−θ)=θ2(1−θ).p(D\mid\theta)=\theta\cdot\theta\cdot(1-\theta)=\theta^2(1-\theta).

That one expression has two free arguments, and which of them you hold fixed decides what kind of object you are looking at. Both readings below are of the formula above — they are not two different formulas.

Fix the parameter, vary the data — a probability

Then it is p(D∣θ)p(D\mid\theta), an ordinary probability over the 23=82^3=8 sequences three tosses could produce. Those eight numbers sum to 1, for every θ\theta.

Fix the data, vary the parameter — a likelihood

Then it is L(θ;D)L(\theta;D), a score on each candidate θ\theta. Nothing normalises it: ∫01θ2(1−θ) dθ=112\int_0^1\theta^2(1-\theta)\,d\theta=\tfrac1{12}, not 1.

Summing to 1 along one axis says nothing about the other. That missing normalisation is exactly what p(D)p(D) supplies in Bayes' rule — which is why a likelihood is not yet a posterior.

For continuous θ\theta, p(θ)p(\theta) is a density: interval areas are probabilities and P(θ=θ0)=0P(\theta=\theta_0)=0. A density may exceed 1. This differs from the two discrete candidates on the previous slide.

Source alignment: original Lecture 2 PDF 10–11.

Lecture 2 · Fundamentals on Bayesian Statistics10 / 96
01 · FOUNDATIONS1 · Introduction and Bayes’ ruleLecture 2

One toss, a sequence, and a count

Q1 Why a distribution?Q2 What does the coin teach us?Q3 How does the model change?Q4 How do we learn a regression?

Assume Yi∣θY_i\mid\theta are independent Bernoulli variables. Let S=∑i=1nYiS=\sum_{i=1}^nY_i.

What is observed? Probability given θ\theta For two heads in three tosses
One toss YiY_i θYi(1−θ)1−Yi\theta^{Y_i}(1-\theta)^{1-Y_i} Head: θ\theta
One particular sequence θS(1−θ)n−S\theta^S(1-\theta)^{n-S} HHT: θ2(1−θ)\theta^2(1-\theta)
Only the total SS (nS)θS(1−θ)n−S\binom nS\theta^S(1-\theta)^{n-S} HHT, HTH, THH: 3θ2(1−θ)3\theta^2(1-\theta)
The factor (nS)\binom nS counts the sequences. It is constant in θ\theta, so it changes the data probability but not the MLE or posterior shape.

Source alignment: original Lecture 2 PDF 11.

Lecture 2 · Fundamentals on Bayesian Statistics11 / 96
01 · FOUNDATIONS1 · Introduction and Bayes’ ruleLecture 2

The coin MLE — maximise the log likelihood

For 0<S<n0<S<n, taking logs turns the product into a sum:

ℓ(θ)=Slog⁡θ+(n−S)log⁡(1−θ)+constant.\ell(\theta)=S\log\theta+(n-S)\log(1-\theta)+\text{constant}.
ℓ′(θ)=Sθ−n−S1−θ=0⟹S(1−θ)=(n−S)θ⟹θ^ML=Sn.\ell'(\theta)=\frac{S}{\theta}-\frac{n-S}{1-\theta}=0 \quad\Longrightarrow\quad S(1-\theta)=(n-S)\theta \quad\Longrightarrow\quad \hat\theta_{\rm ML}=\frac Sn.

The second derivative is negative on (0,1)(0,1), so this is the maximum. For HHT, θ^ML=2/3\hat\theta_{\rm ML}=2/3.

If S=0S=0 or S=nS=n, the maximum is at the boundary, 00 or 11; there is no interior derivative-zero solution. Here SS is sufficient for θ\theta: the order adds no information under this model.

Source alignment: original Lecture 2 PDF 12.

Lecture 2 · Fundamentals on Bayesian Statistics12 / 96
01 · FOUNDATIONS1 · Introduction and Bayes’ ruleLecture 2

Experiment 1 — repeat the coin experiment

Predict: will 100 tosses vary less than 5? Try: change tosses per experiment, then repeat the 200 experiments. Explain: the coin stays fixed; the datasets and their estimates change.

Source alignment: original Lecture 2 PDF 4–7, 11–12.

Lecture 2 · Fundamentals on Bayesian Statistics13 / 96
01 · FOUNDATIONS1 · Introduction and Bayes’ ruleLecture 2

The Bayesian move — carry a distribution

Object Question it answers Coin example
Prior p(θ)p(\theta) What is plausible before these data? How biased might the coin be?
Likelihood p(D∣θ)p(D\mid\theta) How compatible is each candidate with the data? How likely is HHT for this bias?
Posterior p(θ∣D)p(\theta\mid D) What is plausible after these data? Which biases still have support?
Predictive p(Y~∣D)p(\tilde Y\mid D) What might happen next? How many heads in future tosses?
The parameter and the next observation are different unknowns. A distribution over one is not a distribution over the other.

Source alignment: original Lecture 2 PDF 13.

Lecture 2 · Fundamentals on Bayesian Statistics14 / 96
01 · FOUNDATIONS1 · Introduction and Bayes’ ruleLecture 2

Prior, posterior, predictive — keep the target clear

Q1 Why a distribution?Q2 What does the coin teach us?Q3 How does the model change?Q4 How do we learn a regression?
Distribution Random quantity Information used
Prior p(θ)p(\theta) parameter before observing DD
Prior predictive p(y~)p(\tilde y) possible data prior + sampling model
Posterior p(θ∣D)p(\theta\mid D) parameter prior + observed data
Posterior predictive p(y~∣D)p(\tilde y\mid D) future observation posterior + sampling model
Posterior: what might the parameter be? Predictive: what might we observe?

Source alignment: original Lecture 2 PDF 14.

Lecture 2 · Fundamentals on Bayesian Statistics15 / 96
01 · FOUNDATIONS1 · Introduction and Bayes’ ruleLecture 2

Check — what the distribution is over

A Bayesian writes p(θ)p(\theta) for a physical constant with one true, fixed value. What does the distribution describe?

The distribution represents epistemic uncertainty. Data can reduce it when informative. Aleatoric uncertainty is variation in future observations conditional on the model's parameters; learning the parameter alone does not remove that variation.

Source alignment: original Lecture 2 PDF 13–14.

Lecture 2 · Fundamentals on Bayesian Statistics16 / 96
02 · BAYESIAN COIN

2 · Bayesian estimation for coin flipping

Keep the same coin: choose a Beta prior, observe heads and tails, quantify uncertainty, and predict the next tosses.
02 · BAYESIAN COIN2 · Bayesian estimation for coin flippingLecture 2

A Beta prior — location and strength

Use a density supported on the possible coin biases, 0<θ<10<\theta<1:

p(θ)=1B(α,β)θα−1(1−θ)β−1,α,β>0.p(\theta)=\frac{1}{B(\alpha,\beta)}\theta^{\alpha-1}(1-\theta)^{\beta-1},\qquad \alpha,\beta>0.

Where is the belief centred?

E[θ]=αα+β.\E[\theta]=\frac{\alpha}{\alpha+\beta}.

Beta(2,2)\mathrm{Beta}(2,2) centres at 1/21/2; Beta(2,8)\mathrm{Beta}(2,8) centres at 1/51/5.

How strong is that belief?

Var[θ]=αβ(α+β)2(α+β+1).\mathrm{Var}[\theta]=\frac{\alpha\beta}{(\alpha+\beta)^2(\alpha+\beta+1)}.

Write μ=E[θ]\mu=\E[\theta] and ν=α+β\nu=\alpha+\beta: this is μ(1−μ)/(ν+1)\mu(1-\mu)/(\nu+1). At a fixed centre the numerator is fixed, so spread falls like 1/ν1/\nu.

Both priors below centre at 1/21/2, so only ν\nu separates them: Beta(2,2)\mathrm{Beta}(2,2) has variance 1/201/20, Beta(20,20)\mathrm{Beta}(20,20) has 1/1641/164 — 8.2 times smaller, and the standard deviation falls from 0.220.22 to 0.080.08. That is what “α+β\alpha+\beta is the strength of the prior” means, and in the next slide it is why they cost ν\nu pseudo-counts to overturn.

B(α,β)B(\alpha,\beta) makes the density integrate to 1. The parameters act like pseudo-counts in the update; they need not represent actual past flips. Uniform Beta(1,1)\mathrm{Beta}(1,1) is still a choice of prior.

Source alignment: original Lecture 2 PDF 15–17.

Lecture 2 · Fundamentals on Bayesian Statistics18 / 96
02 · BAYESIAN COIN2 · Bayesian estimation for coin flippingLecture 2

Derive the posterior by collecting powers

Start with a Beta prior and observe SS heads in nn tosses:

p(θ∣D)∝θS(1−θ)n−S⏟likelihoodθα−1(1−θ)β−1⏟prior=θα+S−1(1−θ)β+n−S−1.\begin{aligned} p(\theta\mid D)&\propto\underbrace{\theta^S(1-\theta)^{n-S}}_{\text{likelihood}} \underbrace{\theta^{\alpha-1}(1-\theta)^{\beta-1}}_{\text{prior}}\\[4pt] &=\theta^{\alpha+S-1}(1-\theta)^{\beta+n-S-1}. \end{aligned}

Recognise a Beta density and restore its normalising constant:

θ∣D∼Beta(α+S, β+n−S).\hl{\theta\mid D\sim\mathrm{Beta}(\alpha+S,\ \beta+n-S).}
Add heads to the first parameter; add tails to the second. For a Beta(2,2)\mathrm{Beta}(2,2) prior and HHT, the posterior is Beta(4,3)\mathrm{Beta}(4,3).

Source alignment: original Lecture 2 PDF 18.

Lecture 2 · Fundamentals on Bayesian Statistics19 / 96
02 · BAYESIAN COIN2 · Bayesian estimation for coin flippingLecture 2

Sequential updating — do not count old data twice

The posterior after one batch becomes the prior before the next batch:

PriorBeta(2,2)\mathrm{Beta}(2,2)
Observe HHBeta(4,2)\mathrm{Beta}(4,2)
Observe TBeta(4,3)\mathrm{Beta}(4,3)

Updating once with HHT gives exactly the same answer. More generally,

p(θ∣D1,D2)∝p(D2∣θ) p(θ∣D1),p(\theta\mid D_1,D_2)\propto p(D_2\mid\theta)\,p(\theta\mid D_1),

when the batches are conditionally independent given θ\theta.

The old data is already in p(θ∣D1)p(\theta\mid D_1). Multiply by the new likelihood once.

Source alignment: original Lecture 2 PDF 18–19.

Lecture 2 · Fundamentals on Bayesian Statistics20 / 96
02 · BAYESIAN COIN2 · Bayesian estimation for coin flippingLecture 2

Bayesian coin updating — the first five tosses

All six early states. Start from Beta(1,1): each head adds 1 to α and each tail adds 1 to β. The dashed line marks θ 0.5.
All six early states. Start from Beta(1,1): each head adds 1 to α and each tail adds 1 to β. The dashed line marks θ = 0.5.
Read left to right, then down. The first two heads pull the curve right; the next tail pulls it back.

Source alignment: original Lecture 2 PDF 19.

Lecture 2 · Fundamentals on Bayesian Statistics21 / 96
02 · BAYESIAN COIN2 · Bayesian estimation for coin flippingLecture 2

Bayesian coin updating — from 8 to 500 tosses

Heads out of tosses: 5/8, 9/15, 26/50 and 259/500. Every curve has area 1; the vertical density scales differ.
Heads out of tosses: 5/8, 9/15, 26/50 and 259/500. Every curve has area 1; the vertical density scales differ.
The final two observed proportions are similar. The larger dataset supports a much more precise estimate.

Source alignment: original Lecture 2 PDF 19.

Lecture 2 · Fundamentals on Bayesian Statistics22 / 96
02 · BAYESIAN COIN2 · Bayesian estimation for coin flippingLecture 2

Beta(4,3) — the mean and the mode, by handHHT and a Beta(2,2) prior gave Beta(4,3); now read the numbers out of it

With α=4, β=3\alpha=4,\ \beta=3 the normaliser is 1/B(4,3)=601/B(4,3)=60, so the posterior is the polynomial p(θ∣D)=60 θ3(1−θ)2p(\theta\mid D)=60\,\theta^{3}(1-\theta)^{2}.

The mean — integrate θ\theta against it

E[θ∣D]=∫01θ⋅60 θ3(1−θ)2 dθ=αα+β=47=0.571\E[\theta\mid D]=\int_0^1\theta\cdot60\,\theta^{3}(1-\theta)^{2}\,d\theta=\frac{\alpha}{\alpha+\beta}=\frac47=0.571

The general Beta mean from earlier, with the numbers put in.

The mode — set the log-derivative to zero

ddθlog⁡p=3θ−21−θ=0  ⟹  θ∗=35=0.600\frac{d}{d\theta}\log p=\frac{3}{\theta}-\frac{2}{1-\theta}=0 \;\Longrightarrow\;\theta^{*}=\frac{3}{5}=0.600

In general α−1α+β−2\frac{\alpha-1}{\alpha+\beta-2}. Under a flat prior it would be the MLE 23\tfrac23; the Beta(2,2)\mathrm{Beta}(2,2) prior pulled it toward 12\tfrac12.

Mean 0.5710.571 and mode 0.6000.600 differ because the posterior is skewed — three tosses cannot make it symmetric.

Source alignment: original Lecture 2 PDF 18–19.

Lecture 2 · Fundamentals on Bayesian Statistics23 / 96
02 · BAYESIAN COIN2 · Bayesian estimation for coin flippingLecture 2

Beta(4,3) — the interval and the next toss

A 95% credible interval — invert the CDF

Integrating the polynomial gives the CDF in closed form,

F(θ)=∫0θ60 t3(1−t)2 dt=15θ4−24θ5+10θ6,F(\theta)=\int_0^{\theta}60\,t^{3}(1-t)^{2}\,dt=15\theta^{4}-24\theta^{5}+10\theta^{6},

and the interval is the pair of roots of F(a)=0.025F(a)=0.025 and F(b)=0.975F(b)=0.975, solved numerically: [0.223, 0.882][0.223,\ 0.882].

The next toss — average θ\theta over the posterior

P(H∣D)=∫01θ⏟P(H∣θ)  p(θ∣D) dθ=E[θ∣D]=47P(H\mid D)=\int_0^1\underbrace{\theta}_{P(H\mid\theta)}\;p(\theta\mid D)\,d\theta=\E[\theta\mid D]=\frac47

For a single toss the predictive integral collapses to the posterior mean. It is the m=1m=1 case of the Beta-Binomial to come.

Three tosses leave substantial uncertainty — the interval spans two thirds of [0,1][0,1]. The mean, mode and interval answer different questions, and none of them is the whole posterior.

Source alignment: original Lecture 2 PDF 19–21. Every number recomputed: the CDF polynomial agrees with direct integration at all three checkpoints, and the roots give 0.2228 and 0.8819.

Lecture 2 · Fundamentals on Bayesian Statistics24 / 96
02 · BAYESIAN COIN2 · Bayesian estimation for coin flippingLecture 2

Experiment 2 — repeat an interval procedure

Predict: must all 26 intervals cover the true value? Try: run the repeated experiments. Explain: compare the long-run coverage statement on the left with posterior probability for the observed dataset on the right.

Source alignment: original Lecture 2 PDF 20–21.

Lecture 2 · Fundamentals on Bayesian Statistics25 / 96
02 · BAYESIAN COIN2 · Bayesian estimation for coin flippingLecture 2

Read the two 95% statements carefully

Confidence interval

Before drawing data, the random interval C(D)C(D) covers a fixed parameter in 95% of repeated experiments:

Pθ{θ∈C(D)}=0.95.P_\theta\{\theta\in C(D)\}=0.95.

Once DD is observed, the interval is fixed. Coverage alone does not assign 95% probability to that particular interval containing θ\theta.

Credible interval

After observing DD, an interval CC contains 95% of the posterior probability:

P(θ∈C∣D)=0.95.P(\theta\in C\mid D)=0.95.

This is conditional on the prior, likelihood and data. Its repeated-sampling coverage need not be 95%.

The previous picture uses Normal observations with known noise and a flat prior on the mean. The endpoints coincide in that special case; the interpretations still differ.

Source alignment: original Lecture 2 PDF 20–21.

Lecture 2 · Fundamentals on Bayesian Statistics26 / 96
02 · BAYESIAN COIN2 · Bayesian estimation for coin flippingLecture 2

The posterior mean is a balance — in numbers

For the coin model,

E[θ∣D]=α+Sα+β+n=α+βα+β+nαα+β+nα+β+nSn.\E[\theta\mid D]=\frac{\alpha+S}{\alpha+\beta+n} =\frac{\alpha+\beta}{\alpha+\beta+n}\frac{\alpha}{\alpha+\beta} +\frac{n}{\alpha+\beta+n}\frac Sn.

For Beta(2,2)\mathrm{Beta}(2,2) and HHT, this becomes

47⏟posterior mean=47⏟prior weight12⏟prior mean+37⏟data weight23⏟sample proportion.\underbrace{\frac47}_{\text{posterior mean}} =\underbrace{\frac47}_{\text{prior weight}}\underbrace{\frac12}_{\text{prior mean}} +\underbrace{\frac37}_{\text{data weight}}\underbrace{\frac23}_{\text{sample proportion}}.
Compare sample size nn with prior strength α+β\alpha+\beta. Small nn alone does not tell us which dominates.

Source alignment: original Lecture 2 PDF 22.

Lecture 2 · Fundamentals on Bayesian Statistics27 / 96
02 · BAYESIAN COIN2 · Bayesian estimation for coin flippingLecture 2

Experiment 3 — change the prior, keep the data

Predict: which prior resists HHT most? Try: load HHT, switch the prior, then simulate more tosses. Explain: compare the displayed prior/data weights. H and T record hypothetical outcomes; simulated tosses use a fixed bias of 0.62.

Source alignment: original Lecture 2 PDF 18–23.

Lecture 2 · Fundamentals on Bayesian Statistics28 / 96
02 · BAYESIAN COIN2 · Bayesian estimation for coin flippingLecture 2

Does more data always make a posterior narrower?

On average, yes, in the following precise sense. The average is over possible datasets under the joint model:

Var(θ)=ED[Var(θ∣D)]+VarD(E[θ∣D]).\mathrm{Var}(\theta)=\E_D[\mathrm{Var}(\theta\mid D)]+\mathrm{Var}_D(\E[\theta\mid D]).

The last term is non-negative, so the expected posterior variance cannot exceed the prior variance. Also ED[E(θ∣D)]=E(θ)\E_D[\E(\theta\mid D)]=\E(\theta).

One surprising observation can widen it

A Beta(1,20)\mathrm{Beta}(1,20) prior has variance 0.002060.00206. After one head, Beta(2,20)\mathrm{Beta}(2,20) has variance 0.003590.00359. The surprising head challenges a strong belief in tails.

Source alignment: original Lecture 2 PDF 23.

Lecture 2 · Fundamentals on Bayesian Statistics29 / 96
02 · BAYESIAN COIN2 · Bayesian estimation for coin flippingLecture 2

From estimating the coin to predicting its next toss

The posterior answers which coin biases are plausible. A prediction asks what might happen next.

p(y~∣D)=∫p(y~∣θ) p(θ∣D) dθ.p(\tilde y\mid D)=\int p(\tilde y\mid\theta)\,p(\theta\mid D)\,d\theta.

For the Beta(4,3) posterior after HHT,

P(Y~=1∣D)=∫01θ p(θ∣D) dθ=47.P(\tilde Y=1\mid D)=\int_0^1\theta\,p(\theta\mid D)\,d\theta=\frac47.
Average the next-toss prediction over the plausible biases. For one Bernoulli outcome, plugging in the posterior mean gives exactly the same distribution.

Source alignment: original Lecture 2 PDF 14, 24.

Lecture 2 · Fundamentals on Bayesian Statistics30 / 96
02 · BAYESIAN COIN2 · Bayesian estimation for coin flippingLecture 2

Predicting a batch — one uncertain coin is shared

Let KK count heads in mm future tosses. All of them are driven by the same unknown θ\theta, so the honest prediction averages over everything θ\theta might be:

p(K∣D)=∫01p(K∣θ)⏟Binomial(m,θ)  p(θ∣D)⏟Beta(α′,β′) dθ.p(K\mid D)=\int_0^1 \underbrace{p(K\mid\theta)}_{\text{Binomial}(m,\theta)}\;\underbrace{p(\theta\mid D)}_{\text{Beta}(\alpha',\beta')}\,d\theta.

This is Act 4's integral with a batch in place of a single y∗y^*. Two things make it worth doing rather than plugging in θ^\hat\theta: the answer is available in closed form, and it is a different distribution, not merely a wider one.

The next slide evaluates that integral. The answer has a name — the Beta-Binomial.

Source alignment: original Lecture 2 PDF 24, 29–31.

Lecture 2 · Fundamentals on Bayesian Statistics31 / 96
02 · BAYESIAN COIN2 · Bayesian estimation for coin flippingLecture 2

Where the Beta-Binomial comes fromthe predictive integral, evaluated by recognising a Beta kernel

Write the binomial coefficient with gammas, (my)=Γ(m+1)Γ(y+1)Γ(m−y+1)\binom my=\frac{\Gamma(m+1)}{\Gamma(y+1)\Gamma(m-y+1)}, and collect the powers of θ\theta:

p(y)=∫01(my)θy(1−θ)m−y⋅Γ(α+β)Γ(α)Γ(β)θα−1(1−θ)β−1 dθ=Γ(m+1)Γ(y+1)Γ(m−y+1)Γ(α+β)Γ(α)Γ(β)∫01θy+α−1(1−θ)m−y+β−1⏟an unnormalised Beta(y+α,  m−y+β)dθ.\begin{aligned} p(y)&=\int_0^1\binom my\theta^y(1-\theta)^{m-y}\cdot\frac{\Gamma(\alpha+\beta)}{\Gamma(\alpha)\Gamma(\beta)}\theta^{\alpha-1}(1-\theta)^{\beta-1}\,d\theta\\[2pt] &=\frac{\Gamma(m+1)}{\Gamma(y+1)\Gamma(m-y+1)}\frac{\Gamma(\alpha+\beta)}{\Gamma(\alpha)\Gamma(\beta)}\int_0^1\underbrace{\theta^{y+\alpha-1}(1-\theta)^{m-y+\beta-1}}_{\text{an unnormalised }\mathrm{Beta}(y+\alpha,\;m-y+\beta)}d\theta. \end{aligned}

The integral is not a new problem — it is a Beta density missing its constant. Supply the constant and it integrates to 1, leaving only gammas:

p(y)=Γ(m+1) Γ(α+β) Γ(y+α) Γ(m−y+β)Γ(y+1)Γ(m−y+1) Γ(α)Γ(β) Γ(m+α+β)=BetaBinomial(y∣m,α,β).p(y)=\frac{\Gamma(m+1)\,\Gamma(\alpha+\beta)\,\Gamma(y+\alpha)\,\Gamma(m-y+\beta)}{\Gamma(y+1)\Gamma(m-y+1)\,\Gamma(\alpha)\Gamma(\beta)\,\Gamma(m+\alpha+\beta)}=\mathrm{BetaBinomial}(y\mid m,\alpha,\beta).
Nothing here used which Beta it was. Feed it the prior and you get the prior predictive; feed it the posterior Beta(α+S,  β+n−S)\mathrm{Beta}(\alpha+S,\;\beta+n-S) and the same formula returns the posterior predictive.

This is the derivation on the original Lecture 2 PDF (prior predictive), reused for the posterior predictive two slides later. The trick — multiply and divide by the normaliser so the integrand becomes a density — is the same one used for the Gaussian in Lecture 4.

Lecture 2 · Fundamentals on Bayesian Statistics32 / 96
02 · BAYESIAN COIN2 · Bayesian estimation for coin flippingLecture 2

Which Beta you feed it is the whole difference

For our coin the posterior was Beta(4,3)\mathrm{Beta}(4,3), so the batch of mm future tosses is BetaBinomial(m,4,3)\mathrm{BetaBinomial}(m,4,3) — while plugging in the posterior mean θ^=4/7\hat\theta=4/7 would give Binomial(m,4/7)\mathrm{Binomial}(m,4/7).

Two future tosses P(K=0)P(K=0) P(K=1)P(K=1) P(K=2)P(K=2) mean variance
Plug in θ=4/7\theta=4/7 9/49=0.1849/49=0.184 24/49=0.49024/49=0.490 16/49=0.32716/49=0.327 8/78/7 24/4924/49
Average over θ\theta 3/14=0.2143/14=0.214 6/14=0.4296/14=0.429 5/14=0.3575/14=0.357 8/78/7 27/4927/49
Identical means, and mass moved out of the middle onto both ends. Averaging did not shift the prediction — it widened it, by exactly the 3/493/49 the plug-in threw away when it pretended to know θ\theta.

Both rows computed by integration and by the closed form; they agree. Try one toss, then a batch, on the next slide.

Lecture 2 · Fundamentals on Bayesian Statistics33 / 96
02 · BAYESIAN COIN2 · Bayesian estimation for coin flippingLecture 2

Experiment 4 — predict one toss, then a batch

Predict: do the two predictions agree for one toss? Try: set future tosses to 1, then 10, then 30. Explain: the posterior stays Beta(4,3); more future tosses share the same uncertain bias.

Source alignment: original Lecture 2 PDF 24, 31.

Lecture 2 · Fundamentals on Bayesian Statistics34 / 96
02 · BAYESIAN COIN2 · Bayesian estimation for coin flippingLecture 2

Three steps in Bayesian approaches

1 · Modellingspecify the sampling model and a prior
2 · Inferencecondition on data to obtain the posterior
3 · Checkinguse predictions to assess the model and its implications

For the coin: Bernoulli/Binomial + Beta → updated Beta → predictions of future heads.

We have completed one full example. Keep these steps; change the data model next.

Source alignment: original Lecture 2 PDF 25.

Lecture 2 · Fundamentals on Bayesian Statistics35 / 96
02 · BAYESIAN COIN2 · Bayesian estimation for coin flippingLecture 2

Check — who wins as the data piles up?

With a fixed Beta(α,β)\mathrm{Beta}(\alpha,\beta) prior and independent Bernoulli observations, what happens to the prior weight in the posterior mean as nn grows?

This is exact for the specified Beta–Binomial model. At n=α+βn=\alpha+\beta, prior and data weights are equal. At finite nn the prior still has positive weight. General claims about posterior concentration require additional assumptions.

Source alignment: original Lecture 2 PDF 22–25.

Lecture 2 · Fundamentals on Bayesian Statistics36 / 96
03 · CONJUGATE MODELS

3 · Conjugate pairs

Repeat the coin workflow in the original order: counts with Poisson–Gamma, measurements with Normal–Normal, and categories with Multinomial–Dirichlet.
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

Conjugacy — recognise the same family after updating

A prior is conjugate to a likelihood when the posterior stays in the prior's family. We can recognise and normalise the result analytically.

Data model and unknown quantity Prior Posterior
Binomial success probability Beta Beta
Poisson rate Gamma (shape, rate) Gamma
Normal mean, variance known Normal Normal
Normal variance, mean known Inverse Gamma Inverse Gamma
Normal mean and precision both unknown Normal–Gamma Normal–Gamma
Multinomial category probabilities Dirichlet Dirichlet

Conjugacy is algebraic convenience. It does not establish model fit or make every posterior mean a simple average of prior and sample means. Unknown Normal mean and variance require an appropriate joint prior.

Source alignment: original Lecture 2 PDF 26–28.

Lecture 2 · Fundamentals on Bayesian Statistics38 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

Binomial–Beta recap — our template for the next models

Step Coin calculation
Sampling model S∣θ∼Binomial(n,θ)S\mid\theta\sim\mathrm{Binomial}(n,\theta)
Prior θ∼Beta(α,β)\theta\sim\mathrm{Beta}(\alpha,\beta)
Posterior θ∣D∼Beta(α+S,β+n−S)\theta\mid D\sim\mathrm{Beta}(\alpha+S,\beta+n-S)
Prediction One toss: posterior mean; a batch: Beta–Binomial
Choose a model for the observations, update its parameters, then predict observations again. The next model counts events rather than successes out of a fixed number of trials.

Source alignment: original Lecture 2 PDF 29–32.

Lecture 2 · Fundamentals on Bayesian Statistics39 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

Counting events — why Poisson and Gamma?

Let YiY_i count events in equal observation windows, with a common unknown rate λ\lambda:

Poisson likelihood

p(Yi=yi∣λ)=λyie−λyi!.p(Y_i=y_i\mid\lambda)=\frac{\lambda^{y_i}e^{-\lambda}}{y_i!}.

YiY_i is a non-negative integer. Its conditional mean and variance both equal λ\lambda.

Gamma prior

p(λ)=baΓ(a)λa−1e−bλ.p(\lambda)=\frac{b^a}{\Gamma(a)}\lambda^{a-1}e^{-b\lambda}.

λ>0\lambda>0. Here aa is shape and bb is rate, with mean a/ba/b and variance a/b2a/b^2.

We use a,ba,b for Gamma to distinguish them from the coin's Beta parameters. A Gamma scale would be 1/b1/b, not bb.

Source alignment: original Lecture 2 PDF 33.

Lecture 2 · Fundamentals on Bayesian Statistics40 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

Poisson–Gamma updating — counts and exposure add

For conditionally independent, equal-exposure counts,

p(λ∣D)∝λ∑iyie−nλ⋅λa−1e−bλ=λa+∑iyi−1e−(b+n)λ.\begin{aligned} p(\lambda\mid D)&\propto\lambda^{\sum_i y_i}e^{-n\lambda}\cdot\lambda^{a-1}e^{-b\lambda}\\ &=\lambda^{a+\sum_i y_i-1}e^{-(b+n)\lambda}. \end{aligned}
λ∣D∼Gamma(a+∑iyi, b+n),E[λ∣D]=bb+nab+nb+nyˉ.\lambda\mid D\sim\mathrm{Gamma}(a+\textstyle\sum_i y_i,\ b+n),\qquad \E[\lambda\mid D]=\frac{b}{b+n}\frac ab+\frac{n}{b+n}\bar y.
Add events to the shape; add exposure to the rate. If Yi∣λ∼Poisson(tiλ)Y_i\mid\lambda\sim\mathrm{Poisson}(t_i\lambda), replace nn by total exposure ∑iti\sum_i t_i.

Source alignment: original Lecture 2 PDF 34–35.

Lecture 2 · Fundamentals on Bayesian Statistics41 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

The original example — Pokémon counts in 20 districts

The twenty districts, with their counts.
The twenty districts, with their counts.

The observations

14, 13, 7, 10, 15, 15, 2, 13, 13, 11, 10, 13, 5, 13, 9, 12, 9, 12, 8, 7.

Twenty districts: n=20n=20, total ∑iyi=211\sum_i y_i=211, average yˉ=10.55\bar y=10.55.

The source specifies prior mean 20 and standard deviation 10. In shape–rate notation,

ab=20,ab2=100⟹b=0.2,a=4.\frac ab=20,\qquad \frac{a}{b^2}=100 \quad\Longrightarrow\quad b=0.2,\qquad a=4.

The model treats districts as comparable units with one common rate. Unequal areas, search effort or spatial dependence require a richer model or exposure adjustment.

Source alignment: original Lecture 2 PDF 36.

Lecture 2 · Fundamentals on Bayesian Statistics42 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

The count estimate — calculateevery number on this slide comes from a formula two slides back

Prior Gamma(a,b)=Gamma(4, 0.2)\mathrm{Gamma}(a,b)=\mathrm{Gamma}(4,\,0.2); data n=20n=20 districts with ∑iyi=211\sum_i y_i=211, so yˉ=10.55\bar y=10.55.

Quantity Formula Put the numbers in Value
Posterior Gamma(a+∑iyi, b+n)\mathrm{Gamma}(a+\sum_i y_i,\ b+n) Gamma(4+211, 0.2+20)\mathrm{Gamma}(4+211,\ 0.2+20) Gamma(215, 20.2)\mathrm{Gamma}(215,\ 20.2)
Posterior mean a+∑iyib+n\dfrac{a+\sum_i y_i}{b+n} 21520.2\dfrac{215}{20.2} 10.644
Posterior variance a+∑iyi(b+n)2\dfrac{a+\sum_i y_i}{(b+n)^2} 21520.22=215408.04\dfrac{215}{20.2^2}=\dfrac{215}{408.04} 0.527
Standard deviation Var\sqrt{\mathrm{Var}} 0.527\sqrt{0.527} 0.726
95% credible interval 2.5% and 97.5% quantiles of Gamma(215, 20.2)\mathrm{Gamma}(215,\,20.2) by software, or the normal approximation 10.644±1.96×0.72610.644\pm1.96\times0.726 [9.27, 12.11] exact; [9.22, 12.07][9.22,\,12.07] approx.

The interval needs a quantile function — qgamma in R, scipy.stats.gamma.ppf in Python. The normal approximation lands within 0.05 of it here because the shape 215 is large; a Gamma is right-skewed, so the exact interval sits slightly to the right.

Lecture 2 · Fundamentals on Bayesian Statistics43 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

The count estimate — interpretthe same numbers, read as statements about the rate

Why the mean is 10.644

Two slides back the posterior mean was written as a weighted average of the prior mean and the sample mean:

E[λ∣D]=0.220.2⏟0.99%×20  +  2020.2⏟99.01%×10.55=0.198+10.446.\E[\lambda\mid D]=\underbrace{\tfrac{0.2}{20.2}}_{0.99\%}\times20\;+\;\underbrace{\tfrac{20}{20.2}}_{99.01\%}\times10.55=0.198+10.446.

The prior rate b=0.2b=0.2 is worth 0.2 of a district. Twenty real districts outweigh it a hundred to one, so the estimate lands next to yˉ\bar y.

What the interval says

With 95% probability the common rate λ\lambda lies in [9.27, 12.11][9.27,\ 12.11]: about nine to twelve sightings per district, on average.

It is a statement about λ\lambda, a parameter. It is not a range for the count in one new district — that count also carries Poisson noise, and its interval is wider. The next slides make that distinction exact.

This interval describes the common rate λ\lambda. It is not a prediction interval for the count in one new district.

Source alignment: original Lecture 2 PDF 35–37.

Lecture 2 · Fundamentals on Bayesian Statistics44 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

Experiment 5 — observe the districts one by one

Predict: will 20 districts outweigh the original prior? Try: move from 0 to 1 to 20 districts; then increase prior exposure while keeping its mean fixed. Explain: distinguish uncertainty in the rate from variability in a new district.

Source alignment: original Lecture 2 PDF 35–37.

Lecture 2 · Fundamentals on Bayesian Statistics45 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

Predict one new district — rate uncertainty is not count noise

Return to λ∣D∼Gamma(215,20.2)\lambda\mid D\sim\mathrm{Gamma}(215,20.2). For one comparable new district, Y~∣λ∼Poisson(λ)\tilde Y\mid\lambda\sim\mathrm{Poisson}(\lambda).

The law of total variance gives

E[Y~∣D]=E[λ∣D]=10.644,Var(Y~∣D)=E[λ∣D]⏟count noise+Var(λ∣D)⏟rate uncertainty≈10.644+0.527≈11.17.\begin{aligned} \E[\tilde Y\mid D]&=\E[\lambda\mid D]=10.644,\\ \mathrm{Var}(\tilde Y\mid D)&=\underbrace{\E[\lambda\mid D]}_{\text{count noise}} +\underbrace{\mathrm{Var}(\lambda\mid D)}_{\text{rate uncertainty}}\\ &\approx10.644+0.527\approx11.17. \end{aligned}
The rate's posterior standard deviation is 0.726; a new count's predictive standard deviation is 3.342. Knowing the rate well does not make every district identical.

The predictive distribution is Negative Binomial, a Poisson–Gamma mixture. Its probability mass function and parameter convention are in the appendix.

Source alignment: original Lecture 2 PDF 37.

Lecture 2 · Fundamentals on Bayesian Statistics46 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

Prior ×\times likelihood — write down what is being multiplied

Let Yi∣θ∼N(θ,σ2)Y_i\mid\theta\sim\mathcal N(\theta,\sigma^2) independently with σ2\sigma^2 known, and θ∼N(μ0,τ02)\theta\sim\mathcal N(\mu_0,\tau_0^2).

Likelihood

p(y∣θ)=∏i=1n12πσ2exp⁡{−(yi−θ)22σ2}p(y\mid\theta)=\prod_{i=1}^{n}\frac{1}{\sqrt{2\pi\sigma^2}}\exp\Big\{-\frac{(y_i-\theta)^2}{2\sigma^2}\Big\}

Prior

p(θ)=12πτ02exp⁡{−(θ−μ0)22τ02}p(\theta)=\frac{1}{\sqrt{2\pi\tau_0^2}}\exp\Big\{-\frac{(\theta-\mu_0)^2}{2\tau_0^2}\Big\}

Only the shape in θ\theta matters, so every factor without a θ\theta in it goes into the constant:

p(θ∣y)  ∝  p(y∣θ) p(θ)  ∝  exp⁡{−12σ2∑i=1n(yi−θ)2−12τ02(θ−μ0)2}p(\theta\mid y)\;\propto\;p(y\mid\theta)\,p(\theta)\;\propto\;\exp\Big\{-\frac{1}{2\sigma^2}\sum_{i=1}^{n}(y_i-\theta)^2-\frac{1}{2\tau_0^2}(\theta-\mu_0)^2\Big\}
Multiply two Gaussians and the exponent is a quadratic in θ\theta — so the posterior has no choice but to be Gaussian.

Source alignment: original Lecture 2 PDF 39–40.

Lecture 2 · Fundamentals on Bayesian Statistics47 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

Multiply out, collect, complete the square

Expand both squares, writing yˉ=1n∑iyi\bar y=\tfrac1n\sum_i y_i and keeping only the terms that carry a θ\theta:

∑i(yi−θ)2=nθ2−2θ nyˉ+∑iyi2(θ−μ0)2=θ2−2θμ0+μ02\begin{aligned} \sum_i (y_i-\theta)^2 &= n\theta^{2}-2\theta\,n\bar y+\textstyle\sum_i y_i^{2}\\[2pt] (\theta-\mu_0)^2 &= \theta^{2}-2\theta\mu_0+\mu_0^{2} \end{aligned}

Divide each by its variance and gather the two powers of θ\theta:

−12[(nσ2+1τ02)⏟A θ2  −  2(nyˉσ2+μ0τ02)⏟B θ]-\frac12\Big[\underbrace{\Big(\tfrac{n}{\sigma^2}+\tfrac{1}{\tau_0^2}\Big)}_{A}\,\theta^{2}\;-\;2\underbrace{\Big(\tfrac{n\bar y}{\sigma^2}+\tfrac{\mu_0}{\tau_0^2}\Big)}_{B}\,\theta\Big]

One completed square finishes it — and the leftover B2/AB^2/A has no θ\theta, so it joins the constant:

Aθ2−2Bθ=A(θ−BA)2−B2A⟹p(θ∣y)∝exp⁡{−A2(θ−BA)2}A\theta^{2}-2B\theta=A\Big(\theta-\tfrac BA\Big)^{2}-\tfrac{B^{2}}{A} \qquad\Longrightarrow\qquad p(\theta\mid y)\propto\exp\Big\{-\tfrac{A}{2}\Big(\theta-\tfrac BA\Big)^{2}\Big\}

That is a Normal density with precision AA and mean B/AB/A. Nothing was assumed; it fell out.

Source alignment: original Lecture 2 PDF 40–41. The appendix repeats the same completion in log space for anyone who prefers it that way.

Lecture 2 · Fundamentals on Bayesian Statistics48 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

Normal observations — combine information by precision

Reading AA and B/AB/A back gives the posterior in the form worth remembering.

Precisions add

1τ12=1τ02⏟prior+nσ2⏟data\frac{1}{\tau_1^{2}}=\underbrace{\frac{1}{\tau_0^{2}}}_{\text{prior}}+\underbrace{\frac{n}{\sigma^{2}}}_{\text{data}}

Yˉ∣θ∼N(θ,σ2/n)\bar Y\mid\theta\sim\mathcal N(\theta,\sigma^2/n), so the data carry precision n/σ2n/\sigma^2 — information adds, variances do not.

The mean is a weighted average

μ1=(1/τ02)μ0+(n/σ2)yˉ1/τ02+n/σ2\mu_1=\frac{(1/\tau_0^{2})\mu_0+(n/\sigma^{2})\bar y}{1/\tau_0^{2}+n/\sigma^{2}}

Each side is weighted by exactly the precision it brings, and θ∣D∼N(μ1,τ12)\theta\mid D\sim\mathcal N(\mu_1,\tau_1^{2}).

Sharper prior

τ02↓\tau_0^{2}\downarrow — the prior mean μ0\mu_0 is more trusted and pulls μ1\mu_1 harder.

Quieter sensor

σ2↓\sigma^{2}\downarrow — each reading is more precise, so yˉ\bar y pulls harder.

More data

n↑n\uparrow — yˉ\bar y pulls harder still, and eventually the prior stops mattering.

Source alignment: original Lecture 2 PDF 42. The three sensitivity readings are the source's own and were missing from the deck.

Lecture 2 · Fundamentals on Bayesian Statistics49 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

Four temperature readings — add the precisionsthe previous slide's two formulas, with numbers put in

Prior θ∼N(20,22)\theta\sim\mathcal N(20,2^2); measurement noise has standard deviation 33; four readings average 2323.

Step 1 — state each source as a precision, since that is the currency that adds:

1τ02=122=14⏟the priorandnσ2=432=49⏟four readings, yˉ=23.\underbrace{\frac{1}{\tau_0^2}=\frac{1}{2^2}=\frac14}_{\text{the prior}} \qquad\text{and}\qquad \underbrace{\frac{n}{\sigma^2}=\frac{4}{3^2}=\frac49}_{\text{four readings, }\bar y=23}.

Step 2 — add them, then invert to come back to a variance:

1τ12=14+49=2536,τ12=3625=1.44,τ1=1.2.\frac{1}{\tau_1^2}=\frac14+\frac49=\frac{25}{36}, \qquad \tau_1^2=\frac{36}{25}=1.44,\qquad \tau_1=1.2.

Source alignment: original Lecture 2 PDF 40–42.

Lecture 2 · Fundamentals on Bayesian Statistics50 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

Four temperature readings — weight the means36% prior, 64% data — and the estimate lands at 21.92

Step 3 — weight each mean by the precision that carries it. Dividing through by the total precision 25/3625/36 turns the formula into an ordinary weighted average:

μ1=14(20)+49(23)2536=925⏟0.36(20)+1625⏟0.64(23)=21.92.\mu_1=\frac{\tfrac14(20)+\tfrac49(23)}{\tfrac{25}{36}} =\underbrace{\tfrac{9}{25}}_{0.36}(20)+\underbrace{\tfrac{16}{25}}_{0.64}(23)=21.92.
The four readings took 64% of the weight and left the prior 36%, so the estimate moved from 20 to 21.92 — most of the way to the data, but not all of it.

Precision decides the split, and four readings at σ=3\sigma=3 are worth just under twice a prior at τ0=2\tau_0=2.

Source alignment: original Lecture 2 PDF 40–42.

Lecture 2 · Fundamentals on Bayesian Statistics51 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

Temperature thread — update an uncertain temperatureshared teaching example · predict before revealing the calculation

The true temperature θ\theta has prior N(20,4)\mathcal N(20,4). One sensor reading is y=22y=22, with y∣θ∼N(θ,1)y\mid\theta\sim\mathcal N(\theta,1). The second parameter here is variance.

Predict: Will the posterior mean be closer to 20 or to 22? Will its variance equal the variance of a new reading?

Calculate and check. The gain is K=4/(4+1)=0.8K=4/(4+1)=0.8. The posterior mean is 20+0.8(22−20)=21.620+0.8(22-20)=\mathbf{21.6} and variance is (1−0.8)4=0.8(1-0.8)4=\mathbf{0.8}. A new reading with independent sensor noise has predictive variance 0.8+1=1.80.8+1=\mathbf{1.8}.

Learning the unknown temperature and predicting another noisy measurement are different questions.
Lecture 2 · Fundamentals on Bayesian Statistics52 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

Try it — the sensor variance rises to 4work independently · reveal only after writing an answer

Keep the prior and the observed value 22. Change the sensor variance from 1 to 4, for both this and the next reading. Find the posterior mean, posterior variance and predictive variance.

Check your answer. K=4/(4+4)=0.5K=4/(4+4)=0.5, posterior mean 21, posterior variance 2, predictive variance 6. The less precise reading pulls the posterior less strongly.

State which distribution each variance belongs to before adding or comparing numbers.
Lecture 2 · Fundamentals on Bayesian Statistics53 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

Experiment 6 — vary precision with the same average

Predict: does noisier data move the estimate closer to 20 or 23? Try: change noise SD, prior SD, and the number of readings. Explain: the average stays at 23; precision determines its weight. The grey likelihood is normalised for plotting.

Source alignment: original Lecture 2 PDF 39–42.

Lecture 2 · Fundamentals on Bayesian Statistics54 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

Normal prediction — restore the observation noise

The updated mean parameter is θ∣D∼N(μ1,τ12)\theta\mid D\sim\mathcal N(\mu_1,\tau_1^2). A new reading is Y~=θ+ϵ\tilde Y=\theta+\epsilon, with independent ϵ∼N(0,σ2)\epsilon\sim\mathcal N(0,\sigma^2).

Y~∣D∼N(μ1, τ12+σ2).\tilde Y\mid D\sim\mathcal N(\mu_1,\ \tau_1^2+\sigma^2).

For the previous four readings:

θ∣D∼N(21.92,1.44),Y~∣D∼N(21.92,10.44).\theta\mid D\sim\mathcal N(21.92,1.44),\qquad \tilde Y\mid D\sim\mathcal N(21.92,10.44).
Same centre, different uncertainty. Learning the temperature does not remove noise from the thermometer.

Source alignment: original Lecture 2 PDF 43–44.

Lecture 2 · Fundamentals on Bayesian Statistics55 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

Experiment 7 — compare the mean and the next reading

Predict: which curve keeps a noise floor? Try: increase readings from 1 to 100. Explain: posterior parameter variance shrinks, while predictive variance remains 9 plus that variance. Here noise SD is fixed at 3 and prior SD at 2.

Source alignment: original Lecture 2 PDF 43–44.

Lecture 2 · Fundamentals on Bayesian Statistics56 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

More than two outcomes — the Dirichlet updatethe categorical model

Suppose each service request is delivery, pickup or return. The probabilities form a vector θ\theta with non-negative entries adding to 1.

θ∼Dirichlet(α1,α2,α3),(Y1,Y2,Y3)∣θ∼Multinomial(n,θ).\theta\sim\mathrm{Dirichlet}(\alpha_1,\alpha_2,\alpha_3),\qquad (Y_1,Y_2,Y_3)\mid\theta\sim\mathrm{Multinomial}(n,\theta).

The likelihood adds one count to the corresponding exponent for each observation:

θ∣D∼Dirichlet(α1+Y1,α2+Y2,α3+Y3).\theta\mid D\sim\mathrm{Dirichlet}(\alpha_1+Y_1,\alpha_2+Y_2,\alpha_3+Y_3).
The Beta update with a longer list of categories. With two categories, the Dirichlet reduces to a Beta distribution for the first probability.

Source alignment: original Lecture 2 PDF 45–46.

Lecture 2 · Fundamentals on Bayesian Statistics57 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

An unseen category need not get zero probability

Start from Dirichlet(1,1,1)\mathrm{Dirichlet}(1,1,1) and observe ten requests: 6 deliveries, 4 pickups, 0 returns.

Category Count MLE Posterior parameter Next-request probability
Delivery 6 6/106/10 1+6=71+6=7 7/137/13
Pickup 4 4/104/10 1+4=51+4=5 5/135/13
Return 0 0/100/10 1+0=11+0=1 1/131/13

The posterior is Dirichlet(7,5,1)\mathrm{Dirichlet}(7,5,1) and predictive probabilities add to 1. The prior gives an unseen but possible category some support.

This update can estimate the rows of a transition matrix. Ten observations without a return do not prove returns are impossible.

Source alignment: original Lecture 2 PDF 46–47.

Lecture 2 · Fundamentals on Bayesian Statistics58 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

Experiment 8 — observe a previously unseen category

Predict: is a return impossible after zero observed returns? Try: add Return, change the prior strength, then clear the data. Explain: posterior parameters are prior pseudo-counts plus observed counts; divide by their sum to predict one request.

Source alignment: original Lecture 2 PDF 46–47.

Lecture 2 · Fundamentals on Bayesian Statistics59 / 96
03 · CONJUGATE MODELS3 · Conjugate pairsLecture 2

Check — what uncertainty belongs in a new measurement?

With known noise variance σ2\sigma^2 and posterior mean variance τ12\tau_1^2, what is the variance of the next Normal observation?

The unknown mean and independent noise both vary in the predictive calculation. More data learns the mean but does not eliminate the assumed observation noise. We will use exactly this idea for regression.

Source alignment: original Lecture 2 PDF 43–47.

Lecture 2 · Fundamentals on Bayesian Statistics60 / 96
04 · REGRESSION

4 · Bayesian regression and regularisation

Follow the original regression route: formulate the task, solve least squares, interpret MLE, keep a Bayesian posterior, then motivate and interpret regularisation.
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

Problem solving — estimate a housing price

Task → model → algorithm. The two house-price observations are illustrative, not an empirical housing dataset.
Task → model → algorithm. The two house-price observations are illustrative, not an empirical housing dataset.
We now learn a vector of weights. The Bayesian logic stays the same as for a coin, a rate, or a mean.

Source alignment: original Lecture 2 PDF 49–51.

Lecture 2 · Fundamentals on Bayesian Statistics62 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

Regression roadmap — the same model from several views

We keep yi=xi⊤w+ϵiy_i=x_i^\top w+\epsilon_i throughout.

1 · Optimisationminimise squared residuals → normal equation
2 · MLEGaussian noise → the same least-squares fit
3 · Full Bayes and MAPprior + likelihood → posterior; its peak is MAP
4 · Regularisationshrink weights → ridge or lasso; interpret the prior
One regression model, several ways to describe what learning means. We will connect the objectives instead of introducing unrelated methods.

Source alignment: original Lecture 2 PDF 52.

Lecture 2 · Fundamentals on Bayesian Statistics63 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

Put a line into matrix form

Include the intercept by adding a constant feature 1. For the illustrative data (x,y)=(0,1),(1,2),(2,2)(x,y)=(0,1),(1,2),(2,2),

X=(101112),w=(w0w1),y=(122),Xw=(w0w0+w1w0+2w1).X=\begin{pmatrix}1&0\\1&1\\1&2\end{pmatrix},\qquad w=\begin{pmatrix}w_0\\w_1\end{pmatrix},\qquad y=\begin{pmatrix}1\\2\\2\end{pmatrix},\qquad Xw=\begin{pmatrix}w_0\\w_0+w_1\\w_0+2w_1\end{pmatrix}.
Each row is one observation. Each column is one feature. The residual vector is y−Xwy-Xw.

In general X∈Rn×dX\in\mathbb R^{n\times d}, w∈Rdw\in\mathbb R^d and y∈Rny\in\mathbb R^n. Here dd includes the intercept. Keep this orientation throughout the derivation.

Source alignment: original Lecture 2 PDF 53–54.

Lecture 2 · Fundamentals on Bayesian Statistics64 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

Least squares — use Lecture 1's optimality condition

Choose weights to minimise squared residuals:

J(w)=12∥y−Xw∥2,∇J(w)=X⊤(Xw−y).J(w)=\frac12\lVert y-Xw\rVert^2,\qquad \nabla J(w)=X^\top(Xw-y).

The Hessian is X⊤X⪰0X^\top X\succeq0, so JJ is convex. Therefore

w^ minimises J⟺X⊤Xw^=X⊤y.\hat w\text{ minimises }J\quad\Longleftrightarrow\quad X^\top X\hat w=X^\top y.
This is the normal equation. The zero-gradient condition is exact here because the problem is convex, differentiable and unconstrained.

Only when XX has full column rank is the minimiser unique and (X⊤X)−1X⊤y(X^\top X)^{-1}X^\top y valid. Otherwise minimisers are not unique; use QR or SVD methods for the linear algebra instead of explicitly forming an inverse.

Source alignment: original Lecture 2 PDF 55–56.

Lecture 2 · Fundamentals on Bayesian Statistics65 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

Solve the three-point regression by hand

For the previous three observations,

X⊤X=(3335),X⊤y=(56).X^\top X=\begin{pmatrix}3&3\\3&5\end{pmatrix},\qquad X^\top y=\begin{pmatrix}5\\6\end{pmatrix}.

The normal equation gives 3w0+3w1=53w_0+3w_1=5 and 3w0+5w1=63w_0+5w_1=6. Subtract to get w1=1/2w_1=1/2, then w0=7/6w_0=7/6.

y^=(7/65/313/6),y−y^=(−1/61/3−1/6),∥y−y^∥2=1/6.\hat y=\begin{pmatrix}7/6\\5/3\\13/6\end{pmatrix},\qquad y-\hat y=\begin{pmatrix}-1/6\\1/3\\-1/6\end{pmatrix},\qquad \lVert y-\hat y\rVert^2=1/6.
A best-fitting line need not pass through every point. It balances the residuals to minimise their squared total.

Source alignment: original Lecture 2 PDF 55–56.

Lecture 2 · Fundamentals on Bayesian Statistics66 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

Why Gaussian noise turns MLE into least squares

Assume independent noise ϵi∼N(0,σ2)\epsilon_i\sim\mathcal N(0,\sigma^2), with known σ2>0\sigma^2>0:

p(y∣X,w)=∏i=1n12πσ2exp⁡ ⁣[−(yi−xi⊤w)22σ2].p(y\mid X,w)=\prod_{i=1}^n\frac{1}{\sqrt{2\pi\sigma^2}} \exp\!\left[-\frac{(y_i-x_i^\top w)^2}{2\sigma^2}\right].

Take the negative log:

−log⁡p(y∣X,w)=n2log⁡(2πσ2)⏟constant in w+12σ2∥y−Xw∥2.-\log p(y\mid X,w)=\underbrace{\frac n2\log(2\pi\sigma^2)}_{\text{constant in }w} +\frac{1}{2\sigma^2}\lVert y-Xw\rVert^2.
For fixed σ2\sigma^2, maximising this likelihood is exactly minimising squared error. Least squares itself does not require Gaussian noise; this probabilistic interpretation does.

Source alignment: original Lecture 2 PDF 57–58.

Lecture 2 · Fundamentals on Bayesian Statistics67 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

Experiment 9 — move the line and watch its residuals

Predict: does making one residual zero minimise the total error? Try: adjust intercept and slope, then select Fit least squares. Explain: the same minimiser gives SSE = 1/6 and maximises the displayed Gaussian likelihood.

Source alignment: original Lecture 2 PDF 53–58.

Lecture 2 · Fundamentals on Bayesian Statistics68 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

From MLE to Bayesian regression

MLE: choose one weight vector

w^ML=arg max⁡wp(y∣X,w).\hat w_{\rm ML}=\argmax_w p(y\mid X,w).

We retain the weight vector giving the largest likelihood.

Bayesian: update a distribution

p(w∣X,y)∝p(y∣X,w)p(w).p(w\mid X,y)\propto p(y\mid X,w)p(w).

MAP is its most dense point; full Bayes also keeps the spread and correlations.

The prior acts on weights, just as the Beta prior acted on the coin bias. A Gaussian prior makes this update analytically tractable.

Source alignment: original Lecture 2 PDF 59–61.

Lecture 2 · Fundamentals on Bayesian Statistics69 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

Complete the square again — now with a matrix

Same two factors as before, with ww in place of θ\theta. Keep only what carries a ww:

log⁡p(w∣X,y)=−12σ2(y−Xw)⊤(y−Xw)−12τ2w⊤w+C.\log p(w\mid X,y)=-\frac{1}{2\sigma^2}(y-Xw)^\top(y-Xw)-\frac{1}{2\tau^2}w^\top w+C.

Expand (y−Xw)⊤(y−Xw)=y⊤y−2w⊤X⊤y+w⊤X⊤Xw(y-Xw)^\top(y-Xw)=y^\top y-2w^\top X^\top y+w^\top X^\top Xw and gather the two powers of ww:

−12[ w⊤(X⊤Xσ2+Iτ2)⏟Aw  −  2 w⊤X⊤yσ2⏟b]-\frac12\Big[\,w^\top\underbrace{\Big(\tfrac{X^\top X}{\sigma^2}+\tfrac{I}{\tau^2}\Big)}_{A}w\;-\;2\,w^\top\underbrace{\tfrac{X^\top y}{\sigma^2}}_{b}\Big]

The scalar identity survives transposition — AA is symmetric, so

w⊤Aw−2w⊤b=(w−A−1b)⊤A (w−A−1b)−b⊤A−1b,w^\top Aw-2w^\top b=(w-A^{-1}b)^\top A\,(w-A^{-1}b)-b^\top A^{-1}b,

and the leftover carries no ww. What is left is a Gaussian with precision matrix AA and mean A−1bA^{-1}b.

Line for line the scalar derivation, with a matrix where the number was: AA was nσ2+1τ02\tfrac n{\sigma^2}+\tfrac1{\tau_0^2} and is now X⊤Xσ2+Iτ2\tfrac{X^\top X}{\sigma^2}+\tfrac I{\tau^2}; the mean was B/AB/A and is now A−1bA^{-1}b. Precision still adds; the mean is still precision-weighted.

Source alignment: original Lecture 2 PDF 60–61. This is the appendix's log-space completion, moved into the body — the result is unusable as a formula to remember unless you have seen where the two terms of AA came from.

Lecture 2 · Fundamentals on Bayesian Statistics70 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

Full Bayes keeps the distribution over weights

With independent noise ϵi∼N(0,σ2)\epsilon_i\sim\mathcal N(0,\sigma^2) and prior w∼N(0,τ2I)w\sim\mathcal N(0,\tau^2I), the posterior is also Gaussian:

w∣X,y∼N(μw,Σw).w\mid X,y\sim\mathcal N(\mu_w,\Sigma_w).

The centre

μw=ΣwX⊤yσ2.\mu_w=\Sigma_w\frac{X^\top y}{\sigma^2}.

Here the mean and mode coincide, so μw\mu_w is also the MAP estimate. Later we will connect it to ridge regression.

The uncertainty

Σw−1=X⊤Xσ2+Iτ2.\Sigma_w^{-1}=\frac{X^\top X}{\sigma^2}+\frac{I}{\tau^2}.

Data precision plus prior precision. Large posterior variance marks directions in the weights that remain uncertain.

Read straight off the previous slide: Σw=A−1\Sigma_w=A^{-1} and μw=A−1b\mu_w=A^{-1}b. Known positive σ2\sigma^2 and τ2\tau^2 are assumed, which is what makes AA positive definite — and therefore invertible — even when XX has fewer rows than columns.

Source alignment: original Lecture 2 PDF 60–61.

Lecture 2 · Fundamentals on Bayesian Statistics71 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

Bayesian regression — one posterior in two spaces

Two rows, three columns, on nested simulated datasets (n 2, 10, 100). Blue: MLE; green: posterior mean/MAP and sampled lines; ×: true weight
Two rows, three columns, on nested simulated datasets (n = 2, 10, 100). Blue: MLE; green: posterior mean/MAP and sampled lines; ×: true weights. Corresponding axes use common scales.
Top: plausible mean functions. Bottom: plausible weights. More informative data contracts both views of the same posterior.

Source alignment: original Lecture 2 PDF 62.

Lecture 2 · Fundamentals on Bayesian Statistics72 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

Experiment 10 — change data, watch the posterior over a line

Predict: which fit is most unstable with two cases? Try: compare 2, 10, and 100 nested cases, then draw new data. Explain: the left panel shows sampled mean functions; the right shows the same uncertainty in coefficient space.

Source alignment: original Lecture 2 PDF 59–62.

Lecture 2 · Fundamentals on Bayesian Statistics73 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

Regression prediction — a line and a noisy observation

At a new feature vector x∗x_*, the fitted mean is f∗=x∗⊤wf_*=x_*^\top w. Since w∣D∼N(μw,Σw)w\mid D\sim\mathcal N(\mu_w,\Sigma_w),

f∗∣D∼N ⁣(x∗⊤μw, x∗⊤Σwx∗).f_*\mid D\sim\mathcal N\!\left(x_*^\top\mu_w,\ x_*^\top\Sigma_w x_*\right).

An individual response also includes noise, Y~∗=f∗+ϵ∗\tilde Y_*=f_*+\epsilon_*:

Y~∗∣D∼N ⁣(x∗⊤μw,x∗⊤Σwx∗⏟parameter uncertainty+σ2⏟observation noise).\tilde Y_*\mid D\sim\mathcal N\!\left(x_*^\top\mu_w, \underbrace{x_*^\top\Sigma_w x_*}_{\text{parameter uncertainty}}+\underbrace{\sigma^2}_{\text{observation noise}}\right).
Drawing posterior lines shows uncertainty about the mean function. Add noise to predict actual observations. Both depend on the linear model being appropriate.

Source alignment: original Lecture 2 PDF 62.

Lecture 2 · Fundamentals on Bayesian Statistics74 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

When the integral is hard — simulate the two stages

For s=1,…,Ms=1,\dots,M:

1 · Draw a parameterθ(s)∼p(θ∣D)\theta^{(s)}\sim p(\theta\mid D)
2 · Draw an observationy~(s)∼p(y~∣θ(s))\tilde y^{(s)}\sim p(\tilde y\mid\theta^{(s)})
3 · Summarisehistogram, quantiles, event probabilities

For regression, draw w(s)w^{(s)} from its Gaussian posterior, then draw noise and set y~∗(s)=x∗⊤w(s)+ϵ∗(s)\tilde y_*^{(s)}=x_*^\top w^{(s)}+\epsilon_*^{(s)}.

Conjugate examples allow direct sampling. Other models may require MCMC or an approximation, whose accuracy must be checked. See the Stan posterior prediction guide for the same two-stage construction.

Source alignment: original Lecture 2 PDF 62.

Lecture 2 · Fundamentals on Bayesian Statistics75 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

Why regularise a regression?

The original goal is to predict new observations, not only to fit the training cases.

A flexible fit

Extra features can lower training error. With limited or correlated data, fitted weights may change substantially when the sample changes.

A controlled fit

Penalising large weights can reduce this sensitivity. We accept some fitting bias in exchange for potentially better predictions.

w^ridge=arg min⁡w{∥y−Xw∥2+λ2∥w∥2}.\hat w_{\rm ridge}=\argmin_w\{\|y-Xw\|^2+\lambda_2\|w\|^2\}.
Check the tradeoff on independent validation data. Neither more flexibility nor more shrinkage is automatically better.

Source alignment: original Lecture 2 PDF 63–65.

Lecture 2 · Fundamentals on Bayesian Statistics76 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

Why shrinkage helps — and what it changes

Ridge changes the normal equation to

(X⊤X+λ2I)w^=X⊤y.\big(X^\top X+\lambda_2 I\big)\hat w=X^\top y.

Small or correlated datasets

Several weight vectors may fit almost equally well. Small data perturbations can then produce large changes in the OLS weights.

Adding λ2I\lambda_2I makes the system positive definite for λ2>0\lambda_2>0 and stabilises these directions.

The tradeoff

Shrinkage introduces bias toward the prior mean for potentially lower variance. It can help prediction but does not guarantee better test performance.

Choose strength with prior knowledge and appropriate validation; inspect feature scaling.

The Gaussian-posterior example gives all weights a prior. The next experiment leaves the intercept unpenalised; applications often treat it separately.

Source alignment: original Lecture 2 PDF 64–65.

Lecture 2 · Fundamentals on Bayesian Statistics77 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

Experiment 11 — compare training fit with validation error

Predict: must the best training fit predict best? Try: sweep λ on one fixed dataset, then change its size or draw new training data. Explain: polynomial features keep the model linear in its weights; validation data stays separate and fixed.

Source alignment: original Lecture 2 PDF 63–66.

Lecture 2 · Fundamentals on Bayesian Statistics78 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

Add a Gaussian prior — derive ridge, not just name it

Let w∼N(0,τ2I)w\sim\mathcal N(0,\tau^2I) with known τ2>0\tau^2>0. The negative log posterior is

−log⁡p(w∣X,y)=12σ2∥y−Xw∥2+12τ2∥w∥2+constant.-\log p(w\mid X,y)=\frac{1}{2\sigma^2}\lVert y-Xw\rVert^2 +\frac{1}{2\tau^2}\lVert w\rVert^2+\text{constant}.

Multiply by 2σ22\sigma^2, which does not change the minimiser:

w^MAP=arg min⁡w{∥y−Xw∥2+λ2∥w∥2},λ2=σ2/τ2.\hat w_{\rm MAP}=\argmin_w\left\{\lVert y-Xw\rVert^2+\lambda_2\lVert w\rVert^2\right\},\qquad \hl{\lambda_2=\sigma^2/\tau^2.}
A smaller prior variance penalises large weights more strongly. The regulariser is the negative log prior, with the noise scale accounted for.

Source alignment: original Lecture 2 PDF 67–68.

Lecture 2 · Fundamentals on Bayesian Statistics79 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

Lasso — a different prior changes the penalty

Independent Laplace priors with scale bL>0b_L>0 have

p(w)=∏j12bLe−∣wj∣/bL.p(w)=\prod_j\frac{1}{2b_L}e^{-|w_j|/b_L}.

With the same Gaussian likelihood,

w^MAP=arg min⁡w{∥y−Xw∥2+λ1∥w∥1},λ1=2σ2bL.\hat w_{\rm MAP}=\argmin_w\left\{\lVert y-Xw\rVert^2+\lambda_1\lVert w\rVert_1\right\},\qquad \lambda_1=\frac{2\sigma^2}{b_L}.
The absolute-value penalty has a corner at zero, so some MAP coefficients can be exactly zero. A continuous Laplace prior has no probability mass at the single point zero.

Source alignment: original Lecture 2 PDF 66, 69.

Lecture 2 · Fundamentals on Bayesian Statistics80 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

Experiment 12 — see why ridge shrinks and lasso can select

Predict: which constraint has corners on the axes? Try: change λ and switch between ridge and lasso. Explain: the contact point is the fitted weight vector. Priors are scaled to peak height 1 to compare shapes; this illustration uses noise variance 1.

Source alignment: original Lecture 2 PDF 65–69.

Lecture 2 · Fundamentals on Bayesian Statistics81 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

Check — the prior wearing a disguise

Under Gaussian noise, what prior gives MAP the ridge objective ∥y−Xw∥2+λ2∥w∥2\lVert y-Xw\rVert^2+\lambda_2\lVert w\rVert^2?

Taking the negative log turns the Gaussian likelihood into squared residuals and the Gaussian prior into a squared-weight penalty. For this unaveraged objective, λ2=σ2/τ2\lambda_2=\sigma^2/\tau^2. Averaging the data loss by nn changes the coefficient convention.

Source alignment: original Lecture 2 PDF 67–69.

Lecture 2 · Fundamentals on Bayesian Statistics82 / 96
04 · REGRESSION4 · Bayesian regression and regularisationLecture 2

What we can now calculate

Task Calculation Example
Update a belief posterior ∝\propto prior ×\times likelihood Beta(2,2) + HHT → Beta(4,3)
Choose a point MLE, MAP or a loss-based posterior summary Gaussian prior + Gaussian noise → ridge
Predict data average the sampling model over the posterior uncertain Poisson rate → Negative Binomial
Check the model simulate, compare and revise replicated counts or regression residuals
Lecture 3 asks how to represent many interacting variables. Conditional independence makes a large probabilistic model manageable.

Source alignment: original Lecture 2 PDF 70.

Lecture 2 · Fundamentals on Bayesian Statistics83 / 96
Questions?
Can you explain which uncertainty is over a parameter, which is over an observation, and where each appears in the calculation?

Source alignment: original Lecture 2 PDF 70.

APPENDIX

Appendix · extra derivations and checks

Optional details for questions and self-study; the main lecture has completed the original route.
APPENDIXAppendix · extra derivations and checksLecture 2

Reading guide — one Bayesian calculation, several observation modelsone main idea to explain, one comparison, one application

Role Read or revisit Question to answer
Core Murphy, Probabilistic Machine Learning: An Introduction (2022): probability, Bayesian statistics and linear regression sections What distribution is specified, updated, and used for prediction?
Compare MLE, MAP and full Bayes on the same coin or regression data Which answer is a point estimate, and which carries uncertainty?
Apply The original Pokémon district counts and housing-price example How does the observation model affect the estimate and prediction?
Use selected sections as a reference, not the entire book as an assignment. For each example write the likelihood, prior, posterior and predictive target before doing algebra.
Lecture 2 · Fundamentals on Bayesian Statistics86 / 96
APPENDIXAppendix · extra derivations and checksLecture 2

Backup — integrate the Beta–Binomial prediction

Write α′=α+S\alpha'=\alpha+S, β′=β+n−S\beta'=\beta+n-S. For KK heads in mm future tosses,

P(K=k∣D)=∫01(mk)θk(1−θ)m−kθα′−1(1−θ)β′−1B(α′,β′) dθ=(mk)B(α′+k,β′+m−k)B(α′,β′).\begin{aligned} P(K=k\mid D)&=\int_0^1\binom mk\theta^k(1-\theta)^{m-k} \frac{\theta^{\alpha'-1}(1-\theta)^{\beta'-1}}{B(\alpha',\beta')}\,d\theta\\[4pt] &=\binom mk\frac{B(\alpha'+k,\beta'+m-k)}{B(\alpha',\beta')}. \end{aligned}

For m=2m=2 and Beta(4,3)\mathrm{Beta}(4,3), probabilities of K=0,1,2K=0,1,2 are 3/14,6/14,5/143/14,6/14,5/14. They sum to 1.

Before observing data, use α,β\alpha,\beta to obtain the prior predictive. With a Beta(2,2)\mathrm{Beta}(2,2) prior and two tosses, its probabilities are 0.3,0.4,0.30.3,0.4,0.3. Future batch size mm need not equal training size nn.

Source alignment: original Lecture 2 PDF 24, 31.

Lecture 2 · Fundamentals on Bayesian Statistics87 / 96
APPENDIXAppendix · extra derivations and checksLecture 2

Backup — MLE, MAP and posterior mean

Estimate Definition Beta(2,2) prior + HHT
MLE arg max⁡θp(D∣θ)\argmax_\theta p(D\mid\theta) 2/32/3
MAP arg max⁡θp(θ∣D)=arg max⁡θp(D∣θ)p(θ)\argmax_\theta p(\theta\mid D)=\argmax_\theta p(D\mid\theta)p(\theta) 3/53/5
Posterior mean E[θ∣D]\E[\theta\mid D] 4/74/7

MAP picks the posterior's mode. The posterior mean minimises posterior expected squared-error loss; the posterior median minimises expected absolute-error loss.

The posterior mode depends on the parameterisation. With abundant informative data and suitable regularity, MLE and MAP may become close; they need not be identical for a finite sample.

Lecture 2 · Fundamentals on Bayesian Statistics88 / 96
APPENDIXAppendix · extra derivations and checksLecture 2

Backup — when does prior influence fade?

In the Beta–Binomial model, for a fixed proper Beta prior,

prior weight=α+βα+β+n⟶0.\text{prior weight}=\frac{\alpha+\beta}{\alpha+\beta+n}\longrightarrow0.

This does not mean any prior can be overcome in any model:

  • An identifiable model needs informative observations about the parameter.
  • A prior excluding a whole region cannot acquire posterior mass there by multiplication.
  • Model misspecification can produce confident but misleading conclusions.
Check prior sensitivity when data is limited. More data is not a substitute for a plausible model.
Lecture 2 · Fundamentals on Bayesian Statistics89 / 96
APPENDIXAppendix · extra derivations and checksLecture 2

Backup — checking a fitted probability model

Before fitting: prior predictive check

Draw parameters from the prior, then simulate data. Do plausible parameters produce plausible counts, prices or temperatures?

After fitting: posterior predictive check

Draw parameters from the posterior and simulate replicated datasets of the same design. Compare their spread, extremes and patterns with the observations.

For the Pokémon example: compare the observed mean 10.55 and sample variance 11.94 with replicated 20-district datasets. Also examine exposure and spatial patterns. Mean–variance similarity alone does not establish the Poisson assumptions.

Use a mismatch to improve the model. Evaluate future prediction on held-out data too; checking training data is not a test of generalisation.

Workflow reference: Stan, posterior and prior predictive checks.

Lecture 2 · Fundamentals on Bayesian Statistics90 / 96
APPENDIXAppendix · extra derivations and checksLecture 2

Backup — Beta–Binomial normalisation and moments

Let SS heads be observed in nn tosses and let α′=α+S\alpha'=\alpha+S, β′=β+n−S\beta'=\beta+n-S.

p(θ∣D)=θα′−1(1−θ)β′−1B(α′,β′),B(a,b)=Γ(a)Γ(b)Γ(a+b).p(\theta\mid D)=\frac{\theta^{\alpha'-1}(1-\theta)^{\beta'-1}}{B(\alpha',\beta')},\qquad B(a,b)=\frac{\Gamma(a)\Gamma(b)}{\Gamma(a+b)}.
E[θ∣D]=α′α′+β′,Var(θ∣D)=α′β′(α′+β′)2(α′+β′+1).\E[\theta\mid D]=\frac{\alpha'}{\alpha'+\beta'},\qquad \mathrm{Var}(\theta\mid D)=\frac{\alpha'\beta'}{(\alpha'+\beta')^2(\alpha'+\beta'+1)}.

For KK heads in mm future tosses and μ=α′/(α′+β′)\mu=\alpha'/(\alpha'+\beta'),

P(K=k∣D)=(mk)B(α′+k,β′+m−k)B(α′,β′),Var(K∣D)=mμ(1−μ)α′+β′+mα′+β′+1.P(K=k\mid D)=\binom mk\frac{B(\alpha'+k,\beta'+m-k)}{B(\alpha',\beta')},\qquad \mathrm{Var}(K\mid D)=m\mu(1-\mu)\frac{\alpha'+\beta'+m}{\alpha'+\beta'+1}.

E[K∣D]=mμ\E[K\mid D]=m\mu. The multiplier of Binomial variance is 1 at m=1m=1 and greater than 1 at m>1m>1. Future tosses share one latent bias; independently resampling a bias for each toss would be a different model.

Lecture 2 · Fundamentals on Bayesian Statistics91 / 96
APPENDIXAppendix · extra derivations and checksLecture 2

Backup — the Poisson–Gamma predictive distribution

With posterior λ∣D∼Gamma(a′,b′)\lambda\mid D\sim\mathrm{Gamma}(a',b') in shape–rate form, a new unit-exposure count has

P(Y~=k∣D)=∫0∞λke−λk!b′a′Γ(a′)λa′−1e−b′λ dλ=Γ(k+a′)Γ(a′)k!(b′b′+1)a′(1b′+1)k,k=0,1,…\begin{aligned} P(\tilde Y=k\mid D)&=\int_0^\infty\frac{\lambda^ke^{-\lambda}}{k!} \frac{b'^{a'}}{\Gamma(a')}\lambda^{a'-1}e^{-b'\lambda}\,d\lambda\\[4pt] &=\frac{\Gamma(k+a')}{\Gamma(a')k!} \left(\frac{b'}{b'+1}\right)^{a'}\left(\frac{1}{b'+1}\right)^k,\quad k=0,1,\ldots \end{aligned}
E[Y~∣D]=a′b′,Var(Y~∣D)=a′b′+a′b′2.\E[\tilde Y\mid D]=\frac{a'}{b'},\qquad \mathrm{Var}(\tilde Y\mid D)=\frac{a'}{b'}+\frac{a'}{b'^2}.

This is Negative Binomial with shape a′a' and probability b′/(b′+1)b'/(b'+1) under the displayed convention; a′a' need not be an integer. For future exposure tt, the mean is ta′/b′ta'/b' and variance ta′/b′+t2a′/b′2ta'/b'+t^2a'/b'^2.

Lecture 2 · Fundamentals on Bayesian Statistics92 / 96
APPENDIXAppendix · extra derivations and checksLecture 2

Backup — the Dirichlet predictive distribution

For category counts yjy_j, let αj′=αj+yj\alpha'_j=\alpha_j+y_j and A′=∑jαj′A'=\sum_j\alpha'_j. Then

p(θ∣D)∝∏jθjαj′−1,P(Y~=j∣D)=αj′A′.p(\theta\mid D)\propto\prod_j\theta_j^{\alpha'_j-1},\qquad P(\tilde Y=j\mid D)=\frac{\alpha'_j}{A'}.

For a future batch of size mm with counts kjk_j adding to mm,

P(K1=k1,…,Kc=kc∣D)=m!∏jkj!Γ(A′)Γ(A′+m)∏jΓ(αj′+kj)Γ(αj′).P(K_1=k_1,\ldots,K_c=k_c\mid D) =\frac{m!}{\prod_j k_j!}\frac{\Gamma(A')}{\Gamma(A'+m)} \prod_j\frac{\Gamma(\alpha'_j+k_j)}{\Gamma(\alpha'_j)}.
This is the Dirichlet–Multinomial distribution. Updated αj′\alpha'_j give the posterior predictive; original αj\alpha_j give the prior predictive.
Lecture 2 · Fundamentals on Bayesian Statistics93 / 96
APPENDIXAppendix · extra derivations and checksLecture 2

Backup — complete the square for the Normal mean

Independent Yi∣θ∼N(θ,σ2)Y_i\mid\theta\sim\mathcal N(\theta,\sigma^2), known σ2\sigma^2, prior θ∼N(μ0,τ02)\theta\sim\mathcal N(\mu_0,\tau_0^2):

log⁡p(θ∣D)=−12σ2∑i(yi−θ)2−12τ02(θ−μ0)2+C=−12[(nσ2+1τ02)θ2−2(nyˉσ2+μ0τ02)θ]+C′=−(θ−μ1)22τ12+C′′.\begin{aligned} \log p(\theta\mid D)&=-\frac{1}{2\sigma^2}\sum_i(y_i-\theta)^2 -\frac{1}{2\tau_0^2}(\theta-\mu_0)^2+C\\ &=-\frac12\left[\left(\frac n{\sigma^2}+\frac1{\tau_0^2}\right)\theta^2 -2\left(\frac{n\bar y}{\sigma^2}+\frac{\mu_0}{\tau_0^2}\right)\theta\right]+C'\\ &=-\frac{(\theta-\mu_1)^2}{2\tau_1^2}+C''. \end{aligned}
τ12=(nσ2+1τ02)−1,μ1=τ12(nyˉσ2+μ0τ02).\tau_1^2=\left(\frac n{\sigma^2}+\frac1{\tau_0^2}\right)^{-1},\qquad \mu_1=\tau_1^2\left(\frac{n\bar y}{\sigma^2}+\frac{\mu_0}{\tau_0^2}\right).

C,C′,C′′C,C',C'' do not depend on θ\theta. A new observation is θ\theta plus independent Gaussian noise, so its predictive distribution is N(μ1,τ12+σ2)\mathcal N(\mu_1,\tau_1^2+\sigma^2).

Lecture 2 · Fundamentals on Bayesian Statistics94 / 96
APPENDIXAppendix · extra derivations and checksLecture 2

Backup — three remarks on the regression posterior

The completion of the square now sits in the body, at Complete the square again — now with a matrix. Three things worth saying only if asked:

Always invertible

X⊤XX^\top X alone is singular whenever XX has fewer rows than columns. Adding I/τ2I/\tau^2 makes AA positive definite, so a Bayesian fit is defined where least squares is not — the prior supplies the missing rank.

Mean equals MAP

A Gaussian's mean, mode and median coincide, so μw\mu_w is simultaneously the posterior mean and the MAP estimate. That is special to the Gaussian and does not survive a skewed posterior.

What the point estimate drops

Σw\Sigma_w is precisely what collapsing to μw\mu_w throws away — and its large directions are the ones a prediction should hedge in.

Lecture 2 · Fundamentals on Bayesian Statistics95 / 96
APPENDIXAppendix · extra derivations and checksLecture 2

Backup — why Lasso can select an exact zero

For one coordinate, write the objective, up to a constant, as

q(wj)=ajwj2−2rjwj+λ1∣wj∣,aj>0.q(w_j)=a_jw_j^2-2r_jw_j+\lambda_1|w_j|,\qquad a_j>0.

At zero, ∂∣wj∣=[−1,1]\partial|w_j|=[-1,1]. The optimality condition is

0∈−2rj+λ1[−1,1]⟺∣rj∣≤λ1/2.0\in-2r_j+\lambda_1[-1,1]\quad\Longleftrightarrow\quad |r_j|\le\lambda_1/2.

Otherwise the solution is the soft-thresholded value

wj=sign⁡(rj)ajmax⁡(∣rj∣−λ1/2,0).w_j=\frac{\operatorname{sign}(r_j)}{a_j}\max(|r_j|-\lambda_1/2,0).

This is a property of MAP optimisation. A continuous Laplace prior and the resulting continuous posterior have no atom at zero; full posterior samples are not sparse in this exact-zero sense.

Lecture 2 · Fundamentals on Bayesian Statistics96 / 96

Keys

→ / space
next reveal, then next slide
←
back
↑ ↓
skip a whole slide
R
reset this slide's widgets and quizzes
M
slide index
P
print → save as PDF (1280×720)
F
fullscreen