An Introduction to the Math
An Introduction to Vectors
Determining cosine similarity has everything to do with the concept of vectors. A vector is simply a quantity that has both a magnitude and direction. There are two standard notations for vectors: we can write a vector as $\langle 1, 1 \rangle$ or as $1\mathbf i + 1\mathbf j$. Both of these mean the same thing and can be plotted on a graph similarly to cartesian coordinates.
We can expand this definition to 3 dimensions by adding a third orthogonal line to our plane. Unlike regular 2d cartesian coordinates which are known as ordered pairs in the (x,y) format, 3 dimensions uses the (x,y,z) format. In vector land we denote these as $\langle 1, 2, 3 \rangle$, or $1\mathbf i + 2\mathbf j + 3\mathbf k$.
The Magnitude of a vector
If we have a vector $\mathbf a = \langle a_1, a_2, a_3 \rangle$, we can find its magnitude by applying the following formula:
$$\sqrt{a_1^2 + a_2^2 + a_3^2}$$
This is denoted as $\|\mathbf a\|$.
Cool, so now we know the basics of vectors, now just one last thing.
The Dot Product
The Dot Product is a technique used to find the product of two vectors. The output of the dot product is always a real number. Vectors go in, one number comes out. The formal definition for the product of two vectors $\mathbf a$ and $\mathbf b$ in two dimensional space is $a_1 b_1 + a_2 b_2$. If we want to take the dot product of a vector in three dimensional space we simply add the third component of the vector into the equation, making $a_1 b_1 + a_2 b_2 + a_3 b_3$.
The other Dot Product
But Andrew, this is about cosine similarity! Where is the trigonometry? Where is the cosine? Yes, there is another kinda worse form of the Dot Product.
$$\mathbf a \cdot \mathbf b = \|\mathbf a\| \|\mathbf b\| \cos\theta$$
As you can probably guess, this form is what we will spend our time working with.