This function computes the cosine similarity between two vectors, defined by $$\cos(\theta) = \frac{x \cdot y}{\|x\|_2\|y\|_2}.$$
cosine_similarity(x, y)
| x | A numeric vector |
|---|---|
| y | A numeric vector, the same length as |
Generally, a scalar between \(-1\) and \(1\).
Or, if x and y are non-negative, a value between \(0\) and \(1\).
Cosine similarity is related to Euclidean distance by
$$\|x - y \|^2 = 2(1 - \cos(\theta)).$$
So $$\cos(\theta) = 1 - \frac12\|x - y\|^2,$$
assuming x and y have been normalised to be unit vectors.
Therefore, if we want to maximise cosine similarity, we can minimise Euclidean distance
and then make the conversion. See nearest_point().
https://en.wikipedia.org/wiki/Cosine_similarity
nearest_cosine(), cos(), acos()