Machine learningLoading progress…

Graphical models

Update a prior, compare posterior means with MAP estimates and interpret predictive uncertainty.

A graph is a factorisation

A joint distribution over many variables is expensive to write down: n yes/no variables need 2^n - 1 numbers. A graphical model uses arrows to say which variables directly depend on which, and the joint becomes a product of small tables. Take Rain (R), Sprinkler (S) and Wet grass (W), where rain and the sprinkler can each wet the grass:

P(R, S, W) = P(R) * P(S) * P(W | R, S)
parameters: 1 + 1 + 4 = 6 numbers instead of 2^3 - 1 = 7
for 20 variables with at most 3 parents each, the product needs at most 20 * 2^3 = 160 numbers; the full table needs 1,048,575

The missing arrow between R and S states an assumption: they are independent unless you condition on something they both influence.

Inference by enumeration

To answer "how likely is rain, given wet grass?" list every world (8 here), compute its probability from the product, keep the worlds that agree with the evidence, and divide. Set evidence in the lab and watch which rows light up.

RainSprinklerWet grass
Rain
Sprinkler on
Grass wet

P(Rain) = 0.2, P(Sprinkler) = 0.3, and P(Wet | Rain, Sprinkler) = 0.99 / 0.9 / 0.85 / 0 for (yes,yes) / (yes,no) / (no,yes) / (no,no).

queryexact P(true | evidence)
Rain0.4761
Sprinkler on0.6764
RSWP(R) P(S) P(W|R,S)kept
0000.56no
0010yes
0100.036no
0110.204yes
1000.014no
1010.126yes
1100.0006no
1110.0594yes
sum of kept rows0.3894

Enumeration: keep the 8 worlds that agree with the evidence, add up the weight of the ones where the query is true, and divide by the total weight kept.

Set each variable to unknown, true or false. The table is the complete joint distribution; the answers are exact sums over the kept rows.

Explaining away

Before any observation rain has probability 0.2. If you learn the grass is wet it jumps to 0.4761: wet grass makes rain a likelier explanation. Now learn the sprinkler was on. The grass is already explained, so rain falls back to 0.2255. Rain and the sprinkler were independent, but become dependent once you condition on their common effect. That is the signature of a collider, and it is easy to get wrong by intuition.

Predict first. You see wet grass and learn the sprinkler was off. What happens to P(rain)?

Arrows are not causes

The same joint distribution can be written with the arrows reversed. Below, A to B to C and C to B to A produce identical numbers in all 8 rows, so no amount of observational data can tell you which direction is causal. Arrows in a probabilistic graph encode dependence structure; reading them as causation needs extra assumptions or experiments (intervening on A and watching C).

A -> B -> C: P(A=1) = 0.3, P(B=1|A) = 0.2 / 0.8, P(C=1|B) = 0.1 / 0.7 C -> B -> A: P(C=1) = 0.328, P(B=1|C) = 0.1696 / 0.811, P(A=1|B) = 0.0968 / 0.6316
ABCarrows A to Carrows C to A
0000.5040.504
0010.0560.056
0100.0420.042
0110.0980.098
1000.0540.054
1010.0060.006
1100.0720.072
1110.1680.168
Two factorisations, the same joint distribution.
Chapter context

Probabilistic modeling represents uncertainty explicitly instead of presenting every fitted number as certain. Priors, likelihoods and approximation choices are assumptions that should be exposed rather than hidden behind a library call.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

For Bernoulli success probability theta and a Beta prior:

p(θ∣D)=Beta⁡(a+s,b+f),E[θ∣D]=a+sa+b+s+f.p(\theta\mid D)=\operatorname{Beta}(a+s,b+f),\quad E[\theta\mid D]=\frac{a+s}{a+b+s+f}.

For Beta(alpha,beta) with both parameters greater than one, the MAP is (α−1)/(α+β−2)(\alpha-1)/(\alpha+\beta-2).

For a zero-mean Gaussian-process regression model with independent noise variance sigma-n squared, a test prediction has

μ∗=k∗T(K+σn2I)−1y,\mu_*=k_*^T(K+\sigma_n^2I)^{-1}y, v∗=k∗∗−k∗T(K+σn2I)−1k∗.v_*=k_{**}-k_*^T(K+\sigma_n^2I)^{-1}k_*.

K is the training covariance matrix, k-star connects the test input with training inputs, and k-star-star is its prior variance. This is latent-function variance; observation noise adds another term for a noisy predicted observation. Practical code solves systems instead of explicitly computing an inverse.

Worked example

Start with Beta(2,2), then observe three successes and one failure. The posterior is Beta(5,3). Its mean is 5/8=0.625. Its MAP is 4/6≈0.6667. Both summarize the same distribution but answer different questions.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import isclose
a, b = 2+3, 2+1
assert (a,b) == (5,3)
assert a/(a+b) == .625
assert isclose((a-1)/(a+b-2), 2/3)

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

A posterior estimate is conditional on the chosen model. MAP is not the same as a posterior mean. A small uncertainty under a misspecified model need not mean a reliable real-world answer.