Objective
Learning objectives
- Explain cos θ as a measure of how aligned two directions are, ranging from 1 (same direction) to -1 (opposite)
- Connect the numeric pattern 1 → 0 → -1 to the aligned → unrelated → opposite intuition
- Compute cos θ for the angle between two directions and interpret the result
- Predict which of two angle-pairs represents more similar directions without calculating
Hook
How does a search engine — or a RAG system retrieving context for an AI model — decide that two documents are "similar"? Named directly, since there's no reason to be coy about it: the real answer is cosine similarity, and its full treatment (with actual embedding vectors) belongs to this platform's Math for AI & ML course. What belongs here is smaller and more foundational: getting genuinely comfortable with what cos θ means, so that when you meet the full formula later, it's finishing a sentence you already understand the start of.
See it
Picture two arrows drawn from the same starting point, each pointing in some direction. Now shrink the angle between them toward 0° — the two arrows point almost exactly the same way. Widen it to 90° — the arrows are perpendicular, sharing no sense of "same direction" at all. Keep widening to 180° — the arrows point in exactly opposite directions. Nothing about magnitude, length, or a real formula has entered yet — just the angle between two directions, shrinking or widening.
This is a preview on purpose, not a shortcut
The real formula behind cosine similarity — v·w = |v||w|cos θ — needs the dot product, which needs vectors treated properly, which is Math for AI & ML's job (specifically its Essence of Linear Algebra-based material). This lesson deliberately stops short of that. The goal here is narrow: make cos θ itself feel familiar, so the real formula reads as "oh, that number I already know" instead of unfamiliar notation on day one of that material.
Name it
cos θ, where θ is the angle between two directions, behaves as a similarity score with exactly three landmark values worth knowing cold: cos(0°) = 1 (identical direction), cos(90°) = 0 (unrelated — perpendicular, sharing nothing directionally), and cos(180°) = -1 (exactly opposite). Every angle in between produces a value smoothly between those landmarks — closer to 1 the more aligned the two directions are, closer to -1 the more opposed.
Code it
import math
def alignment(angle_a_deg, angle_b_deg):
diff = math.radians(angle_b_deg - angle_a_deg)
return round(math.cos(diff), 4)
print(alignment(20, 25))
print(alignment(20, 35))
print(alignment(20, 110))
print(alignment(20, 200))
0.9962
0.9659
0.0
-1.0
Two directions only 5° apart score 0.9962 — nearly perfectly aligned. Pull them 15° apart and the score drops a little, to 0.9659, still clearly "similar." Push the gap out to 90° and it hits exactly 0.0 — the numeric signature of "unrelated." Push it all the way to 180° apart and it bottoms out at exactly -1.0 — the numeric signature of "opposite." The score isn't an arbitrary similarity metric someone invented; it's cos θ, doing exactly what See It described, expressed as a number instead of a picture.
print(alignment(20, 160))
The gap between 20° and 160° is 140° — well past the 90° "unrelated" mark, heading toward (but not all the way to) 180° "opposite." A score of -0.766 fits exactly where that reasoning predicts: solidly negative, closer to -1 than to 0, but not the full -1 you'd only get at exactly 180°.