Angles and Cosine Similarity

The dot product combines two ideas:

  • how long the vectors are
  • how much they point in the same direction

For two nonzero vectors aa and bb:

ab=abcos(θ)a \cdot b = \|a\|\|b\|\cos(\theta)

Here, θ\theta is the angle between them.

This formula says the dot product is large when the vectors are long and point in a similar direction.

Direction Without Length

Sometimes we want to compare direction without rewarding length.

Cosine similarity does that:

cosine similarity=abab\text{cosine similarity} = \frac{a \cdot b}{\|a\|\|b\|}

For nonzero real vectors, the result is between -1 and 1.

  • 1 means the vectors point in the same direction.
  • 0 means they are perpendicular.
  • -1 means they point in opposite directions.
-6-4-2246-4-2246a = [4, 1]b = [2, 3]0
a · b = (4 × 2) + (1 × 3) = 11Drag either endpoint. Drag the plane to pan.

Drag the vectors. When they point more similarly, the dot product tends to grow. Cosine similarity turns that into a direction comparison by dividing out both lengths.

MATH-C02-T08-001Exercise: Spot perpendicular vectors

If a=[1,0]a = [1, 0] and b=[0,1]b = [0, 1], what is aba \cdot b?

Compute it first, then check your number.

HintMultiply matching entries

Compute (1×0)+(0×1)(1 \times 0) + (0 \times 1).

SolutionZero dot product
ab=(1×0)+(0×1)=0a \cdot b = (1 \times 0) + (0 \times 1) = 0

For these two nonzero vectors, the zero dot product means they are perpendicular.

Same Direction, Different Length

Consider:

a = [1, 0]
b = [5, 0]

The dot product is:

ab=5a \cdot b = 5

The lengths are:

a=1,b=5\|a\| = 1,\qquad \|b\| = 5

So:

abab=51×5=1\frac{a \cdot b}{\|a\|\|b\|} = \frac{5}{1 \times 5} = 1

Cosine similarity says they point in the same direction, even though one vector is longer.

MATH-C02-T08-002Exercise: Remove length from the comparison

What is the cosine similarity between a=[1,0]a = [1,0] and b=[5,0]b = [5,0]?

Compute it first, then check your number.

HintSame ray

Both vectors point along the positive horizontal axis.

SolutionLength cancels

First compute the dot product and the two lengths:

[1,0][5,0][1,0][5,0]=51×5=1\frac{[1,0]\cdot[5,0]}{\|[1,0]\|\|[5,0]\|} = \frac{5}{1 \times 5} = 1

The longer vector has length 5, but the denominator also contains that length. After division, only direction remains. Both vectors point along the positive horizontal axis, so the cosine similarity is 1.

Opposite Direction

If:

a = [1, 0]
b = [-2, 0]

then bb points exactly opposite to aa.

The dot product is -2, and the product of the lengths is 2, so the cosine similarity is:

22=1\frac{-2}{2} = -1
MATH-C02-T08-003Exercise: Recognize opposite direction

What is the cosine similarity between a=[1,0]a = [1,0] and b=[2,0]b = [-2,0]?

Compute it first, then check your number.

HintCompute the sign

[1,0][2,0]=2[1,0]\cdot[-2,0] = -2.

SolutionOpposite direction

The vectors lie on the same line, but they point in opposite directions. The numerator keeps that sign:

[1,0]=1,[2,0]=2\|[1,0]\| = 1,\qquad \|[-2,0]\| = 2[1,0][2,0][1,0][2,0]=21×2=1\frac{[1,0]\cdot[-2,0]}{\|[1,0]\|\|[-2,0]\|} = \frac{-2}{1 \times 2} = -1

Lengths are never negative, so the negative result comes from the dot product. A cosine similarity of -1 means exactly opposite direction.

Why This Matters

Embeddings are often compared by direction.

Two sentence embeddings can have different lengths but still point in a similar direction. Cosine similarity helps compare the pattern of coordinates rather than only their size.

This does not mean length is useless. Some systems may use vector length to store confidence, frequency, or another signal. Cosine similarity is useful when direction is the signal we want to compare.

MATH-C02-T08-004Exercise: Choose the right comparison

Enter 1 if cosine similarity is designed to compare direction while reducing the direct effect of vector length.

Compute it first, then check your number.

HintLook at the denominator

The denominator is ab\|a\|\|b\|.

SolutionLength normalization

Enter 1. Cosine similarity computes:

abab\frac{a\cdot b}{\|a\|\|b\|}

Dividing by the lengths makes the result focus on direction.

Code Mirror

import numpy as np

a = np.array([1, 0])
b = np.array([0, 1])

cosine = np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
print(cosine)

The output is:

0.0

The vectors are perpendicular.

Next, we ask how much of one vector lies along another.