Skip to content
nonconvexcard

Retrieval as a Latent-Variable Model

RAG written as marginalisation over a latent document, the exact decomposition of its error, and why retrieval failures propagate rather than average out.

RAGResearch⏱ 1 day13 min readraglatent variablemarginalisationerror propagationreranking
5 problems →

The model

DefinitionRAG as marginalisation over a latent document

Let xx be a query, yy a response, and zz a document from a corpus \Zcal\Zcal. Treat zz as a latent variable:

p(y∣x)=∑z∈\Zcalpη(z∣x)⏟retriever pθ(y∣x,z)⏟generator.p(y\mid x) = \sum_{z\in\Zcal} \underbrace{p_\eta(z\mid x)}_{\text{retriever}}\ \underbrace{p_\theta(y\mid x,z)}_{\text{generator}} .

In practice the sum is truncated to the top-kk retrieved documents, with the retriever posterior

pη(z∣x)=exp⁡(⟨fη(x),gη(z)⟩)∑z′∈top-kexp⁡(⟨fη(x),gη(z′)⟩).p_\eta(z\mid x) = \frac{\exp\big(\ip{f_\eta(x)}{g_\eta(z)}\big)}{\sum_{z'\in\mathrm{top}\text{-}k}\exp\big(\ip{f_\eta(x)}{g_\eta(z')}\big)} .
PropositionRAG-sequence and RAG-token are different models
pseq(y∣x)=∑zpη(z∣x)∏t=1∣y∣pθ(yt∣x,z,y<t),ptok(y∣x)=∏t=1∣y∣∑zpη(z∣x) pθ(yt∣x,z,y<t).p_{\text{seq}}(y\mid x) = \sum_z p_\eta(z\mid x)\prod_{t=1}^{|y|}p_\theta(y_t\mid x,z,y_{<t}), \qquad p_{\text{tok}}(y\mid x) = \prod_{t=1}^{|y|}\sum_z p_\eta(z\mid x)\,p_\theta(y_t\mid x,z,y_{<t}).

These are not equal: the first is a mixture of products and the second a product of mixtures. pseqp_{\text{seq}} commits to a single document for the whole response; ptokp_{\text{tok}} can draw different tokens from different documents.

A variational view

TheoremA lower bound on the RAG log-likelihood

For any distribution q(z)q(z) over documents,

log⁡p(y∣x)  ≥  Ez∼q[log⁡pθ(y∣x,z)]−KL(q(z) ∥ pη(z∣x)),\log p(y\mid x) \;\ge\; \E_{z\sim q}\big[\log p_\theta(y\mid x,z)\big] - \mathrm{KL}\big(q(z)\,\|\,p_\eta(z\mid x)\big),

with equality if and only if q(z)=p(z∣x,y)q(z) = p(z\mid x,y), the true posterior given both query and answer.

Key idea

The bound identifies what a retriever should approximate: not p(z∣x)p(z\mid x), the documents relevant to the query, but p(z∣x,y)p(z\mid x,y), the documents that explain the answer. Those differ, and the gap is the reason two-stage training helps — the retriever trained on query–document relevance is optimising the wrong posterior, and methods that distil the generator's likelihood back into the retriever (REALM, and the retriever-update schemes that follow it) are closing exactly this gap.

Error propagation

TheoremRetrieval error propagates into generation

Fix a query xx and suppose there is a set Z⋆Z^\star of documents sufficient to answer it. Let

γ=pη(Z⋆∣x)\gamma = p_\eta\big(Z^\star \mid x\big)

be the retriever's probability of supplying a sufficient document (its recall, in probability form). Let pRAG(⋅∣x)p_{\mathrm{RAG}}(\cdot\mid x) be the RAG output distribution and poracle(⋅∣x)p_{\mathrm{oracle}}(\cdot\mid x) the distribution obtained by conditioning on Z⋆Z^\star with certainty. Then

∥pRAG(⋅∣x)−poracle(⋅∣x)∥TV  ≤  1−γ.\big\| p_{\mathrm{RAG}}(\cdot\mid x) - p_{\mathrm{oracle}}(\cdot\mid x)\big\|_{\mathrm{TV}} \;\le\; 1-\gamma .
PropositionThe bound is attained

There are instances with equality. Take a binary answer space {y0,y1}\{y_0,y_1\}, a generator that outputs y1y_1 with probability 11 given any z∈Z⋆z\in Z^\star and y0y_0 with probability 11 given any z∉Z⋆z\notin Z^\star. Then ∥p~Ac−p~A∥TV=1\norm{\tilde p_{A^c}-\tilde p_A}_{\mathrm{TV}}=1 and the bound holds with equality.

Key idea

Read the bound as: retrieval error is a hard ceiling, not a soft degradation. A retriever that fails 10%10\% of the time permits the output distribution to be wrong on 10%10\% of the probability mass, and the tightness proposition shows a generator can realise the whole of that error.

There is no averaging-out. One might hope that the k−1k-1 irrelevant documents in the context act as neutral noise the model ignores; the evidence is against that. Shi et al. show that irrelevant context measurably degrades reasoning accuracy, and Liu et al. show that relevant context placed in the middle of a long prompt is used substantially less than context at either end. So the failure modes compound rather than cancel: missing documents cap the achievable quality, and extra documents actively hurt.

Combined with the multiplicative recall of the pipeline, the arithmetic is unforgiving. A chunker that splits the answer across boundaries (γ1=0.95\gamma_1 = 0.95), an ANN index at γ2=0.9\gamma_2 = 0.9, and a top-kk truncation at γ3=0.95\gamma_3 = 0.95 give γ≈0.81\gamma \approx 0.81 — so nearly a fifth of the output distribution is unconstrained by the evidence, before the generator makes a single mistake of its own.

Reranking

PropositionWhat reranking can and cannot do

Let the first stage return a candidate set CC with P(Z⋆∩C≠∅)=γ1\P(Z^\star\cap C\ne\emptyset) = \gamma_1, and let a reranker select the top k′<∣C∣k'<|C| from CC. Then the post-rerank recall satisfies

γrerank=γ1⋅P(relevant document in top k′∣it is in C)≤γ1.\gamma_{\text{rerank}} = \gamma_1\cdot\P\big(\text{relevant document in top }k' \mid \text{it is in } C\big) \le \gamma_1 .

Reranking can only improve the precision of the final set, never the recall of the pipeline.

The variant landscape

HeuristicRAG variants as choices about the latent structure

Heuristic — a reasoning shortcut that predicts the right answer in the regimes discussed here. It is not a proof and can fail outside them.

The named RAG variants are all interventions on the posterior pη(z∣x)p_\eta(z\mid x) or on the latent space \Zcal\Zcal, within the same probabilistic model.

VariantWhat it changesIn the model
Naive RAG—one-shot pη(z∣x)p_\eta(z\mid x), top-kk, concatenate
Hybrid retrievalscoring functionpη∝exp⁡(λsdense+(1−λ)sBM25)p_\eta \propto \exp(\lambda s_{\text{dense}} + (1-\lambda)s_{\text{BM25}})
Rerankingtruncation of the supporttwo-stage construction of pηp_\eta
Query expansion / HyDEthe conditioning variablereplace xx by x~=h(x)\tilde x = h(x) closer to document space
Corrective RAGconditional re-retrievalevaluate sufficiency, re-draw zz if inadequate
Agentic / iterative RAGsequential latentz1,…,zmz_1,\dots,z_m drawn adaptively, a latent sequence not a latent variable
Graph RAGthe latent space itself\Zcal\Zcal becomes a graph; retrieval returns a subgraph, so multi-hop paths are single latents
Fusion-in-Decoderwhere mixing happensattention over separately encoded documents, not an explicit sum

What this does not say

Two honest caveats about this table.

First, the fact that all of these fit one framework does not mean they are equally good or that the framework predicts which will win. The probabilistic view organises the design space; it does not supply a theory of retrieval quality, and the comparative evidence between variants is empirical and benchmark-dependent.

Second, the iterative and agentic variants strictly leave the single-latent model: with mm adaptive retrieval rounds the object is a latent trajectory, the marginalisation is over sequences, and the error analysis above does not apply directly — each round's failure probability compounds, and there is no clean analogue of the total-variation bound. That is a genuine gap: the systems being built are ahead of the analysis, and the error propagation for multi-round retrieval has not, to this author's knowledge, been characterised.

Problems

RAG-sequence and RAG-token are different modelsProof · Intermediate · 30 min The RAG evidence lower boundProof · Advanced · 30 min Retrieval error propagates into generationProof · Graduate · 1 hour End-to-end recall arithmetic for a RAG systemNumerical · Beginner · 15 min Build and instrument a minimal RAG pipelineCoding · Advanced · 1 day

References

These works are cited as sources to read, not reproduced. All exposition, examples and problems on this site are written independently; where an argument is standard, the citation points to a canonical treatment.

Results stated on this page

Problems (5)

All problems

Papers that use this mathematics