You're viewing the readable version of this site. The interactive extras (search, diagrams, read-aloud) need JavaScript and a current browser. Enable JavaScript; if it is already enabled, update your browser.

Learn · The Art of Not Looking

budding

Rare Words Are Evidence

Ranking begins when a match changes belief. Rarity supplies the odds, repetition saturates, and length explains how repetition arose.

search, information-retrieval, ranking, idf, bm25, probability, algorithms, learn

“The general object of statistical weighting schemes is to assign high values to discriminating terms.”

— Stephen E. Robertson and Karen Spärck Jones, “Relevance Weighting of Search Terms

This is the sixth chapter in a book about search from first principles. The first five chapters established which documents truly match. Here you will derive a term's relevance weight as a log-odds ratio, see the collection-only Robertson–Spärck Jones estimate emerge when relevance judgments are absent, and distinguish that signed weight from popular nonnegative IDF variants. You will derive the asymptote and derivative of term-frequency saturation, treat length normalization as an interpolation with two meaningful endpoints, and assemble BM25 term by term. You will also see why low weight is not the same as deleting a stopword and why BM25's false independence assumption can remain a useful approximation. The next chapter asks what happens when the same term occurs in a title, path, heading, and body.

The librarian does not count every word equally

A reader asks for “the apple pie.” Imagine a librarian hearing the words one at a time.

  • the appears in almost every document and barely narrows the shelves;
  • apple removes much of the room;
  • pie may leave one small neighborhood.

The useful question is not “did the term occur?” but “how much should seeing this term change belief that the document is relevant?”

Raw term frequency answers a different question. A 40-page history of orchards might mention apple 80 times. A one-page recipe that directly answers the query might mention it 4 times. Counting alone rewards the long document for having more opportunities to repeat.

Relevance — whether a document satisfies the information need represented by the query. It is a judgment about the document–need pair, not an intrinsic property of either one. Learn more.

Prediction — choose the observation that carries the most discriminating evidence.

Odds turn evidence into addition

Let RR mean that a document is relevant to the query. For one query term tt, define

pt=P(t occursR) p_t=P(t\text{ occurs}\mid R)

and

qt=P(t occurs¬R). q_t=P(t\text{ occurs}\mid \neg R).

Seeing tt changes the odds of relevance by the likelihood ratio

ptqt. \frac{p_t}{q_t}.

But absence also carries information. Compare the odds for a document where the term occurs with the odds for a document where it does not. The resulting binary-independence weight is

wt=logpt(1qt)qt(1pt). w_t=\log\frac{p_t(1-q_t)}{q_t(1-p_t)}.

Log-odds — the logarithm of p/(1p)p/(1-p). A likelihood ratio multiplies prior odds; taking its logarithm turns independent multiplicative evidence into an additive score. Learn more.

The logarithm is not cosmetic compression. Under the model's conditional- independence assumption, likelihood ratios for query terms multiply. Logs turn that product into a sum:

logtqΛt=tqlogΛt. \log\prod_{t\in q}\Lambda_t =\sum_{t\in q}\log\Lambda_t.

That additive form will later let term contributions be accumulated in any grouping and bounded before every document is fully scored.

The sign has meaning:

  • pt>qtp_t>q_t gives positive evidence;
  • pt=qtp_t=q_t gives zero evidence;
  • pt<qtp_t<q_t gives negative evidence under the model.

Ranking by any positive-base logarithm gives the same order. The base changes units, not winners.

Estimating the weight without relevance judgments

Suppose the corpus has:

  • NN documents;
  • ntn_t documents containing term tt;
  • RR judged-relevant documents;
  • rtr_t judged-relevant documents containing tt.

With half-count smoothing, estimate

pt=rt+0.5R+1 p_t=\frac{r_t+0.5}{R+1}

and

qt=ntrt+0.5NR+1. q_t=\frac{n_t-r_t+0.5}{N-R+1}.

Substitution into the log-odds ratio yields the Robertson–Spärck Jones relevance weight

wt=log(rt+0.5)(NntR+rt+0.5)(Rrt+0.5)(ntrt+0.5). w_t=\log \frac{(r_t+0.5)(N-n_t-R+r_t+0.5)} {(R-r_t+0.5)(n_t-r_t+0.5)}.

Most first-pass searches have no query-specific relevance judgments. A common neutral estimate takes pt=0.5p_t=0.5 and estimates nonrelevant occurrence from the whole collection. The weight reduces to

RSJ(t)=logNnt+0.5nt+0.5. \operatorname{RSJ}(t)= \log\frac{N-n_t+0.5}{n_t+0.5}.

Now rarity has emerged from an odds model. When ntn_t is small, the numerator dominates and the weight is positive. At about half the corpus it crosses zero. Above half, the raw RSJ weight is negative: under these assumptions, presence is more characteristic of nonrelevant documents than relevant ones.

Inverse document frequency is a corpus statistic with a probabilistic interpretation. It is not a dictionary of which words matter in every domain.

There is more than one formula called IDF

Implementations often need nonnegative term contributions for pruning, explanation, or product behavior. Two common alternatives are

IDFratio(t)=logNnt \operatorname{IDF}_{\mathrm{ratio}}(t)=\log\frac{N}{n_t}

and

IDF+(t)=log(1+Nnt+0.5nt+0.5). \operatorname{IDF}_{+}(t)= \log\left(1+\frac{N-n_t+0.5}{n_t+0.5}\right).

Both approach zero as ntn_t approaches NN. Neither is algebraically identical to the signed RSJ weight. Flooring negative RSJ values at zero is another policy. These choices can change rankings and whether an upper-bound algorithm is legal, so an index and query engine must name the exact variant rather than store a field called merely idf.

For N=1000N=1000:

ntn_t signed RSJ qualitative reading
1 large positive highly discriminating
100 positive useful collection evidence
500 zero presence alone does not move the modeled odds
900 negative common enough to be evidence against relevance in the raw model
1000 strongly negative ubiquitous presence cannot distinguish relevant documents

The last row exposes an often-repeated but incorrect simplification: the raw RSJ expression does not tend to zero for a ubiquitous term. Nonnegative variants do. A careful system says which one it uses and why.

The tenth occurrence is not ten first occurrences

Document frequency measures how widely a term is distributed. Term frequency ft,df_{t,d} measures how often it occurs in one document. Linear frequency would give every repetition the same increment:

tf(f)=f. \operatorname{tf}(f)=f.

That rewards verbosity and repetition without limit. BM25 uses a saturating shape. Start with

s(f)=fk1+f, s(f)=\frac{f}{k_1+f},

where k1>0k_1>0. Its asymptote is

limfs(f)=1, \lim_{f\to\infty}s(f)=1,

and its derivative is

s(f)=k1(k1+f)2. s'(f)=\frac{k_1}{(k_1+f)^2}.

The derivative is positive and decreases toward zero. Every additional occurrence helps, but each helps less than the previous one.

BM25 commonly multiplies by k1+1k_1+1:

sat(f)=(k1+1)fk1+f. \operatorname{sat}(f)= \frac{(k_1+1)f}{k_1+f}.

This makes the contribution equal to 1 when f=1f=1 while preserving the ranking shape for fixed k1k_1. The asymptote becomes k1+1k_1+1. A smaller k1k_1 saturates sooner; a larger k1k_1 behaves more like linear frequency across the observed range.

Reveal — saturation preserves monotonicity while refusing unlimited repetition credit.

A count needs a denominator

Four occurrences in a 100-token note are denser than four occurrences in a 100,000-token book. Let

  • |d||d| be the indexed length of document dd;
  • |d|¯\overline{|d|} be the corpus average indexed length;
  • b[0,1]b\in[0,1] control length normalization.

Define the length factor

Kd=k1(1b+b|d||d|¯). K_d=k_1\left(1-b+b\frac{|d|}{\overline{|d|}}\right).

Replace k1k_1 in the saturation denominator with KdK_d:

tfNorm(t,d)=(k1+1)ft,dft,d+k1(1b+b|d||d|¯). \operatorname{tfNorm}(t,d)= \frac{(k_1+1)f_{t,d}} {f_{t,d}+k_1\left(1-b+b\frac{|d|}{\overline{|d|}}\right)}.

The endpoints explain the parameter:

  • at b=0b=0, Kd=k1K_d=k_1 and document length has no effect;
  • at b=1b=1, Kd=k1|d|/|d|¯K_d=k_1|d|/\overline{|d|} and the count is fully normalized by relative length in this formula;
  • when |d|=|d|¯|d|=\overline{|d|}, every bb gives Kd=k1K_d=k_1.

This is an interpolation between two assumptions, not a universal correction. Some long documents cover many topics and need normalization. Others are long because the answer itself is detailed. Tuning bb changes that judgment.

The “document length” must be measured after the analyzer choices that define the index. If one side counts stopwords and the other does not, the formula's symbols agree while its data does not.

Assemble BM25 from the three decisions

For query qq and document dd, a common BM25 form is

BM25(q,d)=tqIDF(t)(k1+1)ft,dft,d+k1(1b+b|d||d|¯). \operatorname{BM25}(q,d)= \sum_{t\in q} \operatorname{IDF}(t) \frac{(k_1+1)f_{t,d}} {f_{t,d}+k_1\left(1-b+b\frac{|d|}{\overline{|d|}}\right)}.

Every factor is now inspectable:

  1. document frequency supplies corpus rarity;
  2. term frequency supplies within-document evidence;
  3. k1k_1 sets how quickly repetition saturates;
  4. relative length and bb control verbosity normalization;
  5. the sum combines per-term evidence under the independence approximation.

BM25 score with explicit IDF policy

BM25-SCORE(queryTerms, document, corpus, k1, b, idfPolicy)
Input:  analyzed query terms, document statistics, corpus statistics, parameters
Output: additive relevance score and per-term explanation

score  0
explanation  empty sequence
for each distinct term t in queryTerms
    n  documents in corpus containing t
    f  occurrences of t in document
    if f = 0
        continue
    idf  IDF(idfPolicy, corpus.documentCount, n)
    lengthRatio  document.length / corpus.averageDocumentLength
    denominator  f + k1 × (1 - b + b × lengthRatio)
    tfWeight  (k1 + 1) × f / denominator
    contribution  idf × tfWeight
    append (t, n, f, idf, tfWeight, contribution) to explanation
    score  score + contribution
return (score, explanation)

The algorithm takes O(|q|)O(|q|) statistic lookups per candidate document when query terms have already been deduplicated and the index supplies ntn_t, ft,df_{t,d}, |d||d|, and |d|¯\overline{|d|}. Candidate generation and postings traversal are separate costs. Its auxiliary working space is O(1)O(1) without an explanation and O(|q|)O(|q|) when the explanation is retained.

Query term frequency is omitted here deliberately. Some BM25 variants include a second saturation factor for it; many short web queries treat each distinct query term once. Again, “BM25” names a family unless every factor and parameter is pinned.

Work one ranking by hand

Take N=1000N=1000, use the nonnegative IDF+\operatorname{IDF}_{+} variant, and query apple pie. Suppose:

Term Documents containing it
apple 400
pie 20

The pie contribution receives much larger IDF. Compare two documents at k1=1.2k_1=1.2 and b=0.75b=0.75:

Document Length apple count pie count Interpretation
recipe 200 3 4 short and focused
orchard history 4000 80 1 long, many apple mentions

The history's 80 repetitions do not become 80 times the evidence of one occurrence, and its length raises the saturation denominator. The rare pie match dominates the distinction. This is exactly the behavior raw frequency could not express.

The explanation should expose the actual numbers rather than say “matched better.” A ranking bug becomes diagnosable when a reader can see n, f, relative length, IDF variant, saturation, and final contribution for each term.

Stopwords: cheap evidence is not absent evidence

A stoplist deletes selected tokens during analysis. Low or nonpositive IDF reduces their ranking contribution without deleting them. Those operations are not equivalent.

Consider the query

how was this website built

A stoplist containing how, was, and this leaves only website built. That remainder describes a broad topic. The removed words carried the query's interrogative shape, phrase positions, and potential exact-match evidence even if their standalone ranking weights were small.

Keeping a common term allows:

  • exact phrase and proximity checks;
  • literal page find;
  • quoted-query semantics;
  • explanations that preserve what the reader typed;
  • later analyzers or tasks to make a different choice.

This does not prove that every deployment should index every token. Storage, privacy, language, and latency can justify exclusions. It proves that deletion is a semantic decision, while IDF is a graded scoring decision. One cannot be described as merely a faster implementation of the other.

The raw signed RSJ weight adds another reason for precision: a ubiquitous term does not automatically receive “almost zero” there; it becomes negative. Systems that want common terms to contribute zero or a small positive amount choose a nonnegative IDF variant or a floor. The policy belongs in the scoring contract.

The false assumption that still helps

The binary independence model assumes query terms are conditionally independent given relevance or nonrelevance. Natural language violates this constantly. new and york are not independent in documents about New York; machine and learning form a concept together.

Why can an approximation built on a false assumption work?

  • The score needs a useful ordering, not calibrated posterior probabilities.
  • Log-odds rarity captures a strong marginal signal even when dependencies remain.
  • Saturation and length normalization repair two major failures of binary term presence for ordinary text.
  • Parameters can be evaluated on held-out judgments for the actual task.
  • Phrase, field, proximity, and learned features can add evidence the independence model omits.

That is an empirical defense, not a proof of universal superiority. The model earns deployment by beating alternatives on named judgments and latency budgets, not by having a probabilistic ancestry.

In the 1970s, Karen Spärck Jones and Stephen Robertson were trying to turn relevance information and collection statistics into principled term weights. Their 1976 paper derived a family of weights from a general probabilistic retrieval theory and tested them on the small judged collections available at the time. The enduring move was not one frozen formula. It was to ask how a term's distribution differs between relevant and nonrelevant documents, then express that distinction as additive log evidence.

BM25 arrived through later Okapi experiments and model revisions. Robertson and Hugo Zaragoza's 2009 account is unusually candid about that lineage: the probabilistic framework, the eliteness interpretation of within-document frequency, and practical parameterizations accumulated rather than descending from one immaculate derivation. That history is a reason to expose the pieces, not to distrust them.

Engineering reality

For each indexed field and segment, a production scorer must pin:

  • the exact analyzer and document-length definition;
  • the IDF expression, smoothing, sign or floor policy;
  • k1k_1, bb, and any query-frequency factor;
  • whether document frequency is global, per shard, or approximated;
  • how deletions and newly committed documents update NN, ntn_t, and average length;
  • the numeric type and deterministic tie-break order.

Distributed statistics create a subtle failure. If each shard computes IDF from only its local NN and ntn_t, identical documents can receive different scores solely because of placement. Merging shard top-k lists then compares numbers built from different corpora. A coordinator needs common statistics, a correction phase, or an explicitly approximate contract.

Very common terms can also dominate traversal cost even when their score is small. Keeping them semantically does not require decoding their entire postings list for every query. Phrase anchors, shortest-list-first planning, impact ordering, and later upper-bound algorithms can avoid much of that work. Storage policy and query execution are separate levers.

What not to do

Wrong turn Failure Reversal condition
count every occurrence linearly verbosity wins without bound controlled equal-length records where count is the task
call every rarity expression “IDF” sign and rankings change silently one exact formula is fixed by the artifact
delete common words because their rank weight is small phrase and query shape disappear a task contract explicitly excludes those semantics
treat BM25 as a calibrated relevance probability assumptions and parameters do not justify calibration calibration is separately measured and fitted
tune k1k_1 and bb by intuition knobs become folklore judged queries and held-out evaluation exist
compute unrelated shard statistics scores are not comparable merge is explicitly approximate or a common statistic is supplied

The differential oracle for the arithmetic is a direct mathematical implementation over small exact statistics. Ranking tests should pin both the total order and the per-term decomposition. Property checks should establish that, under nonnegative IDF, contribution is nondecreasing in frequency, bounded by its stated asymptote, and decreases as document length grows when b>0b>0 and other values are fixed.

The field changes the meaning of an occurrence

BM25 has treated a document as one bag of analyzed tokens. Real documents have titles, paths, headings, tags, captions, and bodies. One occurrence in a title often carries more editorial authority than one in a footnote. The fields also have different typical lengths.

A tempting extension scores every field with BM25 and adds the already- saturated answers. That gives the same term several “first occurrences,” one per field, and can overreward documents that scatter weak evidence across the schema.

The next chapter derives BM25F's answer: normalize and weight field frequencies, combine them into one pseudo-frequency, and saturate once.

Lessons

  • The binary-independence term weight is a log-odds ratio comparing term presence with term absence under relevance and nonrelevance.
  • Without relevance judgments, a neutral estimate yields the signed Robertson–Spärck Jones weight log((Nnt+0.5)/(nt+0.5))\log((N-n_t+0.5)/(n_t+0.5)).
  • Signed RSJ, log(N/nt)\log(N/n_t), nonnegative log-one-plus, and floored variants are different scoring policies.
  • Saturation is monotone with a decreasing derivative: repetition helps, but each occurrence contributes less.
  • bb interpolates between no document-length normalization and the formula's full relative-length normalization.
  • BM25 combines corpus rarity, within-document frequency, saturation, and length. It is a family until every variant and parameter is fixed.
  • Low ranking weight is not token deletion. Stopword removal can destroy phrase, literal, and query-shape evidence.
  • Conditional term independence is false for language; BM25 remains an approximation whose value must be established by evaluation.

Practice

Retrieval — recover the reason for the logarithm.
Discrimination — distinguish raw RSJ from a nonnegative variant.
Transfer — preserve semantics while reducing common-term cost.
  1. Starting from ptp_t and qtq_t, derive log(pt(1qt)/(qt(1pt)))\log(p_t(1-q_t)/(q_t(1-p_t))) as the difference between the log-odds when a term is present and absent.
  2. Compute signed RSJ and nonnegative log-one-plus IDF for N=1000N=1000 and nt{1,100,500,900,1000}n_t\in\{1,100,500,900,1000\}. Identify every ordering difference.
  3. Differentiate (k1+1)f/(k1+f)(k_1+1)f/(k_1+f) and prove that it is increasing and concave for f0f\ge0.
  4. Hold term frequency fixed and compare the BM25 term-frequency factor at b=0b=0 and b=1b=1 for documents half, equal to, and twice the average length.
  5. Construct a query where deleting stopwords changes an exact phrase answer even though the same stopwords would receive almost no weight under your chosen nonnegative IDF policy.

References

  1. Stephen E. Robertson and Karen Spärck Jones. “Relevance Weighting of Search Terms.” Journal of the American Society for Information Science 27, no. 3, 1976.
  2. Stephen Robertson and Hugo Zaragoza. “The Probabilistic Relevance Framework: BM25 and Beyond.” Foundations and Trends in Information Retrieval 3, no. 4, 2009.
  3. Karen Spärck Jones. “A Statistical Interpretation of Term Specificity and Its Application in Retrieval.” Journal of Documentation 28, no. 1, 1972.