Dot product, norms, and similarity

core 10 minvectorssimilarityembeddingsdistances

The idea

The dot product is the one operation underneath almost every notion of similarity in data science. Computationally it is trivial:

a⋅b=∑i=1daibia \cdot b = \sum_{i=1}^{d} a_i b_i

Geometrically it is a signed alignment score:

a⋅b=∥a∥ ∥b∥cos⁡θa \cdot b = \|a\| \, \|b\| \cos\theta

where θ\theta is the angle between the vectors. Same direction: large and positive. Orthogonal: zero. Opposite: negative. Two things scale it — how long the vectors are, and how aligned they are. Every similarity measure you use is a choice about which of those two factors you want to keep.

From the dot product you get norms (size):

∥a∥2=a⋅a=∑iai2,∥a∥1=∑i∣ai∣\|a\|_2 = \sqrt{a \cdot a} = \sqrt{\textstyle\sum_i a_i^2}, \qquad \|a\|_1 = \textstyle\sum_i |a_i|

and Euclidean distance (separation), tied back to the dot product by an identity worth memorizing:

∥a−b∥2=∥a∥2+∥b∥2−2 a⋅b\|a - b\|^2 = \|a\|^2 + \|b\|^2 - 2 \, a \cdot b

Cosine similarity strips magnitude out and keeps only alignment:

cos-sim(a,b)=a⋅b∥a∥ ∥b∥=cos⁡θ∈[−1,1]\text{cos-sim}(a, b) = \frac{a \cdot b}{\|a\| \, \|b\|} = \cos\theta \in [-1, 1]

The decision rule between them: is magnitude signal or nuisance? Document length, purchase counts, and embedding norms are usually nuisance — use cosine. Sensor readings, standardized features, and coordinates are usually signal — use Euclidean. And the two choices are secretly one after normalization: for unit vectors, ∥a−b∥2=2−2cos⁡θ\|a - b\|^2 = 2 - 2\cos\theta, so Euclidean ranking on L2-normalized vectors is cosine ranking. That is why the standard production pattern is "normalize once at index time, then use whichever the index computes fastest."

One more reusable tool: the projection of aa onto bb,

projb(a)=a⋅b∥b∥2 b\text{proj}_b(a) = \frac{a \cdot b}{\|b\|^2} \, b

is "the part of aa that lies along bb" — the geometric primitive behind least squares, PCA reconstruction, and orthogonalization.

Drag the two vectors below and watch dot product and cosine similarity respond differently to length versus direction:

Dot product
5.40
Cosine similarity
0.66
Angle
49°

Drag either vector: stretching one along its own direction moves the dot product but leaves cosine similarity unchanged — magnitude is nuisance, alignment is signal.

Worked example

Word-count vectors over the vocabulary (data, model, pizza):

DocVectorDescription
AA(2,1,0)(2, 1, 0)short ML article
BB(4,2,0)(4, 2, 0)the same article pasted twice
CC(0,1,2)(0, 1, 2)restaurant review

Compute similarities of BB and CC against AA:

For BB against AA:

A⋅B=2⋅4+1⋅2=10,∥A∥=5≈2.24,∥B∥=20≈4.47,cos⁡(A,B)=10520=1.0,∥A−B∥=(−2)2+(−1)2=5≈2.24.\begin{aligned} A \cdot B &= 2\cdot4 + 1\cdot2 = 10, \\ \lVert A\rVert &= \sqrt{5} \approx 2.24, & \lVert B\rVert &= \sqrt{20} \approx 4.47, \\ \cos(A,B) &= \frac{10}{\sqrt{5}\sqrt{20}} = 1.0, \\ \lVert A-B\rVert &= \sqrt{(-2)^2+(-1)^2} = \sqrt{5} \approx 2.24. \end{aligned}

For CC against AA:

A⋅C=1,∥C∥=5,cos⁡(A,C)=155=0.2,∥A−C∥=4+0+4=8≈2.83.\begin{aligned} A \cdot C &= 1, & \lVert C\rVert &= \sqrt{5}, \\ \cos(A,C) &= \frac{1}{\sqrt{5}\sqrt{5}} = 0.2, \\ \lVert A-C\rVert &= \sqrt{4+0+4} = \sqrt{8} \approx 2.83. \end{aligned}

Read the punchline off the numbers: Euclidean distance claims the pasted duplicate BB (distance 2.242.24) is nearly as far from AA as the pizza review CC (distance 2.832.83). Cosine correctly says BB is the same document (1.01.0) and CC is off-topic (0.20.2). Magnitude — here, document length — was pure nuisance, and Euclidean distance charged for it.

Now normalize everything to unit length and recompute Euclidean distance: A^=B^≈(0.894,0.447,0)\hat{A} = \hat{B} \approx (0.894, 0.447, 0), so ∥A^−B^∥=0\|\hat{A} - \hat{B}\| = 0, and ∥A^−C^∥2=2−2(0.2)=1.6\|\hat{A} - \hat{C}\|^2 = 2 - 2(0.2) = 1.6. The normalized Euclidean ranking now agrees with cosine exactly, as the identity promises.

Cheat sheet

Core formulas

QuantityFormulaNotes
Dot producta⋅b=∑iaibi=∥a∥∥b∥cos⁡θa \cdot b = \sum_i a_i b_i = \lVert a \rVert \lVert b \rVert \cos\thetasigned alignment
L2 norm∥a∥2=a⋅a\lVert a \rVert_2 = \sqrt{a \cdot a}rotation-invariant
L1 norm∥a∥1=∑i∣ai∣\lVert a \rVert_1 = \sum_i \lvert a_i \rvertrobust to one big coordinate
Euclidean distance∥a−b∥2\lVert a - b \rVert_2squared: ∥a−b∥22=∥a∥2+∥b∥2−2a⋅b\lVert a - b \rVert_2^2 = \lVert a \rVert^2 + \lVert b \rVert^2 - 2 a \cdot b
Cosine similaritya⋅b∥a∥∥b∥∈[−1,1]\frac{a \cdot b}{\lVert a \rVert \lVert b \rVert} \in [-1, 1]magnitude-blind
Projection of aa on bba⋅b∥b∥2b\frac{a \cdot b}{\lVert b \rVert^2} bbasis of least squares and PCA

Decision rules

Gotchas