What is BM25? How does it work?
- User
What is BM25? How does it work?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
What is BM25? How does it work?
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
BM25 (Best Match 25) is a popular and effective ranking function used by search engines and information retrieval systems to estimate the relevance of documents to a given search query. It's a "bag-of-words" model, meaning it considers the presence and frequency of query terms in documents, but not their order or proximity (in its basic form).
It's an evolution of earlier probabilistic models and the TF-IDF (Term Frequency-Inverse Document Frequency) weighting scheme, adding important refinements like term frequency saturation and document length normalization.
How BM25 Works - The Core Idea:
The BM25 score for a document D given a query Q (composed of terms q1, q2, ..., qn) is calculated by summing the scores for each query term qi found in the document.
The formula for the score of a single query term qi in document D is:
Score(D, Q) = Σ [ IDF(qi) * ( (f(qi, D) * (k1 + 1)) / (f(qi, D) + k1 * (1 - b + b * (|D| / avgdl))) ) ]
Let's break down each component:
Σ (Summation):
qi present in the document.IDF(qi) (Inverse Document Frequency of query term qi):
IDF(qi) = log( (N - n(qi) + 0.5) / (n(qi) + 0.5) + 1 )
N: Total number of documents in the collection.n(qi): Number of documents containing the query term qi.+0.5 and +1 are smoothing terms to prevent division by zero or log of zero/negative values, especially for terms not in the corpus or present in all documents.n(qi) is small (rare term), IDF is high. If n(qi) is large (common term), IDF is low.f(qi, D) (Term Frequency of qi in Document D):
qi appears in the specific document D.qi in D.The TF Component (with Saturation and Length Normalization):
(f(qi, D) * (k1 + 1)) / (f(qi, D) + k1 * (1 - b + b * (|D| / avgdl)))
This is the most complex part and introduces two key improvements over simple TF:
k1 (Term Frequency Saturation Parameter):
k1 dampens this effect.1.2 and 2.0.k1 models this diminishing return. A lower k1 means TF saturates faster.b (Document Length Normalization Parameter):
D relative to the average document length in the collection (avgdl). Longer documents naturally have a higher chance of containing query terms, so they might be unfairly favored without normalization.0.75. b=0 means no length normalization; b=1 means full normalization.|D| (length of document D) is greater than avgdl (average document length), the denominator increases, reducing the TF component's contribution (penalizing longer documents).|D| is less than avgdl, the denominator decreases, increasing the TF component's contribution (favoring shorter, concise documents that still contain the term).|D| (Length of Document D):
D.avgdl (Average Document Length):
Putting it all together (Intuition):
For each query term:
f(qi, D)).k1).b, |D|, avgdl), penalizing very long documents that might contain the term by chance and boosting shorter documents where the term's presence is more significant.The final BM25 score for a document is the sum of these weighted term scores. Documents with higher BM25 scores are considered more relevant to the query.
Advantages of BM25:
k1 and b can be tuned (often empirically) for optimal performance on a specific dataset.Limitations of BM25:
k1 and b can sometimes require experimentation.Despite its limitations, BM25 remains a strong baseline in information retrieval and is often a core component in more complex ranking systems.