Term frequency, document frequency, and sparse retrieval

← Back to Blog & Articles

Represent a document by the counts of its words.

A query is scored by a weighted sum over the words it shares with the document. The two decisions are how to weight a word across the corpus and how to weight it within a document.

Across the corpus. A word that occurs in nearly every document tells you nothing; a word that occurs in few documents tells you a lot. Sparck Jones (1972) proposed weighting a word by the log of the inverse of its document frequency. The following result puts that weight on a probabilistic footing.

Let R∈{0,1}R\in\{0,1\} indicate whether a document is relevant to a fixed query, and let xt∈{0,1}x_t\in\{0,1\} indicate whether the document contains term tt. The binary independence model assumes that, given RR, the xtx_t are independent. Write pt=ℙ(xt=1∣R=1)p_t=\mathbb{P}(x_t=1\mid R=1) and ut=ℙ(xt=1∣R=0)u_t=\mathbb{P}(x_t=1\mid R=0), and assume pt=utp_t=u_t for terms that are not in the query.

Theorem 4.1 (term weights under binary independence). Under the binary independence model, the posterior odds that a document with term vector xx is relevant are ℙ(R=1∣x)ℙ(R=0∣x)=c⋅exp⁡(∑t∈qxtwt),wt=log⁡pt(1−ut)ut(1−pt),\frac{\mathbb{P}(R=1\mid x)}{\mathbb{P}(R=0\mid x)}\ =\ c\cdot\exp\Big(\sum_{t\in q} x_t\, w_t\Big),\qquad w_t=\log\frac{p_t(1-u_t)}{u_t(1-p_t)}, where cc does not depend on the document. Ranking by posterior odds is therefore ranking by the sum of wtw_t over the query terms that the document contains.

Proof. By Bayes’ rule the posterior odds equal the prior odds times ℙ(x∣R=1)/ℙ(x∣R=0)\mathbb{P}(x\mid R=1)/\mathbb{P}(x\mid R=0). By independence this ratio is ∏t[ptxt(1−pt)1−xt]/[utxt(1−ut)1−xt]\prod_t \big[p_t^{x_t}(1-p_t)^{1-x_t}\big]\big/\big[u_t^{x_t}(1-u_t)^{1-x_t}\big]. Each factor equals 1−pt1−ut(pt(1−ut)ut(1−pt))xt\frac{1-p_t}{1-u_t}\big(\frac{p_t(1-u_t)}{u_t(1-p_t)}\big)^{x_t}. For t∉qt\notin q the assumption pt=utp_t=u_t makes the factor equal to one. The product of the factors 1−pt1−ut\frac{1-p_t}{1-u_t} over t∈qt\in q does not depend on xx, and the remaining product is exp⁡(∑t∈qxtwt)\exp(\sum_{t\in q}x_tw_t). ◼\blacksquare

The theorem has not yet produced an IDF. For that we need values of ptp_t and utu_t. Almost all documents are not relevant, so ut≈nt/Nu_t\approx n_t/N, where ntn_t is the number of documents containing tt and NN is the corpus size. If we know nothing about relevant documents we may set pt=12p_t=\tfrac12, which makes pt/(1−pt)=1p_t/(1-p_t)=1. Then wt=log⁡1−utut=log⁡N−ntntw_t=\log\frac{1-u_t}{u_t}=\log\frac{N-n_t}{n_t}. The usual estimate adds one half to each count to keep the logarithm finite, and the implementation here adds one inside the logarithm so that a term in more than half of the documents gets a small positive weight instead of a negative one: idf(t)=log⁡(1+N−nt+0.5nt+0.5).\mathrm{idf}(t)=\log\Big(1+\frac{N-n_t+0.5}{n_t+0.5}\Big). The weight is large for rare terms and close to zero for ubiquitous ones. the companion book example shows how large the spread is in this corpus: the mean IDF is 7.40 for entity words, 4.18 for topic words, 2.87 for common words and 0.69 for attribute words, which occur in about half of all documents.

Within a document. Raw term frequency over-rewards repetition: a document that says “mitochondria” twenty times is probably not twenty times more relevant than one that says it three times. Robertson and Zaragoza (2009) motivate a saturating function through the 2-Poisson model of “eliteness”: a document is either about a term or not, and term frequency is a noisy signal of which. Once a document has plausibly shown that it is about the term, further occurrences add little evidence. The BM25 score is BM25(q,d)=∑t∈qidf(t)⋅tft,d(k1+1)tft,d+k1(1−b+bℓd/ℓ‾),\mathrm{BM25}(q,d)=\sum_{t\in q}\mathrm{idf}(t)\cdot\frac{\mathrm{tf}_{t,d}\,(k_1+1)}{\mathrm{tf}_{t,d}+k_1\big(1-b+b\,\ell_d/\bar\ell\big)}, where ℓd\ell_d is the length of dd and ℓ‾\bar\ell the mean length. The factor in tf\mathrm{tf} is increasing and concave, equals one at tf=1\mathrm{tf}=1 when ℓd=ℓ‾\ell_d=\bar\ell, and cannot exceed k1+1k_1+1. Its marginal gain from one more occurrence is k1(k1+1)K/(tf+K)2k_1(k_1+1)K/(\mathrm{tf}+K)^2 with K=k1(1−b+bℓd/ℓ‾)K=k_1(1-b+b\ell_d/\bar\ell), which decays quadratically. The parameter b∈[0,1]b\in interpolates between no length normalisation (b=0b=0) and full normalisation (b=1b=1), since a long document has more chances to contain a term by accident.

The saturation table shows the limit in numbers: with k1=1.2k_1=1.2 the factor is 1.96 at tf=10\mathrm{tf}=10 and 2.15 at tf=50\mathrm{tf}=50, against a limit of 2.2.

The parameter grid is a lesson in humility. Across k1∈{0.5,1.2,2,5}k_1\in\{0.5,1.2,2,5\} and b∈{0,0.5,0.75,1}b\in\{0,0.5,0.75,1\} the development nDCG@10 ranges only from 0.5846 to 0.5976, the best cell is k1=1.2k_1=1.2, b=0.5b=0.5, and on the test queries plain TF-IDF cosine (nDCG@10 0.603) is not worse than BM25 (0.599). Here the evidence is one rare word, and a ranking built on one word is insensitive to the shape of the term-frequency curve. That does not mean BM25 does not matter; the last test shows when it does. For each of 300 queries we add a spam document that repeats two of the query’s topic words 40 times each, and count how often it is ranked first. With b=0b=0 the share is 0.170 at k1=0.5k_1=0.5, 0.303 at k1=1.2k_1=1.2, 0.580 at k1=3k_1=3, and 0.750 when saturation is effectively removed (k1=106k_1=10^6, which makes the tf factor linear). Saturation is a defence against term-frequency manipulation, and a partial one. the companion book example returns to what happens when the manipulation is deliberate.

The recall of the sparse methods is capped by vocabulary mismatch: recall@10 is 0.523 for both TF-IDF and BM25, close to the 0.504 of relevant documents that contain the query’s entity word. A word-matching method cannot know that the alias and the name refer to one entity. A dense method can learn it.

Continue with the book

Explore the full treatment, worked examples, and exercises in Applied AI Under Uncertainty.


Companion article for Applied AI Under Uncertainty, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.

Scroll to Top