---
title: "Distance Metrics Selection Guide"
---

# Distance Metrics Selection Guide

## Overview

Choosing the right distance metric is crucial for optimal vector search performance. This guide helps you select and configure distance metrics for your use case.

## Available Metrics

### 1. Cosine Distance

**Formula**: `1 - (a·b) / (||a|| × ||b||)`

**Range**: [0, 2]
- 0 = identical vectors
- 1 = orthogonal vectors
- 2 = opposite vectors

**When to use**:
- Text embeddings (BERT, GPT, Sentence Transformers)
- Normalized vectors
- Semantic similarity search
- Document retrieval
- Recommendation systems

**Requirements**:
- Vectors should be normalized (unit length)
- Works best with high-dimensional embeddings (>64D)

**Performance**:
- Scalar: ~600ns per calculation (128D)
- AVX2: ~100ns per calculation (6x speedup)
- AVX-512: ~55ns per calculation (11x speedup)

---

### 2. Euclidean Distance (L2)

**Formula**: `sqrt(Σ(ai - bi)²)`

**Range**: [0, ∞)
- 0 = identical vectors
- larger values = more different

**When to use**:
- Image embeddings (ResNet, VGG, CLIP)
- General-purpose vector search
- Clustering (K-means, DBSCAN)
- Anomaly detection
- Non-normalized vectors

**Requirements**:
- None (works with any vectors)
- Consider normalizing for scale-invariance

**Performance**:
- Scalar: ~500ns per calculation (128D)
- AVX2: ~80ns per calculation (6x speedup)
- AVX-512: ~45ns per calculation (11x speedup)

---

### 3. Manhattan Distance (L1)

**Formula**: `Σ|ai - bi|`

**Range**: [0, ∞)

**When to use**:
- Sparse vectors
- High-dimensional data (>1000D)
- Grid-based features
- Taxi-cab geometry
- Robust to outliers

**Performance**:
- Scalar: ~450ns per calculation (128D)
- AVX2: ~75ns per calculation
- AVX-512: ~40ns per calculation

---

### 4. Dot Product (Inner Product)

**Formula**: `Σ(ai × bi)`

**Range**: (-∞, ∞)
- larger positive = more similar
- negative = opposite

**When to use**:
- Already normalized vectors
- Maximum Inner Product Search (MIPS)
- Collaborative filtering
- Matrix factorization models
- Neural network activations

**Performance**:
- Scalar: ~400ns per calculation (128D)
- AVX2: ~65ns per calculation
- AVX-512: ~35ns per calculation

---

### 5. Hamming Distance

**Formula**: Count of differing bits

**Range**: [0, n] where n = number of bits

**When to use**:
- Binary vectors/hashes
- LSH (Locality Sensitive Hashing)
- SimHash for deduplication
- Fingerprint matching
- Genomic sequences

**Performance**:
- Extremely fast (hardware instruction)
- ~50ns per calculation (512 bits)

---

### 6. Jaccard Distance

**Formula**: `1 - (|A ∩ B| / |A ∪ B|)`

**Range**: [0, 1]

**When to use**:
- Set similarity
- Tag/category matching
- Sparse binary vectors
- Collaborative filtering
- Content similarity

---

## Decision Tree

### Use Cosine Distance if:
- Working with text embeddings (BERT, GPT, etc.)
- Vectors are normalized or can be normalized
- Need scale-invariant similarity
- Doing semantic search or document retrieval

### Use Euclidean (L2) Distance if:
- Working with image embeddings
- Vectors have meaningful magnitudes
- Need geometric distance
- General-purpose similarity search

### Use Manhattan (L1) Distance if:
- Vectors are sparse
- Very high dimensionality (>1000D)
- Need robustness to outliers
- Working with grid-based data

### Use Dot Product if:
- Implementing MIPS (Maximum Inner Product Search)
- Vectors are already normalized
- Working with collaborative filtering
- Using matrix factorization models

### Use Hamming Distance if:
- Working with binary vectors/hashes
- Implementing LSH or SimHash
- Need the fastest comparison
- Working with bit signatures

### Use Jaccard Distance if:
- Comparing sets or tags
- Sparse binary vectors
- Content-based similarity
- Category overlap

## Common Use Cases

### Text/Document Search

**Best metric**: Cosine Distance
**Why**: Text embeddings are normalized, cosine measures angular similarity

---

### Image Search

**Best metric**: Euclidean (L2)
**Why**: Image embeddings have meaningful magnitudes, L2 captures geometric distance

---

### Product Recommendations

**Best metric**: Dot Product or Cosine
**Why**: MIPS is ideal for collaborative filtering and matrix factorization

---

### Near-Duplicate Detection

**Best metric**: Hamming Distance
**Why**: SimHash uses bit-level comparison, Hamming is fastest

---

## Performance Comparison

### Throughput (calculations per second, 128D vectors)

Indicative figures measured on representative fixtures; reproduce on your own hardware.

| Metric | Scalar | AVX2 | AVX-512 | Speedup |
|--------|--------|------|---------|---------|
| Cosine | 1.7M/s | 10M/s | 18M/s | 10.6x |
| Euclidean | 2.0M/s | 12.5M/s | 22M/s | 11x |
| Manhattan | 2.2M/s | 13M/s | 25M/s | 11.4x |
| Dot Product | 2.5M/s | 15M/s | 28M/s | 11.2x |
| Hamming | 20M/s | N/A | N/A | 1x (already fast) |

**Tested on**: Intel Xeon (3.5 GHz), 128-dimensional vectors

---

## Normalization

### When to Normalize

**Always normalize for**:
- Cosine distance
- Dot product (for similarity)
- Scale-invariant comparison

**Don't normalize for**:
- Euclidean distance (if magnitude matters)
- Manhattan distance
- Hamming distance

---

## Switching Metrics

### Rebuilding Index

To change metrics, rebuild the index.

---

## FAQ

**Q: Can I use multiple metrics on the same index?**

A: No, each HNSW index uses one metric. Create separate indices for different metrics.

**Q: Which metric is fastest?**

A: Dot product is slightly faster, but all metrics have similar SIMD performance. Choose based on accuracy, not speed.

**Q: Should I normalize image embeddings?**

A: Depends. For ResNet/VGG, L2 works well without normalization. For CLIP, normalize and use Cosine.

**Q: Can I mix normalized and non-normalized vectors?**

A: Not recommended. Normalize all vectors or none for consistent results.

**Q: How do I know if my vectors are normalized?**

A: Check if `||v|| = 1`.

**Q: Which metric for multilingual embeddings?**

A: Cosine distance with normalized embeddings (most multilingual models output normalized vectors).

**Q: Can I implement custom distance functions?**

A: Yes, but they must satisfy metric properties (non-negative, symmetric, triangle inequality).

---

## Best Practices

1. **Match your model**: Use the metric your embedding model was trained with
2. **Normalize when needed**: Always normalize for Cosine, usually for Dot Product
3. **Benchmark both**: Test L2 vs Cosine on your data to see which works better
4. **Use SIMD**: Enable AVX2/AVX-512 for 5-10x speedup
5. **Consider sparsity**: Use Manhattan for very sparse vectors
6. **Test recall**: Verify metric choice gives good recall on validation set

---

## References

- [Cosine Similarity](https://en.wikipedia.org/wiki/Cosine_similarity)
- [Euclidean Distance](https://en.wikipedia.org/wiki/Euclidean_distance)
- [Hamming Distance](https://en.wikipedia.org/wiki/Hamming_distance)
- [MIPS vs Cosine](https://arxiv.org/abs/1602.02745)
- [Distance Metrics Survey](https://arxiv.org/abs/1708.04321)
