The idea: enumeration with the repeated work done once

Variable elimination computes the same exact answer as inference by enumeration, but it does the sums one variable at a time and keeps the partial results in small tables called factors. A sub-product that enumeration recomputes in many terms is computed once and reused.

Before: the pool holds f1(B), f2(E), f3(B,E,A) with 8 rows, f4(A) and f5(A). The three factors that mention A are joined into one 8-row table over B, E, A, then A is summed out, giving f6(B,E) with 4 rows. After: the pool holds f1(B), f2(E) and f6(B,E)
Eliminating A: join every factor that mentions A, sum A out, put the smaller factor back.

The page uses the burglary alarm network with the same numbers as Bayesian Network Inference, so the answers match: P(b | j, m) = 0.2842. That page explains the CPTs and enumeration term by term. Here you pick a problem (a query and an elimination order) and press Next Step to run one phase, or Run All. The COST panel counts multiplications and additions for both methods.

Factors and evidence

A factor is a table with one number per combination of its variables. Each CPT starts as a factor: P(A | B, E) is f3(B,E,A) with 8 rows, P(J | A) is f4(A,J) with 4 rows. Evidence restricts a factor: keep the rows that agree with it, and the evidence variable disappears. With J = true, f4(A,J) becomes f4(A) with 2 rows, 0.9 and 0.05. Restricting needs no arithmetic (Demo 1).

With evidence b, the factor P(B) shrinks to a single number, 0.001: a factor with no variables at all (Demo 6).

Join, then sum out

To eliminate a hidden variable X:

  1. Join: take every factor in the pool that mentions X and multiply them into one table over all their variables. Each row multiplies the matching entry of each factor.
  2. Sum out: add the rows that differ only in X. The result no longer mentions X.
  3. Put the new factor back into the pool. Factors that do not mention X are not touched.

For P(B | j, m) with the order A, E (Demo 2):

stepjoined table×+new factor
eliminate Af3 × f4 × f5 over B, E, A: 8 rows164f6(B,E) = 0.5985, 0.5922, 0.1831, 0.00113
eliminate Ef2 × f6 over B, E: 4 rows42f7(B) = 0.5922, 0.001493
finalf1 × f7 over B: 2 rows200.0005922 and 0.001492

The last two numbers are P(B, j, m), not yet probabilities. Normalising divides each by their total 0.002084: P(b | j, m) = 0.2842.

Cost compared with enumeration

The page counts (k − 1) multiplications per row when it joins k factors, and one addition per row it produces when it sums a variable out. Enumeration with h hidden variables builds 2·2h terms of 5 CPT entries each. Normalising costs the same in both, so it is not counted.

problemorderlargest factor× / + elimination× / + enumerationanswer
P(B | j, m)A, E8 rows22 / 632 / 60.2842
P(B | j, m)E, A8 rows18 / 632 / 60.2842
P(B | m)J, E, A8 rows18 / 864 / 140.0561
P(B | m)A, E, J16 rows42 / 1464 / 140.0561
P(J | b)M, E, A4 rows14 / 664 / 140.8490

On five variables the saving is small. It grows fast with size: in a chain of n variables, enumeration costs about 2n while elimination in the right order costs about 4n multiplications, because no factor ever has more than two variables. The forward algorithm of an HMM is exactly this.

The elimination order

Any order gives the same answer, but not the same work. The cost is set by the largest factor the joins build, which is 2 to the power of the number of variables in it.

Query P(B | m). Order A, E, J builds joined tables of 16, 8, 4 and 2 rows, 42 multiplications. Order J, E, A builds 4, 8, 4 and 2 rows, 18 multiplications. Both give 0.0561
Eliminating A first joins four variables into one 16-row table; the order J, E, A never goes above 8.

In P(B | m), eliminating A first must join P(A | B, E), P(J | A) and P(m | A) into one table over A, B, E, J: 16 rows (Demo 5). The order J, E, A keeps every table at 8 rows or fewer (Demo 4). For P(B | j, m) both orders build 8 rows, but E first is cheaper, 18 multiplications against 22 (Demo 3).

Finding the best order is NP-hard. Simple greedy rules work well in practice: eliminate next the variable whose join would be smallest (min-size or min-degree), or the one that adds the fewest new links between variables (min-fill). The size of the largest factor under the best order is set by the network's treewidth; exact inference is fast when it is small and hopeless when it is large, which is when sampling methods take over.

Barren variables

In P(B | m), JohnCalls is hidden and has no evidence below it. Summing it out of P(J | A) gives an all-ones factor, because P(j | a) + P(¬j | a) = 1 (Demo 4). Those additions are wasted work. Any variable that is not the query, the evidence or one of their ancestors can be dropped before starting, which is the pruning step on Bayesian Network Inference. The page keeps the barren variable on purpose to show why pruning is safe. In P(J | b), MaryCalls is barren the same way (Demo 6).

See also 8-puzzle heuristics, heuristic search from the same AI course.

What the page leaves out

Variables with more than two values, continuous variables, pruning before elimination, heuristics that choose the order automatically, the junction tree (clique tree) algorithm that reuses the factors to answer every query at once, and approximate methods. A Naive Bayes classifier is a network where elimination is trivial, since each feature hangs directly off the class.

On the mind map

Open the AI: Search, Games & Reasoning mind mapAI: Search, Games& ReasoningGame search: best move against an opponentGame searchConstraint satisfaction: assign values, no rule brokenConstraint satisfactionLocal search: improve one solution step by stepLocal searchSequential decisions: states, actions, rewardsSequential decisionsProbabilistic reasoning: belief from uncertain evidenceProbabilistic reasoningLogic & knowledge: rules and inferenceLogic & knowledgeState-space search: uninformed searchState-space searchBayesian networkBayesian networkCPTs; sum out hidden variablesVariable EliminationJoin, sum out; order sets the largest tableViterbi on an HMMViterbi on an HMMbest hidden path by DP and tracebackPOS Tagging HMMPOS Tagging HMMCount tables, Viterbi finds best tagsParticle filterParticle filterPredict, weight by sensor, resampleKalman FilterKalman FilterGain K = P⁻/(P⁻+R) blends guess and readingSampling in Bayes netsSampling in Bayes netsRejection, likelihood weighting, GibbsOpen the AI: Search, Games & Reasoning mind mapAI: Search, Games & Reasoningbelief from uncertain evidenceProbabilistic reasoningBayesian networkBayesian networkCPTs; sum out hidden variablesVariable EliminationJoin, sum out; order sets the largest tableViterbi on an HMMViterbi on an HMMbest hidden path by DP and tracebackPOS Tagging HMMPOS Tagging HMMCount tables, Viterbi finds best tagsParticle filterParticle filterPredict, weight by sensor, resampleKalman FilterKalman FilterGain K = P⁻/(P⁻+R) blends guess and readingSampling in Bayes netsSampling in Bayes netsRejection, likelihood weighting, Gibbs
This page on the AI: Search, Games & Reasoning mind map · 24 pages in 7 branches · open it to see the whole map and test yourself in recall mode.