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 — Implementation

Open in Colab
  1. Checking variable elimination against enumeration on random networks

  2. Elimination-order heuristics: min-degree against min-fill

  3. Pruning: variables a query does not need

  4. Belief propagation on a chain

  5. How many samples is a Gibbs chain worth? Autocorrelation and effective sample size

library ready

1. Checking Against Enumeration

Any new inference code should be checked against brute force on networks small enough to enumerate: 12 variables, random structures and tables, random queries and evidence.

30 random queries: largest difference from enumeration 1.22e-15

2. Min-Degree Against Min-Fill

Min-fill eliminates the variable whose elimination adds the fewest new edges between its neighbours; it is usually at least as good as min-degree. We compare the largest factor each builds, on random networks of 60 variables.

network       min-degree    min-fill
seed 0             1,024       1,024
seed 1             1,024       1,024
seed 2             1,024       1,024
seed 3             1,024       1,024
seed 4             1,024       1,024

On these five networks the two heuristics find orders of the same cost: the largest factor has 1 024 entries every time, against millions for a topological order. They differ on other structures; libraries often try both and keep the better order.

3. Pruning What a Query Does Not Need

A variable that is not an ancestor of the query or of the evidence sums to 1 and can be removed before anything else (a barren node): its table contributes nothing. For a query near the roots of a large network, this removes most of it.

whole network   60 variables, largest factor   1024,   14.6 ms, P = 0.676416
pruned           5 variables, largest factor      4,    0.2 ms, P = 0.676416

4. Belief Propagation on a Chain

On a tree, exact inference passes one message per edge in each direction. On a chain X1→X2→⋯→XnX_1 \to X_2 \to \dots \to X_n with evidence on the last variable, the forward messages are the prior marginals and the backward messages carry the evidence back: this is the forward–backward algorithm of hidden Markov models (Chapter 26).

P(x10 = 1 | x29 = 1): belief propagation 0.310979, variable elimination 0.310979

Belief propagation computes the posterior of every variable with two passes, where variable elimination would be run once per variable. On graphs with cycles, the same message passing run anyway (loopy belief propagation) often gives good approximations, and underlies the decoding of modern error-correcting codes.

5. How Many Samples Is a Gibbs Chain Worth?

Successive Gibbs samples are correlated. The effective sample size (ESS) estimates how many independent samples they are worth: n/(1+2∑kρk)n / (1 + 2 \sum_k \rho_k), with ρk\rho_k the autocorrelation at lag kk (summed until it becomes small).

correlation 0.0: 10 000 Gibbs samples are worth about 10,000 independent ones (lag-1 autocorrelation -0.01)
correlation 0.9: 10 000 Gibbs samples are worth about 1,111 independent ones (lag-1 autocorrelation 0.80)
correlation 0.99: 10 000 Gibbs samples are worth about 90 independent ones (lag-1 autocorrelation 0.98)

Without correlation, every sample counts. With a correlation of 0.9 between the two variables, 10 000 Gibbs samples are worth about 1 100 independent ones; with 0.99, about 90: the chain crawls along the narrow ridge of the distribution, one small step at a time. Strongly dependent variables are where Gibbs sampling struggles, and where block updates or other MCMC methods (Hamiltonian Monte Carlo) are used.

Summary

  • Check exact inference against enumeration on small random networks before trusting it.

  • Min-fill and min-degree heuristics keep elimination tractable; neither is always best.

  • Pruning barren variables shrinks the problem before inference starts.

  • Belief propagation on trees computes every posterior in two passes; on a chain it is forward–backward.

  • Correlated MCMC samples are worth fewer independent ones: report the effective sample size.