# compute the similarity between two vectors

Euclidean distance is a measure that may be used to compute the similarity between two vectors. Given a query $q$ and documents $d_1, \ldots, d_n$, we may rank the documents $\mathcal{D} = \{d_k\}_{k=1}^n$ in the increasing order of Euclidean distance from $q$.

Prove that if $q$ and the document vectors $\mathcal{D}$ are all normalized to unit vectors, then the rank ordering produced by Euclidean distance is identical to that produced by cosine similarity.

-
What is a cosine similarity? Computing $\dfrac{\vec{x}\cdot \vec{y}}{\|\vec{x}\| \|\vec{y}\|}$? –  gt6989b Mar 20 '13 at 17:10
–  benji_r Mar 20 '13 at 17:11
Doesn't seem true to me. Take $\mathcal{D}=\{(1,0), (0,1)\}$ and $q = (a,b)$ on the unit circle. Then, the cosine similarity would result in $q \cdot d_1 = a$ and $q \cdot d_2 = b$. Euclidean distance (we consider the square of the actual distance, it doesn't alter comparisons), however, would have $(a-1)^2+b^2$ and $a^2 + (b-1)^2$.
Note that $$\begin{split} (a-1)^2+b^2 &< a^2 + (b-1)^2 \\ a^2 -2a + 1+b^2 &< a^2 + b^2 -2b +1 \\ -2a &< -2b \\ b &< a \end{split}$$
So the distance ranking orders $d_1 < d_2$ iff the cosine ranking orders $d_2 < d_1$.