Solved – Proving that cosine distance function defined by cosine similarity between two unit vectors does not satisfy triangle inequality

cosine distancecosine similaritydistancemetricproof

How to prove that the cosine distance function defined by cosine similarity between two unit vectors does not satisfy the triangle inequality?

Best Answer

$$\text{cos-dist}(A, B) = 1 - \text{cos-sim}(A, B)$$ $$\text{cos-sim}(A, B) = \frac{\langle A, B \rangle}{||A|| \cdot ||B||} = \frac{\sum\limits_{i=1}^n A_i \cdot B_i}{\sqrt{\sum\limits_{i=1}^n A_i^2} \cdot \sqrt{\sum\limits_{i=1}^n B_i^2}}$$

Triangle inequality for cosine distance tooks a form of (of course it doesn't hold): $$\text{cos-dist}(A,C) \nleq \text{cos-dist}(A, B) + \text{cos-dist}(B, C)$$ which is equivalent to: $$1 - \text{cos-sim}(A,C) \nleq 1 - \text{cos-sim}(A, B) + 1 - \text{cos-sim}(B, C)$$ and after simple transformations: $$1 + \text{cos-sim}(A, C) \ngeq \text{cos-sim}(A, B) + \text{cos-sim}(B, C)$$

Now, you're trying to find such three vectors A, B and C that: $$1 + \text{cos-sim}(A, C) < \text{cos-sim}(A, B) + \text{cos-sim}(B, C)$$

Let $A, B, C \in \mathbb{R}^2$ and all of them are of unit length A = [1, 0], B = $\left[\frac{\sqrt{2}}{2}, \frac{\sqrt{2}}{2}\right]$, C = [0, 1]. Note that vectors A and C are orthogonal, so we would get simply $0$: $$\text{cos-sim}(A, C) = \frac{0}{\sqrt{1}\sqrt{1}} = 0$$ Each pair of vectors A & B as well as B & C would give the same value: $$ \text{cos-sim}(A, B) = \frac{\frac{\sqrt{2}}{2} + 0}{\sqrt{1}\sqrt{1}} = \frac{\sqrt{2}}{2},~~~ \text{cos-sim}(B, C) = \frac{0+\frac{\sqrt{2}}{2}}{\sqrt{1}\sqrt{1}} = \frac{\sqrt{2}}{2}$$. Finally, we could defeat primary inequality by proving that: $$ 1 + 0 < \frac{\sqrt{2}}{2} + \frac{\sqrt{2}}{2}$$ $$ 1 < \sqrt{2} \approx 1.41 \dots$$

Related Question