Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Probabilistic retrieval and BM25

Probabilistic retrieval models estimate document relevance by Bayes-style odds; BM25 is a practical, popular scoring function that combines term occurrence probabilities, term frequency saturation and document-length normalisation.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Search engineers use probabilistic retrieval to rank documents by how likely they are to be relevant to a query. BM25 is the most widely used practical implementation: it is simple, fast, and gives strong quality on typical text collections, so you will implement or tune it in search systems, IR labs and exam problems.

Key ideas

  • Probabilistic retrieval model: model that ranks documents by the probability they are relevant given the document and the query. The ranking uses the posterior odds P(Rel|d,q)/P(NotRel|d,q) or a monotone transform (log-odds).
  • Binary Independence Model (BIM): a specific probabilistic model that assumes (1) terms are binary features (present/absent in a document), and (2) term occurrences are independent conditioned on relevance. Under these assumptions the log-odds score (Retrieval Status Value, RSV) decomposes into a sum over the query terms.

Derivation (step-by-step) of the classical BIM RSV. State assumptions: documents are represented as binary vectors of term presence; terms independent given relevance. Let R denote event “relevant”, N denote “non‑relevant”, and let x_i indicate presence (1) or absence (0) of term t_i in document d.

  1. Start with posterior odds for document d: P(R | d, q) / P(N | d, q).
  2. Using Bayes' rule this is proportional to P(d | R, q) P(R | q) / (P(d | N, q) P(N | q)). For ranking we can ignore priors P(R|q)/P(N|q) or treat them as a constant.
  3. Under binary independence, P(d | R, q) = ∏_i p_i^{x_i} (1 - p_i)^{1 - x_i}, where p_i = P(x_i = 1 | R, q). Similarly for non-relevant: use u_i = P(x_i = 1 | N, q).
  4. Take log of odds (logarithm is monotone): log-odds = Σ_i [ x_i · log(p_i/u_i) + (1 - x_i) · log((1 - p_i)/(1 - u_i)) ]. Terms independent of x_i cancel or become constants. After simplification, RSV for ranking becomes (only terms present in d matter): RSV(d) ∝ Σ_{t in d ∩ q} log( p_t (1 - u_t) / (u_t (1 - p_t)) ).
  5. This weight can be estimated from collection counts. If r_t = #relevant docs containing t, R = #relevant docs total, n_t = #documents containing t, N = total documents, a common smoothed estimate uses +0.5 additive smoothing (Robertson-Sparck Jones, RSJ): weight ≈ log( (r_t + 0.5)/(R - r_t + 0.5) · (N - n_t - R + r_t + 0.5)/(n_t - r_t + 0.5) ).
  6. For initial retrieval we usually have no relevance information yet (R = 0, r_t = 0). That simplifies the weight to an IDF-like form: idf_t = log( (N - n_t + 0.5) / (n_t + 0.5) ).
  • BM25: a widely used practical ranking function that extends the BIM idea to account for term frequency (tf) saturation and document length. BM25 uses:
    • term frequency saturation: additional occurrences have diminishing returns;
    • document-length normalisation through a parameter b and average document length avgdl;
    • tunable parameter k1 controlling tf saturation; optional query-term-frequency factor controlled by k3; to omit query tf effects set k3 = 0 (so qfactor = 1); with short queries where qf = 1, setting k3 large also makes qfactor ≈ 1.

State BM25 design assumptions: approximate probabilistic weighting; independence between terms still assumed for summation; parameters chosen empirically (k1 about 1.2–2.0, b ≈ 0.75 commonly used).

If a diagram is asked in an answer, draw: one column labelled "query terms" listing t1,t2,...; one column per document showing term counts f_{t,d}; draw arrows from terms to per-term BM25-weight blocks; label avgdl and doc length |d|; show summation to final BM25(d,Q) score.

Formulas

RSV(d) ∝ Σ_{t in d ∩ q} log( p_t (1 - u_t) / (u_t (1 - p_t)) ) — p_t = P(term t present | relevant), u_t = P(term t present | non-relevant). Units: none. Applies to the Binary Independence Model under independence and binary-term assumptions.

RSV_estimate ≈ Σ_{t in d ∩ q} log( (r_t + 0.5)/(R - r_t + 0.5) · (N - n_t - R + r_t + 0.5)/(n_t - r_t + 0.5) ) — r_t = #relevant docs with t, R = #relevant docs, n_t = #docs with t, N = total docs. Units: none. Applies when using Robertson-Sparck Jones smoothing.

idf_t = log( (N - n_t + 0.5) / (n_t + 0.5) ) — N = total docs, n_t = #docs containing t. Units: none. Applies to initial retrieval when R = 0.

BM25(d,Q) = Σ_{t in Q} idf_t · ( f_{t,d} · (k1 + 1) ) / ( f_{t,d} + k1 · (1 - b + b·|d|/avgdl) ) — f_{t,d} = term frequency of t in doc d (count), |d| = document length in words, avgdl = average document length, k1 (typical 1.2–2.0) controls tf saturation, b (0 ≤ b ≤ 1, typical 0.75) controls length normalisation. Units: none. Applies to standard BM25 ranking for ad-hoc retrieval.

Optional query-frequency factor: qfactor = ( qf·(k3 + 1) ) / ( k3 + qf ) — qf = term frequency in the query. Units: none. Often omitted by setting k3 = 0 (so qfactor = 1 for any qf); with short queries where qf = 1, a large k3 also makes qfactor ≈ 1.

Notes: All logs above are natural logarithms in many implementations; some use base 10 — only the relative order matters, but exams may ask to state which base you use.

Worked examples

Example 1 — single-term BM25 (numerical steps): Given: N = 5 documents, term t occurs in n_t = 2 documents. Document d has length |d| = 100 words, avgdl = 120 words, term frequency f_{t,d} = 3. Use k1 = 1.5, b = 0.75.

  1. Compute idf_t: idf_t = log((N - n_t + 0.5)/(n_t + 0.5)) = log((5 - 2 + 0.5)/(2 + 0.5)) = log(3.5/2.5) = log(1.4) = 0.33647.
  2. Compute length factor: 1 - b + b·|d|/avgdl = 1 - 0.75 + 0.75·(100/120) = 0.25 + 0.75·0.83333 = 0.25 + 0.625 = 0.875.
  3. Compute denominator term: f + k1·(length factor) = 3 + 1.5·0.875 = 3 + 1.3125 = 4.3125.
  4. TF weight numerator: f·(k1 + 1) = 3·2.5 = 7.5.
  5. TF weight = 7.5 / 4.3125 = 1.73913.
  6. BM25 score = idf_t · TF weight = 0.33647 · 1.73913 = 0.5852. Final answer: BM25(d, {t}) = 0.5852.

Example 2 — two-term query and ranking two documents: Given: N = 10, avgdl = 200, k1 = 1.5, b = 0.75. Query Q = {t1, t2}. Term document frequencies: n1 = 3, n2 = 6. Document A: |A| = 150, f_{t1,A} = 4, f_{t2,A} = 0. Document B: |B| = 250, f_{t1,B} = 1, f_{t2,B} = 2. Step 1: idf values:

  • idf1 = log((10 - 3 + 0.5)/(3 + 0.5)) = log(7.5/3.5) = 0.76214.
  • idf2 = log((10 - 6 + 0.5)/(6 + 0.5)) = log(4.5/6.5) = -0.36772 (negative because t2 is common). Document A (only t1 contributes):
  • length factor = 1 - b + b·|A|/avgdl = 0.25 + 0.75·(150/200) = 0.25 + 0.5625 = 0.8125.
  • denom = f + k1·lenfactor = 4 + 1.5·0.8125 = 4 + 1.21875 = 5.21875.
  • numerator = f·(k1+1) = 4·2.5 = 10.
  • TF weight = 10 / 5.21875 = 1.916.
  • term score = idf1 · TF weight = 0.76214 · 1.916 = 1.460. Document B (both terms contribute):
  • length factor = 0.25 + 0.75·(250/200) = 0.25 + 0.9375 = 1.1875. For t1 in B (f = 1):
  • denom = 1 + 1.5·1.1875 = 1 + 1.78125 = 2.78125.
  • numerator = 1·2.5 = 2.5.
  • TF weight = 2.5 / 2.78125 = 0.89928.
  • t1 score = 0.76214 · 0.89928 = 0.6854. For t2 in B (f = 2):
  • denom = 2 + 1.5·1.1875 = 2 + 1.78125 = 3.78125.
  • numerator = 2·2.5 = 5.
  • TF weight = 5 / 3.78125 = 1.32275.
  • t2 score = -0.36772 · 1.32275 = -0.4863. Total BM25(B,Q) = 0.6854 + (-0.4863) = 0.1991. Ranking: Document A score 1.460 > Document B score 0.1991, so A ranks higher.

Common mistakes

  • Using raw IDF = log(N/n) vs the smoothed Robertson variant without saying which one is used.
  • Forgetting to include document length normalization (b) in BM25 calculations — this changes scores significantly.
  • Treating BM25 tf component as linear in f (it is sublinear / saturating) and computing it incorrectly.
  • Neglecting that idf_t can be negative for very common terms (this is allowed and meaningful).

For your exam

Question types you will see: definitions (define BIM, RSV, idf, BM25), short notes (assumptions and limitations), derivations (derive RSV under BIM using Bayes), numericals (compute BM25 for small collections), diagrams (show BM25 computation flow), and short programs (rare: implement BM25 term-scoring loop in pseudocode/Python). What full-marks answers include:

  • For derivations: clear statement of assumptions, step-by-step Bayes/log-odds manipulation, and final RSV expression.
  • For formulas: show the exact BM25 formula, define each symbol and typical parameter values.
  • For numericals: show every step, compute idf, compute length normalisation, compute tf weight, then multiply and sum; give final numeric score with units (none) and comment on ranking.

Quick check

  1. Name the two main assumptions of the Binary Independence Model.
  2. Write the BM25 tf factor for a term with f_{t,d} = 3, k1 = 1.5 and length factor = 0.9 (compute numeric value of the fraction only).
  3. If a term appears in all documents (n_t = N), what is the Robertson-smoothed idf value sign (positive/zero/negative)? Answers: 1) Terms are binary (presence/absence) and term occurrences are independent conditioned on relevance. 2) fraction = (3·2.5) / (3 + 1.5·0.9) = 7.5 / (3 + 1.35) = 7.5 / 4.35 = 1.72414. 3) negative (strictly < 0 with RSJ smoothing), because (N - n_t + 0.5)/(n_t + 0.5) < 1 when n_t = N, giving log < 0.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?