Understanding Maximal Marginal Relevance (MMR): Balancing Relevance and Diversity in Document Retrieval

Table of contents
Understand Maximal Marginal Relevance (MMR), how it balances document relevance and diversity, and how relevance vectors, redundancy matrices, lambda weighting and iterative selection help identify useful information without unnecessary repetition.

Introduction

When working with Large Language Models (LLMs), one of the challenges we encounter is determining what information should be included in the model's context. This becomes particularly important when dealing with applications that rely on external knowledge sources.

One approach to addressing this challenge is Retrieval-Augmented Generation (RAG). As discussed in our article on Understanding Retrieval-Augmented Generation (RAG), RAG allows us to retrieve relevant information from an external knowledge source and provide it to an LLM when generating a response.

However, retrieving relevant information does not necessarily mean that all the retrieved information should be included in the final context. We might retrieve several document chunks that discuss almost the same thing, while other chunks contain different information that could be equally important.

Consider an organisation with thousands of internal documents, including employee handbooks, company policies, HR circulars and other administrative records. An employee asks about the company's maternity leave policy. The retrieval system might return 50 candidate chunks. We can then use a reranking model to reassess their relevance and retain, for example, the top 25 candidates.

But even among those 25 candidates, we might find several chunks explaining the same maternity leave entitlement, while others discuss notification requirements, application procedures and other relevant provisions.

Including all 25 chunks might introduce unnecessary repetition and consume more context tokens than necessary. We therefore need a way of selecting a smaller collection that preserves relevant information while avoiding excessive overlap.

This is where Maximal Marginal Relevance (MMR) becomes useful.

MMR helps us select documents by considering two things: how relevant each document is to the original query and how similar it is to documents that have already been selected.

Let's understand how it works.

What is Maximal Marginal Relevance (MMR)?

Maximal Marginal Relevance (MMR) is a document-selection and reranking algorithm that balances relevance against redundancy.

It was introduced by Jaime Carbonell and Jade Goldstein in their 1998 research paper, The Use of MMR, Diversity-Based Reranking for Reordering Documents and Producing Summaries.

The underlying idea is that we should not select documents based entirely on their relevance to a query. We should also consider how much additional information each document contributes compared with the documents already selected.

For example, imagine that an employee asks:

"What is the company's maternity leave policy, and what procedure must I follow?"

The retrieval system returns several documents:

  • Employee handbook explaining maternity leave entitlement.
  • Maternity leave policy outlining eligibility and entitlement.
  • HR circular explaining when an employee should notify HR.
  • Leave application procedure explaining the required documentation.
  • An older staff guide containing information about maternity leave entitlement.

All these documents might be relevant to the question. However, some contain substantially overlapping information.

If we select documents based entirely on relevance, we might end up with several versions of the same explanation.

MMR addresses this by considering the relevance of a candidate document alongside its similarity to documents already selected.

The objective is to select a collection that is relevant to the query while providing as much useful additional information as possible.

Where Does MMR Fit in the Retrieval Process?

To understand where MMR is applied, let's consider the following retrieval process.

Suppose we have an organisation with 100,000 indexed document chunks.

An employee submits a question, and the retrieval system identifies 50 potentially relevant chunks.

We then use a reranking model, such as a cross-encoder, to reassess these candidates and retain the top 25.

At this point, we have 25 candidates ordered according to their relevance to the original question.

However, these 25 candidates might still contain overlapping information.

We can now apply MMR to select a smaller collection, perhaps five documents, for inclusion in the final context.

The process looks like this:

Table 0: How MMR is Applied in the Retrieval Process
Initial retrieval 50 candidate chunks.
Reranking The 50 candidates are reassessed, and the top 25 are retained.
MMR selection MMR evaluates the 25 candidates and selects the five that provide the best balance between relevance and diversity.
Final context The five selected chunks are supplied to the LLM as context.

These numbers are illustrative. The actual retrieval count and final selection depend on the application, the complexity of the query and the available context budget.

There is also an important distinction here. MMR can be used after a separate reranking model, as in our example, or it can perform the diversity-aware reranking and selection itself. We do not always need a separate cross-encoder before applying MMR.

Understanding the Two Main Components of MMR

MMR relies on two important measurements:

  1. Relevance.
  2. Redundancy.

These measurements provide the information needed to evaluate candidate documents.

1. Relevance

Relevance measures how closely a candidate document relates to the user's original query.

Suppose an employee asks about the company's maternity leave policy.

After retrieval and reranking, we have the following five candidate chunks for our illustration:

Table 1: Retrieved Documents and Their Corresponding Relevance Scores
Document Description Relevance score
D1 Employee handbook explaining maternity leave entitlement 0.96
D2 Maternity leave policy explaining eligibility and entitlement 0.93
D3 HR circular explaining notification requirements 0.89
D4 Leave application procedure and required documentation 0.85
D5 Older staff guide repeating maternity leave entitlement 0.78

Each document has a relevance score.

For example, D1 has a relevance score of 0.96, while D5 has a score of 0.78.

We can represent these scores using a relevance vector.

A relevance vector is simply a collection of relevance scores, where each value corresponds to one of the candidate documents.

For our example:

R = [0.96, 0.93, 0.89, 0.85, 0.78]

The first value represents D1, the second D2, and so on.

The relevance vector allows us to identify how closely each candidate relates to the original query.

Where do these relevance scores come from?

They can come from the initial retrieval system or a subsequent reranking model.

For example, vector search can calculate similarity between the query embedding and document embeddings. Alternatively, a cross-encoder reranker can evaluate the query and each document together to produce relevance scores.

When a separate reranker has already been used, its relevance scores can be used by MMR. However, the scores should be appropriately calibrated if they are being combined with similarity scores from another measurement method.

For our calculations, we will assume that the relevance scores have been scaled to a comparable range between 0 and 1.

2. Redundancy

Relevance tells us how closely a document relates to the query.

Redundancy considers how similar a candidate document is to other documents that have already been selected.

Let's return to our example.

D1 and D2 both discuss maternity leave entitlement. D5 also contains information about the same entitlement, although it comes from an older staff guide.

These documents might therefore contain substantially overlapping information.

However, D3 discusses notification requirements, while D4 explains the application procedure.

Although all five documents relate to maternity leave, they do not necessarily provide the same information.

To establish how similar the documents are, we calculate similarity between their embeddings.

This is different from calculating the similarity between a document and the user's query.

The resulting pairwise similarity scores are used to construct what we call a redundancy matrix.

Constructing the Redundancy Matrix

A redundancy matrix is a similarity matrix containing pairwise similarity scores between candidate documents.

Suppose we have five candidate documents:

D1, D2, D3, D4 and D5.

We need to establish:

  • How similar is D1 to D2?
  • How similar is D1 to D3?
  • How similar is D1 to D4?
  • How similar is D1 to D5?
  • How similar is D2 to D3?

We continue until we have established the similarities between all document pairs.

One common approach is to use cosine similarity between the document embeddings.

Suppose our calculations produce the following matrix:

Table 2: Redundancy Matrix Showing Pairwise Similarity Between Retrieved Documents
D1 D2 D3 D4 D5
D1 1.00 0.82 0.22 0.28 0.90
D2 1.00 0.25 0.35 0.84
D3 1.00 0.40 0.18
D4 1.00 0.22
D5 1.00

Let's understand how to interpret the values.

A value of 1.00 indicates perfect similarity between identical vector representations.

For example:

Similarity(D1, D1) = 1.00

This is why the diagonal contains values of 1.00.

A value close to 1.00 indicates a high degree of similarity.

For example:

Similarity(D1, D5) = 0.90

This suggests that D1 and D5 are highly similar.

A value closer to zero indicates relatively little similarity.

For example:

Similarity(D1, D3) = 0.22

This indicates that D1 and D3 are less similar to each other.

Cosine similarity technically ranges from -1 to 1. The values in our illustration are non-negative for simplicity, but an actual implementation should account for the range of scores produced by its embedding model.

Why Can We Use Only One Half of the Matrix?

There is another important observation.

Cosine similarity is symmetrical.

This means that the similarity between D1 and D2 is the same as the similarity between D2 and D1.

Therefore:

Similarity(D1, D2) = Similarity(D2, D1)

Consequently, we do not need to store both halves of the matrix.

We can store the upper or lower triangular portion, including the diagonal, because the other half contains exactly the same information.

The purpose of the redundancy matrix is to provide the pairwise similarity information needed during MMR selection.

In an actual implementation, we do not necessarily need to calculate and store the entire matrix in advance. We can calculate similarities when needed and cache the results.

Understanding Diversity

We now have the two measurements required by MMR:

  • Relevance: How closely does a document relate to the original query?
  • Redundancy: How similar is a document to other selected documents?

These measurements allow us to encourage diversity.

Diversity describes how much the selected documents differ from one another.

Consider two possible selections:

Selection A

  • Employee handbook explaining maternity leave entitlement.
  • Maternity leave policy explaining the same entitlement.
  • Older staff guide repeating the entitlement.

These documents are relevant, but they contain substantial overlap.

Selection B

  • Employee handbook explaining maternity leave entitlement.
  • HR circular explaining notification requirements.
  • Leave application procedure explaining required documentation.

Selection B covers different aspects of the employee's question.

This is the distinction MMR helps us achieve.

It is important to understand that MMR does not calculate diversity as an entirely separate measurement. Instead, it encourages diversity by penalising candidates that are too similar to documents already selected.

In other words, the lower the redundancy between selected documents, the greater the diversity we can achieve.

However, diversity is not always the objective. Sometimes we might deliberately want several closely related documents, particularly when we need supporting evidence or multiple sources discussing the same subject.

This is why MMR provides a way to control the balance.

Understanding the Lambda Parameter

Lambda (λ) is a weighting parameter that determines the importance assigned to relevance compared with diversity.

There are two equivalent ways of expressing the weighting convention. In the original MMR formulation, lambda is assigned to relevance.

We will use that original convention in this article.

Table 3: Lambda Values and Their Effect on Relevance and Diversity
Lambda Relevance weight Diversity weight
0.0 0% 100%
0.2 20% 80%
0.5 50% 50%
0.7 70% 30%
0.9 90% 10%
1.0 100% 0%

Let's interpret these values.

When λ = 0

We prioritise diversity entirely.

After the first document has been selected, subsequent selections are based entirely on avoiding similarity to already selected documents.

When λ = 0.5

We assign equal coefficients to relevance and diversity.

Both measurements contribute equally to the selection score.

When λ = 0.8

We prioritise relevance.

Documents receive a stronger weighting based on how closely they relate to the original query, while the redundancy penalty receives less weight.

When λ = 1

We completely prioritise relevance.

The redundancy penalty has no influence on subsequent selections. This is effectively ordinary relevance-based selection.

The choice of lambda depends on the requirements of the application.

For example, if we want a broad collection of documents covering different aspects of a subject, we might choose a lower lambda.

If we want several highly relevant documents that may also reinforce or support the same information, we might choose a higher lambda.

The original MMR paper discusses this trade-off and demonstrates how the parameter can be adjusted depending on whether we want to explore a broader information space or focus more heavily on relevant documents.

One important detail: always check the formula used by a particular library or implementation. Some implementations use the complementary convention, where the parameter is assigned to diversity instead of relevance.

The MMR Formula

We can now bring relevance, redundancy and lambda together.

The original MMR formulation is:

MMR(d) = λ × Relevance(d) − (1 − λ) × Redundancy(d)

Where:

  • MMR(d): The marginal relevance score of a candidate document.
  • λ: The weighting parameter assigned to relevance.
  • Relevance(d): How relevant the candidate document is to the original query.
  • Redundancy(d): The maximum similarity between the candidate document and any previously selected document.

There is an important detail concerning redundancy.

We do not take the average similarity between a candidate and all previously selected documents.

Instead, we take the maximum similarity.

For example, suppose we have already selected three documents, and we are evaluating D4.

Its similarity scores are:

  • Similarity to D1 = 0.25.
  • Similarity to D2 = 0.80.
  • Similarity to D3 = 0.30.

The maximum similarity is 0.80.

We use 0.80 as the redundancy value when calculating the MMR score.

Why do we use the maximum?

Because even if a candidate is different from most of the selected documents, it might still be highly similar to one of them.

Using the maximum ensures that this overlap is considered when evaluating the candidate.

How the MMR Algorithm Works

We now understand the relevance vector, redundancy matrix, diversity and lambda parameter.

Let's bring everything together and see how the algorithm selects documents.

Suppose our retrieval system returns 25 candidates after reranking, but we only want five documents in the final context.

Our objective is to select five documents that are relevant to the query while avoiding unnecessary overlap.

For simplicity, we will use the five candidate documents introduced earlier to demonstrate the calculations.

We will use:

λ = 0.7

This means:

  • Relevance weight = 0.7.
  • Diversity weight = 0.3.

Step 1: Select the Most Relevant Document

Initially, we have not selected any documents.

Therefore, there are no previously selected documents against which we can calculate redundancy.

We begin by examining the relevance vector.

R = [0.96, 0.93, 0.89, 0.85, 0.78]

The highest relevance score is 0.96.

Therefore, we select D1.

Our selected collection becomes:

Selected = [D1]

Notice that we have not calculated an MMR score for D1.

This is because there are no previously selected documents against which to calculate its redundancy.

The first document is therefore selected directly using its relevance score.

Step 2: Evaluate the Remaining Documents

Now that D1 has been selected, we evaluate the remaining candidates.

For each unselected document, we calculate its similarity to D1.

We then combine its relevance and redundancy scores to calculate MMR.

Let's calculate the scores.

Document D2

Relevance = 0.93

Similarity to D1 = 0.82

MMR = (0.7 × 0.93) − (0.3 × 0.82)

MMR = 0.651 − 0.246

MMR = 0.405

Document D3

Relevance = 0.89

Similarity to D1 = 0.22

MMR = (0.7 × 0.89) − (0.3 × 0.22)

MMR = 0.623 − 0.066

MMR = 0.557

Document D4

Relevance = 0.85

Similarity to D1 = 0.28

MMR = (0.7 × 0.85) − (0.3 × 0.28)

MMR = 0.595 − 0.084

MMR = 0.511

Document D5

Relevance = 0.78

Similarity to D1 = 0.90

MMR = (0.7 × 0.78) − (0.3 × 0.90)

MMR = 0.546 − 0.270

MMR = 0.276

Let's compare the results:

Table 4: MMR Scores After Selecting the First Document (D1)
Document Relevance Maximum redundancy MMR
D2 0.93 0.82 0.405
D3 0.89 0.22 0.557
D4 0.85 0.28 0.511
D5 0.78 0.90 0.276

D3 has the highest MMR score.

Therefore, D3 becomes our second selected document.

Our selected collection is now:

Selected = [D1, D3]

Notice that D2 had a higher relevance score than D3. However, D3 was selected because it is considerably less similar to D1.

This demonstrates how MMR balances relevance and diversity.

Step 3: Evaluate the Remaining Documents Again

We now have two selected documents:

Selected = [D1, D3]

We need to evaluate D2, D4 and D5.

This time, we compare every candidate against both selected documents.

For each candidate, we calculate its similarity to D1 and D3, then take the maximum.

Document D2

Similarity to D1 = 0.82

Similarity to D3 = 0.25

Maximum similarity = 0.82

Its relevance score remains 0.93.

MMR = (0.7 × 0.93) − (0.3 × 0.82)

MMR = 0.405

Document D4

Similarity to D1 = 0.28

Similarity to D3 = 0.40

Maximum similarity = 0.40

Its relevance score remains 0.85.

MMR = (0.7 × 0.85) − (0.3 × 0.40)

MMR = 0.595 − 0.120

MMR = 0.475

Document D5

Similarity to D1 = 0.90

Similarity to D3 = 0.18

Maximum similarity = 0.90

Its relevance score remains 0.78.

MMR = (0.7 × 0.78) − (0.3 × 0.90)

MMR = 0.276

Let's compare the results:

Table 5: MMR Scores After Selecting the First Two Documents (D1 and D3)
Document Relevance Maximum redundancy MMR
D2 0.93 0.82 0.405
D4 0.85 0.40 0.475
D5 0.78 0.90 0.276

D4 has the highest MMR score.

We therefore select D4.

Our selected collection is now:

Selected = [D1, D3, D4]

Step 4: Repeat Until the Required Number Is Reached

We continue the same process for any remaining candidates.

At every iteration:

  1. Take each unselected candidate.
  2. Compare it against every selected document.
  3. Find the maximum similarity score.
  4. Use that value together with the candidate's relevance score to calculate MMR.
  5. Select the candidate with the highest MMR score.
  6. Add the selected candidate to the collection.

We repeat until we have selected the required number of documents.

Notice that the relevance scores remain unchanged throughout the selection process, assuming the candidate set and relevance-scoring method remain fixed.

What changes is the redundancy calculation because the selected collection grows with every iteration.

How Many Iterations Are Required?

The number of selection iterations is determined by the number of documents we want to select, not the number of documents initially retrieved.

For example:

  • Retrieve 50 documents.
  • Rerank and retain 25.
  • Select five documents using MMR.

MMR performs five selection iterations.

The first iteration selects the document with the highest relevance score. The remaining four iterations use the MMR calculation.

Therefore, the number of selection iterations is K, where K represents the number of documents we want to select.

If we retrieve 25 documents and select all 25, we have not reduced the number of documents, although MMR can still change their ordering.

For meaningful reduction in the number of documents, the final selection count should normally be smaller than the candidate pool.

Can We Introduce an MMR Threshold?

So far, we have assumed that we always want a fixed number of documents.

For example, we retrieve 25 candidates and select five.

However, what happens if the remaining candidates have very low MMR scores?

Should we continue selecting them simply because we have not reached five?

Not necessarily.

We can introduce a minimum acceptable MMR score as an additional stopping condition.

For example, suppose we establish an MMR threshold of 0.4.

The selection process could work as follows:

Table 6: Example of MMR Thresholds and Document Selection Decisions
Highest remaining MMR score Decision
0.65 Select the document
0.48 Select the document
0.35 Stop selecting

This means that instead of always selecting five documents, we select up to five, provided the candidates meet the minimum acceptable score.

However, MMR thresholds are not part of the fundamental algorithm. They are an additional condition we can introduce when designing the selection process.

We also need to be careful about how we choose the threshold.

MMR scores depend on the relevance scores, redundancy scores and lambda parameter. Changing any of these can change the resulting scores.

Furthermore, a low MMR score does not necessarily mean that a document is irrelevant. It might simply mean that the document is too similar to information already selected.

For this reason, we can combine an MMR threshold with a separate minimum relevance requirement.

This allows us to ensure that a candidate is sufficiently relevant to the query before considering whether it adds enough new information.

In practice, we should establish appropriate thresholds through testing rather than assuming that a particular value will work for every application.

Practical Considerations When Implementing MMR

Although MMR is relatively straightforward conceptually, there are several considerations when implementing it.

1. Choosing the Candidate Pool Size

The number of candidates passed to MMR affects the selection process.

For example, retrieving 50 candidates and retaining 25 after reranking gives MMR a broader collection from which to select the final documents.

However, larger candidate pools can increase computational cost.

The appropriate number depends on retrieval quality, the complexity of the query, processing requirements and the expected size of the final context.

2. Choosing the Final Number of Documents

The final number of documents should depend on how much information is required to answer the question.

A simple question might require only a few document chunks, while a more complex question may need several supporting sections.

Selecting a fixed number of documents is convenient, but it does not guarantee that the selected collection will fit within the available token budget.

3. Choosing an Appropriate Lambda

Lambda determines the balance between relevance and diversity.

A lower value gives greater importance to diversity under the original MMR convention.

A higher value gives greater importance to relevance.

The appropriate value should be tested against the actual retrieval requirements of the application.

4. Maintaining Document Metadata

Similarity scores alone do not establish whether two documents are interchangeable.

For example, two company policies might contain similar information but have different effective dates.

A system should consider additional information such as:

  • Document title.
  • Publication date.
  • Effective date.
  • Version number.
  • Department.
  • Document status.
  • Access permissions.

This is particularly important when dealing with company records, where an outdated policy should not automatically be treated as equally authoritative as the current approved version.

5. Managing Computational Cost

If we retrieve 50 candidate documents, there are 1,225 unique pairwise similarity comparisons, excluding self-similarity.

We do not necessarily need to recalculate every comparison at every iteration.

One approach is to calculate the pairwise similarities once and reuse them.

Alternatively, we can calculate similarities as needed and maintain the maximum redundancy score for each remaining candidate.

For example, when a new document is selected, we only need to compare the remaining candidates against that newly selected document. We then update their maximum redundancy scores where necessary.

This avoids repeatedly performing comparisons that have already been calculated.

When Should We Use MMR?

MMR is particularly useful when the retrieved information contains overlapping content and we want a selection that covers different aspects of a subject.

For example, consider an AI assistant used to answer questions about company policies.

An employee asks:

"What do I need to know before applying for maternity leave?"

The retrieval system might return several sections from the employee handbook, maternity leave policy, HR circulars and leave application procedures.

Some sections might repeat the same entitlement information, while others explain notification requirements, application procedures and supporting documentation.

MMR can help select information covering these different aspects without unnecessarily repeating the same explanation.

However, MMR is not always the best choice.

Suppose a user asks for several official documents supporting the same company policy. In this situation, similar documents might be useful because they provide additional evidence or confirmation.

Applying too much diversity could remove information that is important to the user's request.

We should therefore consider the nature of the query before deciding how much importance to assign to diversity.

The objective is not to make every selected document different. It is to ensure that the selected documents collectively provide useful information without unnecessary repetition.

Conclusion

Maximal Marginal Relevance (MMR) provides a way of selecting documents by balancing relevance against redundancy.

Instead of relying entirely on how closely each document relates to the user's query, MMR also considers how similar a candidate is to documents that have already been selected.

The algorithm uses two important components: a relevance vector, which contains the relevance scores of candidate documents, and a redundancy matrix, which contains pairwise similarity scores between those documents.

It then selects documents iteratively, using the relevance score and maximum similarity to previously selected documents to calculate marginal relevance.

The lambda parameter controls the balance between relevance and diversity, allowing us to adapt the selection process to different requirements.

Ultimately, the objective is not simply to select the most relevant documents individually. It is to select a collection of documents that collectively provides useful information while avoiding unnecessary overlap.

Further Reading

  1. The Use of MMR, Diversity-Based Reranking for Reordering Documents and Producing Summaries – Jaime Carbonell and Jade Goldstein (1998)
  2. Understanding Retrieval-Augmented Generation (RAG)

About the author

Barasa Michael Murunga

Barasa Murunga Author

Founder

Barasa Murunga Michael is a backend engineer and technical lead based in Nairobi, Kenya, and the founder of Jisort Ublow. He designs, builds and deploys Python applications and API-driven systems using Flask, FastAPI and Django, with particular depth in payment integrations such as M-PESA Daraja and Pesapal. He also works with retrieval-augmented generation (RAG) and LLM tooling to bring intelligent features into real products. Through Jisort Ublow, he mentors developers and writes practical, implementation-focused guides on Python, backend architecture and payment integration. He holds a BSc in Informatics and Computer Science (First Class Honours) from Strathmore University.

See author profile

Discussion (1)

1 comment

Join the discussion

Share your thoughts and engage with the community

Your comment will appear immediately
Barasa Michael Staff
(edited)

One additional implementation point worth emphasizing is that MMR should refine good retrieval, not compensate for poor document ingestion.

A useful observation from InnovateML Solutions is the importance of fetching a sufficiently broad candidate pool before applying MMR, tuning lambda (λ) against an evaluation set for each query type, and deduplicating near-identical chunks during ingestion.

I would particularly stress the evaluation aspect. Lambda should not be assigned arbitrarily. Different query types have different retrieval requirements.

Consider a legal RAG application:
- Exact statutory provision queries: Prioritise relevance to retrieve the specific applicable provision.
- Legal interpretation queries: Balance relevance and diversity to retrieve complementary statutory provisions, regulations and supporting judicial decisions.
- Broad tax research queries: Encourage diversity to capture different applicable provisions, interpretations and relevant legal authorities.

The appropriate balance should be established by evaluating different lambda values against representative query sets and actual retrieval performance, rather than assuming one configuration works universally.

One further consideration is preserving document provenance, effective dates and version information during deduplication. Similarity alone does not mean two legal provisions are interchangeable.

Credit: InnovateML Solutions (https://www.linkedin.com/company/innovateml-solutions/) for highlighting these practical considerations.