Language Models for Retrieval

Ranking documents by the probability that a language model estimated from each document would generate the query. The query likelihood model, why an unsmoothed model behaves like Boolean AND, Jelinek-Mercer and Dirichlet smoothing, the derivation showing that smoothing supplies IDF-like weighting and length normalization, Cranfield query 185 scored by hand, and an experiment on all of Cranfield in which query likelihood trails BM25.

The query likelihood model ranks each document by the probability $P(q \mid M_d)$ that a unigram language model estimated from it generates the query. With unsmoothed maximum likelihood estimates it retrieves only documents containing every query term, like Boolean AND: on Cranfield it returns nothing for 216 of 225 queries (MAP 0.010). Smoothing with the collection model, by Jelinek-Mercer or Dirichlet, repairs this, and the rank-equivalent Dirichlet score $\sum_{t} \log(1 + \mathit{tf}_{t,d}/(\mu P(t \mid M_c))) + n \log(\mu/(L_d + \mu))$ shows that smoothing supplies an IDF-like weight and length normalization. On all 225 Cranfield queries the best settings ($\mu$ of 200 to 300, document weight 0.2 to 0.3) reach a MAP of 0.284, below BM25's 0.307 with default settings; the recommended $\mu = 2000$ gives 0.266. On held-out queries the gap persists (0.284 against 0.297).

Cranfield query 185 asks for experimental studies on panel flutter. BM25 ranks the abstracts for it by estimating, term by term, how much each occurrence says about relevance. A searcher who types those four words is usually doing something simpler: guessing words that a good abstract would contain. Can a ranking be built directly on that guess, by asking which abstract would be most likely to produce the query as a sample of its own text?

The language modeling approach to retrieval does this. A language model is a probability distribution over strings of words. The approach estimates one model from each document, then ranks documents by the probability that the document's model generates the query. Ponte and Croft (1998) presented the first experiments with it (Manning, Raghavan, and Schütze, 2008, §12.2.3). Manning, Raghavan and Schütze (2008, chapter 12) give it a chapter of its own, after the vector space model and the probabilistic relevance models of this unit. Later articles on query expansion build on the definitions given here.

Here is the route. First we define a unigram language model and the query likelihood model, and work the textbook's two-document example. Then we see why the unsmoothed model fails, and how two smoothing methods repair it. Next we derive the rank-equivalent form of the score, which shows that smoothing supplies the IDF-like weighting and length normalization that the other models build in by hand. Finally we score query 185, measure the model on all of Cranfield, and place it beside the Binary Independence Model and BM25. The article assumes the probability primer of this unit.

A document as a source of words

Think of each document as a bag from which words are drawn at random, with replacement. Abstract 856, one of the nine relevant to query 185, has 172 tokens after stop words are removed and the rest stemmed, and 16 of them are panel. Drawing one token at random from it gives panel with probability $16/172 \approx 0.093$. A unigram language model is exactly this: a probability $P(t \mid M_d)$ for each term $t$ of the vocabulary, summing to 1, where $M_d$ names the model estimated from document $d$. "Unigram" means each word is drawn independently of the words before it, so the probability of a sequence is the product of the probabilities of its words.

Under that independence assumption, word order does not matter, and the model is a multinomial distribution over the bag of words (Manning, Raghavan, and Schütze, 2008, §12.1.3). The textbook notes that, for unigram models, the name "language model" is partly historical. Richer models condition each word on the previous one or two, but most language modeling work in retrieval has used unigram models (§12.1.2), and they are used throughout this article.

The simplest estimate is the maximum likelihood estimate (MLE): the term's relative frequency in the document,

$$\hat P_{\mathrm{mle}}(t \mid M_d) = \frac{\mathit{tf}_{t,d}}{L_d},$$

where $\mathit{tf}_{t,d}$ is the number of times $t$ occurs in $d$ and $L_d$ is the number of tokens in $d$. (Maximum Likelihood Estimation derives estimates of this kind, and treats the case that matters below, an estimate of exactly zero.)

The query likelihood model

The goal of a ranking is to order documents by how likely each is to be what the searcher wants. The textbook frames this as ranking by $P(d \mid q)$ and applies Bayes' rule (§12.2.1):

$$P(d \mid q) = \frac{P(q \mid d)\, P(d)}{P(q)}.$$

The denominator $P(q)$ is the same for every document and can be dropped. The prior $P(d)$ could encode evidence that a document is generally useful, such as its authority or recency. With a uniform prior it drops out too, and documents are ranked by $P(q \mid d)$, the probability of the query under the document's model. This is the query likelihood model. With a unigram model, the query's probability is a product over its tokens:

$$P(q \mid M_d) = \prod_{t \in q} P(t \mid M_d).$$

A query term that appears twice contributes its probability twice. The multinomial coefficient, which counts the orders in which the query's words could have been drawn, is $L_q! \big/ \prod_t \mathit{tf}_{t,q}!$ for a query of $L_q$ tokens. It is built only from the query's own counts, so it is the same for every document and is ignored.

Ponte and Croft's original model was slightly different: it treated each vocabulary term as present or absent in the query, in the manner of the Bernoulli event model. The multinomial version, used by Miller, Leek and Schwartz (1999) among others, became standard in later work (Manning, Raghavan, and Schütze, 2008, §12.2.3). The textbook also points out a connection with this site's classification unit. The multinomial query likelihood model is a multinomial Naive Bayes model in which every document is its own class.

The textbook's example

Manning, Raghavan and Schütze (2008, Example 12.3) use two one-line documents:

  • $d_1$: Xyzzy reports a profit but revenue is down
  • $d_2$: Quorus narrows quarter loss but revenue decreases further

Each has 8 tokens, so the collection has 16. The query is revenue down. By maximum likelihood, $P(\textit{revenue} \mid M_{d_1}) = 1/8$ and $P(\textit{down} \mid M_{d_1}) = 1/8$, so $P(q \mid M_{d_1}) = 1/64$.

Example (one missing word). The maximum likelihood model gives $d_2$ a probability of zero. The word down does not occur in $d_2$, so $P(\textit{down} \mid M_{d_2}) = 0$, and one zero factor makes the whole product zero. The document is about revenue decreasing, which a reader would count as relevant to revenue down, and the model rules it out entirely.

Why the unsmoothed model fails

Under the maximum likelihood estimate, any document missing any query term receives probability zero. Query likelihood then becomes a conjunctive query: only documents containing every query term are ranked at all. That is Boolean AND with a ranking of the survivors.

Query 185 shows the effect on a real collection. After processing, its terms are experiment, studi, panel and flutter. Only 2 of the 1,400 abstracts contain all four. Both are relevant, so they occupy ranks 1 and 2, and the other seven relevant abstracts are not retrieved; average precision is $(1 + 1)/9 = 0.222$. Over all 225 Cranfield queries, the unsmoothed model returns nothing for 216, and its mean average precision (MAP) is 0.010. Boolean AND in comparing ranking models also returns nothing for those 216 queries, as it must, since both retrieve exactly the documents that contain every query term. The two MAPs agree to three decimals (0.0098 and 0.0102) but not exactly, because Boolean AND lists those documents in document order and query likelihood ranks them.

The zeros are only the visible part of the problem. A word that occurs once in a document has a maximum likelihood probability well above what a second sample of text on the same subject would show; it occurred partly by chance. The textbook makes a stronger point: smoothing "actually implements major parts of the term weighting component" (§12.2.2). The derivation below shows how.

Smoothing with the collection

The repair is to borrow probability from the collection as a whole. The collection model $M_c$ is the relative frequency of each term across all documents:

$$P(t \mid M_c) = \frac{\mathit{cf}_t}{T},$$

where $\mathit{cf}_t$ is the collection frequency of $t$ (its total number of occurrences) and $T$ is the total number of tokens. A term absent from a document should then have a small probability, no larger than its frequency in the collection would suggest.

Jelinek-Mercer smoothing mixes the two models with a fixed weight $\lambda$ between 0 and 1 (Manning, Raghavan, and Schütze, 2008, eq. 12.10):

$$P(t \mid d) = \lambda\, \hat P_{\mathrm{mle}}(t \mid M_d) + (1 - \lambda)\, P(t \mid M_c).$$

Here $\lambda$ is the weight on the document, as in the textbook. Zhai and Lafferty (2001) write the same mixture with $\lambda$ on the collection, so their $\lambda$ is our $1 - \lambda$. A smaller document weight means more smoothing. (Nearest neighbor language models mix two next-token distributions with a fixed weight in the same way.)

Dirichlet smoothing treats the collection model as a prior and the document's counts as evidence (Manning, Raghavan, and Schütze, 2008, eq. 12.11; Zhai and Lafferty, 2001):

$$P(t \mid d) = \frac{\mathit{tf}_{t,d} + \mu\, P(t \mid M_c)}{L_d + \mu}.$$

The parameter $\mu > 0$ acts as a number of imaginary tokens, distributed in proportion to the collection model and added to the document before its relative frequencies are taken. It is the same idea as the add-one smoothing of Naive Bayes, with a nonuniform prior. The probability primer derives that kind of estimate from a Beta prior; the Dirichlet distribution is the Beta's generalization to many outcomes, and the derivation carries over.

Where the Dirichlet estimate comes from. Let $\theta_t$ be the document model's unknown probability of term $t$, with $\sum_t \theta_t = 1$. Take as prior a Dirichlet density proportional to $\prod_t \theta_t^{a_t - 1}$ with $a_t = \mu\, P(t \mid M_c)$, so that the pseudo-counts $a_t$ total $\mu$. The document's tokens, drawn independently, have probability proportional to $\prod_t \theta_t^{\mathit{tf}_{t,d}}$. By Bayes' rule the posterior is proportional to the product of the two, $\prod_t \theta_t^{\mathit{tf}_{t,d} + a_t - 1}$, which is again a Dirichlet density, with each count added to its pseudo-count. The mean of coordinate $t$ of a Dirichlet density with parameters $b_t$ is $b_t / \sum_s b_s$ (Bishop, 2006, appendix B, eq. B.17; for two outcomes this is the Beta mean $a/(a + b)$ proved in the primer). Here $b_t = \mathit{tf}_{t,d} + \mu P(t \mid M_c)$ and $\sum_s b_s = L_d + \mu$, so the posterior mean is the estimate above. Because it is a mean of probability vectors, it sums to 1 over the vocabulary, which the script confirms for two abstracts.

The two methods are closer than they look. Rewriting the Dirichlet estimate,

$$\frac{\mathit{tf}_{t,d} + \mu P(t \mid M_c)}{L_d + \mu} = \frac{L_d}{L_d + \mu} \cdot \frac{\mathit{tf}_{t,d}}{L_d} + \frac{\mu}{L_d + \mu} \cdot P(t \mid M_c),$$

which is Jelinek-Mercer with a document weight $\lambda_d = L_d/(L_d + \mu)$ that depends on the document's length. A long document has more evidence of its own and is smoothed less.

Zhai and Lafferty (2004) study a third method, absolute discounting, which subtracts a constant $\delta$ between 0 and 1 from the count of every term the document contains and gives the freed probability to the collection model:

$$P(t \mid d) = \frac{\max(\mathit{tf}_{t,d} - \delta,\ 0)}{L_d} + \frac{\delta\, U_d}{L_d}\, P(t \mid M_c),$$

where $U_d$ is the number of distinct terms in $d$. Each of the $U_d$ distinct terms gives up $\delta / L_d$, so the total removed is $\delta\, U_d / L_d$, exactly the weight placed on the collection model, and the probabilities still sum to 1. The rest of this article uses the first two methods.

Example (the textbook's documents, smoothed). Jelinek-Mercer smoothing with $\lambda = 1/2$ and Dirichlet smoothing with $\mu = 8$ give the two documents the same probabilities. With $\lambda = 1/2$, revenue has collection probability $2/16$ and down has $1/16$. For $d_1$, $P(\textit{revenue}) = (1/8 + 2/16)/2 = 1/8$ and $P(\textit{down}) = (1/8 + 1/16)/2 = 3/32$, so $P(q \mid d_1) = 3/256$. For $d_2$, $P(\textit{revenue}) = 1/8$ and $P(\textit{down}) = (0 + 1/16)/2 = 1/32$, so $P(q \mid d_2) = 1/256$. These are the textbook's numbers. Dirichlet smoothing with $\mu = 8$ gives exactly the same values, because both documents have $L_d = 8$ and so $\lambda_d = 8/16 = 1/2$. The document $d_2$ now has a nonzero probability, ranked below $d_1$, which contains both words.

What smoothing does to the score

Here is the idea in one sentence: once the score is rewritten as a sum over the query terms a document actually contains, the collection probability appears in the denominator of each term's weight, which acts like an inverse document frequency, and the smoothing parameter leaves behind a per-document factor that acts like length normalization. Zhai and Lafferty (2001, §2; 2004) give the general form, for any smoothing method that gives a term the document lacks a probability proportional to its collection probability, $P(t \mid d) = \alpha_d\, P(t \mid M_c)$, with a factor $\alpha_d$ that may depend on the document. All three methods above are of this kind. The general form is derived first, and then worked for Dirichlet and for Jelinek-Mercer smoothing.

The formal version. Take logs, so the product becomes a sum, and split the query terms by whether the document contains them. For a term with $\mathit{tf}_{t,d} = 0$, the estimate is $\alpha_d P(t \mid M_c)$; for Dirichlet smoothing, setting $\mathit{tf}_{t,d} = 0$ in the formula gives $\alpha_d = \mu/(L_d + \mu)$. So

$$\log P(q \mid d) = \sum_{t \in q,\ \mathit{tf}_{t,d} > 0} \log P(t \mid d) \;+ \sum_{t \in q,\ \mathit{tf}_{t,d} = 0} \log \big(\alpha_d P(t \mid M_c)\big).$$

The second sum runs over the terms the document lacks, which is awkward to compute. Add and subtract $\log(\alpha_d P(t \mid M_c))$ for the matched terms, so that the second sum covers every query term:

$$\log P(q \mid d) = \sum_{t \in q,\ \mathit{tf}_{t,d} > 0} \log \frac{P(t \mid d)}{\alpha_d P(t \mid M_c)} \;+\; n \log \alpha_d \;+\; \sum_{t \in q} \log P(t \mid M_c),$$

where $n$ is the number of query tokens. The last sum is the same for every document, so it can be dropped without changing the ranking. Up to here nothing depended on the smoothing method: this is the general form. For Dirichlet smoothing, the ratio in the first sum simplifies:

$$\frac{(\mathit{tf}_{t,d} + \mu P(t \mid M_c))/(L_d + \mu)}{\mu P(t \mid M_c)/(L_d + \mu)} = 1 + \frac{\mathit{tf}_{t,d}}{\mu\, P(t \mid M_c)}.$$

The rank-equivalent score is therefore

$$\mathrm{score}(q, d) = \sum_{t \in q,\ \mathit{tf}_{t,d} > 0} \log\!\left(1 + \frac{\mathit{tf}_{t,d}}{\mu\, P(t \mid M_c)}\right) + n \log \frac{\mu}{L_d + \mu}.$$

Three familiar components can be read off it:

  • Term frequency. Each matched term's contribution grows with $\mathit{tf}_{t,d}$, logarithmically.
  • An IDF-like weight. The contribution is larger when $P(t \mid M_c)$ is small, so rare terms count for more. Zhai and Lafferty note that the collection model as a reference distribution "has turned out to play a role very similar to the well-known IDF." It is computed from collection frequency rather than document frequency.
  • Length normalization. The last term is negative and grows in magnitude with $L_d$. A long document pays a larger penalty, which offsets the higher counts its length gives it.

Jelinek-Mercer. For a term the document lacks, the Jelinek-Mercer estimate is $(1 - \lambda)\, P(t \mid M_c)$, so $\alpha_d = 1 - \lambda$ for every document. For a matched term the ratio in the general form is

$$\frac{\lambda\, \mathit{tf}_{t,d}/L_d + (1 - \lambda)\, P(t \mid M_c)}{(1 - \lambda)\, P(t \mid M_c)} = 1 + \frac{\lambda\, \mathit{tf}_{t,d}}{(1 - \lambda)\, L_d\, P(t \mid M_c)}.$$

The term $n \log \alpha_d = n \log(1 - \lambda)$ is now the same for every document and drops out with the other constant, leaving

$$\mathrm{score}_{\mathrm{JM}}(q, d) = \sum_{t \in q,\ \mathit{tf}_{t,d} > 0} \log\!\left(1 + \frac{\lambda}{1 - \lambda} \cdot \frac{\mathit{tf}_{t,d}}{L_d} \cdot \frac{1}{P(t \mid M_c)}\right).$$

The same three components appear, arranged differently. Term frequency enters as the relative frequency $\mathit{tf}_{t,d}/L_d$, so length normalization is inside each term's contribution and not a separate penalty. The factor $1/P(t \mid M_c)$ is the IDF-like weight. The ratio $\lambda/(1 - \lambda)$ sets how much a match counts against the background. The script checks both rank-equivalent forms against the full log probability for all 542 abstracts that contain a term of query 185.

Example (a common misstatement). "Smoothing is only a fix for zero probabilities." The derivation shows more. Without the collection model in the denominator there would be no IDF-like weight at all: the unsmoothed score $\sum_t \log(\mathit{tf}_{t,d}/L_d)$ treats a match on experiment and a match on flutter alike. The precise statement is that smoothing does two jobs, removing the zeros and supplying the term weighting, and the second changes the ranking even among documents that contain every query term.

The rewritten form also makes the model efficient: only documents that contain at least one query term need to be scored, as with an inverted index and BM25. As a check on the algebra, for abstract 858 and query 185 with $\mu = 2000$, the full log probability is $-21.870$. The rank-equivalent score is $2.718$, and the dropped constant $\sum_t \log P(t \mid M_c)$ is $-24.588$. They add up to $-21.870$, as they should.

Query 185 by hand

The Cranfield collection, processed as in the rest of this unit, has 156,309 tokens. The collection model for the four query terms:

Term Collection frequency $P(t \mid M_c)$ Document frequency
experiment 515 0.003295 339
studi 373 0.002386 240
panel 231 0.001478 45
flutter 282 0.001804 56

With $\mu = 2000$, a single occurrence of flutter contributes $\log(1 + 1/(2000 \times 0.001804)) = 0.245$, and a single occurrence of experiment contributes 0.141. The rarer term counts for more, as the derivation predicted.

Take abstract 856, with 172 tokens. It contains experiment 4 times, studi 5, panel 16 and flutter 11. For experiment, $\mu P(t \mid M_c) = 2000 \times 0.003295 = 6.590$, and the contribution is $\log(1 + 4/6.590) = \log 1.607 = 0.474$. The four contributions are 0.474, 0.717, 1.858 and 1.398. The length term is $4 \log(2000/2172) = -0.330$. The score is $4.118$, the highest in the collection.

The top 10 under Dirichlet smoothing with $\mu = 2000$, with each document's rank under BM25 ($k_1 = 1.2$, $b = 0.75$). Values are rounded to three decimals, so a row may not add exactly; R marks the relevant abstracts.

Rank Abstract Length tf (exp., studi, panel, flutter) Matched-term sum Length term Score BM25 rank
1 856 R 172 4, 5, 16, 11 4.447 −0.330 4.118 1
2 857 R 175 3, 2, 16, 7 3.661 −0.336 3.326 2
3 766 R 192 4, 0, 14, 10 3.548 −0.367 3.182 3
4 859 R 195 0, 0, 16, 9 3.109 −0.372 2.737 9
5 858 R 149 3, 0, 11, 7 3.005 −0.287 2.718 5
6 658 166 1, 0, 13, 6 2.806 −0.319 2.488 7
7 948 142 0, 0, 11, 7 2.630 −0.274 2.356 11
8 390 90 0, 2, 5, 5 2.210 −0.176 2.034 4
9 391 91 0, 0, 6, 7 2.187 −0.178 2.009 10
10 1008 R 58 1, 0, 6, 4 1.996 −0.114 1.882 6

The two models agree on the top three and on most of the top ten. Their disagreements follow one pattern.

Example (repetition explains both moves). Abstract 859 contains neither experiment nor studi, yet it rises from rank 9 under BM25 to rank 4. Abstract 390 contains three of the four terms and falls from 4 to 8. The way the two scoring functions treat repetition explains both moves. Abstract 859 uses panel 16 times, and abstract 390 uses it 5 times. Under query likelihood with $\mu = 2000$, panel contributes 0.990 at 5 occurrences and 1.858 at 16, a ratio of 1.88. BM25's term frequency factor, for a document of average length, is 1.774 at 5 occurrences and 2.047 at 16, a ratio of 1.15. BM25 saturates: after a few occurrences, more add little. Query likelihood grows with the logarithm of the count and keeps rewarding repetition. Here that helped, since 859 is relevant, and BM25's extra credit to 390 for a third matching term went to a nonrelevant abstract. Over the whole ranking the two effects nearly cancel: average precision is 0.785 for query likelihood and 0.784 for BM25.

An experiment on Cranfield

Query 185 is one query. The experiment below runs query likelihood on all 225 Cranfield queries, with the setup of comparing ranking models: the same tokenization, stop list and stemmer, the top 1,000 documents per query, and documents that contain at least one query term. It sweeps the Dirichlet prior $\mu$ from 50 to 10,000 and the Jelinek-Mercer document weight $\lambda$ from 0.1 to 0.9.

Mean average precision on all 225 Cranfield queries as the amount of smoothing changes. Left: Dirichlet smoothing, with the prior mu on a log scale; more smoothing is to the right. Right: Jelinek-Mercer smoothing; more smoothing is to the left. Notice that both curves peak at the same MAP, 0.284, and that the commonly recommended mu of 2000 lies well down the right-hand slope. Neither curve reaches BM25's 0.307, the dashed line.
Mean average precision on all 225 Cranfield queries as the amount of smoothing changes. Left: Dirichlet smoothing, with the prior mu on a log scale; more smoothing is to the right. Right: Jelinek-Mercer smoothing; more smoothing is to the left. Notice that both curves peak at the same MAP, 0.284, and that the commonly recommended mu of 2000 lies well down the right-hand slope. Neither curve reaches BM25's 0.307, the dashed line.

The results, rounded to three decimals:

Model MAP P@10 nDCG@10
BM25 ($k_1 = 1.2$, $b = 0.75$) 0.307 0.234 0.368
Dirichlet, $\mu = 300$ 0.284 0.220 0.351
Dirichlet, $\mu = 2000$ 0.266 0.200 0.320
Jelinek-Mercer, $\lambda = 0.3$ 0.284 0.215 0.347
Jelinek-Mercer, $\lambda = 0.5$ 0.281 0.218 0.347
Unsmoothed (maximum likelihood) 0.010

Several observations follow.

  • Smoothing is not optional. Without it, the model returns nothing for 216 queries. With any of the settings tried, its MAP lies between the BIM's 0.220 and TF-IDF's 0.294.
  • The best $\mu$ is far below 2000. Zhai and Lafferty (2004) report that on their TREC collections the optimal prior "appears to have a wide range (500–10000) and usually is around 2,000". On Cranfield the best values are 200 and 300, with MAP falling steadily as $\mu$ grows beyond 500 (0.266 at 2000, 0.235 at 10,000). One plausible reason is document length. $\mu$ is measured in tokens, and a Cranfield abstract averages about 112 tokens after processing. Zhai and Lafferty's collections were full news and web documents, mostly far longer. With $\mu = 2000$, an average abstract gets a document weight $\lambda_d$ of about $112/2112 \approx 0.05$, so its own counts barely register. Chosen without any queries, as the value under which the abstracts themselves are most probable, $\mu$ is 114.6, close to that average length (Choosing a Prior).
  • Verbose queries want heavy smoothing in Jelinek-Mercer. Zhai and Lafferty found the best Jelinek-Mercer setting to depend on query type: a collection weight of about 0.1 for short title queries, and about 0.7 for long verbose ones. Cranfield's queries are full sentences, such as "what similarity laws must be obeyed when constructing aeroelastic models of heated high speed aircraft". The best document weights here are 0.2 and 0.3, which is a collection weight of 0.7 to 0.8, in line with their finding for verbose queries.
  • Query likelihood trails BM25 on Cranfield. Even the best settings, chosen on the very queries they are reported on, reach 0.284 against BM25's 0.307 with default settings, and also fall short of TF-IDF's 0.294. With $\mu = 2000$, query likelihood is worse than BM25 on 159 queries and better on 59 (paired randomization test, $p < 0.001$).

To choose settings fairly, the queries were split as in comparing ranking models into 112 development and 113 test queries. The development queries chose $\mu = 200$ and $\lambda = 0.2$. On the test queries both reached a MAP of 0.284, against 0.297 for BM25 with default settings ($p = 0.045$ for the Dirichlet run). The gap is smaller than on the full set, but it does not close.

Is the language modeling approach worse than BM25?

It would be easy to read the last row as a verdict on the model. That reading goes too far. The textbook reports that recent work had shown the approach to be very effective, beating TF-IDF and BM25 weights, while adding that there was perhaps still insufficient evidence of an advantage large enough to justify replacing a well-tuned vector space system (Manning, Raghavan, and Schütze, 2008, §12.3). Ponte and Croft (1998) reported an 11-point average precision of 0.2233 against 0.1868 for the INQUERY TF-IDF baseline on TREC topics 202 to 250, as summarized in the textbook's Figure 12.4.

Cranfield is one small collection of short abstracts with long, sentence-like queries, and it differs from those collections in ways the comparison is sensitive to. Query 185 points to one candidate explanation, the absence of saturation, which helps some queries and hurts others. Another is the IDF-like component: query likelihood weighs terms by collection frequency, and BM25 by document frequency. This experiment does not separate those effects. What it does show is that the ranking produced by a principled model still depends on estimation choices, here the amount and form of smoothing, as much as on the principle.

The score is also useful in combination. Learning to Rank makes query likelihood with $\mu = 200$ one of eight features of a learned ranker. Re-ranking BM25's top 100 candidates on its own, it reaches a MAP of 0.287, behind only BM25, proximity and TF-IDF, and its negative learned weight reflects its strong correlation with BM25 and TF-IDF, not a lack of value.

Relevance, revisited

The Probability Ranking Principle ranks by the probability of relevance, and the Binary Independence Model estimates that probability from a model of relevant and nonrelevant documents. Query likelihood mentions no relevance variable at all. A common misreading follows: that the language modeling approach abandons relevance and ranks by something unrelated to it.

The textbook calls the absence of an explicit relevance variable the main apparent difference from the BIM, and adds that "this may not be the correct way to think about things" (§12.3). Lafferty and Zhai (2003) argue that a unified account can be given of the probabilistic semantics underlying both approaches (Manning, Raghavan, and Schütze, 2008, §12.5). The textbook also notes that reversing the direction, generating the document from a model of the query, leads with appropriate modeling choices to the BIM (§12.4). The textbook's justification for the model is a story about relevance: the searcher imagines a document that would satisfy the need and draws query words from it. Ranking by $P(q \mid d)$ then ranks documents by how plausibly each is the one the searcher had in mind.

The lack of an explicit relevance variable has a practical cost all the same. The BIM re-estimates its weights directly from judged documents, as in relevance feedback. Query likelihood has nothing comparable to re-estimate, so the textbook lists relevance feedback as hard to integrate into the basic model (§12.3).

The divergence view

Query likelihood has a second reading, which the extensions below and query expansion rely on. Here is the idea: estimate a language model from the query as well as from each document, and rank documents by how little the document's model diverges from the query's. Lafferty and Zhai (2001) proposed this, with the Kullback–Leibler divergence as the measure:

$$\mathrm{KL}(M_q \,\|\, M_d) = \sum_t P(t \mid M_q) \log \frac{P(t \mid M_q)}{P(t \mid M_d)}.$$

The divergence is zero when the two models agree and positive otherwise, so documents are ranked by increasing divergence, that is, by decreasing $-\mathrm{KL}$.

The formal version. Split the logarithm of the ratio:

$$-\mathrm{KL}(M_q \,\|\, M_d) = \sum_t P(t \mid M_q) \log P(t \mid M_d) \;-\; \sum_t P(t \mid M_q) \log P(t \mid M_q).$$

The second sum is the entropy of the query model. It does not involve the document, so it is the same for every document and can be dropped. What remains, $\sum_t P(t \mid M_q) \log P(t \mid M_d)$, is the ranking function. Now let the query model be the maximum likelihood estimate from the query alone, $P(t \mid M_q) = \mathit{tf}_{t,q} / n$ for a query of $n$ tokens. Then

$$\sum_t \frac{\mathit{tf}_{t,q}}{n} \log P(t \mid M_d) = \frac{1}{n} \sum_{t \in q} \log P(t \mid M_d) = \frac{1}{n} \log P(q \mid M_d),$$

where the middle sum runs over the query's tokens, each term counted as often as it occurs. Dividing by the constant $n$ changes no order. So ranking by divergence from a maximum likelihood query model is exactly ranking by query likelihood, with the same smoothed document model.

For abstract 858 and query 185 with $\mu = 2000$, $\log P(q \mid d) = -21.870$ and the query model gives each of the four terms probability $\tfrac14$, with entropy $\log 4 = 1.386$. The divergence is $21.870/4 - 1.386 = 5.467 - 1.386 = 4.081$, which the script confirms from the definition. For abstract 856 it is 3.731, smaller, as its higher rank requires.

The value of the second reading is that the query model need not be the maximum likelihood one. It can give weight to terms the searcher did not type, for example terms estimated from feedback documents, and the ranking function $\sum_t P(t \mid M_q) \log P(t \mid M_d)$ uses them with no further change. That is how relevance models enter in query expansion. The same divergence, taken between a query model built from the documents that match the query and the collection model, is the clarity score of Query Performance Prediction, which predicts how well a query will be answered.

Extensions

The extensions of the basic model add what the unigram query likelihood model leaves out (Manning, Raghavan, and Schütze, 2008, §12.4):

  • Comparing models. The divergence ranking of Lafferty and Zhai (2001), derived above. With a query model estimated from the query alone, it ranks documents exactly as query likelihood does. Its value is that the query model can be improved, for example from feedback documents.
  • Relevance models. Lavrenko and Croft (2001) estimate a model of the relevant documents' language from the top-ranked documents, and use it in place of the query's own words. This brings pseudo-relevance feedback into the framework, and its RM3 variant is worked on Cranfield in query expansion.
  • Translation models. Berger and Lafferty (1999) let a document generate a query word it does not contain through a translation probability from a word it does contain, such as decreases to down. This addresses vocabulary mismatch inside the model. Learned sparse retrieval attacks the same mismatch outside it: a trained model sets each document's term weights and adds weighted terms the text lacks, still served from an inverted index.
  • Structured queries. Metzler and Croft (2004) estimated the beliefs of the inference network model from smoothed language models, keeping INQUERY's query operators. The combined model was implemented in the Indri search engine (Croft, Metzler, and Strohman, 2010, chapter 7, References and Further Reading).

The name can mislead a modern reader. "Language model" now usually means a large neural network that predicts the next word from a long context. The models of this article are counts and their ratios, estimated from a single document, with one parameter. They share the name and the central idea, a probability distribution over text, but little else.

Worked problems

Problems 1 to 3 are Exercises 12.6, 12.7 and 12.9 of the course text (Manning, Raghavan, and Schütze, 2008). Problem 4 is posed on query 185. The script recomputes every number, the first three in exact fractions.

Problem 1 (Exercise 12.6 of the course text). A language model is estimated from the training text the martian has landed on the latin pop sensation ricky martin. (a) Under a maximum likelihood unigram model, find $P(\textit{the})$ and $P(\textit{martian})$. (b) Under a maximum likelihood bigram model, find $P(\textit{sensation} \mid \textit{pop})$ and $P(\textit{pop} \mid \textit{the})$.

(a) The text has 11 tokens. The occurs twice and martian once, so $P(\textit{the}) = 2/11 \approx 0.182$ and $P(\textit{martian}) = 1/11 \approx 0.091$.

(b) A bigram model conditions each word on the one before it, and its maximum likelihood estimate is the number of times the pair occurs divided by the number of times the first word occurs. Pop occurs once and is followed by sensation, so $P(\textit{sensation} \mid \textit{pop}) = 1/1 = 1$. The occurs twice, followed by martian and by latin and never by pop, so $P(\textit{pop} \mid \textit{the}) = 0/2 = 0$. Both answers show the weakness of unsmoothed estimates from little text: one continuation is certain and another impossible, on the evidence of a single sentence.

Problem 2 (Exercise 12.7 of the course text). A collection has four documents:

Document Text
1 click go the shears boys click click click
2 click click
3 metal here
4 metal shears click here

Build the query likelihood model with a mixture of the document model and the collection model, each weighted 0.5, both estimated by maximum likelihood. Find the probabilities of the queries click, shears and click shears for each document, and rank the documents for each query.

The documents have 8, 2, 2 and 4 tokens, 16 in all. Click occurs $4 + 2 + 0 + 1 = 7$ times and shears $1 + 0 + 0 + 1 = 2$ times, so the collection model gives $P(\textit{click} \mid M_c) = 7/16$ and $P(\textit{shears} \mid M_c) = 2/16$. Each smoothed probability is $\tfrac12 \cdot \mathit{tf}/L_d + \tfrac12 \cdot P(t \mid M_c)$. For example, for click in document 1 it is $\tfrac12 \cdot \tfrac48 + \tfrac12 \cdot \tfrac{7}{16} = \tfrac{15}{32}$, and for shears in document 4 it is $\tfrac12 \cdot \tfrac14 + \tfrac12 \cdot \tfrac{2}{16} = \tfrac{3}{16}$. The query click shears multiplies the two.

Query Doc 1 Doc 2 Doc 3 Doc 4
click 15/32 = 0.4688 23/32 = 0.7188 7/32 = 0.2188 11/32 = 0.3438
shears 1/8 = 0.1250 1/16 = 0.0625 1/16 = 0.0625 3/16 = 0.1875
click shears 15/256 = 0.0586 23/512 = 0.0449 7/512 = 0.0137 33/512 = 0.0645

The rankings are: for click, documents 2, 1, 4, 3; for shears, documents 4, 1, and then 2 and 3 tied; for click shears, documents 4, 1, 2, 3. Document 3 contains neither word and still has a nonzero probability, the product of the two background terms. Document 4 wins the two-word query although document 1 contains click four times, because document 4 is short and a quarter of it is shears, the rarer word.

Problem 3 (Exercise 12.9 of the course text). In the mixture model, a term's probability combines its frequency in the document with its frequency in the collection. Explain how this implements a form of term weighting, with a numeric example.

The mixture gives a document credit for a query term according to the ratio of the matched probability to the unmatched one, and that ratio is larger for rarer terms. A document that lacks term $t$ gets the background value $(1 - \lambda) P(t \mid M_c)$ for it. A document that contains it gets that plus $\lambda\, \mathit{tf}/L_d$. Since every document receives at least the background value, what separates documents is the factor by which a match multiplies it, $1 + \frac{\lambda\, \mathit{tf}/L_d}{(1 - \lambda) P(t \mid M_c)}$, the quantity inside the logarithm of the Jelinek-Mercer score above. The same match is worth more when $P(t \mid M_c)$ is small. That is the behavior of inverse document frequency, obtained here from collection frequency.

For the example take the collection model of Problem 2 and two documents of four tokens, one containing click once and not shears, the other containing shears once and not click. A match adds $\tfrac12 \cdot \tfrac14 = \tfrac18$ to the background in each case.

  • For click the background is $\tfrac12 \cdot \tfrac{7}{16} = \tfrac{7}{32}$ and the matched value $\tfrac{11}{32}$: a match multiplies the probability by $11/7 = 1.57$.
  • For shears the background is $\tfrac{1}{16}$ and the matched value $\tfrac{3}{16}$: a match multiplies it by 3.

For the query click shears, the first document scores $\tfrac{11}{32} \times \tfrac{1}{16} = \tfrac{11}{512} = 0.0215$ and the second $\tfrac{7}{32} \times \tfrac{3}{16} = \tfrac{21}{512} = 0.0410$. Each matched one query word once, at the same length, and the one that matched the rarer word scores nearly twice as high.

Problem 4. Score abstract 856 for query 185 under Jelinek-Mercer smoothing with $\lambda = 0.5$, using the rank-equivalent form. The abstract has 172 tokens and contains experiment 4 times, studi 5, panel 16 and flutter 11; the collection probabilities are in the table above.

With $\lambda = 0.5$ the factor $\lambda/(1 - \lambda)$ is 1, so each matched term contributes $\log\bigl(1 + \mathit{tf}_{t,d} / (L_d\, P(t \mid M_c))\bigr)$.

Term tf $L_d\, P(t \mid M_c)$ Ratio Contribution
experiment 4 $172 \times 0.003295 = 0.5667$ 7.058 $\log 8.058 = 2.087$
studi 5 $172 \times 0.002386 = 0.4104$ 12.182 $\log 13.182 = 2.579$
panel 16 $172 \times 0.001478 = 0.2542$ 62.945 $\log 63.945 = 4.158$
flutter 11 $172 \times 0.001804 = 0.3103$ 35.449 $\log 36.449 = 3.596$

The score is $2.087 + 2.579 + 4.158 + 3.596 = 12.419$, the highest of any abstract for this query, so abstract 856 is first under this smoothing too. The middle column is the number of occurrences the collection model expects in 172 tokens, and each ratio says how many times that expectation the abstract exceeds: 63 times for panel. For the whole query, this setting gives an average precision of 0.739, against 0.785 for Dirichlet smoothing with $\mu = 2000$. That is a difference on one query, not a comparison of the methods.

What we have learned

A document can be treated as a sample from a language model, and documents ranked by the probability that their models generate the query. Without smoothing, that probability is zero for any document missing a query term, and the model collapses into Boolean AND: on Cranfield it returned nothing for 216 of 225 queries. With smoothing, the score becomes a sum over matched terms of a weight that grows with term frequency and falls with collection frequency, plus a length penalty. That is TF-IDF and length normalization, derived rather than assumed. On query 185 it matched BM25 almost exactly. Over all of Cranfield the best smoothing gave a MAP of 0.284, below BM25's 0.307, and the best $\mu$ was far smaller than the value recommended for longer documents.

Every model so far scores one term at a time and ranks documents for the query the searcher typed. Term dependence models establish how a ranking function can also score the proximity of query terms. Relevance feedback then establishes how a searcher's judgments of a few results become a better query.

References

  • Berger, A., and Lafferty, J. (1999). Information retrieval as statistical translation. In Proceedings of SIGIR 1999, 222–229. record
  • Bishop, C. M. (2006). Pattern Recognition and Machine Learning, appendix B. Springer. record · 10 pages
  • Croft, W. B., Metzler, D., and Strohman, T. (2010). Search Engines: Information Retrieval in Practice, section 7.4. Addison-Wesley. record · 12 pages
  • Lafferty, J., and Zhai, C. (2001). Document language models, query models, and risk minimization for information retrieval. In Proceedings of SIGIR 2001, 111–119. record
  • Lafferty, J., and Zhai, C. (2003). Probabilistic relevance models based on document and query generation. In W. B. Croft and J. Lafferty (Eds.), Language Modeling for Information Retrieval, 1–10. Kluwer Academic Publishers. record
  • Lavrenko, V., and Croft, W. B. (2001). Relevance-based language models. In Proceedings of SIGIR 2001, 120–127. record · 3 pages
  • Manning, C. D., Raghavan, P., and Schütze, H. (2008). Introduction to Information Retrieval, chapter 12. Cambridge University Press. record · 102 pages
  • Metzler, D., and Croft, W. B. (2004). Combining the language model and inference network approaches to retrieval. Information Processing and Management, 40(5), 735–750. record · 2 pages
  • Miller, D. R. H., Leek, T., and Schwartz, R. M. (1999). A hidden Markov model information retrieval system. In Proceedings of SIGIR 1999, 214–221. record
  • Ponte, J. M., and Croft, W. B. (1998). A language modeling approach to information retrieval. In Proceedings of SIGIR 1998, 275–281. record · 2 pages
  • Zhai, C., and Lafferty, J. (2001). A study of smoothing methods for language models applied to ad hoc information retrieval. In Proceedings of SIGIR 2001, 334–342. record
  • Zhai, C., and Lafferty, J. (2004). A study of smoothing methods for language models applied to information retrieval. ACM Transactions on Information Systems, 22(2), 179–214. record · 2 pages

Reviewed: verified, 16 Nov 2026 · checked against sources, 23 Nov 2026 · citations checked, 16 Nov 2026 · editorial review, 23 Nov 2026 · 10 revisions