4
$\begingroup$

I am trying to find a simple similarity metric that will compare vectors of indeterminate length. These vectors will be populated with values between 1 and 5. In this situation a 1 is closer to a 2 then it is to a 5 etc etc.

I am new to this type of math. I naively considered using cosine similarity. However, when I looked into this further I realized that this probably wasn't the right metric for this sort of calculation.

Thanks in advance and I apologize for the newbie question.

Note: I will be programming this in PHP.

$\endgroup$
5
$\begingroup$

You could just use a norm: given vectors $\mathbf{x}$ and $\mathbf{y}$, we define the "2-norm" by $$ ||\mathbf{x} - \mathbf{y}||_2 = \sqrt{\sum_i (x_i - y_i)^2} $$ Similarly we define the"p-norm" by $$ ||\mathbf{x} - \mathbf{y}||_p = \left(\sum_i |x_i - y_i|^p\right)^{1/p} $$ In the limit, as $p \to \infty$, we get the "infinity norm" $$ ||\mathbf{x} - \mathbf{y}||_\infty = \max_i |x_i - y_i| $$

Note the two vectors are assumed to be the same length! If you want your "similarity metric" to be sensibly defined when, say, comparing two vectors of length 3 or two vectors of length 300, you probably have to normalize by the size. That is, you should choose a similarity metric of the form $$d(\mathbf{x},\mathbf{y}) = \frac{||\mathbf{x} - \mathbf{y}||_p}{n^{1/p}}$$ where the vectors are of length $n$.

edit: (I changed $d$ above slightly) to turn this distance metric into a similarity metric, I would abuse the fact that the vectors are known to range between $1$ and $5$. This tells me the maximum value that $d(\mathbf{x},\mathbf{y})$ can take occurs when one is a vector of all $1$s and the other is a vector of all $5$s. For the definition of $d$ above, the maximum value it takes is $4$. The proposed similarity metric is then: $$f(\mathbf{x},\mathbf{y}) = 4 - \frac{||\mathbf{x} - \mathbf{y}||_p}{n^{1/p}}$$

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you so much? However, my question is, in this situation, the more similar two vectors are, the smaller the norm will be. What is an easy way to invert this whereby more similar vectors output higher similarity values. $\endgroup$ – Spencer Mar 2 '11 at 18:13
  • $\begingroup$ @Spencer The norm in your case is capped; the vectors (4,4,4,4...) and (1,1,1,1...) are as far apart as possible. You could calculate the highest norm possible and subtract the actual norm from that if you want a similarity measure. $\endgroup$ – rm999 Mar 2 '11 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.