Inferensys

Glossary

Maximal Marginal Relevance (MMR)

Maximal Marginal Relevance (MMR) is a ranking algorithm used in information retrieval to reduce redundancy in result sets by selecting items that are both relevant to a query and sufficiently diverse from items already selected.
Developer working on RAG retrieval system, document chunks visible on screen, technical workspace with code editor.
ALGORITHM

What is Maximal Marginal Relevance (MMR)?

Maximal Marginal Relevance (MMR) is a ranking algorithm designed to optimize information retrieval result sets by balancing relevance with diversity, thereby reducing redundancy.

Maximal Marginal Relevance (MMR) is a ranking algorithm used in information retrieval and search systems to select a subset of items that are both highly relevant to a query and maximally diverse from one another. It formalizes the trade-off between relevance and novelty by iteratively choosing the document that maximizes a weighted combination of its similarity to the query and its dissimilarity to documents already selected. This process directly combats redundancy, ensuring the final result set covers distinct aspects of the information need.

The algorithm is defined by a lambda (λ) parameter that controls the balance between relevance and diversity. A higher λ prioritizes pure relevance, behaving like a standard similarity search, while a lower λ emphasizes novelty. MMR is foundational in semantic search, retrieval-augmented generation (RAG), and recommender systems where presenting a varied, non-repetitive set of answers or items is critical. It operates on vector embeddings, calculating cosine similarity for both relevance to the query and dissimilarity within the result set.

ALGORITHM MECHANICS

Core Characteristics of MMR

Maximal Marginal Relevance (MMR) is a ranking algorithm that optimizes information retrieval by balancing relevance to a query with diversity among selected results. It is defined by its core operational parameters and mathematical formulation.

01

The Core Trade-off: Relevance vs. Novelty

MMR formalizes the fundamental tension in result ranking. It selects the next document d_i from a candidate set R that maximizes a weighted combination of two scores:

  • Relevance Score: How well d_i matches the original query Q, typically computed via cosine similarity with a query embedding or a score like BM25.
  • Novelty Score: How dissimilar d_i is from all documents S already selected in the result set, measured by maximal similarity to any document in S.

The algorithm explicitly prevents the top results from being near-duplicates of each other, ensuring each selected item adds new information.

02

The Lambda (λ) Parameter

The lambda (λ) parameter is a tunable hyperparameter between 0 and 1 that controls the balance between relevance and diversity in the MMR equation: MMR(d_i) = λ * Sim1(d_i, Q) - (1 - λ) * max_{d_j in S} Sim2(d_i, d_j)

  • λ = 1.0: Pure relevance ranking. Diversity is ignored, reverting to a standard similarity search.
  • λ = 0.5: Equal weight to relevance and novelty. A common default for general-purpose diversification.
  • λ = 0.0: Pure diversity ranking. The query is ignored; results are selected solely to be different from each other.

Engineering teams tune λ based on the use case: lower for exploratory search, higher for fact-finding missions.

03

Greedy Selection Algorithm

MMR uses a greedy, iterative selection process because the optimal set selection is NP-hard. The algorithm proceeds as follows:

  1. Start with an empty result set S and a full candidate set R.
  2. Select the document from R with the highest relevance score to Q and move it to S.
  3. For each remaining position k:
    • For every document d_i in R, compute its MMR score.
    • Select the document d_i with the highest MMR score.
    • Remove d_i from R and add it to S.
  4. Repeat step 3 until S reaches the desired size.

This greedy approach is computationally efficient, requiring O(kn) similarity computations for k results and n candidates.

04

Similarity Function Flexibility

MMR is agnostic to the specific similarity functions used for Sim1 (query-document) and Sim2 (document-document). This allows integration with various retrieval backends:

  • Sparse Retrieval: Use BM25 or TF-IDF for Sim1 and Sim2.
  • Dense Retrieval: Use cosine similarity between vector embeddings (e.g., from Sentence-BERT).
  • Hybrid Retrieval: Use a weighted combination of sparse and dense scores for Sim1.
  • Cross-Encoder Reranking: Use a more accurate but slower neural model for final Sim1 scoring after an initial fast retrieval.

The choice of similarity function directly impacts the semantic definition of both "relevance" and "novelty."

05

Application in RAG and Agentic Systems

In Retrieval-Augmented Generation (RAG) and autonomous agent workflows, MMR is critical for context window management.

  • Preventing Redundancy: When constructing a context window from multiple retrieved documents, MMR ensures each chunk provides unique information, maximizing the utility of limited model tokens.
  • Mitigating Bias: Without MMR, a retriever might return several highly similar documents that reinforce a single perspective or fact, potentially biasing the LLM's synthesis.
  • Supporting Multi-Step Reasoning: For agents, diverse context chunks can contain different pieces of evidence required for complex, multi-faceted reasoning, as opposed to repetitive evidence for a single sub-task.
06

Computational Complexity & Optimizations

The naive MMR implementation has a complexity of O(kn) for k results and n candidates, with each step requiring a similarity computation against all items in S. For large-scale systems, this can be optimized:

  • Pre-computed Document-Document Similarity: For static corpora, a pre-computed similarity matrix can cache Sim2 values, trading memory for speed.
  • Approximate Nearest Neighbor (ANN) Search: Use vector indexes like HNSW for fast retrieval of top-relevance candidates (Sim1), reducing n in the initial candidate set R.
  • Cluster-Based Pruning: First cluster candidates, then apply MRR to select diverse clusters before selecting within clusters.
  • Threshold-Based Novelty: Ignore the max operation if similarity to any doc in S is below a threshold, speeding up the calculation.
MAXIMAL MARGINAL RELEVANCE (MMR)

Frequently Asked Questions

Maximal Marginal Relevance is a core ranking algorithm in information retrieval designed to balance relevance with diversity, preventing redundant results. These FAQs address its mechanics, applications, and implementation for engineers building retrieval systems.

Maximal Marginal Relevance (MMR) is a ranking algorithm used in information retrieval to select a subset of items from a larger candidate set that are both highly relevant to a query and sufficiently diverse from one another, thereby reducing redundancy in the final result list.

Introduced in a 1998 paper by Carbonell and Goldstein, MMR formalizes the trade-off between relevance and novelty. It operates by iteratively selecting the document that maximizes a weighted combination of its similarity to the query and its dissimilarity (or minimal similarity) to the documents already chosen. The core equation is:

MMR = argmax [ λ * Sim(D_i, Q) - (1 - λ) * max Sim(D_i, D_j) ]

Where Sim(D_i, Q) is the relevance score of document i to query Q, max Sim(D_i, D_j) is its maximum similarity to any already-selected document j, and λ is a tunable parameter between 0 and 1 that controls the diversity-relevance balance.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.