Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 24: Inference in Depth

Open in Colab

Probability theory is nothing but common sense reduced to calculation.

Pierre-Simon Laplace (1819)

24.1 The Inference Problem

Inference answers questions such as P(lung cancer∣positive X-ray, dyspnoea)P(\text{lung cancer} \mid \text{positive X-ray, dyspnoea}): the probability of query variables given evidence, with every other variable summed out. Chapter 23 did it by building the whole joint table, 2n2^n rows: fine for 8 variables, hopeless for 50 (1015 rows). This chapter covers the two ways out: exact inference that never builds the joint, and approximate inference by sampling.

We use the Asia network of Chapter 23.

P(lung cancer | X-ray, dyspnoea) = 0.6213 (by enumerating 256 rows)

24.2 Factors

A factor is a table of non-negative numbers over some variables: a CPT is a factor over a variable and its parents; evidence makes smaller factors. Three operations are enough for exact inference:

  • product ϕ1⋅ϕ2\phi_1 \cdot \phi_2: a factor over the union of their variables, multiplying matching entries;

  • sum-out ∑Xϕ\sum_X \phi: remove a variable by adding over its values;

  • reduction ϕ[X=x]\phi[X = x]: keep only the entries consistent with evidence.

With NumPy, a factor is an array with one axis per variable, and np.einsum does products and sums in one call.

φ(T, L, E) · φ(S, L) has variables ['T', 'L', 'E', 'S'] and shape (2, 2, 2, 2)

24.3 Variable Elimination

Instead of building the joint and summing it, push the sums inside the product. For each variable to eliminate, in some order: multiply only the factors that mention it, sum it out, and put the result back. At the end, multiply what is left and normalise.

P(lung cancer | X-ray, dyspnoea) = 0.6213 by variable elimination; largest factor 8 entries

The same answer as enumeration, 0.6213, and the largest table built has 8 entries instead of 256. A patient with a positive X-ray and dyspnoea has a 62% chance of lung cancer.

The order decides the cost. Eliminating a variable creates a factor over all its current neighbours; a bad order creates huge factors. On a small network it barely matters, so we build a larger one: 40 binary variables, each with up to 3 parents among earlier ones, random tables. Enumeration would need 240≈10122^{40} \approx 10^{12} rows.

min-degree           largest factor          256 entries         1 ms   P(v39 = 1 | evidence) = 0.524537
topological          largest factor    4,194,304 entries       450 ms   P(v39 = 1 | evidence) = 0.524537
reverse topological  largest factor        2,048 entries         2 ms   P(v39 = 1 | evidence) = 0.524537

All three orders give the same answer, as they must. But eliminating in topological order builds a factor of more than 4 million entries and takes a third of a second; the min-degree heuristic never builds one larger than 256 and takes a millisecond. On bigger networks the gap becomes the difference between seconds and never. Finding the best order is itself NP-hard, so greedy heuristics (min-degree, min-fill) are used.

The size of the largest factor of the best order is governed by the graph’s treewidth. Networks shaped like trees (and polytrees: no undirected cycles) have treewidth 1 and are solved in linear time by belief propagation, which passes messages along edges; general networks are turned into a tree of clusters (the junction tree algorithm), and exact inference is exponential in their treewidth. Exact inference is NP-hard in general (Cooper, 1990): for large, densely connected networks we must approximate.

24.4 Sampling

Draw many complete assignments from the network, then count. Three methods, on a query whose evidence is rare: the probability of tuberculosis for a patient who visited Asia, has a positive X-ray and dyspnoea.

exact P(T = 1 | A, X, D) = 0.3917;  the evidence itself has probability 0.00099

Rejection sampling throws away every sample that disagrees with the evidence: here almost all of them. Likelihood weighting never wastes a sample: it fixes the evidence and gives each sample a weight, the probability of the evidence given its sampled parents.

   1000 samples: rejection keeps    0, estimate nan;   likelihood weighting 0.3774   (exact 0.3917)
  10000 samples: rejection keeps    9, estimate 0.1111;   likelihood weighting 0.3971   (exact 0.3917)
 100000 samples: rejection keeps   99, estimate 0.3535;   likelihood weighting 0.3909   (exact 0.3917)

The evidence has probability 0.00099: of 100 000 samples, rejection sampling keeps 99, and with 10 000 only 9, giving 0.11 against the exact 0.39. Likelihood weighting uses every sample, and is within 0.001 of the answer with 100 000. Its weakness appears when much evidence sits downstream, far from the roots: a few samples then carry nearly all the weight.

24.5 Gibbs Sampling

Gibbs sampling is a Markov chain Monte Carlo (MCMC) method. Start from any assignment consistent with the evidence; repeatedly pick a non-evidence variable and resample it from its distribution given all the others, which depends only on its Markov blanket (Chapter 23):

P(xi∣everything else)∝P(xi∣parents(xi))∏c∈children(xi)P(xc∣parents(xc))P(x_i \mid \text{everything else}) \propto P(x_i \mid \text{parents}(x_i)) \prod_{c \in \text{children}(x_i)} P(x_c \mid \text{parents}(x_c))

After a burn-in, the states visited are (dependent) samples from the posterior. Gibbs sampling scales to models with thousands of variables, where nothing else does.

Gibbs, 20 000 sweeps after 500 of burn-in: 0.0000   (exact 0.3917)

The answer is 0.0000, against the exact 0.3917. Something is badly wrong, and nothing in the output says so.

When Gibbs fails. Gibbs changes one variable at a time, so a deterministic relation can split the states into regions the chain cannot cross. Here E is “T or L”. With E = 0, T and L must both be 0, and resampling either keeps it at 0; with E = 1, setting both T and L to 0 is impossible. So a chain that starts with E = 0 stays in that region for ever, and one that starts with E = 1 never leaves it: neither visits the whole posterior.

chain started from all zeros (E = 0)     : estimate 0.0000
chain started from tuberculosis (E = 1)  : estimate 0.5130
chain started from lung cancer (E = 1)   : estimate 0.5130
exact: 0.3917;  exact within the E = 1 region, P(T = 1 | A, X, D, E = 1): 0.4814

The chain started from all zeros (E = 0) never leaves T = 0 and answers 0. The chains started with E = 1 both answer 0.5130, close to the probability of tuberculosis within the E = 1 region, 0.4814 (5 000 correlated sweeps do not pin it down more precisely). The true answer, 0.3917, mixes the two regions, and no single chain can find it.

A softened network, where E is “T or L” with probability 0.99 (and 0.01 otherwise), lets the chain move:

exact (softened network) 0.3658;  three chains from different starts: [np.float64(0.3696), np.float64(0.3568), np.float64(0.3646)]
Source
<Figure size 900x340 with 1 Axes>

With E only nearly deterministic, the chains cross between the regions; the three, started from different states, end at 0.370, 0.357 and 0.365, around the exact 0.366, though the plot shows how slowly they settle in their first hundreds of sweeps.

Diagnosing MCMC. Run several chains from different starts and check that they agree (the Gelman–Rubin statistic R^\hat R formalises this); discard a burn-in; watch the running estimates settle; and remember that successive samples are correlated, so 20 000 sweeps are worth fewer independent samples.

24.6 Choosing a Method

MethodExact?CostGood when
Enumerationyes2n2^ntiny networks, checking other methods
Variable eliminationyesexponential in the treewidthsmall or tree-like networks
Belief propagation, junction treeyeslinear on trees, exponential in the treewidth otherwisetrees, sparse networks, many queries
Rejection samplingnowastes samples on unlikely evidenceevidence that is not rare
Likelihood weightingnoweights degenerate with much downstream evidenceevidence near the roots, moderate size
Gibbs sampling (MCMC)noneeds burn-in, mixes slowly with strong dependenceslarge networks; no determinism

24.7 Summary

  • Inference sums the joint over unobserved variables; building the joint is exponential.

  • Factors and three operations (product, sum-out, reduction) support exact inference.

  • Variable elimination pushes sums inside products; the elimination order decides the size of the factors, bounded below by the treewidth.

  • Rejection sampling wastes samples on rare evidence; likelihood weighting fixes the evidence and weights samples.

  • Gibbs sampling resamples each variable from its Markov blanket; it needs burn-in and several chains, and fails with deterministic relations.

Exercises

  1. Write the factors of the Asia network for the query P(L∣X=1)P(L \mid X = 1). Which can be dropped before eliminating anything? (Hint: variables that are not ancestors of the query or evidence.)

  2. Eliminate the variables of the chain A→B→C→DA \to B \to C \to D to compute P(D)P(D), by hand. What is the largest factor? Which order would be bad?

  3. Moralise the Asia network. What is its treewidth?

  4. With 1 000 000 samples, about how many does rejection sampling keep for the evidence A=1,X=1,D=1A = 1, X = 1, D = 1? Why does likelihood weighting not have this problem?

  5. Write the Gibbs update for BB in the Asia network: which factors does it involve?

  6. Two Gibbs chains give 0.12 and 0.31 after 10 000 sweeps. What do you conclude, and what do you do?

Further Reading

  • D. Koller, N. Friedman, Probabilistic Graphical Models, MIT Press, 2009, Chapters 9–12.

  • S. Russell, P. Norvig, Artificial Intelligence: A Modern Approach, 4th ed., 2020, Chapter 13.

  • G. Cooper, “The computational complexity of probabilistic inference using Bayesian belief networks”, Artificial Intelligence, 1990.

  • S. Geman, D. Geman, “Stochastic relaxation, Gibbs distributions, and the Bayesian restoration of images”, IEEE PAMI, 1984.

  • A. Gelman, D. Rubin, “Inference from iterative simulation using multiple sequences”, Statistical Science, 1992.