S1431 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

Name it

Bayesian networks

A directed acyclic graph whose nodes are variables states that the joint distribution factorizes as

p(x1,…,xK)=∏kp(xk∣pak)

where pak are node k's parents. A binary node with m binary parents needs 2m numbers, one probability per parent configuration, so a sparse graph needs far fewer than the 2K−1 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 2(N−1)K2 operations instead of the KN 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
  1. The chain. 1 for the first variable and 2 for each of the other nine (one per value of its parent): 1+9×2=19 against 210−1 = 1023 for a table.
  2. The sensor model. 2 for the hidden state's distribution and 3 for each sensor (one per state): 2+5×3=17 against 3×25−1 = 95 for the full joint.
  3. 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
  1. Alarm with a power fault. P(alarm∣A=1)=1−0.999×0.05×(1−0.05×0.9)=0.952
  2. Alarm without one. P(alarm∣A=0)=1−0.999×(1−0.045)=0.046
  3. Power fault given the alarm. P(alarm) = 0.0550, so P(A∣alarm)=0.01×0.9520.0550=17%
  4. Sensor fault given the alarm. The same calculation gives 82%: the alarm most likely comes from the more common fault.
  5. 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
  1. The full sum. It has 1020 terms (strictly 1019 for one marginal, still hopeless).
  2. Messages. They pass in both directions along the 19 links, each a 10 × 10 matrix–vector product of about 100 multiply-adds: 2×19×100=3800 operations for every marginal at once.
  3. What the structure buys. The graph's structure turns an exponential sum into a linear one.

Answer the full sum has 1020 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 KN)?

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°.

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?

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.