RL Bible

RL Bible · Appendix B

Math Refresher

Probability, expectation, gradients, optimization, and information theory as used in this book.

This appendix collects the mathematics the book uses, at the depth it uses it. It is a refresher, not a course: each section states the facts, the notation, and where in the book they carry load. If any section reads as new rather than half-remembered, the linked references are the efficient repair.

1. Probability

Random variables and distributions. A random variable XX takes values by chance; its distribution assigns probabilities. We write XpX \sim p ("distributed as"), Pr(X=x)\Pr(X = x) or p(x)p(x) for probability (mass or density), and use capital letters for random variables, lowercase for realized values — the convention that lets E[GtSt=s]\E[G_t \mid S_t = s] parse correctly everywhere in this book.

Joint, marginal, conditional. p(x,y)p(x, y) is the joint; the marginal integrates the other variable out, p(x)=yp(x,y)p(x) = \sum_y p(x, y); the conditional is p(yx)=p(x,y)/p(x)p(y \mid x) = p(x, y)/p(x). The chain rule p(x1,,xn)=ip(xix1:i1)p(x_1, \dots, x_n) = \prod_i p(x_i \mid x_{1:i-1}) is used every time the book factorizes a trajectory probability (Chapters 3, 11): p(τ)=μ0(s0)tπ(atst)p(st+1st,at)p(\tau) = \mu_0(s_0) \prod_t \pi(a_t \mid s_t)\, p(s_{t+1} \mid s_t, a_t).

Independence and conditional independence. XYX \perp Y iff p(x,y)=p(x)p(y)p(x,y) = p(x)p(y). Conditional independence XYZX \perp Y \mid Z — "given Z, Y adds nothing about X" — is the Markov property (Chapter 3): the future is conditionally independent of the past given the present. Nearly every derivation that "drops the history" (Bellman equations, Chapter 5's importance-sampling cancellation) is an application.

Bayes' rule. p(zx)=p(xz)p(z)p(x)p(z \mid x) = \frac{p(x \mid z)\, p(z)}{p(x)} — posterior ∝ likelihood × prior. Load-bearing in: Thompson sampling (Chapter 2), POMDP belief updates (Chapter 3), and the variational posterior/prior structure of world models (Chapter 23).

2. Expectation, Variance, and the Tools of Estimation

Expectation is the probability-weighted average, E[X]=xxp(x)\E[X] = \sum_x x\, p(x), and is linear unconditionally: E[aX+bY]=aE[X]+bE[Y]\E[aX + bY] = a\E[X] + b\E[Y] whether or not X, Y are independent — the fact that makes returns decompose (Gt=Rt+1+γGt+1G_t = R_{t+1} + \gamma G_{t+1} ⟹ Bellman equations) and baselines subtract cleanly (Chapter 11).

The tower rule (law of total expectation). E[X]=E[E[XY]]\E[X] = \E\left[ \E[X \mid Y] \right]. The book's most-used invisible tool: it converts "average over trajectories" into "average over states of the average over what follows" — the step inside the Bellman derivation (Chapter 3), the regret decomposition (Chapter 2, Exercise 2.2), and the unbiasedness proofs of Chapters 5 and 11.

Variance measures spread: Var[X]=E[X2]E[X]2\mathrm{Var}[X] = \E[X^2] - \E[X]^2. For independent variables variances add — which is why an nn-step return's variance grows with nn (each reward contributes; Chapter 7) and why averaging nn i.i.d. samples shrinks standard error like 1/n1/\sqrt{n} (Monte Carlo, Chapter 5).

Estimators. An estimator θ^\hat\theta of θ from samples is unbiased if E[θ^]=θ\E[\hat\theta] = \theta and consistent if θ^θ\hat\theta \to \theta with more data. The book's recurring trade: unbiased/high-variance (Monte Carlo returns, ordinary importance sampling, REINFORCE) versus biased/low-variance (TD targets, weighted importance sampling, critics). Bias–variance decomposition: mean squared error = bias² + variance — the accounting behind every "n-step / λ" dial (Chapter 7) and GAE (Chapter 11).

Concentration. Hoeffding's inequality: for i.i.d. bounded samples, Pr(Xˉnμu)2e2nu2/(ba)2\Pr\left( \lvert \bar X_n - \mu \rvert \ge u \right) \le 2 e^{-2 n u^2 / (b-a)^2} — deviations decay exponentially. It is the engine of every confidence bound in the book: UCB (Chapter 2), UCBVI's confidence sets (Chapter 21). The union bound, Pr(iAi)iPr(Ai)\Pr(\cup_i A_i) \le \sum_i \Pr(A_i), is how per-arm/per-state bounds become uniform ones (and why log factors appear everywhere).

Jensen's inequality. For convex ff: E[f(X)]f(E[X])\E[f(X)] \ge f(\E[X]). With f=maxf = \max: E[maxiXi]maxiE[Xi]\E[\max_i X_i] \ge \max_i \E[X_i] — maximization bias, the single inequality responsible for Double Q-learning, Double DQN, and TD3's twin critics (Chapters 6, 10, 13).

Importance sampling. To estimate Exp[f(x)]\E_{x \sim p}[f(x)] using samples from qq: Ep[f]=Eq ⁣[p(x)q(x)f(x)]\E_p[f] = \E_q\!\left[ \frac{p(x)}{q(x)} f(x) \right], valid wherever p>0q>0p > 0 \Rightarrow q > 0 (coverage). Unbiased; variance governed by the ratio's spread — the mathematics of all off-policy corrections (Chapters 5, 7, 12) and their failure modes (offline RL, Chapter 17).

3. The Gaussian, and Sampling Tricks

The Gaussian N(μ,σ2)\mathcal{N}(\mu, \sigma^2) has density 12πσe(xμ)2/2σ2\frac{1}{\sqrt{2\pi}\sigma} e^{-(x-\mu)^2 / 2\sigma^2}; its log-density (xμ)22σ2logσ12log2π-\frac{(x-\mu)^2}{2\sigma^2} - \log\sigma - \frac{1}{2}\log 2\pi appears verbatim in every Gaussian-policy log-prob computation (Chapters 11, 13). Products stay Gaussian; sums of independent Gaussians are Gaussian; conditionals and marginals of joint Gaussians are Gaussian — the closure properties PILCO's moment matching exploits (Chapter 14).

Change of variables. If y=g(x)y = g(x) with invertible g, densities transform with the Jacobian: pY(y)=pX(g1(y))detg1yp_Y(y) = p_X(g^{-1}(y)) \left| \det \frac{\partial g^{-1}}{\partial y} \right|. This is the tanh-correction in SAC's squashed Gaussian (Chapter 13) — forget it and entropies come out wrong.

Reparameterization. Sampling xN(μθ,σθ2)x \sim \mathcal{N}(\mu_\theta, \sigma_\theta^2) blocks gradients; writing x=μθ+σθξx = \mu_\theta + \sigma_\theta \xi, ξN(0,1)\xi \sim \mathcal{N}(0,1) restores them: θE[f(x)]=Eξ[θf(μθ+σθξ)]\nabla_\theta \E[f(x)] = \E_\xi[\nabla_\theta f(\mu_\theta + \sigma_\theta \xi)]. One of the book's two fundamental gradient estimators (Chapter 13; world models, Chapter 23) — the other being the score function identity θEpθ[f]=Epθ[fθlogpθ]\nabla_\theta \E_{p_\theta}[f] = \E_{p_\theta}[f\, \nabla_\theta \log p_\theta], derived from p=plogp\nabla p = p \nabla \log p (Chapter 11).

4. Linear Algebra and Calculus, as Used

Vectors and norms. x2=xi2\|x\|_2 = \sqrt{\sum x_i^2}; x=maxixi\|x\|_\infty = \max_i |x_i| (the sup-norm of Chapter 4's contraction proofs); x1=xi\|x\|_1 = \sum |x_i| (total-variation-flavored bounds, Chapter 21). Inner product xyx^\top y; vectors are columns.

Matrices as operators. A stochastic matrix PP (rows sum to 1) maps value vectors to expected-next values; its spectral radius is 1, so γP\gamma P contracts and (IγP)1=k(γP)k(I - \gamma P)^{-1} = \sum_k (\gamma P)^k converges (the Neumann series = "sum over all discounted paths" — Chapter 3). Positive definiteness (xAx>0x^\top A x > 0) is what makes expected TD updates stable (Chapter 9) and the Fisher matrix a metric (Chapter 12).

Gradients. θf\nabla_\theta f points uphill; the chain rule composes through networks (backprop) and through sampled computation graphs (reparameterization). Two named gradients recur: the policy gradient E[logπA]\E[\nabla \log \pi \cdot A] and the pathwise/deterministic gradient E[aQθμ]\E[\nabla_a Q\, \nabla_\theta \mu] — Chapters 11 and 13; know both derivations cold.

Taylor expansion. f(θ+δ)f+fδ+12δHδf(\theta + \delta) \approx f + \nabla f^\top \delta + \frac{1}{2} \delta^\top H \delta — the tool behind trust regions: TRPO expands the objective to first order and the KL constraint to second (whose Hessian is the Fisher matrix — Chapter 12, Exercise 12.4).

Fixed points and contractions. TT is a γ-contraction under norm \|\cdot\| if TxTyγxy\|Tx - Ty\| \le \gamma \|x - y\|; Banach's theorem gives a unique fixed point and geometric convergence of iteration. Proved and used in Chapter 4; echoed for soft Bellman operators (Chapter 13) and distributional operators (Chapter 10).

5. Optimization

Gradient descent/ascent with step size α; stochastic versions replace the gradient with an unbiased sample. Convergence for decaying steps under the Robbins–Monro conditions αt=\sum \alpha_t = \infty, αt2<\sum \alpha_t^2 < \infty (Chapters 2, 6, 21); constant steps track nonstationary targets instead (everywhere in deep RL). Momentum/Adam: adaptive per-parameter step sizes; the book's code uses Adam throughout without ceremony.

Constrained optimization and Lagrange multipliers. To maximize ff subject to gcg \le c: form L=fλ(gc)L = f - \lambda(g - c), optimize over the primal, adjust λ by dual ascent (raise λ while the constraint is violated). Used for: TRPO's trust region (Chapter 12), SAC's automatic temperature (Chapter 13), and the max-ent derivations (Chapters 13, 16, 20 — where the Boltzmann distribution pef/λp \propto e^{f/\lambda} appears as the closed-form solution of entropy-regularized objectives; deriving that once, via calculus of variations on the simplex, pays off four separate times).

Convexity (one definition, three uses): ff convex iff chords lie above the graph. Uses: Jensen (Section 2), the log-sum-exp softmax bound (CQL, Chapter 17), and convex conjugates ψ\psi^* (GAIL's occupancy duality, Chapter 16 — used at the "trust the statement" level).

6. Information Theory

Entropy H(p)=xp(x)logp(x)\mathcal{H}(p) = -\sum_x p(x) \log p(x): expected surprise; maximized by uniform. The exploration bonus of Chapter 11, the objective term of max-ent RL (Chapter 13), and the tie-breaking principle of max-ent IRL (Chapter 16).

KL divergence DKL(pq)=xp(x)logp(x)q(x)\KL(p \| q) = \sum_x p(x) \log \frac{p(x)}{q(x)}: expected log evidence for p over q; nonnegative (Gibbs' inequality), zero iff p=qp = q, asymmetricDKL(pq)\KL(p\|q) punishes q lacking mass where p has it (mode-covering), DKL(qp)\KL(q\|p) punishes q putting mass where p lacks it (mode-seeking); which direction appears in a loss is never an accident (RSSM's two-directional balance, Chapter 23; the RLHF leash, Chapter 20). Its local quadratic expansion DKL(pθpθ+δ)12δFδ\KL(p_\theta \| p_{\theta + \delta}) \approx \frac{1}{2}\delta^\top F \delta defines the Fisher information matrix F=E[logplogp]F = \E[\nabla \log p\, \nabla \log p^\top] — the metric of natural gradients (Chapter 12).

Mutual information I(X;Y)=DKL(p(x,y)p(x)p(y))I(X; Y) = \KL\left( p(x,y) \,\|\, p(x)p(y) \right): shared information; the formal objective behind information-gain exploration (Chapter 15) and latent-action discovery (Genie, Chapter 23).

Cross-entropy Ep[logq]-\E_p[\log q] = entropy + KL: minimizing it in q fits q to p — every classification loss, C51's projection loss (Chapter 10), and behavior cloning's objective (Chapter 16) in one identity.

7. Where to Repair Gaps

  • Probability & estimation: Wasserman, All of Statistics — chapters 1–5 cover everything above at the right level. link.springer.com/book/10.1007/978-0-387-21736-9
  • Linear algebra: Strang's lectures, or Axler for the operator view that makes Chapter 4 natural.
  • Optimization: Boyd & Vandenberghe, Convex Optimization, chapters 1–5 — free at stanford.edu/~boyd/cvxbook. Lagrange duality is the one section this book truly leans on.
  • Information theory: Cover & Thomas, Elements of Information Theory, chapters 1–2; or MacKay's free Information Theory, Inference, and Learning Algorithmsinference.org.uk/mackay/itila.
  • Stochastic approximation (optional depth): Borkar, Stochastic Approximation: A Dynamical Systems Viewpoint — the machinery behind Chapters 6 and 21's convergence claims.