TF-IDF and BM25 Are Exact KL Divergences
Abstract
TF-IDF and BM25 are two of the most widely used methods for scoring query–document relevance, yet neither has a standard probabilistic derivation that justifies it as a statistical method within a unified framework. We address this gap by showing that both scoring methods admit an exact interpretation as Kullback-Leibler divergences between two probability models. We treat the BM25 variant that includes the correction in the IDF term, which is the one used in practice, and also discuss the original BM25 formulation without that correction. The resulting framework provides a common theoretical basis for TF-IDF and BM25, clarifies what they measure, and allows them to be compared theoretically with other information retrieval methods rather than only experimentally.
1 Introduction
TF-IDF and BM25 are two ways of calculating the relevance of a query to a document from a target corpus Robertson et al. (1994) Robertson and Zaragoza (2009) Manning et al. (2008). Both methods use token frequencies and document lengths as key statistics in their calculation.
Mathematically speaking, suppose that we have a vocabulary of tokens and define a document as a function where for some is the total number of times occurs in document . Then, we define as the length of the document . Finally, suppose that we have a corpus of documents. We define as the frequency of the token in the entire corpus. Formally, the definition of the Term Frequency-Inverse Document Frequency (TF-IDF) scoring method for a given token and document index is as follows:
Meanwhile, the formal definition of the Okapi Best Matching 25 (BM25) scoring method for a given token and document is as follows, where the term frequency saturation and document length normalisation are modifiable hyper-parameters and is the average document length:
The version of BM25 we focus on in this paper is the one used in practice, such as in production-grade document search software Milvus (2025). We also discuss the original BM25 formula from Robertson et al. (1994) in section 3.3.
The problem behind these two scoring methods is the lack of an exact mainstream probabilistic derivation of both that would justify them as statistical methods within a unified probabilistic framework rather than as designed heuristics. This is a problem because there is no common theoretical basis on which to compare TF-IDF and BM25, as well as other information retrieval methods similar to them; their practical utility can only be compared empirically.
In this short paper, we demonstrate that each of TF-IDF and BM25 is actually an exact Kullback-Leibler divergence between two specific probability models. The KL divergence has long been used in IR, but to our knowledge, it has not been shown that the exact TF-IDF and BM25 term scores arise as KL divergences under a unified model.
The consequence of the paper is an exact probabilistic model that refines the interpretation of what the two scoring methods measure, since the KL divergence has a precise physical interpretation.
In section 2, we cover the mathematical theory of KL divergence and how to derive both score functions from it. Finally, in section 3, we give a unified interpretation of what TF-IDF and BM25 represent under our divergence framework, as well as what the original version of BM25 without the correction in the logarithm stands for.
2 Theory
2.1 Kullback-Leibler Divergence
Given a measurable space , where is the state space and is the event space, a sigma algebra on subsets of , and two probability measures and defined on it such that for all (i.e. is absolutely continuous with respect to ), the Kullback-Leibler divergence of with respect to is given by the following formula:
Where is the -almost surely unique Radon-Nikodym derivative of with respect to . When working with laws of random variables, we use specialised notation. Suppose and are random variables on , then we define for any probability measure and . For conditional divergences, we adopt the following notation: . These notational definitions can be intuitively extended for multiple variables. Finally, we mention the chain rule for divergences:
The divergence has the physical interpretation of being the average number of bits of information gained when your initial guess about the system changes to , given that is the actual probability distribution of the system (MacKay, 2003, p. 98).
2.2 Term Frequency-Inverse Document Frequency
To define TF-IDF as an exact divergence, we first define four random variables on .
- 1.
The document index:
- 2.
The token:
- 3.
The event
- 4.
The event
Next, we state that . We set and independent under and set:
We finally derive the following conditional divergence:
From here, we can confidently set our probabilities:
- •
- •
We derive TF-IDF exactly.
We can set to any query distribution, such as , where is a query document, which gives us the total relevance score of some query to the document :
2.3 Okapi Best Matching 25
To define BM25 as an exact divergence, we use the exact same four variables as for TF-IDF and enforce the same probabilistic constraints between them. However, this time we set the following:
The negative log of this probability is exactly the IDF term in BM25:
To define , we use a surrogate probability model on the same measurable space . Let , where the event is defined as . Then we use Bayes rule on this conditional law to re-express it as follows:
We set the following:
- •
- •
- •
- •
- •
- •
- •
- •
Where the event is defined as . This substitution yields us the following probability, assuming :
| (1) | ||||
| (2) |
We also assume that if . Multiplying this probability by gives exactly the TF term in BM25. We derive BM25 exactly up to a scalar multiple.
Using the query distribution defined in the TF-IDF section, we get the following total relevance score of some query to the document :
3 Interpretation
3.1 Score function
The score function for both TF-IDF and BM25 indicates how much the query document consists of uncommon keywords contained in document . In terms of the divergence, it shows how strongly does the guess that a keyword status affects its rarity differ from the naive assumption that keywords are just as common as any word.
When the query consists of rare keywords from the target document, the query is very relevant to the document and the score is high. Otherwise, the score is lower.
3.2 Events
We deliberately made the interpretations of the events and arbitrary, but as logically defined as possible given their probabilistic values, as the original mathematical frameworks for TF-IDF and BM25 do not give concrete definitions for what it means for a word to be a keyword, to be common and for a document to be verbose.
3.3 Original BM25 definition
The IDF term of the original definition of BM25 is given by the following expression:
This term is achieved by defining another probability model on . This model is the adversary to and differs from the original in that it sets the following counter-intuitive probability:
Then we redefine our score function as the following adversarial divergence form:
This new scoring function aligns with the original BM25 scoring method Robertson et al. (1994) and can be interpreted as a measure of relevance that not only challenges the relationship between keyword status and rarity, but also the counter-intuitive hypothesis that keyword status decreases word rarity represented by the adversary probability model .
References
- Information theory, inference, and learning algorithms. Cambridge University Press, Cambridge, UK. External Links: ISBN 978-0521642989 Cited by: §2.1.
- Introduction to information retrieval. Cambridge University Press, Cambridge, UK. External Links: ISBN 978-0521865715 Cited by: §1.
- BM25. Note: Milvus DocumentationVersion 2.5.x External Links: Link Cited by: §1.
- Okapi at TREC-3. In Proceedings of the Third Text REtrieval Conference (TREC-3), NIST Special Publication, Vol. 500-225, pp. 109–126. Cited by: §1, §1, §3.3.
- The probabilistic relevance framework: BM25 and beyond. Foundations and Trends® in Information Retrieval 3 (4), pp. 333–428. External Links: Document Cited by: §1.