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.

Language models for IR

Notes on language models for information retrieval: unigram document models, query-likelihood scoring, smoothing (Jelinek–Mercer, Dirichlet), KL-divergence view, and pseudo-relevance feedback (RM3).

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

Language models let an IR system treat each document (or the query) as a probability model over words. An engineer uses language models when implementing ranking functions (query-likelihood), smoothing unseen terms, and when adding relevance feedback — these are core to search engines and to many modern retrieval algorithms.

Key ideas

Start with first principles and simple definitions.

  • Language model (LM): a probability distribution over the vocabulary V that assigns to each word w a probability p(w). Define it before using: for our notes a language model is a distribution that describes how likely each word is to appear when text is generated by that model. (Dimensionless probabilities.)

  • Document language model (θ_d): the LM estimated from a single document d. We assume a unigram model unless stated otherwise: words are generated independently given θ_d.

  • Query-likelihood model: a generative ranking approach that assumes the query Q was generated by the language model of a relevant document. The ranking score is the likelihood p(Q | θ_d). Under the unigram assumption and independent query terms, p(Q | θ_d) = ∏_{w in Q} p(w | θ_d) (if repeated terms count multiplicatively).

  • Maximum likelihood estimate (MLE) for a unigram LM: For document d with token counts tf(w,d) and length |d| (number of tokens), the MLE is p_MLE(w | d) = tf(w,d) / |d|. This follows from maximizing the multinomial likelihood subject to probabilities summing to 1 (use Lagrange multipliers in exams).

    Derivation (standard exam step): Given counts c_w and total N = |d|, maximize L = ∏_w p_w^{c_w} s.t. ∑_w p_w = 1. Take log: l = ∑_w c_w log p_w + λ(∑_w p_w - 1). Differentiate: c_w / p_w + λ = 0 → p_w ∝ c_w → p_w = c_w / N.

  • Smoothing: Because many query words may not appear in a document, raw MLE may assign zero probability and kill the product. Smoothing mixes document and collection evidence to give non-zero probabilities. Two common methods:

    • Jelinek–Mercer (linear interpolation): p(w|d) = λ p_MLE(w|d) + (1 − λ) p(w|C), with 0 ≤ λ ≤ 1.
    • Dirichlet (Bayesian) smoothing: p(w|d) = (tf(w,d) + μ p(w|C)) / (|d| + μ), where μ > 0 is a parameter.

    Define collection model p(w|C) as total count of w in the collection divided by total tokens in collection.

  • Log-space scoring: For numerical stability, take log of likelihood: log p(Q|θ_d) = ∑_{w in Q} log p(w|θ_d). Ranking by log-likelihood is identical to ranking by likelihood.

  • KL-divergence view: If we estimate a unigram query model θ_Q with probabilities p(w|Q) (for example empirical counts in the query), then ranking by likelihood is equivalent to ranking by minimizing the Kullback–Leibler (KL) divergence D(θ_Q || θ_d). Show the algebra in exams: D(θ_Q || θ_d) = H(θ_Q, θ_d) − H(θ_Q), where H(θ_Q, θ_d) = −∑ p(w|Q) log p(w|d). Since H(θ_Q) depends only on the query, minimizing KL is equivalent to minimizing cross-entropy H(θ_Q, θ_d), which is equivalent to maximizing ∑ p(w|Q) log p(w|d) — the expected log-likelihood of the query.

  • Pseudo-relevance feedback (PRF) and RM3: Start by assuming top-k documents are relevant. Build a new query model by mixing original query LM and a feedback LM estimated from top documents. RM3 is a common variant: θ_RM3 = α θ_Q + (1 − α) θ_feedback, where θ_feedback is estimated from top documents (often using term frequencies weighted by document scores). Then re-rank using the new query model.

What to draw in an exam (diagram): Draw three boxes left-to-right labelled "collection C", "document d (tokens w1 ... wn)", and "query Q (tokens q1 ... qm)". Draw an arrow from d to each token wi and label it p(wi | θ_d). Draw another arrow from C to tokens with label p(w|C). For feedback, draw arrows from top-k documents to a box "feedback LM" then arrow mixing with "original query" to produce "RM3 query model".

Formulas

p_MLE(w | d) = tf(w,d) / |d| — tf(w,d) = count of word w in document d; |d| = number of tokens in d; dimensionless; applies to unigram MLE.

p(w | C) = cf(w) / |C| — cf(w) = collection frequency of w; |C| = total tokens in collection; dimensionless; applies to collection LM.

p_JM(w | d) = λ·p_MLE(w | d) + (1 − λ)·p(w | C) — λ in [0,1]; dimensionless; applies to Jelinek–Mercer smoothing.

p_Dir(w | d) = (tf(w,d) + μ·p(w|C)) / (|d| + μ) — μ > 0 (Dirichlet prior); dimensionless.

p(Q | θ_d) = ∏_{i=1..m} p(q_i | θ_d) — product over query tokens q_i; dimensionless; under unigram independence.

score(Q,d) = log p(Q | θ_d) = ∑_{i=1..m} log p(q_i | θ_d) — use logs for numerical stability; dimensionless.

D(θ_Q || θ_d) = ∑_{w∈V} p(w|Q) · log (p(w|Q) / p(w|d)) — KL divergence; dimensionless; measures how different two distributions are.

θ_RM3 = α·θ_Q + (1 − α)·θ_feedback — RM3 interpolation of original and feedback query models; 0 ≤ α ≤ 1. A common RM3 estimation for the feedback model is: p(w | θ_feedback) ∝ Σ_{d ∈ top-k} p(w | d) · p(Q | d) (with p(w | d) a smoothed document model); normalize p(w | θ_feedback) so that it sums to 1 over the vocabulary.

perplexity(θ, T) = 2^{−(1/N) ∑_{i=1..N} log_2 p(w_i | θ)} — perplexity of model θ on test tokens T (w_i); dimensionless; lower is better.

Worked examples

Example 1 — Numerical: query-likelihood with Jelinek–Mercer smoothing.

Given collection of three documents (tokens shown):

  1. D1 = [data, retrieval, data] (|D1| = 3)
  2. D2 = [information, retrieval] (|D2| = 2)
  3. D3 = [data, structures, algorithms] (|D3| = 3) Collection size |C| = 8. Query Q = [data, retrieval]. Use Jelinek–Mercer with λ = 0.6. Compute and rank documents.

Steps:

  1. Compute tf and collection frequencies: cf(data)=3, cf(retrieval)=2 → p(data|C)=3/8=0.375, p(retrieval|C)=2/8=0.25.
  2. MLEs:
    • D1: p_MLE(data|D1)=2/3≈0.6666667; p_MLE(retrieval|D1)=1/3≈0.3333333.
    • D2: p_MLE(data|D2)=0; p_MLE(retrieval|D2)=1/2=0.5.
    • D3: p_MLE(data|D3)=1/3≈0.3333333; p_MLE(retrieval|D3)=0.
  3. Jelinek–Mercer smoothing p = 0.6·p_MLE + 0.4·p_C:
    • D1: p(data|D1)=0.6·0.6666667 + 0.4·0.375 = 0.55; p(retrieval|D1)=0.6·0.3333333 + 0.4·0.25 = 0.3.
    • D2: p(data|D2)=0.6·0 + 0.4·0.375 = 0.15; p(retrieval|D2)=0.6·0.5 + 0.4·0.25 = 0.4.
    • D3: p(data|D3)=0.6·0.3333333 + 0.4·0.375 = 0.35; p(retrieval|D3)=0.6·0 + 0.4·0.25 = 0.1.
  4. Query-likelihood (product over query terms):
    • Score(D1) = 0.55 × 0.3 = 0.165.
    • Score(D2) = 0.15 × 0.4 = 0.06.
    • Score(D3) = 0.35 × 0.1 = 0.035.

Final ranking: D1 (0.165) > D2 (0.06) > D3 (0.035).

Example 2 — Derivation: KL equivalence with query-likelihood.

Goal: show ranking by p(Q|θ_d) is equivalent to ranking by minimizing D(θ_Q || θ_d) for a given query model θ_Q.

Steps:

  1. Define empirical query model p(w|Q) = count(w in Q) / |Q|.
  2. Cross-entropy H(θ_Q, θ_d) = −∑_w p(w|Q) log p(w|d).
  3. KL divergence D(θ_Q || θ_d) = ∑_w p(w|Q) log (p(w|Q) / p(w|d)) = −∑_w p(w|Q) log p(w|d) − (−∑_w p(w|Q) log p(w|Q)) = H(θ_Q, θ_d) − H(θ_Q).
  4. H(θ_Q) depends only on the query, so for ranking different documents d the term H(θ_Q) is constant. Therefore minimizing D(θ_Q || θ_d) ⇔ minimizing H(θ_Q, θ_d) ⇔ maximizing ∑_w p(w|Q) log p(w|d), which is the expected log-likelihood of the query under θ_d. For an empirical query, this is proportional to summing log p(q_i|d) over query tokens, the usual log query-likelihood score (the 1/|Q| factor is constant across documents).

Hence the equivalence.

Example 3 — Theory/model answer: explain RM3 pseudo-relevance feedback in steps (short-answer exam style).

Model answer outline students should write:

  1. Define pseudo-relevance feedback: assume top-k retrieved documents are relevant; use them to expand the query.
  2. Estimate the feedback model θ_feedback from top-k documents: count-weighted frequencies (optionally weight by document score) and normalize to a distribution.
  3. Interpolate with original query model: θ_RM3 = α θ_Q + (1 − α) θ_feedback.
  4. Re-score documents using query-likelihood or KL divergence with θ_RM3.
  5. Mention why this helps: adds synonyms/related terms, combats short-query problem; warn of query drift.

A full-marks answer includes formulas for θ_feedback, the interpolation step, and discussion of parameters (k, α) and risks (drift).

Common mistakes

  • Using collection model counts incorrectly (forgetting total tokens |C|).
  • Confusing p(Q|θ_d) (query-likelihood) with p(d|Q) (posterior); Bayes rule needs prior p(d) to convert.
  • Misunderstanding RM3: failing to normalize the feedback model or not re-weighting by document score.
  • Using interpolation λ values without explanation — always state and justify parameter choice.

For your exam

Types of questions you will see:

  • Definitions: define language model, unigram LM, smoothing, KL divergence.
  • Short notes: explain Jelinek–Mercer vs Dirichlet smoothing; advantages of LM approach.
  • Derivations: derive MLE for unigram LM; show equivalence of KL minimization and query-likelihood ranking.
  • Numericals: compute query-likelihood scores with given documents and smoothing parameter.
  • Diagrams: draw the flow from collection → document LM → query generation.
  • Programs (rare): small Python to compute scores for documents.

What a full-marks answer includes:

  • Clear definitions with bolded terms, formulae with symbols defined, step-by-step derivations, and numeric answers with units or dimensionless statement. For diagrams, label boxes and arrows as noted above.

Quick check

  1. Why is smoothing needed in document language models?
  2. Write the Jelinek–Mercer smoothing formula.
  3. State the relationship between minimizing KL(θ_Q || θ_d) and maximizing query-likelihood.

Answers: 1) To avoid zero probabilities for unseen query terms and to incorporate collection statistics. 2) p_JM(w|d) = λ p_MLE(w|d) + (1−λ) p(w|C) 3) Minimizing KL(θ_Q || θ_d) is equivalent to maximizing the expected log-likelihood of the query under θ_d (since KL = cross-entropy − constant).

(Where I am unsure: I gave typical parameter ranges qualitatively but did not fix recommended λ or μ — these must be tuned on validation; see verify list.)

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

Stuck on something here?