Information retrieval with the vector space model
Vector space model: represent documents and queries as vectors (bag-of-words or TF–IDF weights) and rank by vector similarity (usually cosine); used for search, recommendation and many IR/NLP pipelines.
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
Engineers use the vector space model to build search engines, document ranking systems, and retrieval components of NLP pipelines. It converts text into numeric vectors so we can compute similarity, rank results, and plug into machine learning or recommender systems.
Key ideas
Term frequency (TF): for a term t in a document d, term frequency TF(t,d) is the count of occurrences of t in d. Variants include raw counts, normalized counts, and log-scaled counts. We define:
- raw TF: TF(t,d) = count(t in d)
- normalized TF: TF_norm(t,d) = TF(t,d) / max_t' TF(t',d)
- log TF: TF_log(t,d) = log(1 + TF(t,d)) Bold: term frequency is the (raw or transformed) scalar measuring how often a term appears in a document.
Document frequency (DF): DF(t) is the number of documents in the corpus that contain term t. Bold: document frequency counts how many documents contain the term.
Inverse document frequency (IDF): a measure of how rare or informative a term is across the corpus. Common exact formula: IDF(t) = ln(N / DF(t)), where N is the number of documents. Variants add smoothing to avoid division by zero while keeping IDF ≈ 0 when DF(t)=N: IDF_smooth(t) = ln((1 + N) / (1 + DF(t))). Bold: inverse document frequency down-weights common terms and up-weights rare terms.
TF–IDF weighting: combine TF and IDF to get a term weight for a document: w(t,d) = TF_variant(t,d) × IDF(t). Bold: TF–IDF is the product weight used widely to represent importance.
Cosine similarity: to compare a query vector q with a document vector d, compute cosine = (q · d) / (||q|| · ||d||). Bold: cosine similarity measures the cosine of the angle between vectors and is popular because it is length-normalised and scale-invariant.
Normalization: normalising each document to unit length (divide by its Euclidean norm) reduces bias from document length. Common practice: store normalized TF–IDF vectors for efficient retrieval. For scalable retrieval, use an inverted index mapping each term to a postings list of (docID, weight) so dot products are computed only over matching terms.
Assumptions commonly used in exams and practice:
- Terms are independent (no order, no syntax) — the model uses bag-of-words.
- Preprocessing (lowercasing, stop-word removal, stemming/lemmatisation) is applied consistently. If not applied, vocabulary and counts change.
- IDF base is natural log (ln) unless stated otherwise; some courses use log10 — state which you use.
Diagram (what to draw): draw a 2-D coordinate plane (label axes t1 and t2). Draw two vectors originating at origin: q and d. Label the angle between them θ, draw the dashed projection of q onto d and annotate q · d = ||q|| ||d|| cos θ. Title the figure "Cosine similarity: geometric view".
Formulas
TF_raw(t,d) = count(t in d) — TF as raw count, unitless, applies when using raw counts.
TF_norm(t,d) = TF_raw(t,d) / max_{t'} TF_raw(t',d) — normalized term frequency, unitless, applies to reduce length bias in a document.
TF_log(t,d) = ln(1 + TF_raw(t,d)) — log-scaled TF, unitless, applies to reduce effect of very frequent terms.
DF(t) = |{d in corpus : TF_raw(t,d) > 0}| — document frequency (count of documents containing t), unit: documents.
IDF(t) = ln( N / DF(t) ) — inverse document frequency, unitless, applies when DF(t) >= 1 and N is total documents; if DF(t)=0 use smoothing.
IDF_smooth(t) = ln( (1 + N) / (1 + DF(t)) ) — smoothed IDF to avoid division by zero and to yield 0 when DF(t) = N, unitless.
w(t,d) = TF_variant(t,d) * IDF_variant(t) — TF–IDF weight for term t in document d, unitless.
q·d = sum_{t in V} w(t,q) * w(t,d) — dot product of two vectors, unitless.
||d|| = sqrt( sum_{t in V} w(t,d)^2 ) — Euclidean norm (L2 norm) of document vector, unitless.
cosine(q,d) = (q·d) / ( ||q|| * ||d|| ) — cosine similarity between q and d, unitless; value in [-1,1] (but non-negative for TF–IDF since weights are non-negative). Undefined when ||q|| = 0 or ||d|| = 0; in practice return 0 or skip such cases.
Rocchio relevance feedback (optional):
q_new = alpha*q_original + beta*(1/|D_R|)*sum_{d in D_R} d - gamma*(1/|D_NR|)*sum_{d in D_NR} d — where alpha,beta,gamma are parameters; used to move query toward relevant docs.
Worked examples
Example 1 — TF–IDF ranking with cosine similarity (numerical, step-by-step). Corpus (preprocessed: lowercase, remove stop words ‘the’, ‘on’):
- Doc1: "cat sat mat" -> tokens: cat, sat, mat
- Doc2: "dog sat log" -> tokens: dog, sat, log
- Doc3: "cat chased dog" -> tokens: cat, chased, dog
Vocabulary V = {cat, dog, sat, mat, log, chased} (m = 6). Number of documents N = 3.
Step 1 — compute DF and IDF (use natural log ln):
- DF(cat) = 2, DF(dog) = 2, DF(sat) = 2, DF(mat) = 1, DF(log) = 1, DF(chased) = 1.
- IDF(cat) = ln(3/2) = 0.405465
- IDF(dog) = 0.405465
- IDF(sat) = 0.405465
- IDF(mat) = ln(3/1) = 1.098612
- IDF(log) = 1.098612
- IDF(chased) = 1.098612
Step 2 — TF (raw counts) and TF–IDF weights w(t,d) = TF * IDF:
- Doc1 weights: cat 0.405465, sat 0.405465, mat 1.098612, others 0.
- Doc2 weights: dog 0.405465, sat 0.405465, log 1.098612.
- Doc3 weights: cat 0.405465, dog 0.405465, chased 1.098612.
Query q = "cat dog" -> TF each = 1 -> query weights: cat 0.405465, dog 0.405465, others 0.
Step 3 — compute cosine similarity for each document.
Compute ||q|| = sqrt(0.405465^2 + 0.405465^2) = 0.573364.
Compute ||d1|| = sqrt(0.405465^2 + 0.405465^2 + 1.098612^2) = 1.239255.
q·d1 = 0.405465 * 0.405465 = 0.164401.
cosine(q,d1) = 0.164401 / (0.573364 * 1.239255) = 0.2316 (approx).
For Doc2: same numeric pattern as Doc1 (dog instead of cat), cosine(q,d2) = 0.2316 (approx).
For Doc3: ||d3|| = 1.239255, q·d3 = 0.405465^2 + 0.405465^2 = 0.328802, cosine(q,d3) = 0.4632 (approx).
Ranking by cosine: Doc3 (0.4632) > Doc1 (0.2316) = Doc2 (0.2316). Final: Doc3, Doc1, Doc2.
Example 2 — effect of term frequency scaling and normalization (short demonstration). Add Doc4: token sequence "cat cat cat cat" (TF(cat)=4).
Using raw TF–IDF: weight w(cat,Doc4) = TF * IDF = 4 * 0.405465 = 1.62186.
- ||Doc4|| = 1.62186.
- q·Doc4 = 0.405465 * 1.62186 = 0.657604.
- cosine(q,Doc4) = 0.657604 / (0.573364 * 1.62186) ≈ 0.7070.
Observation: Doc4 (repetition of single term) scores higher than Doc3 (which had both cat and dog) if raw TF is used. This shows repeated terms can dominate ranking.
Fix: use log TF or normalized TF. For log TF: TF_log(4) = ln(1+4) = 1.609438 and TF_log(1) = ln 2 = 0.693147. Recompute weights using TF_log × IDF will reduce the repetition effect and typically restore intuition that a document containing both query terms outranks one with only many repeats of one term.
Common mistakes
- Using raw TF without normalizing and then claiming cosine similarity already compensates (cosine reduces length bias but TF scaling still matters).
- Not applying smoothing for IDF when DF = 0 or not handling out-of-vocabulary terms for queries.
- Treating TF–IDF weights as probabilities — they are heuristic weights, not probabilities unless specifically normalized and interpreted.
- Incorrectly computing dot product by summing over full vocabulary including many zero terms — simpler is to sum only over terms present in both vectors.
For your exam
Kinds of questions:
- Definitions: define TF, DF, IDF, TF–IDF, cosine similarity, document vector, vocabulary.
- Short notes: explain advantages/limits of vector space model, compare TF variants, or describe normalization techniques.
- Derivations: derive cosine similarity formula from vector projection or show how TF–IDF is computed for a small corpus.
- Numericals: compute TF, IDF, TF–IDF weights and cosine similarities for 2–4 small documents and a query (show steps and final ranking).
- Diagrams: draw two vectors and label components and the angle between them.
What a full-marks answer includes:
- Clear statement of preprocessing assumptions (stop words, case-folding, stemming).
- Correct formulas, with the base of logs stated.
- Step-by-step numeric calculations for TF, DF, IDF and TF–IDF, showing intermediate numbers and final cosine values to 3 significant digits.
- A short discussion of limitations (no word order, sensitivity to vocabulary, need for smoothing and normalization).
Quick check
- What is DF(t) and how is it used in IDF?
- Give the formula for cosine similarity between vectors q and d.
- Why might a document with many repetitions of one query term outrank a document that has two different query terms when using raw TF–IDF?
Answers: 1) DF(t) = number of documents containing term t; used in IDF = ln(N/DF(t)) to downweight common terms. 2) cosine(q,d) = (q·d) / (||q|| * ||d||). 3) Because raw TF scales with count, repetition increases the dot product more than length-normalisation offsets it; use log TF or normalized TF to reduce this effect.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?