Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What's doing guys? I was going to post this on MathOverflow initially, but got scared off by their FAQ, haha.

Apologies in advance if I butcher any of the terminology; I majored in math in undergrad but a lot of that knowledge left me like water through a sieve.

So, what I've been playing with are similarity calculations for n-dimensional vectors. Specifically, cosine similarity. What I start out with are multiple arrays/matrices/sets. For example:

SetN = { red, white, blue }

Set1 = { 30, 25, 25 }
Set2 = { 20, 18, 6 }

Where the individual elements represent frequency, e.g. for Set1, there were 30 instances of red, 25 instances of white, and 25 instances of blue, and so on.

The examples above are simplified, and the actual sets I'm working with have exponential distributions and other complications. This necessitates other questions which I will save for StackExchange. At the moment though, what I want to figure out is if I am calculating the cosine similarity correctly because with certain sets, I get answers that look plain wrong.

SetX = { 20, 0, 0 }
SetY = { 20, 20, 20 }

Norm = |( red, white, blue )| = sqrt( red^2 + white^2 + blue^2 )

Then I use the norm to convert the sets to unit vectors by dividing each element in the set:

uvX = { 1, 0, 0 }
uvY = { 1/sqrt(3), 1/sqrt(3), 1/sqrt(3) }

Then, I find the dot product to calculate the cosine similarity, and I get 1/sqrt(3). Which comes out to:


The thing is, even taking into account that I normalized the vectors to unit vectors, 20 units of red in Set X does not seem like 57.7% of Set Y with 20 red, 20 white, and 20 blue.

It seems like the similarity figure is a bit too large. Are my calculations botched? Or is there some kind of mental illusion I'm failing to see here.

share|cite|improve this question
up vote 2 down vote accepted

Your calculation is correct. I'm not sure what you mean by "20 units of red in Set X does not seem like 57.7% of Set Y with 20 red, 20 white, and 20 blue". You didn't calculate what fraction of Set Y is in Set X, you just calculated the cosine of an angle. The cosine would be $1$ for the vectors $(1,1,1)$ and $(100,100,100)$, which might seem even worse as a similarity measure, depending on what you want to do with it. If there's a problem here, it's in your choice of a similarity measure, not in your calculation of it.

share|cite|improve this answer
Hey thanks for the prompt response! Yeah, that's what I was doing. I was visualizing it as a fraction. What I want to do is to actually take a list of appended words (descriptors) from word/mindmaps and compare their similarities. The distribution of terms in these mindmaps follow the power law, which means ultimately I will not be calculating the similarity of mere unit vectors. I tried a preliminary tweak using TF-IDF, and the logarithm didn't work out (assuming I was doing the calculators correctly). – Murmur Aug 23 '11 at 12:53
@Murmur, does this mean joriki has successfully answered the question you posted? or is there still some part of it unanswered? – Gerry Myerson Aug 23 '11 at 12:55
@Gerry I guess it's answered. It's still kind of weird in my mind, but joriki triggered that 'epiphany' in me when he said the cosim wasn't a simple fraction. I guess I check off the answer then? – Murmur Aug 23 '11 at 13:35
@Murmur, if you're satisfied with the answer, then, yes, you click on the check (as I see you've done). – Gerry Myerson Aug 24 '11 at 1:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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