Module 3: Similarity and Distance
7. Capstone Exercise: Computing and Comparing Metrics
Exercise overview
This is the capstone exercise for Module 3. Here you're going to compute Euclidean and Manhattan distances and cosine similarity for 2D/3D vectors, compare the results, and decide which metric to use in different cases.
Part 1: Manual calculations
The data:
A = [3, 4]
B = [6, 8]
C = [0, 5]
D = [9, 1]
Task 1.1: Compute all the Euclidean distances
Compute:
- dist(A, B)
- dist(A, C)
- dist(A, D)
- dist(B, C)
See solutions
dist(A, B) = √((6-3)² + (8-4)²) = √(9+16) = 5
dist(A, C) = √((0-3)² + (5-4)²) = √(9+1) ≈ 3.16
dist(A, D) = √((9-3)² + (1-4)²) = √(36+9) ≈ 6.71
dist(B, C) = √((0-6)² + (5-8)²) = √(36+9) ≈ 6.71
Task 1.2: Compute the cosine similarities
Compute:
- cos(A, B)
- cos(A, C)
- cos(A, D)
See solutions
cos(A, B):
A·B = 3×6 + 4×8 = 50
||A|| = 5, ||B|| = 10
cos = 50/(5×10) = 1.0
cos(A, C):
A·C = 3×0 + 4×5 = 20
||A|| = 5, ||C|| = 5
cos = 20/25 = 0.8
cos(A, D):
A·D = 3×9 + 4×1 = 31
||A|| = 5, ||D|| = √82 ≈ 9.06
cos = 31/(5×9.06) ≈ 0.68
Part 2: Interpretation
Question 2.1:
According to Euclidean, which vector is closest to A?
According to cosine, which vector is most similar to A?
See answer
Euclidean: C is closest to A (dist = 3.16)
Cosine: B is most similar to A (cos = 1.0)
Interpretation: B has the same direction as A (only a different magnitude). If direction represents "the kind of concept", B is more similar conceptually.
Part 3: Use cases
Case 1: Text embeddings
You have embeddings of 3 documents:
Doc1: [0.6, 0.8] (normalized)
Doc2: [0.6, 0.8] (normalized)
Doc3: [0.8, 0.6] (normalized)
Query: [0.7, 0.71] (normalized)
Which metric do you use? Why?
See answer
Metric: Cosine (or the dot product, which is equivalent when they're normalized)
Why:
- Normalized embeddings → only direction matters
- Cosine ignores magnitude → perfect for this case
- Interpretable values (0.95 vs 0.7 is clear)
Case 2: GPS coordinates
You have 3 locations (latitude, longitude):
Home: [19.43, -99.13]
Work: [19.45, -99.15]
Restaurant: [19.50, -99.10]
Which metric do you use to "find the closest place to home"?
See answer
Metric: Euclidean (or Haversine for geographic precision)
Why:
- Physical distance matters
- Magnitude has real meaning (kilometers)
- Cosine isn't appropriate ("conceptual direction" is irrelevant here)
Part 4: Reflection
Question 4.1:
Why does semantic search use cosine instead of Euclidean?
See reflection guide
Answer:
-
Embeddings capture direction (the kind of concept), not magnitude
- Magnitude can be noise (frequency, text length)
- Cosine ignores magnitude → it only measures direction
-
It's robust in high dimensions
- In 1536D, Euclidean distances are all similar
- Angles (cosine) are more discriminative
-
Interpretable values
- Cosine ∈ [-1, 1] → 0.95 = very similar, 0.3 = barely similar
- Euclidean ∈ [0, ∞) → hard to interpret ("is 20 a lot or a little?")
Exercise summary
What you did:
- ✅ You computed Euclidean and cosine by hand
- ✅ You compared the results (different rankings)
- ✅ You decided which metric to use based on the use case
- ✅ You reflected on why cosine is used in semantic search
The intuition, consolidated:
"Cosine measures direction (the kind of concept), Euclidean measures distance (magnitude matters). For embeddings, only direction matters → cosine is superior."
Module 3 conclusion
Congratulations on completing Module 3: Similarity and Distance. 🎉
What you achieved:
- ✅ You understand Euclidean distance (the straight line)
- ✅ You know Manhattan and other metrics
- ✅ You've mastered cosine similarity (the angle)
- ✅ You can compare metrics (trade-offs)
- ✅ You know why cosine is used in AI (direction > magnitude)
- ✅ You see how it's used in production (Pinecone, Weaviate)
- ✅ You applied all of it conceptually
The module's key intuition:
Cosine similarity is the standard metric in semantic search because it measures direction (the kind of concept), not magnitude (noise). In high dimensions, cosine is more robust and more discriminative than Euclidean.
Next module: Module 4: Proximity Search — kNN conceptually, indexes (HNSW, IVF) without implementing them.