Factor graph

On this page 7

A factor graph is a bipartite graph that represents how a global function, most often a joint probability distribution, factorizes into a product of local functions, each depending on only a subset of the variables. In probabilistic models, it serves as a unifying representation for Bayesian networks, Markov random fields and many coding and signal-processing models, because it makes every factor explicit as its own node.

Factor graphs matter because the structure of the factorization determines how efficiently inference can be performed. The sum-product algorithm, also known as belief propagation, runs directly on a factor graph and computes marginal distributions by passing messages between variable nodes and factor nodes. The same representation underlies error-correcting decoders, robot localization and mapping systems and many probabilistic programming back ends.

How a factor graph encodes a factorization

A factor graph has two kinds of nodes: variable nodes, usually drawn as circles, and factor nodes, usually drawn as squares. An edge connects a variable node to a factor node if and only if that variable is an argument of that factor. Edges never connect two variables or two factors directly, which is what makes the graph bipartite.

For a distribution p(a, b, c) proportional to f1(a, b) times f2(b, c) times f3(c), the graph contains three variable nodes and three factor nodes. The joint distribution equals the product of all factors divided by a normalization constant Z, the partition function. Factors need not be probabilities: they can be any non-negative potential functions, conditional probability tables or likelihood terms.

The table below lists the elements of a factor graph and their role in the factorization.

ElementNotationMeaning
Variable nodeCircle labeled xA random variable, observed or latent
Factor nodeSquare labeled fA local function over its neighboring variables
EdgeLine between x and fVariable x is an argument of factor f
Partition functionZConstant that normalizes the product of factors
EvidenceShaded or clamped nodeA variable fixed to an observed value
Elements of a factor graph

Factor graphs compared with other graphical models

A factor graph is more explicit than other probabilistic graphical models because it shows the exact factorization rather than only conditional independence relations. A Markov random field with a fully connected triangle of three variables cannot reveal whether the distribution contains one three-way potential or three pairwise potentials; the factor graph distinguishes these cases.

Any Bayesian network or Markov random field can be converted into a factor graph. For a Bayesian network, each conditional probability distribution becomes one factor connected to the child and its parents. For a Markov random field, each clique potential becomes one factor.

RepresentationEdgesWhat it showsTypical use
Bayesian networkDirected, between variablesCausal or generative orderingExpert systems, generative modeling
Markov random fieldUndirected, between variablesConditional independence via cliquesImage models, spatial statistics
Factor graphUndirected, variable to factorExact factorization structureMessage passing, decoding, SLAM
Conditional random fieldUndirected, conditioned on inputsDiscriminative output structureSequence labeling, segmentation
Comparison of graphical model representations

A Forney-style factor graph is a variant in which variables are edges and factors are nodes. It is common in coding theory and signal processing because it yields compact block diagrams, but it encodes the same mathematical content.

Inference by message passing on a factor graph

The sum-product algorithm computes marginals by sending messages along edges. A message from a variable to a factor is the product of messages from all other neighboring factors. A message from a factor to a variable multiplies the factor by incoming variable messages and sums out all other variables. Replacing sums with maximization gives the max-product algorithm, which finds the most probable configuration, as the Viterbi algorithm does for hidden Markov models.

On a tree-structured factor graph, two sweeps of messages, from the leaves to a root and back, give exact marginals for every variable. The cost is linear in the number of factors and exponential only in the size of the largest factor. On graphs with cycles, the same updates can be iterated as loopy belief propagation, which often converges to useful approximations but offers no general guarantee of convergence or accuracy.

The following table summarizes the main inference algorithms that operate on factor graphs.

AlgorithmOutputExact onNotes
Sum-productMarginal distributionsTreesEquivalent to belief propagation
Max-product or max-sumMost probable assignmentTreesMax-sum works in log space
Loopy belief propagationApproximate marginalsNone in generalUsed in LDPC and turbo decoding
Junction tree algorithmExact marginalsAny graphCost grows with treewidth
Expectation propagationApproximate marginalsSpecial casesProjects messages onto tractable families
Inference algorithms on factor graphs

Where factor graphs are used in practice

Factor graphs are the standard formalism in several engineering fields. In channel coding, low-density parity-check codes and turbo codes are decoded by loopy belief propagation on their factor graphs, an approach that brought practical performance close to the Shannon limit. In robotics, simultaneous localization and mapping is formulated as a factor graph of poses and landmarks, and solved as a nonlinear least-squares problem.

  • Error-correcting codes: parity checks become factors, transmitted bits become variables.
  • Robot state estimation: odometry, GPS and camera measurements become factors over poses.
  • Probabilistic programming: many systems compile models into factor graphs before inference.
  • Computer vision: stereo matching and denoising use pairwise factors on pixel grids.
  • Recommender and ranking systems: skill rating models such as TrueSkill use factor graphs with expectation propagation.

Limitations and common misconceptions

A factor graph is a representation, not an inference guarantee. Drawing a model as a factor graph does not make inference tractable: if large factors or dense cycles are present, exact computation remains exponential, and approximate methods may converge slowly or to poor fixed points.

MisconceptionReality
Factors must be probabilitiesAny non-negative function works; normalization comes from Z
Loopy BP always convergesIt may oscillate; damping and scheduling help
Factor graphs imply causalityThey encode factorization only, not causal direction
Exact inference is always cheapCost depends on treewidth and factor size
Misconceptions about factor graphs

Practitioners should also watch for numerical underflow in long message chains, which is usually avoided by normalizing messages or working with log-domain messages.

Explained without jargon

Many problems involve many unknown things that affect each other. A factor graph is a drawing that shows which unknowns are linked by which rules. Circles stand for the unknowns. Squares stand for the rules. A line means that a rule involves that unknown.

This drawing helps a computer split one huge puzzle into small pieces. Each piece passes short notes to its neighbors about what it believes. The main benefit is speed: the computer reaches a good answer without checking every possible combination.

A typical use case

A warehouse robot must know where it is while it drives between shelves. Its wheel sensors report how far it moved, a laser scanner sees shelf corners and a ceiling camera occasionally recognizes fixed markers. Each measurement is noisy and incomplete on its own.

The navigation software builds a factor graph with one variable node for each robot pose, recorded every half second, and one for each landmark. Odometry adds a factor between consecutive poses, each scanner observation adds a factor between a pose and a landmark, and each marker sighting adds a strong prior factor. After ten minutes the graph holds about 1,200 poses and several thousand factors.

An incremental optimizer updates only the affected part of the graph when new measurements arrive. When the camera recognizes a marker seen earlier, the new loop closure factor corrects accumulated drift of about 30 centimeters, and the robot keeps its position estimate within a few centimeters.