Cosine similarity and Pearson correlation coefficient
The dot product
The problem of the dot product as a measure of alignment is that it’s always influenced by the magnitude of the compared vectors, therefore comparing two dot products trying to infer alignment similarity is meaningless.
Cosine similarity
Equation in dot-product-and-law-of-cosines when solved for gives the cosine similarity formula
that scales the dot product by the norms of the compared vectors, thus ignoring their magnitudes and therefore making cosine similarity a comparable measure of alignment similarity.
It’s worth noting, though, that cosine similarity, whether with normalized vectors —which have norms equal to 1, and would thus simplify the cosine similarity formula to since the denominator would precisely equal —or not, is always influenced by the baseline of the vectors: vectors with different offsets but equal deviations aren’t perfectly cosine-similar.
Pearson correlation coefficient
Take this one step further and do mean centering to also remove the baseline: get Pearson correlation coefficient
where the represents mean centering. Pearson correlation thus is an even more comparable measure of alignment similarity because it ignores both magnitudes and offsets.
Cosine similarity versus Pearson correlation
Cosine similarity measures alignment of raw vectors from the origin. Pearson correlation measures alignment of deviations from the mean. captures perfect linear relationship (), while captures perfect proportionality through the origin (, no intercept).
Progressive layers of forgiveness
Here’s a summary of the progressive layers of normalization
| Measure of similarity | Formula | = 1 when | Ignores |
|---|---|---|---|
| Dot product | never reaches an highest possible limit, has no real meaning in the context of alignment similarity. however, is meaningful: it indicates orthogonality regardless of magnitudes | nothing, any difference is captured | |
| Cosine similarity | , the two vectors are proportional, regardless of magnitude | magnitude | |
| Pearson correlation | , when the two centered vectors are proportional | magnitude and baseline |
Examples
| Vector a | Vector b | Dot | Cosine | Pearson |
|---|---|---|---|---|