Skip to content
mlmentorship

TF-IDF and BM25

TF-IDF and BM25 are lexical retrieval baselines for search and RAG. BM25 adds term-frequency saturation and document-length normalization.

Published · 7 min read ·Role-specific ·Intermediate
Reading depth

Summary

Lexical scoring combines analyzed token matches with corpus statistics, not a direct measurement of relevance. BM25 separates diminishing frequency gain from document-length normalization; TF-IDF uses a declared weighting and vector-normalization convention. Neither lexical, dense, nor fused retrieval is guaranteed to win every query slice.

Term frequency and its problem

Raw counts can be replaced with damped weights, such as for positive frequency and zero otherwise. This heuristic limits growth relative to raw count; it does not prove that repeated words are relevant. The log-TF weight is still unbounded, unlike BM25’s fixed-length frequency factor.

Inverse document frequency

One unsmoothed TF-IDF convention uses natural-log inverse document frequency:

Here counts indexed documents containing the analyzed term. Dot-product sums and cosine-normalized TF-IDF vectors define different scorers; state which is intended.

BM25: the production formula

For , , and positive average field length, use:

  • counts term occurrences; and use the index’s field-length convention.
  • controls saturation; the displayed factor approaches at fixed length.
  • controls the length adjustment; removes it.

Sum each distinct analyzed query term once here; repeated-query weighting is a separate convention. An absent term contributes zero, including empty fields. Robertson’s IDF can be negative. Lucene’s log-one-plus variant is and stays positive for indexed terms.

Fix , , and positive IDF one. At average length, frequencies give factors , approaching ceiling . At frequency three, half/average/double length gives .

One query term, two controls

Repeated matches saturate while extra document length can reduce their score

More matches help less, and length changes the same match countWith k1=1.2 and b=0.75, the term-frequency factor saturates. Doubling relative document length reduces the factor at fixed frequency.Count the matches; keep documentlength explicit4 matcheslength 1xscore 1.6928 matcheslength 1xscore 1.9134 matcheslength 2xscore 1.443More matches help less. Extra lengthdilutes the same matches.
Derivation
BM25 term-frequency saturation and document-length normalization Two horizontal bar groups show the BM25 term-frequency factor without IDF, using k one equals 1.2 and b equals 0.75. In an average-length document, one occurrence scores 1.00, three score 1.57, and ten score 1.96, approaching the 2.20 ceiling. Holding frequency at three, a half-average-length document scores 1.76, an average-length document scores 1.57, and a double-average-length document scores 1.29. Thus repeated occurrences have diminishing returns, and the same count contributes less in a longer document. TERM FACTOR ONLY · IDF HELD CONSTANT · k₁ = 1.2 · b = 0.75 1 · SATURATION Average-length document; increase term frequency f. f = 1 1.00 f = 3 1.57 f = 10 1.96 ceiling 2.20 2 · LENGTH NORMALIZATION Three matches in every document; increase document length. 0.5× avg 1.76 1× avg 1.57 2× avg 1.29 Bars use the formula above; multiply each value by that term's IDF.
Read it this way: the upper bars isolate frequency gain below the 2.20 ceiling. The lower bars hold frequency fixed while length changes. Multiply by the chosen term's IDF. Original calculations follow the Stanford IR textbook, Robertson and Zaragoza, and Lucene's documented variant.
Check: Why does term-frequency gain diminish? Why does a longer document reduce the factor at fixed frequency?
Read it this way: With k1=1.2 and b=0.75, the term-frequency factor saturates. Doubling relative document length reduces the factor at fixed frequency.
Do extra nonmatching words improve the same four-match score?

Remove Length Normalization

New task: Stop penalizing field length under a justified field-specific requirement.

Picture and change: Set , keeping the other factors fixed.

Check: All three frequency-three documents now contribute under IDF one, regardless of their length.

Use when: Evaluate the changed field semantics and query slices; normalization is not universally beneficial.

Add Semantic Coverage

New task: Retrieve paraphrases sharing no analyzed query token.

Picture and change: Compare query expansion or dense candidates, then evaluate fusion and exact-identifier protection.

Check: Under RRF with constant sixty, a lexical-only rank-one document scores . A document ranked second lexically and first densely scores , potentially displacing it at a top-one cutoff.

Use when: Candidate coverage, not an assumed superiority of hybrid retrieval, is the measured bottleneck.

TF-IDF vs BM25

TF saturationLength normalizationTunableStatus
TF-IDFConvention-dependent; log-TF is unboundedOptional vector normalizationConfigurable weighting/analysisLexical vector scorer
BM25Displayed factor tends to Explicit via , field policiesLexical ranking scorer

Their scores depend on the analyzer, corpus, field statistics, and normalization policy.

Lexical vs dense (and why hybrid wins)

Lexical (BM25)Dense (embeddings)
MatchesAnalyzed tokensLearned representation similarities
IDs/paraphrases/domain shiftEvaluate task-specific behaviorEvaluate task-specific behavior
IndexTypically inverted postingsExact or approximate vector search

Rank-based RRF and weighted-score fusion use different assumptions; raw scores are not automatically comparable across retrievers. Reranking cannot recover candidates absent from its input set.

What an interviewer expects you to say

Reconstruct the six bar values, name the IDF/analyzer convention, and identify whether candidate coverage or ordering needs improvement.

Common confusions

  • Rarity as relevance. Corpus rarity does not establish semantic importance.
  • Implicit synonym guarantees. BM25 matches analyzed terms; explicit expansion/analyzer policies can introduce synonyms.
  • Negative-IDF confusion. A bounded positive TF factor does not imply increasing total score when its multiplier is negative.

Related: RAG overview, Designing a RAG system that actually works, Approximate nearest neighbors, Word embeddings, Two-tower retrieval.