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.
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:
- 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.
- Sum out: add the rows that differ only in X. The result no longer mentions X.
- 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):
| step | joined table | × | + | new factor |
|---|---|---|---|---|
| eliminate A | f3 × f4 × f5 over B, E, A: 8 rows | 16 | 4 | f6(B,E) = 0.5985, 0.5922, 0.1831, 0.00113 |
| eliminate E | f2 × f6 over B, E: 4 rows | 4 | 2 | f7(B) = 0.5922, 0.001493 |
| final | f1 × f7 over B: 2 rows | 2 | 0 | 0.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.
| problem | order | largest factor | × / + elimination | × / + enumeration | answer |
|---|---|---|---|---|---|
| P(B | j, m) | A, E | 8 rows | 22 / 6 | 32 / 6 | 0.2842 |
| P(B | j, m) | E, A | 8 rows | 18 / 6 | 32 / 6 | 0.2842 |
| P(B | m) | J, E, A | 8 rows | 18 / 8 | 64 / 14 | 0.0561 |
| P(B | m) | A, E, J | 16 rows | 42 / 14 | 64 / 14 | 0.0561 |
| P(J | b) | M, E, A | 4 rows | 14 / 6 | 64 / 14 | 0.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.
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.