Cosine similarity measures the similarity of two vectors according to the cosine of the angle that’s between them. Meaning that two vectors that point to similar directions are considered similar.

It’s equal to:

Note

We can get the distance by just doing

The function ranges between and captures orientation instead of magnitude.

Let be the angle between the two vectors, having respectively and as angles between the axis.

We can define the coordinates of the 2D vectors as:

Then we can define the dot product of the two vectors as:

\begin{align*} \boldsymbol{x} \cdot \boldsymbol{y} = x_1y_1 + x_2y_2 = \|\boldsymbol{x}\|\cos\alpha\|\boldsymbol{y}\|\cos\beta + \|\boldsymbol{x}\|\sin\beta \|\boldsymbol{y}\|\sin\beta \\ = \|\boldsymbol{x}\|\|\boldsymbol{y}\|(\cos\alpha \cos\beta +\sin\alpha \sin\beta ) \\ = \|\boldsymbol{x}\|\|\boldsymbol{y}\|\cos(\alpha - \beta) \\ = \|\boldsymbol{x}\|\|\boldsymbol{y}\| \cos \theta \end{align*}

and so

This example was with vectors of 2 dimensions, but it applies with vectors of dimensions too.