§3.10–3.13Useful Functions · Bayes' Rule · Continuous Details · Information Theory

Part I DL pp. 67–75 · ~7 min read

  • bayes rule
  • measure zero
  • self-information
  • shannon entropy
  • kullback-leibler divergence
  • cross-entropy

§3.10 Useful properties of common functions

Two functions recur constantly when building probability distributions. The logistic sigmoid σ(x)=1/(1+ex)\sigma(x) = 1/(1 + e^{-x}) has range (0,1)(0, 1) — exactly a Bernoulli ϕ\phi — but saturates at both extremes (flat, insensitive to small input changes). The softplus ζ(x)=log(1+ex)\zeta(x) = \log(1 + e^x) has range (0,)(0, \infty) — a valid σ\sigma or β\beta for a Gaussian — and is a smoothed (“softened”) version of the positive part x+=max(0,x)x^+ = \max(0, x):

Figs 3.3–3.4 (interactive) — drag the probe: note ζ(x) hugging max(0,x), and σ saturating
0.05.010.0-10010
σ(x)= 0.731ζ(x)= 1.313max(0, x)= 1.000
x1.00

The identities the book suggests memorizing (eqs 3.33–3.41) — click a row for why it matters:

The σ/ζ toolkit — click rows
IdentityReading
3.34σ'(x) = σ(x)(1−σ(x))self-referential derivative
3.351 − σ(x) = σ(−x)complement symmetry
3.36log σ(x) = −ζ(−x)log-sigmoid IS softplus
3.37ζ'(x) = σ(x)σ is the slope of ζ
3.38σ⁻¹(x) = log(x/(1−x))the logit
3.41ζ(x) − ζ(−x) = xsoft positive/negative parts
Dotted-underlined cells have explanations — click one.

§3.11 Bayes’ rule

Knowing P(yx)P(\mathrm{y}|\mathrm{x}) but needing P(xy)P(\mathrm{x}|\mathrm{y}) happens constantly; with the prior P(x)P(\mathrm{x}), Bayes’ rule inverts the conditional:

Term by term

P(xy)=P(x)P(yx)P(y)P(\mathrm{x} \mid \mathrm{y}) = \frac{P(\mathrm{x})\, P(\mathrm{y} \mid \mathrm{x})}{P(\mathrm{y})}
P(x)P(\mathrm{x})the prior — belief before the observationknown
P(yx)P(\mathrm{y} \mid \mathrm{x})the likelihood — how the observation behaves under each xknown
P(y)P(\mathrm{y})usually not given, but computable: Σₓ P(y|x)P(x) — no extra knowledge needednormalizer
P(xy)P(\mathrm{x} \mid \mathrm{y})the posterior — belief after the observationwanted

(It follows directly from the conditional definition; Bayes found a special case, Laplace the general form.)

Worked example — the classic diagnostic surprise

Disease prevalence P(d)=0.01P(d) = 0.01; test sensitivity P(+d)=0.9P(+|d) = 0.9; false-positive rate P(+¬d)=0.09P(+|\neg d) = 0.09. Then P(+)=0.90.01+0.090.99=0.098P(+) = 0.9·0.01 + 0.09·0.99 = 0.098, so P(d+)=0.010.90.0980.092P(d\,|\,+) = \frac{0.01 · 0.9}{0.098} \approx 0.092 — a positive test means only a 9 % chance of disease, because the prior is so small. Intuition fails here; the rule does not.

§3.12 Technical details of continuous variables

Rigorous continuous probability needs measure theory; the book keeps just three take-aways. (1) Pathological sets (fractal-like, built from the infinite precision of the reals) can break naive integration — measure theory characterizes the safe sets, and everything integrated in this book is safe. (2) A set of measure zero occupies no volume (points, lines in R2\mathbb{R}^2, countable unions of such); a property holding almost everywhere holds except on such a set — good enough for most purposes. (3) The change-of-variables trap: for y=g(x)y = g(x) with invertible smooth gg, it is tempting but wrong to write py(y)=px(g1(y))p_y(y) = p_x(g^{-1}(y)).

Aside — the bug, with numbers

Let xU(0,1)\mathrm{x} \sim U(0, 1) and y=x/2y = x/2… the book uses y=x2y = \tfrac{x}{2}‘s cousin: suppose py(y)=px(2y)p_y(y) = p_x(2y). Then pyp_y would be 1 on [0,12][0, \tfrac12] and 0 elsewhere — integrating to 12\tfrac12, not 1. The missing ingredient: gg distorts space, so density must be corrected by how much an infinitesimal interval stretches.

Preserving py(g(x))dy=px(x)dx|p_y(g(x))\,dy| = |p_x(x)\,dx| gives the correct rule — in higher dimensions the stretch factor is §2.11’s determinant, applied to the Jacobian matrix Ji,j=xi/yjJ_{i,j} = \partial x_i / \partial y_j:

Term by term

px(x)=py(g(x))det ⁣(g(x)x)p_x(\boldsymbol{x}) = p_y(g(\boldsymbol{x}))\, \left| \det\!\left( \frac{\partial g(\boldsymbol{x})}{\partial \boldsymbol{x}} \right) \right|
ggthe invertible, continuous, differentiable transformation between the variablesℝⁿ → ℝⁿ
g/x\partial g / \partial \boldsymbol{x}the Jacobian — the local linear map of the transformationn×n
det()|\det(\cdot)|how much g locally expands or contracts volume (§2.11) — the density correctionscalar ≥ 0

§3.13 Information theory

Born for optimal codes over noisy channels, information theory gives ML its tools for characterizing distributions and comparing them. The seed intuition: learning that an unlikely event occurred is more informative (“solar eclipse this morning” ≫ “the sun rose”). Three requirements — certain events carry zero information, less likely means more information, independent events add — force one definition:

Term by term

I(x)=logP(x)I(x) = -\log P(x)
I(x)I(x)the self-information of observing xnats (log base e; bits if base 2)
log-\logthe only shape satisfying all three requirements: P=1 → 0 info; smaller P → more; products → sumsmonotone ↓

Self-information covers one outcome; the Shannon entropy is its expectation — the uncertainty of the whole distribution, and the lower bound on average code length for symbols drawn from it: H(x)=ExP[I(x)]=E[logP(x)]H(\mathrm{x}) = \mathbb{E}_{\mathrm{x} \sim P}[I(x)] = -\mathbb{E}[\log P(x)] (continuous case: differential entropy; convention 0log0=00 \log 0 = 0).

Fig 3.5 (interactive) — entropy of a Bernoulli: deterministic ends are certain (H=0), the uniform middle is maximally uncertain
0.00.40.8011p = P(x = 1)
H(p) = −(1−p)log(1−p) − p log p= 0.693
p = P(x = 1)0.50

To compare two distributions over the same variable, the Kullback-Leibler divergence :

Term by term

DKL(PQ)=ExP[logP(x)Q(x)]D_{\mathrm{KL}}(P \| Q) = \mathbb{E}_{\mathrm{x} \sim P}\left[ \log \frac{P(x)}{Q(x)} \right]
PPthe distribution reality samples from — expectations are taken under Preference
QQthe distribution your code (or model) was designed forapproximation
DKLD_{\mathrm{KL}}the EXTRA message length paid for using Q’s code on P’s data; ≥ 0, zero iff P = Q (a.e.) — but NOT symmetric, so not a distancenats ≥ 0

The asymmetry has real consequences (fig 3.6): approximating a bimodal pp with a single Gaussian qq,

q* = argmin D(p‖q): q COVERS bothmodes (blurs them together)q* = argmin D(q‖p): q PICKS one mode(avoids p’s valleys)— p (bimodal truth)- - q* (single Gaussian)

Fig 3.6 (recreated) — which direction you minimize decides whether q blurs the modes together or commits to one. The choice is problem-dependent; ch. 19 lives inside this trade-off.

Finally, the cross-entropy H(P,Q)=H(P)+DKL(PQ)=ExPlogQ(x)H(P, Q) = H(P) + D_{\mathrm{KL}}(P \| Q) = -\mathbb{E}_{\mathrm{x} \sim P} \log Q(x): minimizing it over QQ is the same as minimizing the KL (the omitted H(P)H(P) doesn’t involve QQ) — which is why “cross-entropy loss” and “KL minimization” name one procedure in ch. 5–6.

Check yourself — functions, Bayes, information

1.Why does the sigmoid 'saturate', and which identity quantifies it?

2.Disease prevalence 1%, sensitivity 90%, false-positive rate 9%. Your test is positive. Roughly what is P(disease | +), and why so low?

3.Why is p_y(y) = p_x(g⁻¹(y)) wrong for a change of variables y = g(x)?

4.In the entropy widget, where does H(p) peak and what does that mean?

5.You must approximate a well-separated bimodal p with a single Gaussian q. What does the direction of KL minimization decide?

5 questions