PythiloLesson 1 of 12 · progress kept in this browser

Practice in Lab: Diversify retrieved evidence with maximal marginal relevance →

Diversify retrieved evidence with maximal marginal relevance

Retrieval can return many near-duplicates that crowd out a different useful source. Maximal marginal relevance makes the relevance-versus-redundancy tradeoff explicit. Each next document is scored against the query and the evidence already selected. The operation reranks existing vectors; it neither computes embeddings nor establishes that the selected text supports an answer. Zero-vector semantics and deterministic identity ties matter when reproducing an experiment. Evaluate downstream coverage and answer quality rather than assuming that greater geometric diversity is always better.

query_scores = {'a':1., 'b':1., 'c':0.}
redundancy = {'b':1., 'c':0.}
weight = .3
print({key:weight*query_scores[key]-(1-weight)*value
       for key,value in redundancy.items()})
Your task

Documents contain unique string id and finite vector matching a nonempty finite query vector. Require integer k>=0 and relevance_weight in [0,1]. Greedily maximize weight*cos(query,document)-(1-weight)*maximum cosine to an already selected document, using zero redundancy for the first choice. A zero vector has cosine zero. Break score ties by smaller id. Return selected IDs, never repeating a document.