AnyLearn
All lessons
AIadvanced

Message Passing and the Algorithms That Reach the Limit

Belief propagation, the cavity method, and approximate message passing: how physics-derived algorithms achieve the best performance any efficient method can, and how state evolution predicts their behaviour exactly before you run them.

Updated · AI-authored, review-gated · how lessons are made

Not signed in: your progress and quiz score won't be saved.
Progress1 / 8

From analysis to algorithm

So far the physics has been diagnostic: it tells you which phase you are in. The remarkable turn is that the same machinery produces algorithms, and in many cases the best efficient algorithms known.

The logic connecting them is worth stating, because it explains why one set of tools does both jobs.

The quantity you want is the posterior distribution over the signal given the data. Computing it exactly requires summing over exponentially many configurations, which is infeasible.

Statistical physics faced the identical obstacle a century earlier. Computing the properties of a magnet requires summing over every configuration of its spins. The response was a family of mean-field methods: approximate the effect of all other variables on a given variable by an average, and solve the resulting self-consistent equations.

That approximation is a computation you can actually run. So the analysis of the problem and the algorithm for solving it come from the same source, which is why the theory predicts algorithmic thresholds rather than merely observing them.

Full lesson text

All 8 steps on one page, for reading, reference, and search.

Show

1. From analysis to algorithm

So far the physics has been diagnostic: it tells you which phase you are in. The remarkable turn is that the same machinery produces algorithms, and in many cases the best efficient algorithms known.

The logic connecting them is worth stating, because it explains why one set of tools does both jobs.

The quantity you want is the posterior distribution over the signal given the data. Computing it exactly requires summing over exponentially many configurations, which is infeasible.

Statistical physics faced the identical obstacle a century earlier. Computing the properties of a magnet requires summing over every configuration of its spins. The response was a family of mean-field methods: approximate the effect of all other variables on a given variable by an average, and solve the resulting self-consistent equations.

That approximation is a computation you can actually run. So the analysis of the problem and the algorithm for solving it come from the same source, which is why the theory predicts algorithmic thresholds rather than merely observing them.

2. Belief propagation

The concrete realisation of that idea is belief propagation, and its structure is simple enough to describe fully.

Represent the problem as a graph. Variables are nodes; observations that couple several variables are factors connecting them.

Each node maintains a belief about its own value and sends messages to its neighbours. A message from variable i to factor a says: here is what I believe about my value, based on everything I have heard except from you. The factor combines incoming messages with its own constraint and sends back what it now believes about each participant.

Iterate until the messages stop changing.

The exclusion in that message rule is the crucial detail. Leaving out the recipient's own contribution prevents information from echoing back and being counted twice as if it were independent evidence.

On a tree, belief propagation is exact: it computes the true marginals. Real problem graphs have loops, so it is an approximation, and it can fail to converge. But on large sparse random graphs, loops are long and rare, the graph is locally tree-like, and the approximation becomes excellent.

3. The cavity method

The cavity method is the analytical counterpart of belief propagation, and it is the tool Decelle, Krzakala, Moore, and Zdeborova used to derive the block model phase diagram.

The idea is in the name. To understand variable i, remove it from the system, leaving a cavity. Analyse the remaining system, which is slightly smaller and, crucially, has one less source of correlation. Then ask what happens when you put i back.

In the large-system limit, the neighbours of the removed variable become effectively independent, because the loops that would correlate them are long. That independence makes the calculation tractable and yields self-consistent equations for the distribution of messages.

The payoff is that you can compute the free energy, which is the physicists' name for the quantity whose structure determines the phase diagram. From it you read off the Bayes-optimal error, the location of the information-theoretic threshold, and the point where the landscape fractures.

So the same construction yields both the map of the phases and a running algorithm. That duality is the field's central methodological asset.

4. Approximate message passing

Belief propagation assumes a sparse graph. Many important problems are dense: in compressed sensing every measurement touches every unknown, so the factor graph is complete and passing a message along every edge is quadratic work per iteration.

Approximate message passing (AMP) solves this. David Donoho, Arian Maleki, and Andrea Montanari introduced it in "Message-passing algorithms for compressed sensing", published in the Proceedings of the National Academy of Sciences, volume 106, number 45, pages 18914 to 18919, in November 2009.

The derivation starts from belief propagation on the dense graph, then observes that in high dimensions each individual message differs from the node's overall belief by a small amount. Expanding to first order collapses the messages: instead of tracking one message per edge, you track one estimate per variable.

What survives from the message structure is a single correction term, historically called the Onsager term, which subtracts the feedback each variable's own previous estimate contributed to what it is now hearing.

That term is the entire difference between AMP and ordinary iterative thresholding, and dropping it destroys the algorithm's properties.

5. AMP in code

The algorithm is short enough to read in full, which is part of why it is studied so heavily.

# recover sparse x from y = A x + noise
x = np.zeros(n)
resid = y.copy()

for t in range(iters):
    # 1. propose an update from the current residual
    prop = x + A.T @ resid

    # 2. apply the denoiser matched to the signal prior
    x_new = eta(prop, theta[t])          # e.g. soft thresholding

    # 3. Onsager correction: average derivative of the denoiser
    onsager = resid * eta_prime(prop, theta[t]).mean() * (n / m)

    # 4. residual with the memory term subtracted
    resid = y - A @ x_new + onsager
    x = x_new

Without line 3 this is iterative soft thresholding, a reasonable but unremarkable algorithm. With it, two things become true that are not true otherwise.

The error at each iteration is exactly predictable in the high-dimensional limit. And for a wide class of problems, AMP achieves the best error any polynomial-time algorithm is believed to achieve, matching the algorithmic threshold.

The denoiser is where problem knowledge enters. Soft thresholding for sparse signals; the appropriate posterior mean for other priors.

6. State evolution

The property that makes AMP unusual among algorithms is state evolution, presented in the companion analysis paper by the same authors.

State evolution is a small set of deterministic equations that track a single scalar, the mean squared error, from one iteration to the next. In the high-dimensional limit these equations predict the algorithm's actual performance exactly.

The practical consequences are unusual.

You can know the final error before running the algorithm, by iterating a one-dimensional recursion instead of the full high-dimensional procedure.

You can determine the number of iterations needed without experimentation.

And you can locate the algorithmic threshold analytically. Sweep the data ratio and find where the state evolution recursion stops converging to the good fixed point. That value is the boundary of the easy phase.

This is what closes the loop with the previous lesson. The hard phase is not identified by trying algorithms and failing. It is the region where the state evolution of the best known algorithm gets stuck at a poor fixed point while the Bayes-optimal calculation says a better answer exists.

7. One framework, three outputs

The unusual feature of this area is that analysis and algorithm are the same object viewed from different sides. That is why thresholds can be predicted analytically rather than discovered by trial.

flowchart TD
  A["Posterior over the signal, intractable to compute"] --> B["Mean-field approximation"]
  B --> C["Belief propagation on sparse graphs"]
  B --> D["AMP on dense problems"]
  B --> E["Cavity method: analytical free energy"]
  C --> F["A runnable algorithm"]
  D --> F
  D --> G["State evolution predicts error exactly"]
  E --> H["Bayes-optimal error and thresholds"]
  G --> I["Algorithmic threshold"]
  H --> J["Information-theoretic threshold"]

8. Where these methods apply, and where they break

The assumptions are restrictive, and knowing them tells you when to reach for these tools.

RequirementWhy it mattersWhat breaks without it
Known generative modelDefines the posteriorNo Bayes-optimal reference
Random, well-behaved measurementsJustifies the mean-field stepState evolution stops predicting
High dimensionGives concentrationPredictions become approximate
Locally tree-like or dense random structureControls correlationBP may oscillate or diverge

Two failure modes are worth recognising in practice. AMP's guarantees are tied to random measurement matrices with independent entries; on structured or strongly correlated matrices it can diverge outright, which motivated variants such as vector and orthogonal AMP. And belief propagation on graphs with short loops may simply fail to converge.

The honest summary of what this buys you: for a specific and well-studied class of problems, you get algorithms that provably reach the computational limit, plus exact analytical prediction of their behaviour.

That is a stronger guarantee than almost anything else in machine learning offers, purchased with much stronger assumptions than almost anything else requires. The final lesson asks what survives when the assumptions do not.

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. Why does a belief propagation message from a variable exclude the recipient's own previous contribution?
    • To reduce the memory footprint of the algorithm
    • To prevent information echoing back and being double-counted as independent evidence
    • To ensure messages remain normalized probabilities
    • To break ties when multiple factors disagree
  2. What problem does approximate message passing solve relative to belief propagation?
    • It handles graphs with short loops where BP fails to converge
    • It removes the need for a known signal prior
    • It works on dense problems where per-edge messages would be prohibitively expensive
    • It provides exact marginals rather than approximations
  3. What does the Onsager correction term do in AMP?
    • It normalizes the measurement matrix to unit variance
    • It adapts the threshold parameter across iterations
    • It enforces sparsity on the recovered signal
    • It subtracts the feedback each variable's own previous estimate contributed to what it now receives
  4. What makes state evolution unusual as an analytical tool?
    • A small deterministic recursion predicts the algorithm's error exactly in the high-dimensional limit, before running it
    • It provides worst-case bounds valid for any measurement matrix
    • It converts the algorithm into a convex optimization problem
    • It eliminates the need to choose a denoiser
  5. When is AMP most likely to fail in practice?
    • When the signal is exactly sparse
    • When the measurement matrix is structured or strongly correlated rather than random with independent entries
    • When the number of iterations exceeds the dimension
    • When the prior distribution is Gaussian

Related lessons

AI
advanced

Grammars, Stacks, and Making It Free

Recursive formats need a machine with a stack, and a stack breaks the precomputed index because the mask now depends on context. This lesson covers context-free grammars and pushdown automata, XGrammar's split between context-independent and context-dependent tokens, why overlapping grammar work with the GPU makes overhead near zero, and how this interacts with batching and speculative decoding.

10 steps·~15 min
AI
advanced

From Schema to Mask: The Automaton Index

The naive mask costs 64 million validity checks per response. The trick that made constrained decoding practical is to notice that the answer depends only on the automaton's current state, so it can be computed once per state and looked up thereafter. This lesson builds that idea: regex to DFA, why the state is a sufficient summary, how JSON Schema compiles down, and what the index costs.

10 steps·~15 min
AI
advanced

Why Asking Nicely Does Not Guarantee JSON

Prompting for a format gives a high success rate, and a high success rate is not a guarantee. This lesson locates the one place in the decoding loop where a guarantee is possible, shows what masking logits does to the probability distribution, works through why a 5 percent failure rate destroys tail latency rather than average latency, and separates the three families of structured output.

10 steps·~15 min
AI
advanced

Measuring the Damage, and Shipping It

Quantization damage does not show up where people look for it. Perplexity barely moves while hard tasks degrade, and long reasoning suffers most because error compounds. This lesson covers building an evaluation that detects real loss, where the published cliffs are, KV cache quantization as a separate lever, end-to-end memory sizing, and the rollout that catches what evals miss.

10 steps·~15 min