Module 3: Similarity and Distance

5. Comparing the Metrics

Overview

Here you compare the three main metrics: Euclidean, Manhattan, cosine. You'll see when to use each one and why cosine dominates in semantic search.


Comparison table

AspectEuclideanManhattanCosine
What it measuresStraight lineGrid pathAngle
Sensitive to magnitudeYesYesNo
Values[0, ∞)[0, ∞)[-1, 1]
High dimensions⚠️ Problematic⚠️ Problematic✅ Robust
Use with embeddingsUncommonRare⭐ Standard
Normalization neededNoNoOptional (it helps)

When to use each metric

Euclidean:

  • ✅ Physical distances (GPS, maps)
  • ✅ When magnitude matters
  • ❌ NOT optimal for high-dimensional embeddings

Manhattan:

  • ✅ Movement on a grid
  • ✅ Robust to outliers
  • ❌ Rare with embeddings

Cosine:

  • ✅ Text embeddings (OpenAI, BERT)
  • ✅ Semantic search
  • ✅ Any case where only direction matters

Why cosine dominates in AI

The problem in high dimensions (e.g. 1536D):

All random vectors are ~equidistant
→ Euclidean distance loses its discriminative power

The solution:

Cosine measures the angle, not the distance
→ Angles are preserved better in high dimensions
→ More interpretable values (0.95 vs 0.45 is clear)

A comparative example

A = [3, 4]
B = [6, 8]
C = [0, 5]

Euclidean:
- dist(A, B) = √((6-3)² + (8-4)²) = 5
- dist(A, C) = √((0-3)² + (5-4)²) ≈ 3.16

Cosine:
- cos(A, B) = 1.0 (same direction)
- cos(A, C) = 20 / (5 × 5) = 0.80 (different direction)

Interpretation: Euclidean says "C is closer to A". Cosine says "B is more similar to A (same direction)".

For embeddings: Cosine is correct (B is conceptually more similar).


Next capsule: 06-metrics-in-semantic-search.md — Use in production.