Probabilistic Inference¶
Introduction¶
A probabilistic graphical model (see Probabilistic Structures Guide) describes a joint distribution compactly, as a product of small factors. Inference answers questions about that distribution: how likely is a cause given an observed effect? How likely is an observation sequence? Which hidden sequence best explains it?
The difficulty is size. With \(n\) binary variables the joint distribution has \(2^n\) entries; summing over all of them is out of reach beyond a few dozen variables. Every algorithm on this page exploits the factorization to avoid that enumeration, and every one of them is an instance of dynamic programming (see Design Paradigms).
Algorithm |
Question |
Model |
|---|---|---|
|
\(P(Q \mid e)\) for chosen query variables |
Bayesian network, Markov random field |
|
\(P(X \mid e)\) for every variable |
Bayesian network, Markov random field |
|
\(P(o_{1:T})\) and \(P(S_t \mid o_{1:t})\) |
hidden Markov model |
|
\(\arg\max_{s_{1:T}} P(s_{1:T}, o_{1:T})\) |
hidden Markov model |
Running Example: the Alarm Network¶
graph TD
B((Burglary<br/>0.001)) --> A((Alarm))
E((Earthquake<br/>0.002)) --> A
A --> J((JohnCalls))
A --> M((MaryCalls))
Burglary, Earthquake |
\(P(A = \text{true} \mid B, E)\) |
|---|---|
true, true |
0.95 |
true, false |
0.94 |
false, true |
0.29 |
false, false |
0.001 |
John calls with probability 0.90 when the alarm rings and 0.05 otherwise; Mary with probability 0.70 and 0.01.
Both neighbours call. How likely is a burglary?
Variable Elimination¶
Idea¶
By definition, with the hidden variables \(E\) and \(A\) summed out:
Enumerating the double sum repeats work: for each value of \(B\) it recomputes every product. Factors that do not mention a summed variable can be pulled out of that sum:
Summing out \(e\) first produces a new table \(\tau_1(a, B)\), computed once; summing out \(a\) then produces \(\tau_2(B)\). The normalized result is \(P(B = \text{true} \mid j, m) \approx 0.284\).
Algorithm¶
tables <- factors of the model, restricted to the evidence
for each hidden variable h (elimination order):
combine <- tables whose scope contains h
tables <- (tables - combine) + { sum_h product(combine) }
return normalize(product(tables))
Each intermediate table is a stored subproblem: this is dynamic programming over the factors.
Cost and Elimination Order¶
Eliminating a variable builds a table over the union of the scopes it touches. With domains of size \(d\) and a largest intermediate scope of \(w\) variables:
\(w\) depends on the order: eliminating the hub of a star graph first
creates a table over all its neighbours, eliminating the leaves first keeps
every table small. Finding the best order is NP-hard; sds uses the
greedy rule “eliminate the variable that builds the smallest table”,
which works well in practice. On tree-shaped models \(w\) never
exceeds the largest factor, and inference is linear in the model size.
Belief Propagation¶
Messages on the Factor Graph¶
The factor graph connects each factor to the variables of its scope. Belief propagation sends two kinds of messages along its edges:
and every marginal is the normalized product of the messages a variable receives:
graph LR
fB[P B] --- B((B))
fE[P E] --- E((E))
B --- fA[P A given B,E]
E --- fA
fA --- A((A))
A --- fJ[P J given A]
A --- fM[P M given A]
fJ --- J((J))
fM --- M((M))
Why It Is Exact on Trees¶
On a tree, the message \(\mu_{f \to x}\) summarizes exactly the part of the model on \(f\)’s side of the edge \(f - x\): removing that edge splits the tree in two, and the recursion above is variable elimination performed along that split. Each message is therefore computed once per edge and direction, and all marginals come out exact for the price of two passes.
When the Graph Has Cycles¶
On a factor graph with cycles the same updates can still be iterated, but
information comes back around each cycle and is counted more than once.
This loopy belief propagation often converges to good approximations, but
not to exact marginals. On the sprinkler network, whose four variables form
a cycle (Cloudy → Sprinkler → WetGrass ← Rain ← Cloudy), it estimates
\(P(\text{Rain} \mid \text{WetGrass}) \approx 0.78\) where the exact
answer is about 0.71. belief_propagation() emits a RuntimeWarning
if the messages do not settle; use variable_elimination when an exact
answer matters.
Summary¶
Algorithm |
Recurrence over |
Time |
Exact |
|---|---|---|---|
|
factors, one hidden variable at a time |
\(O(n\, d^{\,w})\) |
yes |
|
messages on the factor graph |
\(O(I \sum_f |f|)\) per run |
on trees |
|
time steps, sum |
\(O(T\, |S|^2)\) |
yes |
|
time steps, max |
\(O(T\, |S|^2)\) |
yes |
References¶
D. Koller and N. Friedman, Probabilistic Graphical Models, chapters 9 (variable elimination) and 10–11 (belief propagation); the open Stanford CS228 notes follow the same path — https://ermongroup.github.io/cs228-notes/
S. Russell and P. Norvig, Artificial Intelligence: A Modern Approach, chapters 13–14 (the alarm network, the umbrella world).
D. Jurafsky and J. H. Martin, Speech and Language Processing (3rd ed. draft, free online), appendix Hidden Markov Models — https://web.stanford.edu/~jurafsky/slp3/
CLRS, chapter 15 (dynamic programming).