Mathematical & Probabilistic Prerequisites

Every chapter of Hoff’s course rests on five compact mathematical pillars: iterated expectation and Jensen’s inequality, kernel recognition via Bayes’ rule, change-of-variables calculus with special functions, positive-definite linear algebra, and the ergodic theory of Monte Carlo and Markov chains. While Notation and Conventions catalogues symbols and parameterization conventions and Appendix A and Appendix B serve as quick-reference sheets, this introductory chapter derives each prerequisite from first principles and verifies it symbolically and numerically in the Wolfram Language (wolfram-ws-bridge JavaScript mode).All eleven working examples below are collected in wolfram_prereqs.mjs and were evaluated live against the Wolfram kernel with zero console.log calls—each symbolic derivation and numerical instantiation appears as a KaTeX equation directly beneath its await show call.

0.4Probability, Expectation, and Jensen’s Inequality

A Bayesian model specifies a joint distribution p(x,z)p(x, z) over observable data and latent unknowns, then extracts marginals by integrating out nuisance variables, p(x)=∫p(x∣z) p(z) dzp(x) = \int p(x \mid z)\,p(z)\,dz, and conditionals by slicing and renormalizing, p(z∣x)=p(x,z)/p(x)p(z \mid x) = p(x, z)/p(x). Two variables X1,X2X_1, X_2 are conditionally independent given ZZ when p(x1,x2∣z)=p(x1∣z) p(x2∣z)p(x_1, x_2 \mid z) = p(x_1 \mid z)\,p(x_2 \mid z)—the structural backbone of exchangeability in Chapter 2, hierarchical modeling in Chapter 8, and graphical copulas in Chapter 12.

Theorem 1 (Laws of Total Expectation and Total Variance). Let XX and ZZ be random variables on a common probability space.

  1. If E∣X∣<∞E\lvert X\rvert < \infty, then E[X]=E{E[X∣Z]}E[X] = E\{E[X \mid Z]\}.
  2. If E[X2]<∞E[X^2] < \infty, then
Var⁡(X)=E{Var⁡(X∣Z)}+Var⁡(E[X∣Z]). \operatorname{Var}(X) = E\{\operatorname{Var}(X \mid Z)\} + \operatorname{Var}(E[X \mid Z]).

Proof sketch. Expanding Var⁡(X)=E[X2]−(E[X])2\operatorname{Var}(X) = E[X^2] - (E[X])^2 and applying part 1 to both E[X2]=E{E[X2∣Z]}E[X^2] = E\{E[X^2 \mid Z]\} and E[X]=E{E[X∣Z]}E[X] = E\{E[X \mid Z]\}, add and subtract E{(E[X∣Z])2}E\{(E[X \mid Z])^2\} to separate the average within-group variance E{Var⁡(X∣Z)}E\{\operatorname{Var}(X \mid Z)\} from the between-group variance Var⁡(E[X∣Z])\operatorname{Var}(E[X \mid Z]). □\square

Corollary 2 (Information Reduces Variance on Average). For any square-integrable parameter θ\theta and data YY, E{Var⁡(θ∣Y)}≤Var⁡(θ)E\{\operatorname{Var}(\theta \mid Y)\} \le \operatorname{Var}(\theta), with strict inequality whenever E[θ∣Y]E[\theta \mid Y] is non-constant. This holds on average over YY; a single surprising observation yy can increase Var⁡(θ∣y)\operatorname{Var}(\theta \mid y) relative to Var⁡(θ)\operatorname{Var}(\theta).

The variance decomposition reappears throughout the book: posterior predictive overdispersion in Chapter 3, Rao–Blackwellization in Chapter 4, MSE decomposition in Chapter 5, within/between-school variance in Chapter 8, and nested GLMM predictive variance in Chapter 11.

0.4.1Working Example 1a: Total Variance of a Mixture

Consider the three-component normal mixture from Chapter 6 with weights wjw_j, means θj\theta_j, and variances σj2\sigma_j^2. We verify symbolically that Var⁡(X)\operatorname{Var}(X) splits into within-component and between-component terms, rewrite the between-component variance as a pairwise sum ∑i<jwiwj(θi−θj)2\sum_{i<j} w_i w_j (\theta_i - \theta_j)^2, and instantiate on Hoff’s trimodal target (w=(0.45,0.10,0.45)w = (0.45, 0.10, 0.45), θ=(−3,0,3)\theta = (-3, 0, 3), σj2=1/3\sigma_j^2 = 1/3):

Write the density of a single 𝒩(m,v)\mathcal{N}(m, v) mixture component.
// 1a. Law of total variance on Chapter 6's trimodal normal mixture
const { x, m, v } = W
const phi = await show`Exp[-(${x} - ${m})^2/(2*${v})]/Sqrt[2*Pi*${v}]`
ϕ(x∣m,v)=e−(x−m)22v2πv \phi(x \mid m, v) = \frac{e^{-\frac{(x-m)^2}{2 v}}}{\sqrt{2 \pi} \sqrt{v}} Compute the first two raw moments of a 𝒩(m,v)\mathcal{N}(m, v) component by symbolic integration.
const mom = await show`Assuming[v > 0 && Element[m, Reals],
  Table[Integrate[x^k*${phi}, {x, -Infinity, Infinity}], {k, 1, 2}]]`
(E[X∣m,v],  E[X2∣m,v])={m,m2+v} \bigl(E[X \mid m, v],\; E[X^2 \mid m, v]\bigr) = \left\{m,m^2+v\right\} Write the within-component variance: the ww-weighted average of the σj2\sigma_j^2.
const within = await show`Sum[w[j]*s2[j], {j, 3}]`
E{Var⁡(X∣Z)}=w1σ12+w2σ22+w3σ32 E\{\operatorname{Var}(X \mid Z)\} = w_1 \sigma_1^2 + w_2 \sigma_2^2 + w_3 \sigma_3^2 Write the between-component variance: the ww-weighted variance of the means θj\theta_j.
const between = await show`Sum[w[j]*th[j]^2, {j, 3}] - Sum[w[j]*th[j], {j, 3}]^2`
Var⁡(E[X∣Z])=w1θ12+w2θ22+w3θ32−(w1θ1+w2θ2+w3θ3)2 \operatorname{Var}(E[X \mid Z]) = w_1 \theta_1^2 + w_2 \theta_2^2 + w_3 \theta_3^2 - \left(w_1 \theta_1 + w_2 \theta_2 + w_3 \theta_3\right)^2 Verify the law of total variance symbolically for a 3-component normal mixture.
await show`Simplify[Sum[w[j]*(${mom}[[2]] /. {m -> th[j], v -> s2[j]}), {j, 3}] -
  Sum[w[j]*(${mom}[[1]] /. m -> th[j]), {j, 3}]^2 - (${within} + ${between})]`
Var⁡(X)−(∑j=13wjσj2+∑j=13wjθj2−(∑j=13wjθj)2)=0 \begin{aligned} &\operatorname{Var}(X) - \Bigl(\sum_{j=1}^3 w_j \sigma_j^2 + \sum_{j=1}^3 w_j\theta_j^2 - \bigl(\textstyle\sum_{j=1}^3 w_j\theta_j\bigr)^2\Bigr) \\ &= 0 \end{aligned} Rewrite the between-component variance as a pairwise sum of squared mean differences.
await show`Simplify[(${between} - Sum[w[i]*w[j]*(th[i] - th[j])^2, {i, 3}, {j, i + 1, 3}]) /. w[3] -> 1 - w[1] - w[2]]`
Var⁡(E[X∣Z])−∑i<jwiwj(θi−θj)2=0 \operatorname{Var}(E[X \mid Z]) - \sum_{i<j} w_i w_j (\theta_i - \theta_j)^2 = 0 Write Chapter 6’s trimodal target as a list of substitution rules.
const hoff = await show`{w[1] -> 9/20, w[2] -> 1/10, w[3] -> 9/20, th[1] -> -3, th[2] -> 0, th[3] -> 3, s2[1] -> 1/3, s2[2] -> 1/3, s2[3] -> 1/3}`
(w,  θ,  σ2)Ch. 6={w1→920,w2→110,w3→920,θ1→−3,θ2→0,θ3→3,σ12→13,σ22→13,σ32→13} \begin{aligned} &\bigl(w,\; \theta,\; \sigma^2\bigr)_{\text{Ch. 6}} \\ &= \Bigl\{w_1\to \frac{9}{20},w_2\to \frac{1}{10},w_3\to \frac{9}{20},\theta_1\to -3,\theta_2\to 0,\theta_3\to 3, \\ &\qquad \sigma_1^2\to \frac{1}{3},\sigma_2^2\to \frac{1}{3},\sigma_3^2\to \frac{1}{3}\Bigr\} \end{aligned} Instantiate the within, between, and total variance on Chapter 6’s trimodal mixture.
await show`{${within}, ${between}, Integrate[x^2*Sum[w[j]*(${phi} /. {m -> th[j], v -> s2[j]}), {j, 3}] /. ${hoff}, {x, -Infinity, Infinity}]} /. ${hoff}`
(E{Var⁡(X∣Z)},  Var⁡(E[X∣Z]),  Var⁡(X))Ch. 6={13,8110,25330} \begin{aligned} &\bigl(E\{\operatorname{Var}(X \mid Z)\},\; \operatorname{Var}(E[X \mid Z]),\; \operatorname{Var}(X)\bigr)_{\text{Ch. 6}} \\ &= \left\{\frac{1}{3},\frac{81}{10},\frac{253}{30}\right\} \end{aligned}

Because E[X]=0E[X] = 0 by symmetry, Var⁡(X)=E[X2]=253/30≈8.433\operatorname{Var}(X) = E[X^2] = 253/30 \approx 8.433. Nearly 96%96\% of the variance (81/1081/10 out of 253/30253/30) lives between modes—explaining why a Gibbs sampler trapped in a single mode drastically underestimates posterior uncertainty.

Theorem 3 (Jensen’s Inequality). If gg is convex on an interval containing the support of XX and both E∣X∣E\lvert X\rvert and E∣g(X)∣E\lvert g(X)\rvert are finite, then E[g(X)]≥g(E[X])E[g(X)] \ge g(E[X]). If gg is strictly convex, equality holds if and only if XX is almost surely constant. (For concave gg, the inequality reverses.)

Proof sketch. Choose a supporting line g(x)≥g(μ)+c(x−μ)g(x) \ge g(\mu) + c(x - \mu) at μ=E[X]\mu = E[X] and take expectations of both sides; the linear term vanishes. □\square

Jensen’s inequality governs every nonlinear reparameterization in the book: log-odds versus probability in Chapter 4, variance σ2=1/σ~2\sigma^2 = 1/\tilde{\sigma}^2 versus precision in Chapter 5, and population-averaged rate E[eη]E[e^\eta] versus typical-group rate eE[η]e^{E[\eta]} in Chapter 11.

0.4.2Working Example 1b: Two Jensen Gaps for a Gamma Precision

For X∼gamma⁡(a,b)X \sim \operatorname{gamma}(a, b) (shape aa, rate bb), x↦1/xx \mapsto 1/x is strictly convex while x↦log⁡xx \mapsto \log x is strictly concave. Computing both expectations symbolically exposes the exact Jensen gaps, which we instantiate at the midge precision posterior (a,b)=(5,3/40)(a, b) = (5, 3/40):

Write the gamma⁡(a,b)\operatorname{gamma}(a, b) density in shape–rate form.
// 1b. Two Jensen gaps for a Gamma(a, b) precision random variable
const { a, b, x } = W
const dens = await show`${b}^${a}/Gamma[${a}]*${x}^(${a} - 1)*Exp[-${b}*${x}]`
p(x∣a,b)=bae−bxx−1+aΓ(a) p(x \mid a, b) = \frac{b^a e^{-b x} x^{-1+a}}{\Gamma (a)} Integrate 1/x1/x against a gamma⁡(a,b)\operatorname{gamma}(a, b) density to obtain E[1/X]E[1/X] symbolically.
const eInv = await show`Assuming[a > 1 && b > 0, FullSimplify[Integrate[${dens}/x, {x, 0, Infinity}]]]`
E[1/X]=b−1+a E[1/X] = \frac{b}{-1+a} Integrate log⁡x\log x against the same gamma density to express E[log⁡X]E[\log X] via the digamma function.
const eLog = await show`Assuming[a > 0 && b > 0, FullSimplify[Integrate[Log[x]*${dens}, {x, 0, Infinity}]]]`
E[log⁡X]=−log⁡(b)+ψ(0)(a) E[\log X] = -\log (b)+\psi^{(0)}(a) Form the two exact Jensen gaps for 1/x1/x (convex) and log⁡x\log x (concave).
const gaps = await show`Assuming[a > 1 && b > 0, {FullSimplify[${eInv} - b/a], FullSimplify[Log[a/b] - ${eLog}]}]`
(E[1/X]−1E[X],  log⁡E[X]−E[log⁡X])={b(−1+a)a,log⁡(a)−ψ(0)(a)} \begin{aligned} &\Bigl(E[1/X] - \frac{1}{E[X]},\; \log E[X] - E[\log X]\Bigr) \\ &= \left\{\frac{b}{(-1+a) a},\log (a)-\psi^{(0)}(a)\right\} \end{aligned} Evaluate both Jensen gaps numerically at the midge precision posterior (a,b)=(5,3/40)(a, b) = (5, 3/40).
await show`N[${gaps} /. {a -> 5, b -> 3/40}]`
(Δ1/x,  Δlog⁡)a=5,  b=3/40={0.00375,0.10332} \bigl(\Delta_{1/x},\; \Delta_{\log}\bigr)_{a=5,\; b=3/40} = \{0.00375,0.10332\}

0.5Conditioning, Bayes’ Rule, and Kernel Recognition

Bayes’ rule turns a generative model p(y∣θ)p(y \mid \theta) and prior p(θ)p(\theta) into the posterior

p(θ∣y)=p(y∣θ) p(θ)p(y),p(y)=∫Θp(y∣θ) p(θ) dθ. p(\theta \mid y) = \frac{p(y \mid \theta)\,p(\theta)}{p(y)}, \qquad p(y) = \int_\Theta p(y \mid \theta)\,p(\theta)\,d\theta.

Dividing two hypotheses H1,H0H_1, H_0 cancels p(y)p(y) and yields posterior odds=BF⁡10×prior odds\text{posterior odds} = \operatorname{BF}_{10} \times \text{prior odds} (used in Chapter 2 and Chapter 9). When observations are conditionally independent given θ\theta, updating sequentially with y1y_1 and then y2y_2 produces the exact same posterior as updating in a single batch with (y1,y2)(y_1, y_2).

Proposition 4 (Kernel Uniqueness). Suppose a density f(θ)f(\theta) satisfies f(θ)∝k(θ)f(\theta) \propto k(\theta) as a function of θ\theta. If 0<∫Θk(θ) dθ<∞0 < \int_\Theta k(\theta)\,d\theta < \infty, then f(θ)=k(θ)/∫Θk(θ) dθf(\theta) = k(\theta) / \int_\Theta k(\theta)\,d\theta uniquely. If ∫Θk(θ) dθ=∞\int_\Theta k(\theta)\,d\theta = \infty, no proper posterior exists.

The integrability clause is not a technicality: Chapter 6 shows an improper prior whose full conditionals are all proper while the joint kernel integrates to ∞\infty.

Proposition 5 (Score Identification). Two strictly positive C1C^1 densities f1,f2f_1, f_2 on the same connected open support are identical if and only if their log-gradients (scores) agree everywhere: ∇log⁡f1(θ)=∇log⁡f2(θ)\nabla \log f_1(\theta) = \nabla \log f_2(\theta).

Every full-conditional derivation in Parts II and III works by matching one of three score fingerprints:

  • Beta(α,β\alpha, \beta): α−1θ−β−11−θ\frac{\alpha - 1}{\theta} - \frac{\beta - 1}{1 - \theta},
  • Gamma(α,β\alpha, \beta): α−1θ−β\frac{\alpha - 1}{\theta} - \beta,
  • Normal(ℓ/q,1/q\ell/q, 1/q): ℓ−qθ\ell - q\theta (linear coefficient ℓ\ell, precision qq).

Remark 6 (What may—and may not—be dropped). A multiplicative factor may be absorbed into ∝\propto only if it does not depend on the variable currently being conditioned on. The marginal likelihood p(y)p(y) drops out when normalizing p(θ∣y)p(\theta \mid y) within a single model, but it is the exact quantity that drives Bayes factors and model averaging across competing models in Chapter 9.

0.5.1Working Example 2a: Normal Kernel Normalization and Precision Weighting

Integrating the unnormalized normal kernel e−12qx2+ℓxe^{-\frac{1}{2}qx^2 + \ell x} yields its log-normalizer log⁡Z(q,ℓ)\log Z(q, \ell), whose first two ℓ\ell-derivatives immediately return the mean ℓ/q\ell/q and variance 1/q1/q. Substituting the sum of prior and data precisions reproduces the conjugate normal posterior of Chapter 5:

Integrate the unnormalized normal kernel e−qx2/2+ℓxe^{-qx^2/2 + \ell x} to find its normalizing constant Z(q,ℓ)Z(q, \ell).
// 2a. Normal kernel normalization Z(q, l) and precision-weighted posterior
const { q, l } = W
const z = await show`Assuming[q > 0 && Element[l, Reals], Integrate[Exp[-${q}*x^2/2 + ${l}*x], {x, -Infinity, Infinity}]]`
Z(q,ℓ)=∫−∞∞e−12qx2+ℓx dx=eℓ22q2πq \begin{aligned} &Z(q,\ell) = \int_{-\infty}^{\infty} e^{-\frac{1}{2}q x^2 + \ell x}\,dx \\ &= \frac{e^{\frac{\ell^2}{2 q}} \sqrt{2 \pi}}{\sqrt{q}} \end{aligned} Differentiate log⁡Z(q,ℓ)\log Z(q, \ell) twice in ℓ\ell to read off the mean ℓ/q\ell/q and variance 1/q1/q.
const mv = await show`Simplify[{D[Log[${z}], l], D[Log[${z}], {l, 2}]}]`
(∂ℓlog⁡Z,  ∂ℓ2log⁡Z)=(E[X],  Var⁡(X))={ℓq,1q} \begin{aligned} &\bigl(\partial_\ell \log Z,\; \partial_\ell^2 \log Z\bigr) = \bigl(E[X],\; \operatorname{Var}(X)\bigr) \\ &= \left\{\frac{\ell}{q},\frac{1}{q}\right\} \end{aligned} Substitute prior and data precisions to obtain the conjugate normal posterior mean and variance.
const post = await show`Together[${mv} /. {q -> 1/t0^2 + n/s2, l -> mu0/t0^2 + n*yb/s2}]`
(μn,  τn2)={μ0σ2+nτ02y¯σ2+nτ02,σ2τ02σ2+nτ02} \begin{aligned} &(\mu_n,\; \tau_n^2) \\ &= \left\{\frac{\mu_0 \sigma^2 +n \tau_0^2 \bar{y}}{\sigma^2 +n \tau_0^2},\frac{\sigma^2 \tau_0^2}{\sigma^2 +n \tau_0^2}\right\} \end{aligned} Instantiate the normal posterior on Hoff’s midge wing-length data at σ2=0.017\sigma^2 = 0.017.
await show`N[${post} /. {mu0 -> 19/10, t0 -> 95/100, n -> 9, yb -> 1804/1000, s2 -> 17/1000}]`
(μn,  τn2)midge,  σ2=0.017={1.8042,0.00188494} (\mu_n,\; \tau_n^2)_{\text{midge},\; \sigma^2 = 0.017} = \{1.8042,0.00188494\}

0.5.2Working Example 2b: Score Fingerprints and Marginal Evidence

We verify all three conjugate score fingerprints simultaneously (Beta–Binomial, Gamma–Poisson, Normal–Normal), and then confirm that dividing the joint density p(y∣θ) p(θ)p(y \mid \theta)\,p(\theta) by the recognized posterior p(θ∣y)p(\theta \mid y) recovers the exact marginal evidence p(y)p(y)—instantiated on the rare-infection example (a=2,b=20,n=20,y=0a=2, b=20, n=20, y=0) of Chapter 1:

Write the three log joint kernels log⁡{p(y∣θ) p(θ)}\log\{p(y \mid \theta)\,p(\theta)\}: Beta–Binomial, Gamma–Poisson, Normal–Normal.
// 2b. Score fingerprints (Beta, Gamma, Normal) and marginal evidence p(y)
const logK = await show`{y*Log[t] + (n - y)*Log[1 - t] + (a - 1)*Log[t] + (b - 1)*Log[1 - t],
  sy*Log[t] - n*t + (a - 1)*Log[t] - b*t,
  -n*(yb - t)^2/(2*s2) - (t - mu0)^2/(2*t0^2)}`
(log⁡kbeta,  log⁡kgamma,  log⁡knormal)(θ)={(−1+a)log⁡(θ)+ylog⁡(θ)+(−1+b)log⁡(1−θ)+(n−y)log⁡(1−θ),−bθ−nθ+(−1+a)log⁡(θ)+sylog⁡(θ),  −n(y¯−θ)22σ2−(θ−μ0)22τ02} \begin{aligned} &\bigl(\log k_{\text{beta}},\; \log k_{\text{gamma}},\; \log k_{\text{normal}}\bigr)(\theta) \\ &= \Bigl\{(-1+a) \log (\theta)+y \log (\theta)+(-1+b) \log (1-\theta)+(n-y) \log (1-\theta), \\ &\qquad -b \theta-n \theta+(-1+a) \log (\theta)+s_y \log (\theta),\; -\frac{n (\bar{y}-\theta)^2}{2 \sigma^2}-\frac{(\theta-\mu_0)^2}{2 \tau_0^2}\Bigr\} \end{aligned} Check that the log-derivatives of the Beta, Gamma, and Normal kernels match their claimed posterior scores.
await show`Simplify[D[${logK}, t] - {
  (a + y - 1)/t - (b + n - y - 1)/(1 - t),
  (a + sy - 1)/t - (b + n),
  (mu0/t0^2 + n*yb/s2) - (1/t0^2 + n/s2)*t}]`
∂θlog⁡{p(y∣θ) p(θ)}−∂θlog⁡pclaimed(θ∣y)∣beta, gamma, normal={0,0,0} \begin{aligned} &\partial_\theta \log\{p(y \mid \theta)\,p(\theta)\} - \partial_\theta \log p_{\text{claimed}}(\theta \mid y)\Big|_{\text{beta},\,\text{gamma},\,\text{normal}} \\ &= \{0,0,0\} \end{aligned} Write the Beta–Binomial joint density p(y∣θ) p(θ)p(y \mid \theta)\,p(\theta).
const joint = await show`Binomial[n, y]*t^y*(1 - t)^(n - y)*t^(a - 1)*(1 - t)^(b - 1)/Beta[a, b]`
p(y∣θ) p(θ)=(1−θ)−1+b+n−yθ−1+a+y(ny)B(a,b) p(y \mid \theta)\,p(\theta) = \frac{(1-\theta)^{-1+b+n-y} \theta^{-1+a+y} \binom{n}{y}}{B(a,b)} Integrate out θ\theta to compute the exact Beta–Binomial marginal likelihood p(y)p(y).
const py = await show`Assuming[a > 0 && b > 0 && n >= y >= 0,
  Integrate[${joint}, {t, 0, 1}] /. Gamma[b + n - y]*Gamma[a + y] -> Beta[a + y, b + n - y]*Gamma[a + b + n]]`
p(y)=∫01p(y∣θ) p(θ) dθ=B(a+y,b+n−y)(ny)B(a,b) p(y) = \int_0^1 p(y \mid \theta)\,p(\theta)\,d\theta = \frac{B(a+y,b+n-y) \binom{n}{y}}{B(a,b)} Confirm that dividing the joint density by the normalized posterior recovers p(y)p(y) identically.
await show`Assuming[a > 0 && b > 0 && n >= y >= 0 && 0 < t < 1,
  FullSimplify[${joint}/(t^(a + y - 1)*(1 - t)^(b + n - y - 1)/Beta[a + y, b + n - y]) - ${py}]]`
p(y∣θ) p(θ)p(θ∣y)−p(y)=0 \frac{p(y \mid \theta)\,p(\theta)}{p(\theta \mid y)} - p(y) = 0 Evaluate the marginal evidence and posterior mean on Chapter 1’s rare-infection dataset (n=20,y=0n=20, y=0).
await show`N[{${py}, (a + y)/(a + b + n)} /. {a -> 2, b -> 20, n -> 20, y -> 0}]`
(p(y),  E[θ∣y])a=2,  b=20,  n=20,  y=0={0.256098,0.047619} \bigl(p(y),\; E[\theta \mid y]\bigr)_{a=2,\; b=20,\; n=20,\; y=0} = \{0.256098,0.047619\}

0.6Transformations, Special Functions, and Log-Concavity

Working in Bayesian inference constantly requires switching parameter scales—from probability θ\theta to log-odds log⁡θ1−θ\log\frac{\theta}{1-\theta} (Chapter 4), from precision σ~2\tilde{\sigma}^2 to variance σ2\sigma^2 (Chapter 5), from centered θj\theta_j to non-centered θ~j\tilde{\theta}_j (Chapter 8), and from positive θ\theta to log⁡θ\log\theta in Metropolis–Hastings proposals (Chapter 10).

Theorem 7 (Change of Variables). Let g:U→Vg : U \to V be a C1C^1 diffeomorphism between open subsets of ℝp\mathbb{R}^p, and let XX have density pXp_X on UU. Then V=g(X)V = g(X) has density

pV(v)=pX(g−1(v)) ∣det⁡Dg−1(v)∣. p_V(v) = p_X\bigl(g^{-1}(v)\bigr)\,\bigl\lvert \det Dg^{-1}(v) \bigr\rvert.

Remark 8 (What commutes with reparameterization). Quantiles are equivariant under strictly increasing maps (Qα(g(X))=g(Qα(X))Q_\alpha(g(X)) = g(Q_\alpha(X))), while strictly decreasing maps send the α\alpha-quantile to the (1−α)(1-\alpha)-quantile. By contrast, modes, HPD intervals, and means are not invariant: the Jacobian factor ∣det⁡Dg−1(v)∣\lvert \det Dg^{-1}(v)\rvert shifts peaks and tilts density contours (a theme explored in Chapter 3 and Chapter 4).

Three special-function identities underpin almost every closed-form integral in Parts I–III:

  • Gamma recursion: Γ(a+1)=a Γ(a)\Gamma(a + 1) = a\,\Gamma(a) for a>0a > 0, with Γ(n)=(n−1)!\Gamma(n) = (n - 1)! and Γ(1/2)=π\Gamma(1/2) = \sqrt{\pi}.

  • Beta–Gamma identity:

    B(a,b)=∫01ua−1(1−u)b−1 du=Γ(a)Γ(b)Γ(a+b). B(a, b) = \int_0^1 u^{a-1}(1-u)^{b-1}\,du = \frac{\Gamma(a)\Gamma(b)}{\Gamma(a+b)}.
  • Gaussian integral: squaring I=∫−∞∞e−x2/2 dxI = \int_{-\infty}^\infty e^{-x^2/2}\,dx and switching to polar coordinates gives

    I2=∫02π∫0∞re−r2/2 dr dφ=2π  ⟹  I=2π. I^2 = \int_0^{2\pi} \int_0^\infty r e^{-r^2/2}\,dr\,d\varphi = 2\pi \implies I = \sqrt{2\pi}.

Proposition 9 (Log-Concavity and Unimodality). A density pp is log-concave if log⁡p\log p is concave on its support. Log-concavity is preserved under products (priors times likelihoods), affine pre-composition (x↦Ax+bx \mapsto Ax + b), and marginalization (Prékopa’s theorem). A strictly log-concave density has at most one mode (existence also requires that the supremum be attained in the interior).

Log-concavity guarantees well-behaved posterior geometry in GLMs (Chapter 10) and ordered probit models (Chapter 12), though it is sufficient rather than necessary for unimodality: both the inverse-gamma and Student-tt densities are unimodal yet not globally log-concave.

0.6.1Working Example 3a: Gamma Precision on Three Scales

Starting from σ~2∼gamma⁡(a,b)\tilde{\sigma}^2 \sim \operatorname{gamma}(a, b), we transform to variance σ2=1/σ~2\sigma^2 = 1/\tilde{\sigma}^2 and log-precision η=log⁡σ~2\eta = \log\tilde{\sigma}^2. Notice how the modes fail to commute (1/mode⁡(σ~2)≠mode⁡(σ2)1/\operatorname{mode}(\tilde{\sigma}^2) \ne \operatorname{mode}(\sigma^2)) whereas the medians match identically, and how η\eta is strictly log-concave for every a>0a > 0 (while σ~2\tilde{\sigma}^2 requires a≥1a \ge 1 and σ2\sigma^2 is never globally log-concave). Here (a,b)=(5,3/40)(a, b) = (5, 3/40) is the midge posterior with σn2≈0.015\sigma_n^2 \approx 0.015, and Wolfram’s GammaDistribution uses scale 1/b=40/31/b = 40/3:

Write the gamma⁡(a,b)\operatorname{gamma}(a, b) density of the precision σ~2\tilde{\sigma}^2.
// 3a. Gamma precision on three scales: precision, variance, and log-precision
const px = await show`b^a/Gamma[a]*x^(a - 1)*Exp[-b*x]`
pσ~2(x)=bae−bxx−1+aΓ(a) p_{\tilde{\sigma}^2}(x) = \frac{b^a e^{-b x} x^{-1+a}}{\Gamma (a)} Apply the 1-D change-of-variables formula to map σ~2∼gamma⁡(a,b)\tilde{\sigma}^2 \sim \operatorname{gamma}(a, b) to variance σ2=1/σ~2\sigma^2 = 1/\tilde{\sigma}^2.
const pv = await show`Assuming[a > 0 && b > 0 && v > 0, FullSimplify[(${px} /. x -> 1/v)*Abs[D[1/v, v]]]]`
pσ2(v)=pσ~2(1/v) ∣ddvv−1∣=bae−bvv−1−aΓ(a) \begin{aligned} &p_{\sigma^2}(v) = p_{\tilde{\sigma}^2}(1/v)\,\bigl\lvert \tfrac{d}{dv} v^{-1}\bigr\rvert \\ &= \frac{b^a e^{-\frac{b}{v}} v^{-1-a}}{\Gamma (a)} \end{aligned} Verify that the resulting inverse-gamma density integrates to 11 over (0,∞)(0, \infty).
await show`Assuming[a > 0 && b > 0, Integrate[${pv}, {v, 0, Infinity}]]`
∫0∞pσ2(v) dv=1 \int_0^\infty p_{\sigma^2}(v)\,dv = 1 Transform the same precision to the log scale η=log⁡σ~2\eta = \log \tilde{\sigma}^2 via its exponential Jacobian.
const pe = await show`Assuming[a > 0 && b > 0 && Element[h, Reals], FullSimplify[(${px} /. x -> Exp[h])*Abs[D[Exp[h], h]]]]`
pη(η)∣η=log⁡σ~2=bae−beη+aηΓ(a) p_{\eta}(\eta)\big|_{\eta = \log \tilde{\sigma}^2} = \frac{b^a e^{-b e^\eta+a \eta}}{\Gamma (a)} Solve ∂log⁡p=0\partial \log p = 0 on both scales to compare the precision mode with the variance mode.
const modes = await show`Assuming[a > 1 && b > 0, Simplify[{x /. First[Solve[D[Log[${px}], x] == 0, x]], v /. First[Solve[D[Log[${pv}], v] == 0, v]]}]]`
(mode⁡(σ~2),  mode⁡(σ2))={−1+ab,b1+a} \begin{aligned} &\bigl(\operatorname{mode}(\tilde{\sigma}^2),\; \operatorname{mode}(\sigma^2)\bigr) \\ &= \left\{\frac{-1+a}{b},\frac{b}{1+a}\right\} \end{aligned} Compute the second log-derivatives on all three scales to test global log-concavity.
await show`Assuming[a > 0 && b > 0 && x > 0 && v > 0 && Element[h, Reals], Simplify[{D[Log[${px}], {x, 2}], D[Log[${pv}], {v, 2}], D[Log[${pe}], {h, 2}]}]]`
(∂x2log⁡pσ~2,  ∂v2log⁡pσ2,  ∂η2log⁡pη)={1−ax2,−2b+v+avv3,−beη} \begin{aligned} &\bigl(\partial_x^2 \log p_{\tilde{\sigma}^2},\; \partial_v^2 \log p_{\sigma^2},\; \partial_\eta^2 \log p_\eta\bigr) \\ &= \left\{\frac{1-a}{x^2},\frac{-2 b+v+a v}{v^3},-b e^\eta\right\} \end{aligned} Instantiate 1/mode⁡(σ~2)1/\operatorname{mode}(\tilde{\sigma}^2) versus mode⁡(σ2)\operatorname{mode}(\sigma^2) to show that modes do not commute.
await show`N[{1/${modes}[[1]], ${modes}[[2]]} /. {a -> 5, b -> 3/40}]`
(1/mode⁡(σ~2),  mode⁡(σ2))a=5,  b=3/40={0.01875,0.0125} \begin{aligned} &\bigl(1/\operatorname{mode}(\tilde{\sigma}^2),\; \operatorname{mode}(\sigma^2)\bigr)_{a=5,\; b=3/40} \\ &= \{0.01875,0.0125\} \end{aligned} Compare 1/med⁡(σ~2)1/\operatorname{med}(\tilde{\sigma}^2) with med⁡(σ2)\operatorname{med}(\sigma^2) to confirm median equivariance.
await show`N[{1/Median[GammaDistribution[5, 40/3]], Median[InverseGammaDistribution[5, 3/40]]}]`
(1/med⁡(σ~2),  med⁡(σ2))a=5,  b=3/40={0.0160568,0.0160568} \begin{aligned} &\bigl(1/\operatorname{med}(\tilde{\sigma}^2),\; \operatorname{med}(\sigma^2)\bigr)_{a=5,\; b=3/40} \\ &= \{0.0160568,0.0160568\} \end{aligned}

0.6.2Working Example 3b: The Beta Integral from a 2-D Jacobian

Let X1∼gamma⁡(a,1)X_1 \sim \operatorname{gamma}(a, 1) and X2∼gamma⁡(b,1)X_2 \sim \operatorname{gamma}(b, 1) be independent, and set S=X1+X2S = X_1 + X_2, U=X1/(X1+X2)U = X_1 / (X_1 + X_2). The inverse map (x1,x2)=(su,s(1−u))(x_1, x_2) = (su, s(1-u)) has Jacobian determinant −s-s, which simultaneously proves B(a,b)=Γ(a)Γ(b)/Γ(a+b)B(a, b) = \Gamma(a)\Gamma(b)/\Gamma(a+b) and shows that S∼gamma⁡(a+b,1)S \sim \operatorname{gamma}(a+b, 1) is independent of U∼beta⁡(a,b)U \sim \operatorname{beta}(a, b):

Compute the 2-D Jacobian determinant of the map (s,u)↦(su,s(1−u))(s, u) \mapsto (su, s(1 - u)).
// 3b. Deriving the Beta integral & independence via a 2D Jacobian
const jac = await show`Simplify[Det[D[{s*u, s*(1 - u)}, {{s, u}}]]]`
det⁡∂(x1,x2)∂(s,u)=−s \det\frac{\partial(x_1, x_2)}{\partial(s, u)} = -s Push the two independent gamma densities through (s,u)(s, u) and multiply by ∣−s∣\lvert -s \rvert to get the joint density of (S,U)(S, U).
const joint = await show`Assuming[s > 0 && 0 < u < 1 && a > 0 && b > 0, Simplify[PowerExpand[
  (x1^(a - 1)*Exp[-x1]/Gamma[a])*(x2^(b - 1)*Exp[-x2]/Gamma[b]) /. {x1 -> s*u, x2 -> s*(1 - u)}]*Abs[${jac}]]]`
pS,U(s,u)=pX1(su) pX2(s(1−u)) ∣−s∣=e−ss−1+a+b(1−u)−1+bu−1+aΓ(a)Γ(b) \begin{aligned} &p_{S,U}(s,u) = p_{X_1}(su)\,p_{X_2}\bigl(s(1-u)\bigr)\,\lvert -s \rvert \\ &= \frac{e^{-s} s^{-1+a+b} (1-u)^{-1+b} u^{-1+a}}{\Gamma (a) \Gamma (b)} \end{aligned} Integrate out the total ss to derive the beta⁡(a,b)\operatorname{beta}(a, b) marginal density of u=X1/(X1+X2)u = X_1/(X_1 + X_2).
const margU = await show`Assuming[0 < u < 1 && a > 0 && b > 0, FullSimplify[Integrate[${joint}, {s, 0, Infinity}]]]`
pU(u)=∫0∞pS,U(s,u) ds=(1−u)−1+bu−1+aΓ(a+b)Γ(a)Γ(b) \begin{aligned} &p_U(u) = \int_0^\infty p_{S,U}(s,u)\,ds \\ &= \frac{(1-u)^{-1+b} u^{-1+a} \Gamma (a+b)}{\Gamma (a) \Gamma (b)} \end{aligned} Check that the joint density factors as pS(s) pU(u)p_S(s)\,p_U(u), proving S⊥US \perp U.
await show`Assuming[s > 0 && 0 < u < 1 && a > 0 && b > 0,
  FullSimplify[${joint}/(${margU}*s^(a + b - 1)*Exp[-s]/Gamma[a + b])]]`
pS,U(s,u)pS(s) pU(u)=1 \frac{p_{S,U}(s,u)}{p_S(s)\,p_U(u)} = 1 Cross-check Γ(3)Γ(9)/Γ(12)\Gamma(3)\Gamma(9)/\Gamma(12) directly against ∫01u2(1−u)8 du\int_0^1 u^2(1-u)^8\,du.
await show`{Gamma[a]*Gamma[b]/Gamma[a + b] /. {a -> 3, b -> 9}, Integrate[u^2*(1 - u)^8, {u, 0, 1}]}`
(Γ(3)Γ(9)Γ(12),  ∫01u2(1−u)8 du)={1495,1495} \begin{aligned} &\Bigl(\tfrac{\Gamma(3)\Gamma(9)}{\Gamma(12)},\; \int_0^1 u^2(1-u)^8\,du\Bigr) \\ &= \left\{\frac{1}{495},\frac{1}{495}\right\} \end{aligned}

0.7Linear Algebra for Multivariate Models

From Chapter 7 onward, scalar variances become symmetric positive-definite (SPD) covariance matrices Σ\Sigma, precisions become Ω=Σ−1\Omega = \Sigma^{-1}, and regression projections become idempotent matrices PXP_X.

Proposition 10 (Characterizations of Positive-Definite Matrices). For a real symmetric p×pp \times p matrix A=ATA = A^T, the following are equivalent: (i) 𝐱TA𝐱>0\mathbf{x}^T A \mathbf{x} > 0 for all 𝐱≠𝟎\mathbf{x} \ne \mathbf{0}; (ii) all eigenvalues of AA are strictly positive; (iii) all leading principal minors of AA are strictly positive; (iv) A=LLTA = L L^T for a unique lower-triangular Cholesky factor LL with strictly positive diagonal entries.

Cholesky factorization turns standard normal draws 𝐳∼𝒩p(𝟎,Ip)\mathbf{z} \sim \mathcal{N}_p(\mathbf{0}, I_p) into arbitrary multivariate normals without explicitly inverting large matrices:

  • Covariance form: if Σ=LLT\Sigma = L L^T, then 𝝁+L𝐳∼𝒩p(𝝁,Σ)\boldsymbol{\mu} + L\mathbf{z} \sim \mathcal{N}_p(\boldsymbol{\mu}, \Sigma).
  • Precision form: if A=LLTA = L L^T, then A−1𝐛+L−T𝐳∼𝒩p(A−1𝐛,A−1)A^{-1}\mathbf{b} + L^{-T}\mathbf{z} \sim \mathcal{N}_p(A^{-1}\mathbf{b}, A^{-1}).

Completing the square ((B.2) in Appendix B) expands −12(𝐱−𝐦)TA(𝐱−𝐦)-\tfrac{1}{2}(\mathbf{x} - \mathbf{m})^T A (\mathbf{x} - \mathbf{m}) around 𝐦=A−1𝐛\mathbf{m} = A^{-1}\mathbf{b} using symmetry A=ATA = A^T, immediately giving the multivariate Gaussian integral

∫ℝpexp⁡ ⁣(−12𝐱TA𝐱+𝐛T𝐱)d𝐱=(2π)p/2∣A∣−1/2exp⁡ ⁣(12𝐛TA−1𝐛), \int_{\mathbb{R}^p} \exp\!\left(-\tfrac{1}{2}\mathbf{x}^T A \mathbf{x} + \mathbf{b}^T\mathbf{x}\right) d\mathbf{x} = (2\pi)^{p/2} \lvert A \rvert^{-1/2} \exp\!\left(\tfrac{1}{2}\mathbf{b}^T A^{-1} \mathbf{b}\right),

used in Chapter 7, Chapter 8, Chapter 9, and Chapter 11.

Theorem 11 (Schur Complement Factorization). Partition an SPD matrix Σ\Sigma and vector 𝐳\mathbf{z} into blocks aa and bb, with Schur complement Σb∣a=Σb,b−Σb,aΣa,a−1Σa,b\Sigma_{b\mid a} = \Sigma_{b,b} - \Sigma_{b,a}\Sigma_{a,a}^{-1}\Sigma_{a,b} and residual 𝐫=𝐳b−Σb,aΣa,a−1𝐳a\mathbf{r} = \mathbf{z}_b - \Sigma_{b,a}\Sigma_{a,a}^{-1}\mathbf{z}_a. Then

∣Σ∣=∣Σa,a∣⋅∣Σb∣a∣,𝐳TΣ−1𝐳=𝐳aTΣa,a−1𝐳a+𝐫TΣb∣a−1𝐫. \lvert \Sigma \rvert = \lvert \Sigma_{a,a} \rvert \cdot \lvert \Sigma_{b\mid a} \rvert, \qquad \mathbf{z}^T \Sigma^{-1} \mathbf{z} = \mathbf{z}_a^T \Sigma_{a,a}^{-1} \mathbf{z}_a + \mathbf{r}^T \Sigma_{b\mid a}^{-1} \mathbf{r}.

Together, those two identities are the marginal ×\times conditional factorization of the multivariate normal density powering missing-data imputation in Chapter 7 and partial correlations in Chapter 12.

Theorem 12 (Orthogonal Projection and Zellner’s g-Prior Identity). Let X∈ℝn×pX \in \mathbb{R}^{n \times p} have full column rank pp, and set PX=X(XTX)−1XTP_X = X(X^T X)^{-1} X^T. Then PXP_X is symmetric and idempotent (PX2=PXP_X^2 = P_X), has tr⁡(PX)=rank⁡(PX)=p\operatorname{tr}(P_X) = \operatorname{rank}(P_X) = p, and satisfies XT(In−PX)=0X^T(I_n - P_X) = 0. Because its eigenvalues lie in {0,1}\{0, 1\} (pp ones and n−pn - p zeros), for every g>−1g > -1,

∣In+gPX∣=(1+g)p,(In+gPX)−1=In−g1+gPX. \lvert I_n + g P_X \rvert = (1 + g)^p, \qquad (I_n + g P_X)^{-1} = I_n - \frac{g}{1 + g} P_X.

This theorem gives the entire closed-form marginal likelihood and SSR⁡g\operatorname{SSR}_g formula of Chapter 9 in two lines.

0.7.1Working Example 4a: Vector Completing the Square and Cholesky

We verify completing the square in ℝ2\mathbb{R}^2 and compute the symbolic lower-triangular Cholesky factor LL of a 2×22 \times 2 covariance matrix (note that Wolfram’s CholeskyDecomposition returns the upper factor RR with Σ=RTR\Sigma = R^T R, so we transpose it), instantiated on the reading-comprehension prior of Chapter 7:

Solve the 2×22 \times 2 linear system A𝐦=𝐛A\mathbf{m} = \mathbf{b} symbolically for the quadratic center.
// 4a. Completing the square in R^2 and symbolic Cholesky factorization
const x = [W.x1, W.x2], b = [W.b1, W.b2], A = [[W.a11, W.a12], [W.a12, W.a22]]
const m = await show`Simplify[Inverse[${A}] . ${b}]`
𝐦=A−1𝐛={−A2,2b1+A1,2b2A1,22−A1,1A2,2,A1,2b1−A1,1b2A1,22−A1,1A2,2} \begin{aligned} &\mathbf{m} = A^{-1}\mathbf{b} \\ &= \left\{\frac{-A_{2,2} b_1 +A_{1,2} b_2}{A_{1,2}^2-A_{1,1} A_{2,2}},\frac{A_{1,2} b_1 -A_{1,1} b_2}{A_{1,2}^2-A_{1,1} A_{2,2}}\right\} \end{aligned} Verify the vector completing-the-square identity in ℝ2\mathbb{R}^2 by symbolic simplification.
await show`Simplify[(-${x} . ${A} . ${x}/2 + ${x} . ${b}) - (-(${x} - ${m}) . ${A} . (${x} - ${m})/2 + ${b} . ${m}/2)]`
(−12𝐱TA𝐱+𝐱T𝐛)−(−12(𝐱−𝐦)TA(𝐱−𝐦)+12𝐛TA−1𝐛)=0 \begin{aligned} &\Bigl(-\tfrac12\mathbf{x}^T A\mathbf{x} + \mathbf{x}^T\mathbf{b}\Bigr) - \Bigl(-\tfrac12(\mathbf{x}-\mathbf{m})^T A(\mathbf{x}-\mathbf{m}) + \tfrac12\mathbf{b}^T A^{-1}\mathbf{b}\Bigr) \\ &= 0 \end{aligned} Write a general 2×22 \times 2 covariance matrix in standard-deviation and correlation form.
const cov = await show`{{s1^2, rho*s1*s2}, {rho*s1*s2, s2^2}}`
Σ=(σ12ρσ1σ2ρσ1σ2σ22) \Sigma = \left( \begin{array}{cc} \sigma_1^2 & \rho \sigma_1 \sigma_2 \\ \rho \sigma_1 \sigma_2 & \sigma_2^2 \\ \end{array} \right) Compute the symbolic lower-triangular Cholesky factor LL of a 2×22 \times 2 covariance matrix.
const L = await show`Assuming[s1 > 0 && s2 > 0 && -1 < rho < 1, FullSimplify[Transpose[CholeskyDecomposition[${cov}]]]]`
L=chol⁡(Σ)=(σ10ρσ21−ρ2σ2) \begin{aligned} &L = \operatorname{chol}(\Sigma) \\ &= \left( \begin{array}{cc} \sigma_1 & 0 \\ \rho \sigma_2 & \sqrt{1-\rho^2} \sigma_2 \\ \end{array} \right) \end{aligned} Instantiate LL on Chapter 7’s reading-comprehension prior and verify LLT=ΣL L^T = \Sigma.
await show`{${L} /. {s1 -> 25, s2 -> 25, rho -> 1/2}, Simplify[${L} . Transpose[${L}] - ${cov}, -1 < rho < 1]}`
(Lreading prior,  LLT−Σ)={(2502522532),(0000)} \begin{aligned} &\bigl(L_{\text{reading prior}},\; LL^T - \Sigma\bigr) \\ &= \left\{\left( \begin{array}{cc} 25 & 0 \\ \frac{25}{2} & \frac{25 \sqrt{3}}{2} \\ \end{array} \right),\left( \begin{array}{cc} 0 & 0 \\ 0 & 0 \\ \end{array} \right)\right\} \end{aligned}

0.7.2Working Example 4b: Schur Complement and Projection Matrix PXP_X

First we verify both Schur-complement identities on a general symbolic 3×33 \times 3 covariance matrix Σ\Sigma, then instantiate at Σ1,1=Σ2,2=Σ3,3=4\Sigma_{1,1} = \Sigma_{2,2} = \Sigma_{3,3} = 4, Σ1,2=Σ1,3=2\Sigma_{1,2} = \Sigma_{1,3} = 2, Σ2,3=1\Sigma_{2,3} = 1—where the regression coefficient on Y2Y_2 vanishes ({1/2,0}\{1/2, 0\}), proving Y2⊥Y3∣Y1Y_2 \perp Y_3 \mid Y_1 despite Σ2,3≠0\Sigma_{2,3} \ne 0:

Compute the regression coefficients Σb,aΣa,a−1\Sigma_{b,a}\Sigma_{a,a}^{-1} of z3z_3 on (z1,z2)(z_1, z_2) for a general symmetric 3×33 \times 3 Σ\Sigma.
// 4b-i. 3x3 Schur complement determinant & quadratic-form factorization
const Sig = [[W.s11, W.s12, W.s13], [W.s12, W.s22, W.s23], [W.s13, W.s23, W.s33]]
const z = [W.z1, W.z2, W.z3], za = [W.z1, W.z2]
const coef = await show`${Sig}[[3, {1, 2}]] . Inverse[${Sig}[[{1, 2}, {1, 2}]]]`
Σb,aΣa,a−1={Σ1,3Σ2,2−Σ1,2Σ2,3Σ1,1Σ2,2−Σ1,22,Σ1,1Σ2,3−Σ1,2Σ1,3Σ1,1Σ2,2−Σ1,22} \begin{aligned} &\Sigma_{b,a}\Sigma_{a,a}^{-1} \\ &= \left\{\frac{\Sigma_{1,3} \Sigma_{2,2}-\Sigma_{1,2} \Sigma_{2,3}}{\Sigma_{1,1} \Sigma_{2,2}-\Sigma_{1,2}^2},\frac{\Sigma_{1,1} \Sigma_{2,3}-\Sigma_{1,2} \Sigma_{1,3}}{\Sigma_{1,1} \Sigma_{2,2}-\Sigma_{1,2}^2}\right\} \end{aligned} Form the Schur complement Σb∣a=Σ3,3−Σb,aΣa,a−1Σa,b\Sigma_{b\mid a} = \Sigma_{3,3} - \Sigma_{b,a}\Sigma_{a,a}^{-1}\Sigma_{a,b}, the conditional variance of z3z_3.
const schur = await show`${Sig}[[3, 3]] - ${coef} . ${Sig}[[{1, 2}, 3]]`
Σb∣a=Σ3,3−Σ2,2Σ1,32−2Σ1,2Σ1,3Σ2,3+Σ1,1Σ2,32Σ1,1Σ2,2−Σ1,22 \Sigma_{b\mid a} = \Sigma_{3,3} - \frac{\Sigma_{2,2} \Sigma_{1,3}^2 - 2 \Sigma_{1,2} \Sigma_{1,3} \Sigma_{2,3} + \Sigma_{1,1} \Sigma_{2,3}^2}{\Sigma_{1,1} \Sigma_{2,2}-\Sigma_{1,2}^2} Verify both the Schur-complement determinant factorization and the precision identity Ωb,bΣb∣a=1\Omega_{b,b}\Sigma_{b\mid a} = 1 in 3×33 \times 3.
await show`Simplify[{Det[${Sig}] - Det[${Sig}[[{1, 2}, {1, 2}]]]*${schur}, Inverse[${Sig}][[3, 3]]*${schur}}]`
(∣Σ∣−∣Σa,a∣ Σb∣a,  Ωb,b Σb∣a)a={1,2},  b={3}={0,1} \begin{aligned} &\bigl(\lvert\Sigma\rvert - \lvert\Sigma_{a,a}\rvert\,\Sigma_{b\mid a},\; \Omega_{b,b}\,\Sigma_{b\mid a}\bigr)_{a=\{1,2\},\; b=\{3\}} \\ &= \{0,1\} \end{aligned} Confirm that the 3×33 \times 3 quadratic form splits into marginal and conditional-residual squares.
await show`Simplify[${z} . Inverse[${Sig}] . ${z} - ${za} . Inverse[${Sig}[[{1, 2}, {1, 2}]]] . ${za} - (z3 - ${coef} . ${za})^2/${schur}]`
𝐳TΣ−1𝐳−𝐳aTΣa,a−1𝐳a−(z3−Σb,aΣa,a−1𝐳a)2Σb∣a=0 \begin{aligned} &\mathbf{z}^T\Sigma^{-1}\mathbf{z} - \mathbf{z}_a^T\Sigma_{a,a}^{-1}\mathbf{z}_a - \frac{(z_3 - \Sigma_{b,a}\Sigma_{a,a}^{-1}\mathbf{z}_a)^2}{\Sigma_{b\mid a}} \\ &= 0 \end{aligned} Instantiate on a 3×33 \times 3 covariance where Y2⊥Y3∣Y1Y_2 \perp Y_3 \mid Y_1 despite nonzero Σ2,3\Sigma_{2,3}.
await show`{${coef}, ${schur}, Det[${Sig}]} /. {s11 -> 4, s22 -> 4, s33 -> 4, s12 -> 2, s13 -> 2, s23 -> 1}`
(Σb,aΣa,a−1,  Σb∣a,  ∣Σ∣)={{12,0},3,36} \begin{aligned} &\bigl(\Sigma_{b,a}\Sigma_{a,a}^{-1},\; \Sigma_{b\mid a},\; \lvert\Sigma\rvert\bigr) \\ &= \left\{\left\{\frac{1}{2},0\right\},3,36\right\} \end{aligned}

Next we verify idempotence, orthogonality, trace, ∣I3+gPX∣=(1+g)2\lvert I_3 + g P_X\rvert = (1 + g)^2, the Woodbury inverse (I3+gPX)−1=I3−g1+gPX(I_3 + g P_X)^{-1} = I_3 - \frac{g}{1+g} P_X, and the Pythagorean decomposition:

𝐲TPX𝐲+𝐲T(I3−PX)𝐲=792+32=41=𝐲T𝐲. \mathbf{y}^T P_X \mathbf{y} + \mathbf{y}^T(I_3 - P_X)\mathbf{y} = \frac{79}{2} + \frac{3}{2} = 41 = \mathbf{y}^T \mathbf{y}. Build the hat matrix PXP_X of a straight-line design with intercept and covariate values x1,x2,x3x_1, x_2, x_3; here D=(x1−x2)2+(x1−x3)2+(x2−x3)2D = (x_1-x_2)^2+(x_1-x_3)^2+(x_2-x_3)^2.
// 4b-ii. OLS projection matrix P_X, g-prior determinant & inverse, and Pythagoras
const X = [[1, W.x1], [1, W.x2], [1, W.x3]]
const P = await show`Simplify[${X} . Inverse[Transpose[${X}] . ${X}] . Transpose[${X}]]`
PX=X(XTX)−1XT=1D((x1−x2)2+(x1−x3)2(x1−x3)(x2−x3)(x1−x2)(x3−x2)(x1−x3)(x2−x3)(x1−x2)2+(x2−x3)2(x2−x1)(x3−x1)(x1−x2)(x3−x2)(x2−x1)(x3−x1)(x1−x3)2+(x2−x3)2) \begin{aligned} &P_X = X(X^TX)^{-1}X^T \\ &= \frac{1}{D}\left( \begin{array}{ccc} (x_1-x_2)^2+(x_1-x_3)^2 & (x_1-x_3) (x_2-x_3) & (x_1-x_2) (x_3-x_2) \\ (x_1-x_3) (x_2-x_3) & (x_1-x_2)^2+(x_2-x_3)^2 & (x_2-x_1) (x_3-x_1) \\ (x_1-x_2) (x_3-x_2) & (x_2-x_1) (x_3-x_1) & (x_1-x_3)^2+(x_2-x_3)^2 \\ \end{array} \right) \end{aligned} Verify symbolically that the OLS hat matrix PXP_X is idempotent and orthogonal to the residual space.
await show`Simplify[{${P} . ${P} - ${P}, Transpose[${X}] . (IdentityMatrix[3] - ${P})}]`
(PX2−PX,  XT(I3−PX))={(000000000),(000000)} \begin{aligned} &\bigl(P_X^2 - P_X,\; X^T(I_3 - P_X)\bigr) \\ &= \left\{\left( \begin{array}{ccc} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{array} \right),\left( \begin{array}{ccc} 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{array} \right)\right\} \end{aligned} Compute tr⁡(PX)\operatorname{tr}(P_X) and the gg-prior determinant ∣I3+gPX∣=(1+g)2\lvert I_3 + g P_X\rvert = (1 + g)^2.
await show`Simplify[{Tr[${P}], Det[IdentityMatrix[3] + g*${P}]}]`
(tr⁡PX,  ∣I3+gPX∣)={2,(1+g)2} \bigl(\operatorname{tr}P_X,\; \lvert I_3 + gP_X\rvert\bigr) = \left\{2,(1+g)^2\right\} Confirm the Woodbury inverse (I3+gPX)−1=I3−g/(1+g)PX(I_3 + g P_X)^{-1} = I_3 - g/(1+g)P_X symbolically.
await show`Simplify[Inverse[IdentityMatrix[3] + g*${P}] - (IdentityMatrix[3] - g/(1 + g)*${P})]`
(I3+gPX)−1−(I3−g1+gPX)=(000000000) \begin{aligned} &(I_3 + gP_X)^{-1} - \Bigl(I_3 - \tfrac{g}{1+g}P_X\Bigr) \\ &= \left( \begin{array}{ccc} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{array} \right) \end{aligned} Instantiate PX𝐲P_X\mathbf{y} and verify the Pythagorean sum-of-squares split on a concrete vector 𝐲\mathbf{y}.
await show`With[{h = ${P} /. {x1 -> -1, x2 -> 0, x3 -> 1}, y = {1, 2, 6}}, {h . y, y . h . y, y . (y - h . y), y . y}]`
(PX𝐲,  𝐲TPX𝐲,  𝐲T(I3−PX)𝐲,  𝐲T𝐲)={{12,3,112},792,32,41} \begin{aligned} &\bigl(P_X\mathbf{y},\; \mathbf{y}^T P_X\mathbf{y},\; \mathbf{y}^T(I_3-P_X)\mathbf{y},\; \mathbf{y}^T\mathbf{y}\bigr) \\ &= \left\{\left\{\frac{1}{2},3,\frac{11}{2}\right\},\frac{79}{2},\frac{3}{2},41\right\} \end{aligned}

0.8Monte Carlo and Markov Chain Foundations

When posterior integrals lack closed forms, Parts I–III replace exact calculus with simulation—first via independent draws (Chapter 4) and then via Markov chains (Chapter 6–Chapter 12).

Theorem 13 (Strong Law and Central Limit Theorem for i.i.d. Monte Carlo). Let ϕ(1),…,ϕ(S)∼i.i.d.π\phi^{(1)}, \dots, \phi^{(S)} \stackrel{\text{i.i.d.}}{\sim} \pi and set g¯S=1S∑s=1Sg(ϕ(s))\bar{g}_S = \frac{1}{S} \sum_{s=1}^S g(\phi^{(s)}). If Eπ∣g(ϕ)∣<∞E_\pi\lvert g(\phi)\rvert < \infty, then g¯S→Eπ[g(ϕ)]\bar{g}_S \to E_\pi[g(\phi)] almost surely. If σ2=Var⁡π(g(ϕ))<∞\sigma^2 = \operatorname{Var}_\pi(g(\phi)) < \infty, then

S (g¯S−Eπ[g(ϕ)])→d𝒩(0,σ2),MCSE=σS, \sqrt{S}\,\bigl(\bar{g}_S - E_\pi[g(\phi)]\bigr) \xrightarrow{d} \mathcal{N}(0, \sigma^2), \qquad \mathrm{MCSE} = \frac{\sigma}{\sqrt{S}},

a convergence rate of O(S−1/2)O(S^{-1/2}) that is independent of parameter dimension pp.

When direct sampling from π\pi is impossible, we construct a Markov chain with transition kernel K(ϕ,ϕ′)K(\phi, \phi') that leaves π\pi stationary (invariant): πK=π\pi K = \pi, i.e., ∫π(ϕ) K(ϕ,ϕ′) dϕ=π(ϕ′)\int \pi(\phi)\,K(\phi, \phi')\,d\phi = \pi(\phi'). A stronger, easily checked sufficient condition is detailed balance (reversibility):

π(ϕ) K(ϕ,ϕ′)=π(ϕ′) K(ϕ′,ϕ). \pi(\phi)\,K(\phi, \phi') = \pi(\phi')\,K(\phi', \phi).

Integrating both sides over ϕ\phi immediately yields πK=π\pi K = \pi. The converse fails: a systematic-scan Gibbs sampler (Chapter 6) is generally stationary without being reversible.

Theorem 14 (Ergodic Theorem for Markov Chains). Let π\pi be an invariant probability distribution of a π\pi-irreducible Markov chain with transition kernel KK.

  1. For every π\pi-integrable function gg, time averages converge almost surely: g¯S→Eπ[g(ϕ)]\bar{g}_S \to E_\pi[g(\phi)] from π\pi-almost every starting state (no aperiodicity required).
  2. If in addition the chain is aperiodic, the ss-step distribution converges in total variation: ∥Ks(ϕ,⋅)−π∥TV→0\lVert K^s(\phi, \cdot) - \pi \rVert_{\text{TV}} \to 0 from π\pi-almost every start.
  3. If the chain is Harris recurrent, both conclusions hold from every starting state ϕ∈Θ\phi \in \Theta.

Remark 15 (Why aperiodicity separates averages from distributions). The deterministic two-state flip

K=(0110) K = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}

is irreducible with invariant distribution π=(1/2,1/2)\pi = (1/2, 1/2): its ergodic averages converge to 12(g(1)+g(2))\frac{1}{2}(g(1) + g(2)), yet KsK^s oscillates forever and never converges in total variation.

Proposition 16 (Asymptotic Variance and Effective Sample Size). For a stationary Markov chain with lag-tt autocorrelations ρt=Corr⁡(g(ϕ(s)),g(ϕ(s+t)))\rho_t = \operatorname{Corr}(g(\phi^{(s)}), g(\phi^{(s+t)})) satisfying ∑t=1∞∣ρt∣<∞\sum_{t=1}^\infty \lvert\rho_t\rvert < \infty,

SVar⁡(g¯S)⟶σ2 ⁣(1+2∑t=1∞ρt),Seff=S1+2∑t=1∞ρt. S\operatorname{Var}(\bar{g}_S) \longrightarrow \sigma^2\!\left(1 + 2\sum_{t=1}^\infty \rho_t\right), \qquad S_{\text{eff}} = \frac{S}{1 + 2\sum_{t=1}^\infty \rho_t}.

When negative autocorrelations dominate (antithetic sampling), SeffS_{\text{eff}} can exceed SS. A Markov chain CLT requires stronger mixing—for example, geometric ergodicity together with Eπ∣g∣2+δ<∞E_\pi\lvert g\rvert^{2+\delta} < \infty.

0.8.1Working Example 5a: Exact Finite-SS Variance Inflation and ESS

For geometric autocorrelation ρt=ρt\rho_t = \rho^t (0<ρ<10 < \rho < 1), we sum the exact finite-SS variance inflation factor 1+2∑t=1S−1(1−t/S)ρt1 + 2\sum_{t=1}^{S-1}(1 - t/S)\rho^t, take its S→∞S \to \infty limit (1+ρ)/(1−ρ)(1+\rho)/(1-\rho), cross-check against the Toeplitz correlation matrix 1S𝟏T(ρ∣i−j∣)𝟏\frac{1}{S}\mathbf{1}^T(\rho^{\lvert i-j\rvert})\mathbf{1}, and tabulate (Seffexact,  Seff∞,  MCSE/σ)(S_{\text{eff}}^{\text{exact}},\; S_{\text{eff}}^{\infty},\; \mathrm{MCSE}/\sigma) at S=1000S = 1000—ending with the bivariate normal Gibbs chain of Chapter 6 where ρ=0.992=0.9801\rho = 0.99^2 = 0.9801 collapses 1,0001{,}000 draws to roughly 1010 effective draws:

Sum the exact finite-SS variance inflation factor for geometric autocorrelation ρt=ρt\rho_t = \rho^t.
// 5a. Exact finite-S variance inflation & ESS for geometric autocorrelation rho^t
const { r, t, ns } = W
const infl = await show`Assuming[0 < r < 1 && ns >= 1 && Element[ns, Integers],
  FullSimplify[1 + 2*Sum[(1 - ${t}/${ns})*${r}^${t}, {t, 1, ns - 1}]]]`
SVar⁡(g¯S)σ2=1+2∑t=1S−1(1−tS)ρt=1+2ρ(−1+S−Sρ+ρS)S(−1+ρ)2 \begin{aligned} &\frac{S\operatorname{Var}(\bar g_S)}{\sigma^2} = 1 + 2\sum_{t=1}^{S-1}\Bigl(1-\frac{t}{S}\Bigr)\rho^t \\ &= 1+\frac{2 \rho \left(-1+S -S \rho+\rho^{S}\right)}{S (-1+\rho)^2} \end{aligned} Take S→∞S \to \infty to recover the asymptotic autocorrelation time (1+ρ)/(1−ρ)(1 + \rho)/(1 - \rho).
const iact = await show`Assuming[0 < r < 1, Limit[${infl}, ns -> Infinity]]`
lim⁡S→∞SVar⁡(g¯S)σ2=1+ρ1−ρ \lim_{S\to\infty}\frac{S\operatorname{Var}(\bar g_S)}{\sigma^2} = \frac{1+\rho}{1-\rho} Cross-check the closed form against the explicit Toeplitz correlation matrix for S∈{2,3,7}S \in \{2, 3, 7\}.
await show`Table[Simplify[Total[Table[r^Abs[i - j], {i, k}, {j, k}], 2]/k - (${infl} /. ns -> k)], {k, {2, 3, 7}}]`
1S𝟏T(ρ∣i−j∣)𝟏−SVar⁡(g¯S)σ2∣S∈{2,3,7}={0,0,0} \begin{aligned} &\frac{1}{S}\mathbf{1}^T\bigl(\rho^{\lvert i-j\rvert}\bigr)\mathbf{1} - \frac{S\operatorname{Var}(\bar g_S)}{\sigma^2}\Big|_{S\in\{2,3,7\}} \\ &= \{0,0,0\} \end{aligned} Tabulate exact ESS, asymptotic ESS, and MCSE/σ\mathrm{MCSE}/\sigma at S=1000S = 1000 across four autocorrelations.
await show`Table[{ns/${infl}, ns/${iact}, Sqrt[${infl}/ns]} /. {ns -> 1000, r -> rv}, {rv, {0., 0.5, 0.9, 0.9801}}]`
(Seffexact,  Seff∞,  MCSE/σ)S=1000;  ρ∈{0, 0.5, 0.9, 0.9801}=(1000.1000.0.0316228333.778333.3330.054735753.13552.63160.13718610.576110.050.307494) \begin{aligned} &\bigl(S_{\text{eff}}^{\text{exact}},\; S_{\text{eff}}^{\infty},\; \mathrm{MCSE}/\sigma\bigr)_{S=1000;\; \rho\in\{0,\, 0.5,\, 0.9,\, 0.9801\}} \\ &= \left( \begin{array}{ccc} 1000. & 1000. & 0.0316228 \\ 333.778 & 333.333 & 0.0547357 \\ 53.135 & 52.6316 & 0.137186 \\ 10.5761 & 10.05 & 0.307494 \\ \end{array} \right) \end{aligned}

0.8.2Working Example 5b: A Three-State Metropolis Chain

Finally, consider a three-state target 𝝅=(π1,π2,π3)\boldsymbol{\pi} = (\pi_1, \pi_2, \pi_3) ordered so π1≥π2≥π3>0\pi_1 \ge \pi_2 \ge \pi_3 > 0, with uniform proposal to either of the two other states (probability 1/21/2 each) and Metropolis acceptance min⁡(1,πj/πi)\min(1, \pi_j/\pi_i). We construct the exact transition matrix KK, verify detailed balance and stationarity symbolically, compute its closed-form spectrum spec⁡(K)\operatorname{spec}(K), and evaluate the total-variation distance ∥Ks(3,⋅)−𝝅∥TV\lVert K^s(3, \cdot) - \boldsymbol{\pi}\rVert_{\text{TV}} at 𝝅=(1/2,3/10,1/5)\boldsymbol{\pi} = (1/2, 3/10, 1/5):

Build the exact 3×33 \times 3 Metropolis transition matrix KK for ordered target probabilities π1≥π2≥π3\pi_1 \ge \pi_2 \ge \pi_3.
// 5b. Three-state Metropolis chain: detailed balance, spectrum, and geometric TV convergence
const tgt = [W.p1, W.p2, W.p3]
const K = await show`Assuming[p1 >= p2 >= p3 > 0, Simplify[With[
  {off = Table[If[i == j, 0, Min[1, ${tgt}[[j]]/${tgt}[[i]]]/2], {i, 3}, {j, 3}]},
  off + DiagonalMatrix[1 - Total[off, {2}]]]]]`
K=(−−2π1+π2+π32π1π22π1π32π112π2−π32π2π32π212120) \begin{aligned} &K \\ &= \left( \begin{array}{ccc} -\frac{-2 \pi_1 +\pi_2 +\pi_3}{2 \pi_1} & \frac{\pi_2}{2 \pi_1} & \frac{\pi_3}{2 \pi_1} \\ \frac{1}{2} & \frac{\pi_2 -\pi_3}{2 \pi_2} & \frac{\pi_3}{2 \pi_2} \\ \frac{1}{2} & \frac{1}{2} & 0 \\ \end{array} \right) \end{aligned} Verify detailed balance diag⁡(𝝅)K=KTdiag⁡(𝝅)\operatorname{diag}(\boldsymbol\pi)K = K^T\operatorname{diag}(\boldsymbol\pi) and stationarity 𝝅TK=𝝅T\boldsymbol\pi^T K = \boldsymbol\pi^T.
await show`Simplify[{DiagonalMatrix[${tgt}] . ${K} - Transpose[DiagonalMatrix[${tgt}] . ${K}], ${tgt} . ${K} - ${tgt}}]`
(diag⁡(𝝅)K−KTdiag⁡(𝝅),  𝝅TK−𝝅T)={(000000000),{0,0,0}} \begin{aligned} &\bigl(\operatorname{diag}(\boldsymbol\pi)K - K^T\operatorname{diag}(\boldsymbol\pi),\; \boldsymbol\pi^T K - \boldsymbol\pi^T\bigr) \\ &= \left\{\left( \begin{array}{ccc} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{array} \right),\{0,0,0\}\right\} \end{aligned} Compute the exact eigenvalues of KK to identify the spectral gap governing convergence.
await show`Simplify[Eigenvalues[${K}]]`
spec⁡(K)={1,−−π1+π2+π32π1,−π32π2} \begin{aligned} &\operatorname{spec}(K) \\ &= \left\{1,-\frac{-\pi_1 +\pi_2 +\pi_3}{2 \pi_1},-\frac{\pi_3}{2 \pi_2}\right\} \end{aligned} Evaluate the total-variation error ∥Ks(3,⋅)−𝝅∥TV\lVert K^s(3, \cdot) - \boldsymbol\pi\rVert_{\text{TV}} at s∈{1,2,5,10}s \in \{1, 2, 5, 10\}.
await show`N[Table[Total[Abs[MatrixPower[${K} /. {p1 -> 1/2, p2 -> 3/10, p3 -> 1/5}, k][[3]] - {1/2, 3/10, 1/5}]]/2, {k, {1, 2, 5, 10}}]]`
∥Ks(3,⋅)−𝝅∥TV∣𝝅=(12,310,15);  s∈{1,2,5,10}={0.2,0.0666667,0.00246914,0.0000101611} \begin{aligned} &\lVert K^s(3,\cdot) - \boldsymbol\pi\rVert_{\text{TV}}\Big|_{\boldsymbol\pi=(\frac12,\frac3{10},\frac15);\; s\in\{1,2,5,10\}} \\ &= \{0.2,0.0666667,0.00246914,0.0000101611\} \end{aligned}

Notice that the four total-variation errors equal 15 3−(s−1)\frac{1}{5}\,3^{-(s-1)} exactly—shrinking at every step by the subdominant eigenvalue magnitude ∣λ3∣=π3/(2π2)=1/3\lvert\lambda_3\rvert = \pi_3/(2\pi_2) = 1/3, a concrete preview of the Metropolis–Hastings convergence machinery developed in Chapter 10.