S14 · Lesson 31 · Graphical models
Bayesian networks as factorizations, the parameters they save, conditional independence read from the graph, explaining away, Markov random fields and factor graphs, and exact inference on chains and trees by passing messages
A joint distribution over 20 binary variables, written as a table, needs over a million numbers, and summing it to answer one question takes as many operations. Most real systems are sparse, though: a sensor depends on the quantity it measures, not on every other sensor. A graphical model draws those dependencies as a graph, and the graph both cuts the number of parameters and tells you which sums can be done locally.
- A power fault (A) or a sensor fault (B) can each trigger an alarm (noisy-OR: 95% and 90%, with a 0.1% false-alarm leak). The faults are independent a priori.
- The solid curve is the probability of a power fault given the alarm, against its prior; the dashed curve adds that the sensor has been found faulty.
- Finding the sensor fault "explains away" the alarm, and the power fault's probability drops nearly back to its prior: two independent causes become dependent once their common effect is seen.
Predict
The power fault has prior 1% and the sensor fault 5%. The alarm sounds, and the power fault's probability rises to 17%. Then the sensor is found to be faulty. What is the power fault's probability now?
The sensor fault alone accounts for the alarm, so the alarm no longer argues for a power fault: P(A | alarm, sensor fault) = 1.1%. The faults were independent before anything was observed, but conditioning on their common effect couples them. That's explaining away, and it's why a graph's arrows matter for reading off independence.
Name it
Bayesian networks
A directed acyclic graph whose nodes are variables states that the joint distribution factorizes as
where are node k's parents. A binary node with m binary parents needs numbers, one probability per parent configuration, so a sparse graph needs far fewer than the of a full table. Naive Bayes (lesson 17) is a class node with every feature as a child; a hidden Markov model (lesson 42) is a chain of hidden states, each with an observed child.
Reading independence from the graph
Three junctions decide everything. In a chain a → c → b and a fork a ← c → b, a and b are dependent, but independent given c. In a collider a → c ← b, a and b are independent, but become dependent once c, or any descendant of c, is observed: explaining away. d-separation applies these rules along every path: two sets of variables are conditionally independent if every path between them is blocked. A node's Markov blanket, its parents, children and children's other parents, makes it independent of everything else.
Markov random fields and factor graphs
An undirected graph writes the joint as a product of nonnegative potentials over its cliques, divided by a normalizing constant Z: natural for pixels in an image or spins in a magnet, where no direction is meaningful. A factor graph draws variables and factors as two kinds of node, covering both forms.
Passing messages
On a tree-shaped graph, marginals come from passing messages: each node sums out its own variable and passes the result to its neighbor. On a chain of N variables with K states each, every message is a K × K matrix–vector product, so all marginals cost about operations instead of the terms of the full sum. This is the forward–backward algorithm of hidden Markov models, and replacing sums by maxima gives the Viterbi algorithm. On graphs with loops the same updates, iterated, give loopy belief propagation, inexact in general but the basis of decoding LDPC and turbo codes.
Worked example
Ten binary variables form a chain, each depending only on the one before. How many numbers specify the joint distribution, compared with a full table? And five binary sensors that each depend only on a hidden three-valued state?
Show the solution
- The chain. 1 for the first variable and 2 for each of the other nine (one per value of its parent): against = 1023 for a table.
- The sensor model. 2 for the hidden state's distribution and 3 for each sensor (one per state): against = 95 for the full joint.
- Growth. The gap grows exponentially with the number of variables.
Answer the chain needs 19 numbers against 1023 for a table, and the sensor model 17 against 95.
Worked example
In the sim's alarm model with the default priors (1% and 5%), find the probability of each fault given the alarm.
Show the solution
- Alarm with a power fault.
- Alarm without one.
- Power fault given the alarm. P(alarm) = 0.0550, so
- Sensor fault given the alarm. The same calculation gives 82%: the alarm most likely comes from the more common fault.
- Explaining away. Finding the sensor faulty then drops the power fault to 1.1%, as the predict question showed.
Answer given the alarm, the power fault has probability 17% and the sensor fault 82%.
Worked example
A chain of 20 variables with 10 states each. How many terms does the full sum for one marginal have, and how many operations do messages take for all marginals?
Show the solution
- The full sum. It has terms (strictly for one marginal, still hopeless).
- Messages. They pass in both directions along the 19 links, each a 10 × 10 matrix–vector product of about 100 multiply-adds: operations for every marginal at once.
- What the structure buys. The graph's structure turns an exponential sum into a linear one.
Answer the full sum has terms, while messages give every marginal in about 3800 operations.
Check
12 binary variables form a Bayesian network in which every variable but the first 1 has exactly 1 binary parents (the first 1 have none).
(a)
How many numbers specify it?
(b)
How many does a full table need?
Check
A chain of 20 variables with 2 states each.
(a)
About how many multiply-adds do forward and backward messages take to give every marginal?
(b)
How many digits long is the number of terms in the full sum (log₁₀ of )?
Check
Two independent causes A and B (priors 0.01 and 0.02) each set off an effect E, with probabilities 0.99 and 0.95; nothing else does (deterministic OR otherwise, no leak).
(a)
What is P(A | E)?
(b)
What is P(A | E, B)?
Check
In the graph rain → wet grass ← sprinkler, which statement is true?
Check
Which variables make up a node's Markov blanket in a Bayesian network?
Check
A hidden state x₁ is 1 with probability 0.1 and persists to x₂ with probability 0.7 (and from 0 to 1 with probability 1 − 0.7). A sensor on x₂ reads 1 with probability 0.9 when x₂ = 1 and 0.2 when x₂ = 0; it reads 1.
(a)
What is the message from x₁ to x₂, the prior P(x₂ = 1)?
(b)
What is P(x₂ = 1 | reading)?
Check
Causes A and B with priors a and b independently set off E with probabilities q and r; nothing else does.
Step 1
What is P(E)?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is P(A | E)?
Step 3
What is P(A | E, B)?
Check
Write chain_posteriors(p1, T, L) for a chain of binary states: P(x₁ = 1) = p1, T[i][j] = P(next = j | now = i), and L[t] = (P(observation t | x = 0), P(observation t | x = 1)). Return P(x_t = 1 | all observations) for every t, by forward and backward messages.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why do two independent causes become dependent once their common effect is observed?
Before anything is observed, knowing one cause tells you nothing about the other, because they arise separately. Observing the effect says that at least one cause is probably present, which raises both their probabilities. If you then learn that one of them is present, it already accounts for the effect, so the evidence for the other falls back toward its prior; if you learn the first is absent, the second must explain the effect and its probability rises. Either way, information about one cause now changes your belief about the other, so they are dependent given the effect, the collider rule that d-separation encodes.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.