Skip to content

Chapter 8 Formula Sheet

One page for the whole chapter. Equation numbers are the book’s; the measured columns are this module’s.

Chapter 8’s own organising idea — §8.2, §8.3 and §8.4 are three answers to one question.

§8.2 empirical risk§8.3 maximum likelihood / MAP§8.4 Bayesian inference
minimise / maximiseRempR_{\text{emp}}logp(yθ)-\log p(\mathbf{y} \mid \boldsymbol\theta)nothing — integrate
needs a priornoMLE no, MAP yesyes
needs p(X)p(\mathcal{X})nono, it cancelsyes, it is an integral
computational problemoptimizationoptimizationintegration
the answera pointa pointa distribution
far-extrapolation coverage0.17960.17960.9498\mathbf{0.9498}
termmeaning
data as vectorsnumerical, with a fixed dimension and an agreed ordering
model as a functiona predictor f:RDRf: \mathbb{R}^D \to \mathbb{R}
model as a distributiona probabilistic description of the process that made the data
learningfinding parameters that generalise, not that memorise

The three phases of learning: prediction / inference, training / parameter estimation, and hyperparameter tuning / model selection.

whatequationnotes
affine predictorf(xn,θ)=θxnf(\mathbf{x}_n, \boldsymbol\theta) = \boldsymbol\theta^\top\mathbf{x}_nEq 8.2, with the bias folded in
the unit-feature trickxn[1,xn,1,]\mathbf{x}_n \leftarrow [1, x_{n,1}, \ldots]^\topEq 8.4; makes affine look linear
empirical riskRemp(f,X,y)=1Nn=1N(yn,y^n)R_{\text{emp}}(f, \mathbf{X}, \mathbf{y}) = \frac{1}{N}\sum_{n=1}^{N}\ell(y_n, \hat{y}_n)Eq 8.7
squared loss(yn,y^n)=(yny^n)2\ell(y_n, \hat{y}_n) = (y_n - \hat{y}_n)^2Eq 8.6
least-squares risk1NyXθ2\frac{1}{N}\lVert\mathbf{y} - \mathbf{X}\boldsymbol\theta\rVert^2Eq 8.8
normal equationsθ=(XX)1Xy\boldsymbol\theta^* = (\mathbf{X}^\top\mathbf{X})^{-1}\mathbf{X}^\top\mathbf{y}Eq 8.9
expected riskRtrue(f)=E(x,y)[(y,f(x))]R_{\text{true}}(f) = \mathbb{E}_{(\mathbf{x},y)}[\ell(y, f(\mathbf{x}))]Eq 8.10 — what you actually want
the gapRempR_{\text{emp}} is computable, RtrueR_{\text{true}} is notthe whole chapter lives here
whatequationnotes
regularized riskminθ 1NyXθ2+λθ2\min_{\boldsymbol\theta}\ \frac{1}{N}\lVert\mathbf{y} - \mathbf{X}\boldsymbol\theta\rVert^2 + \lambda\lVert\boldsymbol\theta\rVert^2Eq 8.12
ridge solutionθ=(XXN+λI)1XyN\boldsymbol\theta = \left(\frac{\mathbf{X}^\top\mathbf{X}}{N} + \lambda\mathbf{I}\right)^{-1}\frac{\mathbf{X}^\top\mathbf{y}}{N}always invertible for λ>0\lambda > 0
1\ell_1 versionλθ1\lambda\lVert\boldsymbol\theta\rVert_1, lassosets coefficients to exactly zero
the readingλ\lambda trades fit against θ\lVert\boldsymbol\theta\rVertand equals σ2/(Nτ2)\sigma^2/(N\tau^2) — see §8.3.2
EV[R(VM)]1Kk=1KR(V(k)M)(Eq 8.39)\mathbb{E}_{\mathcal{V}}[R(\mathcal{V} \mid M)] \approx \frac{1}{K}\sum_{k=1}^{K} R(\mathcal{V}^{(k)} \mid M) \qquad \text{(Eq 8.39)}

with standard error σ/K\sigma/\sqrt{K}, σ\sigma the spread of the per-fold risks.

KKmean estimatespreadbias vs truth
220.2384850.2384850.1467490.146749+0.045981+0.045981
550.1935860.1935860.0555250.055525+0.001082+0.001082
10100.1882880.1882880.0483850.0483850.004215-0.004215
40400.1862290.1862290.0459370.0459370.006274-0.006274

Small KK trains on less data and overstates the risk. K=5K = 5 or 1010 captures nearly all the variance reduction.

whatequationnotes
likelihoodp(ynxn,θ)p(y_n \mid \mathbf{x}_n, \boldsymbol\theta)a function of θ\boldsymbol\theta with data fixed
negative log-likelihoodL(θ)=n=1Nlogp(ynxn,θ)\mathcal{L}(\boldsymbol\theta) = -\sum_{n=1}^{N}\log p(y_n \mid \mathbf{x}_n, \boldsymbol\theta)Eq 8.14
Gaussian noisep(ynxn,θ)=N(ynxnθ,σ2)p(y_n \mid \mathbf{x}_n, \boldsymbol\theta) = \mathcal{N}(y_n \mid \mathbf{x}_n^\top\boldsymbol\theta, \sigma^2)Eq 8.17
worked outL=12σ2n(ynxnθ)2+const\mathcal{L} = \frac{1}{2\sigma^2}\sum_n (y_n - \mathbf{x}_n^\top\boldsymbol\theta)^2 + \text{const}Eq 8.18d

Asymptotics (§8.3.2’s remark). The MLE is consistent, asymptotically normal, and its error variance decays as 1/N1/N — but “the size of the samples necessary to achieve these properties can be quite large”, and “especially in the ‘small’ data regime, maximum likelihood estimation can lead to overfitting.”

whatequationnotes
Bayes for parametersp(θx)=p(xθ)p(θ)p(x)p(\boldsymbol\theta \mid \mathbf{x}) = \dfrac{p(\mathbf{x} \mid \boldsymbol\theta)p(\boldsymbol\theta)}{p(\mathbf{x})}Eq 8.19
the proportionalityp(θx)p(xθ)p(θ)p(\boldsymbol\theta \mid \mathbf{x}) \propto p(\mathbf{x} \mid \boldsymbol\theta)p(\boldsymbol\theta)Eq 8.20p(x)p(\mathbf{x}) is constant in θ\boldsymbol\theta
MAP objectiveminimise logp(xθ)logp(θ)-\log p(\mathbf{x} \mid \boldsymbol\theta) - \log p(\boldsymbol\theta)one extra term
the correspondenceλ=σ2Nτ2\boxed{\lambda = \dfrac{\sigma^2}{N\tau^2}} for p(θ)=N(0,τ2I)p(\boldsymbol\theta) = \mathcal{N}(\mathbf{0}, \tau^2\mathbf{I})ridge and MAP are the same estimator
invertedτ2=σ2Nλ\tau^2 = \dfrac{\sigma^2}{N\lambda}to state a tuned λ\lambda as a prior
Gaussian mean, closed formθ^MAP=NN+σ2/τ2yˉ=yˉ1+λ\hat\theta_{\text{MAP}} = \dfrac{N}{N + \sigma^2/\tau^2}\bar{y} = \dfrac{\bar{y}}{1+\lambda}shrinkage toward zero

Measured across τ2[102,106]\tau^2 \in [10^{-2}, 10^{6}]: worst ridgeMAP=1.24×1014\lvert\text{ridge} - \text{MAP}\rvert = 1.24\times10^{-14}.

priorimplied penaltybehaviour
N(0,τ2I)\mathcal{N}(\mathbf{0}, \tau^2\mathbf{I})λθ2\lambda\lVert\boldsymbol\theta\rVert^2, ridgesmooth shrinkage
Laplaceλθ1\lambda\lVert\boldsymbol\theta\rVert_1, lassoexact zeros
uniform (improper)nonerecovers the MLE
N(m,τ2I)\mathcal{N}(\mathbf{m}, \tau^2\mathbf{I})λθm2\lambda\lVert\boldsymbol\theta - \mathbf{m}\rVert^2shrinks toward m\mathbf{m}
casedegreetraining riskexpected riskratioθ\lVert\boldsymbol\theta\rVert
overfitting11110.005232\mathbf{0.005232}3.91733.9173748.8748.83616.983616.98
underfitting110.4777710.4777710.72540.72541.51.50.810.81
fitting well440.1139670.1139670.3806\mathbf{0.3806}3.33.35.675.67

Overfitting has the lowest training risk of the three. Underfitting is visible from training data alone; overfitting is not — the parameter norm is the only training-side warning.

The risk decomposition, verified to Monte Carlo error:

E[risk]=σ2noise+Ex ⁣[(fˉ(x)f(x))2]bias2+Ex ⁣[Var(f(x))]variance\mathbb{E}[\text{risk}] = \underbrace{\sigma^2}_{\text{noise}} + \underbrace{\mathbb{E}_x\!\left[(\bar{f}(x) - f^*(x))^2\right]}_{\text{bias}^2} + \underbrace{\mathbb{E}_x\!\left[\mathrm{Var}(f(x))\right]}_{\text{variance}}
degreenoisebias2^2variancesummeasured
110.1225000.1225000.456620\mathbf{0.456620}0.0521420.0521420.6312620.6312620.6322360.632236
330.1225000.1225000.0576730.0576730.0502140.0502140.2303870.2303870.2306330.230633
550.1225000.1225000.0012610.0012610.1915470.1915470.3153080.3153080.3155330.315533
990.1225000.1225000.7046710.7046712104.812958\mathbf{2104.812958}2105.6401292105.6401292105.5509692105.550969
p(ynxn,θ)=ϕ(θxn)(Eq 8.21)p(y_n \mid \mathbf{x}_n, \boldsymbol\theta) = \phi(\boldsymbol\theta^\top\mathbf{x}_n) \qquad \text{(Eq 8.21)}

ϕ\phi is the link function; the family is the generalized linear model (McCullagh and Nelder, 1989; Agresti, 2002). Binary classification and count data live here, via the exponential family of §6.6.

§8.4 Probabilistic modeling and inference

Section titled “§8.4 Probabilistic modeling and inference”

§8.4.1’s definition: a probabilistic model is specified by the joint distribution of all its random variables. Everything else follows by a rule of probability:

what you wantfrom the joint, by
prior and likelihoodthe product rule — they are its factors
marginal likelihood p(x)p(\mathbf{x})the sum rule — integrate out θ\boldsymbol\theta
posteriordivide the joint by the marginal
whatequation
posteriorp(θX)=p(Xθ)p(θ)p(X)p(\boldsymbol\theta \mid \mathcal{X}) = \dfrac{p(\mathcal{X} \mid \boldsymbol\theta)p(\boldsymbol\theta)}{p(\mathcal{X})},  p(X)=p(Xθ)p(θ)dθ\ p(\mathcal{X}) = \displaystyle\int p(\mathcal{X} \mid \boldsymbol\theta)p(\boldsymbol\theta)\,d\boldsymbol\thetaEq 8.22
predictivep(x)=p(xθ)p(θ)dθ=Eθ[p(xθ)]p(\mathbf{x}) = \displaystyle\int p(\mathbf{x} \mid \boldsymbol\theta)p(\boldsymbol\theta)\,d\boldsymbol\theta = \mathbb{E}_{\boldsymbol\theta}[p(\mathbf{x} \mid \boldsymbol\theta)]Eq 8.23

Gaussian closed form. With ΣN=(ΦΦ/σ2+I/τ2)1\boldsymbol\Sigma_N = \left(\boldsymbol\Phi^\top\boldsymbol\Phi/\sigma^2 + \mathbf{I}/\tau^2\right)^{-1} and mN=ΣNΦy/σ2\mathbf{m}_N = \boldsymbol\Sigma_N\boldsymbol\Phi^\top\mathbf{y}/\sigma^2:

mean=ϕmN,variance=σ2noise+ϕΣNϕnot knowing θ\text{mean} = \boldsymbol\phi^\top\mathbf{m}_N, \qquad \text{variance} = \underbrace{\sigma^2}_{\text{noise}} + \underbrace{\boldsymbol\phi^\top\boldsymbol\Sigma_N\boldsymbol\phi}_{\text{not knowing }\boldsymbol\theta}

The mean is identical to the plug-in prediction. mN\mathbf{m}_N equals the MAP estimate to 8.88×10168.88\times10^{-16}.

Scalar case: 1σN2=Nσ2+1τ2\dfrac{1}{\sigma_N^2} = \dfrac{N}{\sigma^2} + \dfrac{1}{\tau^2}precisions add.

NNlargest posterior sdfull / plug-in width
550.8219580.8219581.2482131.248213
25250.3926920.3926921.0394721.039472
100010000.0742930.0742931.0011091.001109
10000100000.0228780.0228781.000113\mathbf{1.000113}

Failure modes, measured. Misspecified model class (sine data, cubic model): full predictive reaches only 0.89080.8908. Misspecified noise scale (assumed σ\sigma half the truth): coverage falls to 0.66970.6697.

Approximations when the prior is not conjugate: MCMC; the Laplace approximation; variational inference; expectation propagation.

whatequation
the generative conditionalp(xθ,z)p(\mathbf{x} \mid \boldsymbol\theta, \mathbf{z})Eq 8.24
the likelihoodp(xθ)=p(xθ,z)p(z)dzp(\mathbf{x} \mid \boldsymbol\theta) = \displaystyle\int p(\mathbf{x} \mid \boldsymbol\theta, \mathbf{z})p(\mathbf{z})\,d\mathbf{z}Eq 8.25
parameter posteriorp(θX)=p(Xθ)p(θ)/p(X)p(\boldsymbol\theta \mid \mathcal{X}) = p(\mathcal{X} \mid \boldsymbol\theta)p(\boldsymbol\theta)/p(\mathcal{X})Eq 8.26
latent posteriorp(zX)=p(Xz)p(z)/p(X)p(\mathbf{z} \mid \mathcal{X}) = p(\mathcal{X} \mid \mathbf{z})p(\mathbf{z})/p(\mathcal{X})Eq 8.27
the tractable onep(zX,θ)=p(Xz,θ)p(z)p(Xθ)p(\mathbf{z} \mid \mathcal{X}, \boldsymbol\theta) = \dfrac{p(\mathcal{X} \mid \mathbf{z}, \boldsymbol\theta)p(\mathbf{z})}{p(\mathcal{X} \mid \boldsymbol\theta)}Eq 8.28

Equation 8.25’s rule: the likelihood must not depend on z\mathbf{z} — only on x\mathbf{x} and θ\boldsymbol\theta. Marginalising both z\mathbf{z} and θ\boldsymbol\theta at once is “not possible in general”; Eq 8.28 is the compromise, and it is what Chapters 10 and 11 actually compute.

EM, measured on a two-component mixture: the E step is Eq 8.28, the M step maximises given it, and the likelihood never falls — smallest step over 40 iterations 9.09×1013-9.09\times10^{-13}, floating-point noise at a converged optimum. No step size, no line search. No guarantee about which local maximum.

whatrule
nodesrandom variables
arrowsconditional probabilities; aba \to b carries p(ba)p(b \mid a)
shaded nodeobserved; unshaded is latent
platerepeat everything inside NN times
hyperpriora prior on a prior’s parameters; deterministic parameters lose their circle
factorization → graphone node per variable; an arrow in from every variable it is conditioned on
graph → factorizationEq 8.31: p(x)=k=1Kp(xkPak)p(\mathbf{x}) = \prod_{k=1}^{K} p(x_k \mid \mathrm{Pa}_k)
KK binary variablesfull joint, 2K12^K-12\le 2 parents eachratio
55313115152.0672.067
20201.049×1061.049\times10^{6}75751.398×1041.398\times10^{4}
1001001.268×1030\mathbf{1.268\times10^{30}}395\mathbf{395}3.209×10273.209\times10^{27}

A ⁣ ⁣ ⁣BCA \perp\!\!\!\perp B \mid C iff every trail (path ignoring arrow direction) from AA to BB is blocked.

meetingshapeblocked whenconditioning…
head to tailunvu \to n \to vnCn \in Cblocks
tail to tailunvu \leftarrow n \to vnCn \in Cblocks
head to headunvu \to n \leftarrow vnCn \notin C and no descendant of nn in CCopens

Example 8.9 on Figure 8.11 (aba \to b, ada \to d, bcb \to c, cdc \to d, ded \to e), measured on 4,000,0004{,}000{,}000 samples:

eqclaimbook sayspartial correlation
8.35b ⁣ ⁣ ⁣da,cb \perp\!\!\!\perp d \mid a, cindependent0.000320-0.000320
8.36a ⁣ ⁣ ⁣cba \perp\!\!\!\perp c \mid bindependent0.000079-0.000079
8.37b⊥̸ ⁣ ⁣ ⁣dcb \not\perp\!\!\!\perp d \mid cdependent0.2721720.272172
8.38a⊥̸ ⁣ ⁣ ⁣cb,ea \not\perp\!\!\!\perp c \mid b, edependent0.146064-0.146064

The collider, isolated: corr(a,c)\mathrm{corr}(a,c) is 0.000079-0.000079 given {b}\{b\}, 0.237939-0.237939 given {b,d}\{b, d\}, 0.146064-0.146064 given {b,e}\{b, e\}. Conditioning on a descendant is enough.

Three families (§8.5.3): directed (Bayesian networks), undirected (Markov random fields), factor graphs. “Not every distribution can be represented in a particular choice of graphical model.”

Inner loop chooses (the validation set); outer loop reports (the test set).

whatequation
generative processMkp(M)M_k \sim p(M),  θkp(θMk)\ \boldsymbol\theta_k \sim p(\boldsymbol\theta \mid M_k),  Dp(Dθk)\ \mathcal{D} \sim p(\mathcal{D} \mid \boldsymbol\theta_k)Eq 8.40–8.42
model posteriorp(MkD)p(Mk)p(DMk)p(M_k \mid \mathcal{D}) \propto p(M_k)\,p(\mathcal{D} \mid M_k)Eq 8.43
model evidencep(DMk)=p(Dθk)p(θkMk)dθkp(\mathcal{D} \mid M_k) = \displaystyle\int p(\mathcal{D} \mid \boldsymbol\theta_k)p(\boldsymbol\theta_k \mid M_k)\,d\boldsymbol\theta_kEq 8.44
the choiceM=argmaxMkp(MkD)M^* = \arg\max_{M_k} p(M_k \mid \mathcal{D})Eq 8.45

With a uniform p(Mk)=1/Kp(M_k) = 1/K, Eq 8.45 reduces to maximising the evidence.

The automatic Occam’s razor. No prior favouring simple models is needed: p(DM)p(\mathcal{D} \mid M) is a normalized distribution over datasets, so breadth costs depth. Measured over 200,000200{,}000 datasets:

KL(M1M2)=3.4062 nats,KL(M2M1)=50.7124 nats\mathrm{KL}(M_1 \Vert M_2) = 3.4062 \text{ nats}, \qquad \mathrm{KL}(M_2 \Vert M_1) = 50.7124 \text{ nats}

Both positive necessarily — each model beats the other on its own data, so none can win everywhere. That is Figure 8.14. Region CC‘s edge, measured: w=0.571273w = 0.571273.

The evidence splits exactly (verified to 5.68×10145.68\times10^{-14}), with A=ΦΦ/σ2+I/τ2\mathbf{A} = \boldsymbol\Phi^\top\boldsymbol\Phi/\sigma^2 + \mathbf{I}/\tau^2:

logp(DM)=logp(Dm)best fitm22τ2D2logτ212logAOccam factor\log p(\mathcal{D} \mid M) = \underbrace{\log p(\mathcal{D} \mid \mathbf{m})}_{\text{best fit}} \underbrace{- \frac{\lVert\mathbf{m}\rVert^2}{2\tau^2} - \frac{D}{2}\log\tau^2 - \frac{1}{2}\log\lvert\mathbf{A}\rvert}_{\text{Occam factor}}

Degree 1111 \to 11: fit improves by 45.7045.70, Occam factor worsens by 12.6712.67. The penalty is real, automatic, and gentle.

p(M1D)p(M2D)posterior odds=p(M1)p(M2)prior odds p(DM1)p(DM2)Bayes factor(Eq 8.46)\underbrace{\frac{p(M_1 \mid \mathcal{D})}{p(M_2 \mid \mathcal{D})}}_{\text{posterior odds}} = \underbrace{\frac{p(M_1)}{p(M_2)}}_{\text{prior odds}}\ \underbrace{\frac{p(\mathcal{D} \mid M_1)}{p(\mathcal{D} \mid M_2)}}_{\text{Bayes factor}} \qquad \text{(Eq 8.46)}

With a uniform model prior this reduces to the Bayes factor alone (Eq 8.47): above 11 choose M1M_1, below it M2M_2.

log Bayes factorJeffreys (1961)
<log3< \log 3barely worth mentioning
log3\log 3 to log10\log 10substantial
log10\log 10 to log30\log 30strong
log30\log 30 to log100\log 100very strong
>log100> \log 100decisive

Measured on 25 points: degrees 0022 are decisive (70.8370.83, 33.2833.28, 31.5031.50); degrees 331111 are all “barely worth mentioning”. The evidence detects underfitting sharply and overfitting bluntly.

criterionformulaequation
AIClogp(xθ)M\log p(\mathbf{x} \mid \boldsymbol\theta) - MEq 8.48
BIClogp(xθ)12MlogN\log p(\mathbf{x} \mid \boldsymbol\theta) - \tfrac{1}{2}M\log NEq 8.49

Choose the largest value. BIC’s penalty exceeds AIC’s whenever N>e27.39N > e^2 \approx 7.39, and the ratio is 12logN\tfrac{1}{2}\log N.

criterionpicks, on the chapter’s 25 points
maximum likelihooddegree 11\mathbf{11} — the largest on offer, always
AICdegree 55
BICdegree 55
exact evidencedegree 66
cross-validationdegree 88, with three statistical ties

BIC as an approximation: mean absolute error 8.02748.0274 nats, worst 13.447413.4474, at N=25N = 25. It is a large-NN approximation. The ranking survives; the value does not. Its derivation drops the prior entirely — which is both why it is inaccurate and why it is immune to the Jeffreys–Lindley paradox.

quantityvaluepage
ridge vs MAP, worst disagreement1.24×10141.24\times10^{-14}805
slope peak under an isotropic prior2.29882.2988 at τ2=31.71\tau^2 = 31.71805
overfitting risk ratio748.8×748.8\times805
posterior mean vs MAP8.88×10168.88\times10^{-16}806
Eq 8.23 coverage, far extrapolation0.9498\mathbf{0.9498} vs plug-in 0.1796\mathbf{0.1796}806
width ratio at N=10000N = 100001.0001131.000113806
Eq 8.31 vs 4M samples0.0001240.000124807
Example 8.9, four claims0.000320-0.000320, 0.000079-0.000079, 0.2721720.272172, 0.146064-0.146064807
collider effect0.0000790.237939-0.000079 \to -0.237939807
KL divergences, Figure 8.143.40623.4062 and 50.712450.7124 nats808
region CC edgew=0.571273w = 0.571273808
Jeffreys–Lindley crossoverτ2=3.71412×106\tau^2 = 3.71412\times10^{6}808
BIC error at N=25N = 258.02748.0274 nats mean808
flat CV on pure noise0.8653430.865343 vs true 1.2541791.254179808
risk decomposition residual0.0002250.000225 at degree 5809
evidence decomposition residual5.68×10145.68\times10^{-14}809
EM smallest step9.09×1013-9.09\times10^{-13}809
  1. Empirical risk is computable; expected risk is what you want. Everything else follows from that gap.
  2. A penalty and a prior are the same object, related by λ=σ2/(Nτ2)\lambda = \sigma^2/(N\tau^2), measured to 1.24×10141.24\times10^{-14}.
  3. Least squares is a consequence of Gaussian noise, not a definition of fitting.
  4. Overfitting has the lowest training risk. It is invisible without held-out data or a look at θ\lVert\boldsymbol\theta\rVert.
  5. A point estimate is not wrong, it is incomplete. The posterior mean equals the MAP point; the covariance is what was discarded.
  6. Equation 8.23 is calibrated where a plug-in interval is not0.94980.9498 against 0.17960.1796.
  7. Estimation is optimization; inference is integration. They converge as NN grows, ratio 1.0001131.000113 at N=10000N = 10000.
  8. A graph is a factorization, and d-separation reads independence off it — four for four on Example 8.9.
  9. Conditioning on a collider opens a path. It is the only rule that runs backwards.
  10. The Occam’s razor is automatic and gentle, and every model-selection number is hostage to a prior or to how hard you searched.

Back to the start: When Models Meet Data Overview

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading