Exact Inference
Inference means computing the posterior distribution of query variables given evidence, \(\mathbf{P}(X \mid \mathbf{e})\). "Given that John and Mary both called, what's the probability of a burglary?" The general recipe from §4.1 still applies: sum the joint over the hidden variables, then normalize. The Bayes net gives us the joint as a product of CPTs, and the cleverness is in how we organize the sums.
Inference by enumeration
Query \(B\), evidence \(j, m\), hidden variables \(E, A\):
$$ \mathbf{P}(B \mid j, m) = \alpha \sum_{e}\sum_{a} \mathbf{P}(B)\, P(e)\, P(a \mid B, e)\, P(j \mid a)\, P(m \mid a). $$Move each factor as far left as it can go, so it sits outside the sums that don't involve it:
$$ \mathbf{P}(B \mid j, m) = \alpha\, \mathbf{P}(B) \sum_{e} P(e) \sum_{a} P(a \mid B, e)\, P(j\mid a)\, P(m\mid a). $$First, \(f(a) = P(j|a)P(m|a)\): for \(a\), \(0.90 \times 0.70 = 0.63\). For \(\neg a\), \(0.05 \times 0.01 = 0.0005\).
For \(B = b\):
- \(e\): \(\sum_a P(a|b,e) f(a) = 0.95(0.63) + 0.05(0.0005) = 0.598525\)
- \(\neg e\): \(0.94(0.63) + 0.06(0.0005) = 0.59223\)
- \(\sum_e P(e)(\cdot) = 0.002(0.598525) + 0.998(0.59223) = 0.592243\)
- Times \(P(b) = 0.001\): 0.000592
For \(B = \neg b\):
- \(e\): \(0.29(0.63) + 0.71(0.0005) = 0.183055\)
- \(\neg e\): \(0.001(0.63) + 0.999(0.0005) = 0.0011295\)
- \(\sum_e\): \(0.002(0.183055) + 0.998(0.0011295) = 0.0014934\)
- Times \(P(\neg b) = 0.999\): 0.001492
Normalize: \(\alpha \langle 0.000592, 0.001492 \rangle = \langle 0.284, 0.716 \rangle\).
So even with both neighbours calling, a burglary is only 28% likely. False alarms (rate 0.001) are far more common than burglaries, and the false-alarm rate is multiplied by 999 non-burglary cases. It's the base-rate effect from §4.2 inside a network.
NET = {
"Burglary": ((), {(): 0.001}),
"Earthquake": ((), {(): 0.002}),
"Alarm": (("Burglary", "Earthquake"),
{(True, True): 0.95, (True, False): 0.94, (False, True): 0.29, (False, False): 0.001}),
"JohnCalls": (("Alarm",), {(True,): 0.90, (False,): 0.05}),
"MaryCalls": (("Alarm",), {(True,): 0.70, (False,): 0.01}),
}
ORDER = ["Burglary", "Earthquake", "Alarm", "JohnCalls", "MaryCalls"]
def p(var, value, e):
parents, cpt = NET[var]
pt = cpt[tuple(e[q] for q in parents)]
return pt if value else 1 - pt
def enumerate_all(variables, e):
"""Sum of products over the remaining variables (AIMA's ENUMERATE-ALL)."""
if not variables:
return 1.0
Y, rest = variables[0], variables[1:]
if Y in e: # evidence or already-fixed variable
return p(Y, e[Y], e) * enumerate_all(rest, e)
return sum(p(Y, y, e) * enumerate_all(rest, {**e, Y: y}) for y in (True, False))
def enumeration_ask(X, e):
dist = {x: enumerate_all(ORDER, {**e, X: x}) for x in (True, False)}
z = sum(dist.values()) # normalize (the alpha)
return {x: v / z for x, v in dist.items()}
print("P(Burglary | j, m) =", {k: round(v, 3) for k, v in enumeration_ask("Burglary", {"JohnCalls": True, "MaryCalls": True}).items()})
print("P(Burglary | j) =", round(enumeration_ask("Burglary", {"JohnCalls": True})[True], 4))
print("P(Burglary | j, m, e) =", round(enumeration_ask("Burglary", {"JohnCalls": True, "MaryCalls": True, "Earthquake": True})[True], 4))
print("P(Earthquake | j, m) =", round(enumeration_ask("Earthquake", {"JohnCalls": True, "MaryCalls": True})[True], 4))
print("P(Alarm) =", round(enumeration_ask("Alarm", {})[True], 5))P(Burglary | j, m) = {True: 0.284, False: 0.716}
P(Burglary | j) = 0.0163
P(Burglary | j, m, e) = 0.0033
P(Earthquake | j, m) = 0.1761
P(Alarm) = 0.00252
Now explore it interactively. Every number is computed exactly by enumerating the 32 worlds.
Things to try:
- Set only JohnCalls = true: P(Burglary) is just 1.6%. John is unreliable, and one call is weak evidence.
- Set Alarm = true and see JohnCalls and MaryCalls update. Then also set JohnCalls = false. MaryCalls doesn't change at all, because J and M are independent given A.
- Set Burglary = true and Earthquake = true and look at Alarm (0.95). Causal reasoning, from causes down to effects, works just like diagnostic reasoning, from effects up to causes.
Complexity of enumeration
Enumeration evaluates a tree of sums of depth \(n\), so it costs \(O(2^n)\) time (but only linear space). It also recomputes the same things repeatedly: the product \(P(j|a)P(m|a)\) was computed separately for every value of \(e\) and \(b\). The fix is the same as in Chapter 5: dynamic programming.
Variable elimination
Variable elimination (VE) evaluates the sum-of-products expression right to left and stores intermediate results as factors: tables indexed by the variables they depend on.
- Make a factor for each CPT, with the evidence variables already fixed: \(f_J(A) = P(j|A)\), \(f_M(A) = P(m|A)\), \(f_A(A,B,E)\), \(f_E(E)\), \(f_B(B)\).
- Eliminate A: multiply all factors mentioning A, then sum A out: $$ f_1(B,E) = \sum_a f_A(a,B,E)\, f_J(a)\, f_M(a). $$ That's a 2×2 table. Its entries are exactly the four sums 0.598525, 0.59223, 0.183055 and 0.0011295 from the hand calculation above.
- Eliminate E: \(f_2(B) = \sum_e f_E(e)\, f_1(B, e)\), giving \(\langle 0.592243, 0.0014934\rangle\).
- Multiply in the remaining factors and normalize: \(\alpha\, f_B(B)\, f_2(B) = \langle 0.284, 0.716\rangle\).
The two operations are the pointwise product of factors (multiply entries that agree on shared variables) and summing out a variable. VE also skips irrelevant work: every variable that is not an ancestor of a query or evidence variable is irrelevant and can be removed. For example, to compute P(J | b), MaryCalls is irrelevant, because \(\sum_m P(m|a) = 1\).
Complexity: it depends on the graph's shape
- Polytrees (singly connected: at most one undirected path between any two nodes, like the burglary net) allow exact inference in time linear in the size of the network.
- Multiply connected networks: exact inference is NP-hard in general (#P-hard, in fact). The cost of VE is exponential in the size of the largest factor created, which depends on the elimination order. Finding the best order is itself NP-hard, so heuristics are used, such as eliminating the variable that creates the smallest factor first.
- Clustering / junction tree algorithms merge nodes into a polytree of "mega-nodes" so that all posteriors can be computed at once.
When exact inference is too expensive, we approximate by sampling. That's the next section.