Skip to content
mlmentorship

Belief propagation (message passing)

Belief propagation computes graphical-model marginals through local messages. Sum-product is exact on trees and approximate on graphs with cycles.

Published · 6 min read ·Role-specific ·Intermediate

Visual quick review

Visual first · depth when needed

Trace how a factor collapses an entire branch into an unnormalized function of one boundary variable, then combine matching entries from independent branches to obtain that variable's marginal.

Preparing the visual…

Summary

Belief propagation is a message-passing algorithm for computing marginal distributions in a graphical model. Nodes exchange local “messages” summarizing the evidence from their part of the graph; the sum-product variant computes marginals, the max-product variant computes the most probable configuration. It is exact on trees and an approximation (loopy BP) on graphs with cycles.

Inference (computing ) is the central operation in probabilistic models, and the naive sum over all configurations is exponential. Belief propagation is the algorithm that exploits the graph’s factorization to make it tractable. It’s the engine behind:

  • HMM forward-backward and CRF training/decoding (these are BP on a chain).
  • Decoding LDPC / turbo codes (loopy BP, the reason your phone’s error correction works).
  • General factor-graph inference in vision, sensor fusion, and probabilistic programming.

Interviewers use it to test whether you understand that inference cost is governed by graph structure, not just by the number of variables.

The intuition

A graphical model factorizes a joint distribution into local factors:

To get a marginal you must sum out every other variable, which is exponential in general. BP avoids the blow-up by noticing that on a tree, the sum distributes: you can push summations inside products and reuse partial sums. Each “message” is exactly one of those reusable partial sums, flowing along an edge.

The sum-product algorithm (factor graphs)

Two message types alternate until they reach the root / converge:

Variable → factor (multiply incoming messages from other factors):

Factor → variable (multiply the factor by incoming messages, then sum out the other variables):

The marginal at a variable is the product of all incoming messages (normalized):

Learning objective

How does a whole branch become one local message?

A factor sums out its branch variable to produce a message about x Variable y sends factor a the two-entry function 0.2, 0.8. Factor a combines each possible x with both possible y values and sums y out. The outgoing message is another two-entry function of x: 0.26, 0.68. 1 · COLLAPSE ONE BRANCH y ψₐ x μᵧ→ₐ(y) [0.20, 0.80] μₐ→ₓ(x) [0.26, 0.68] SUM OUT y; KEEP x AS THE INDEX x = 0: 0.9 × 0.2 + 0.1 × 0.8 = 0.26 x = 1: 0.2 × 0.2 + 0.8 × 0.8 = 0.68 The branch is now a function of x only.
The target variable multiplies incoming branch messages and normalizes At variable x, the left message 0.26, 0.68 and right message 0.50, 0.25 are multiplied entry by entry. The unnormalized products 0.13, 0.17 sum to 0.30 and normalize to the belief 0.43, 0.57. 2 · COMBINE AT THE TARGET ψₐ x belief ψᵦ [0.26, 0.68] [0.50, 0.25] MULTIPLY MATCHING ENTRIES x = 0: 0.26 × 0.50 = 0.13 x = 1: 0.68 × 0.25 = 0.17 Normalize [0.13, 0.17] → p(x) = [0.43, 0.57]
Read it this way: follow one branch toward x: the factor multiplies its local compatibility by the incoming evidence and sums out y, leaving a two-entry function of x. At x, multiply matching entries from every branch, then normalize once to read the marginal. A message summarizes a branch; it is not itself a normalized probability.

Replace the inner with a and you get max-product (a.k.a. max-sum in log space), which finds the MAP configuration, the general version of Viterbi.

Exact vs approximate

Graph structureBP behaviorCost
Tree / chainExact marginals in two passes (leaves→root→leaves)
General graphLoopy BP: iterate messages until (hopefully) convergence; approximateper-iteration linear in edges
Treewidth- graphExact via the junction-tree algorithmexponential in treewidth

The deep fact: exact inference is exponential in the graph’s treewidth, not its size. A chain has treewidth 1 (cheap); a fully connected grid has high treewidth (hard). That’s why we fall back to loopy BP, variational inference, or sampling on dense graphs.

Loopy BP

Run the same message updates on a graph with cycles, iterating until messages stop changing. There’s no guarantee of convergence or correctness, yet it works remarkably well in practice: it’s how modern error-correcting codes are decoded and is closely connected to variational (Bethe free energy) approximations.

What an interviewer expects you to say

  1. State that BP computes marginals by passing local messages and exploits the factorization of the joint to avoid the exponential sum.
  2. Distinguish sum-product (marginals) from max-product (MAP / Viterbi).
  3. Know it is exact on trees, and that HMM forward-backward and CRF inference are special cases of sum-product on a chain.
  4. State the key complexity insight: exact inference is exponential in treewidth; use junction tree for exactness or loopy BP / variational / sampling otherwise.
  5. Bonus: loopy BP has no convergence guarantee but powers LDPC/turbo decoding.

Common confusions

  • “BP always gives the right answer.” Only on trees (and junction trees). On loopy graphs it’s a heuristic approximation.
  • “Sum-product and max-product are different algorithms.” Same message structure; one sums to marginalize, the other maxes to find the mode.
  • “Inference is hard because there are many variables.” It’s hard because of treewidth / connectivity. A million-variable chain is easy; a 50-variable dense graph can be infeasible.
  • “Forward-backward is unrelated to BP.” It is sum-product BP on a chain. Viterbi is max-product BP on a chain.
  • “Messages are probabilities.” They’re unnormalized factors (functions of a variable); you normalize only at the end to read off a marginal.

Related: Graphical models, Forward-backward and Viterbi, Conditional random fields, Hidden Markov models, Expectation-maximization.