# Simple similarity metric

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.

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}}$$