# Cosine Similarity Implementation

This is my first time posting a question in here so please bear with me. I am trying to calculate cosine similarity for two set of data. The cosine similarity formula as I understand is as given below:

Cosine Similarity

(where A_i and B_i are components of vector A and B respectively.)

Ochiai Coefficient

n(A) = number of elements of A
n(B) = number of elements of B
= size of the intersection of A and B

However, my peer who is also working on the same project uses a slightly different formula - Ochiai Coefficient. The two formulas doesn't look similar to me. I am not from a math background and I am having problems understanding these two formulas.

The data we are trying to calculate is similarity between two sets of data as shown below:

set A = {a, b, c, d, e}
set B = {b, c, k, p, r, s}

Union A_B = {a, b, c, d, e, k, p, r, s}
vector A  = {1, 1, 1, 1, 1, 0, 0, 0, 0}
vector B  = {0, 1, 1, 0, 0, 1, 1, 1, 1}


Both formula gives the same similarity for the data sets given above.

Cosine Similarity = 0.3651
K = 0.3561


So, I guess my question would be are both the formulas the same? It would be easier to implement the second formula as it would eliminate the use of matrix that could potentially get really large.

• Lots of unexplained notation there. I'm guessing $n(A)$ stands for the number of elements of $A$, so for the second formula, the numerator is the size of the intersection, which is 2, and the denominator is $\sqrt{5\times6}=\sqrt{30}$. I'm guessing the denominator of the first formula is $n(A)\times n(B)=30$, but I don't know how to interpret the first numerator to get $2\sqrt{30}$, which is what you need for the two to be equal. So, what exactly do all those symbols stand for? Feb 1, 2016 at 10:12
• The edit makes it even more confusing. You're calling $A$ and $B$ sets, but you're also calling them vectors, and you're taking the dot product of two vectors, one with 5 components, the other with 6, which makes no sense. How do you take a dot product of $\{a,b,c,d,e\}$ and $\{b,c,k,p,r,s\}$, and come up with a number? Feb 1, 2016 at 10:47
• @GerryMyerson A and B are sets and n(A) is the number of elements in A. A can be represented as bit vector. en.wikipedia.org/wiki/Cosine_similarity. Sorry for the confusion. I am as confused as you are hence my question here. But I got the formulas from wikipedia itself. Feb 1, 2016 at 10:59
• @Cryssie It's crucial to your question that your vectors are bit vectors that indicate which elements of a set are included in the sets that you are comparing. Put that information in the question! (I think in this particular case the two formulas agree and you can use the simpler one. With more general vector elements the question would not make much sense.)
– user306935
Feb 1, 2016 at 11:09
• Incidentally, such bit vectors are not vectors in the sense that mathematics uses the word (unless you can provide some relevant notion of scaling and adding them that always produce bit vectors and satisfy the equations of a vector space, but I doubt you have any such notion in mind).
– user306935
Feb 1, 2016 at 11:14

OK, so now it should be clear that $A\cdot B$ adds up the number of places where both $A$ and $B$ have a 1, so it gives you the number of elements $A$ and $B$ have in common, so it's just $n(A\cap B)$, and $|A|$ is the length of $A$, which is the square root of the sum of the squares of the entries of $A$, which is the square root of the number of ones in the bitvector $A$, which is the square root of the number of elements of the set $A$, which is $\sqrt{n(A)}$. So is it clear now that the two formulas are identical? And what's more, that they must take exactly the same amount of effort to compute?

• Thanks for the explanation. I know they are somewhat equivalent but I am trying to figure out how the second formula is derived. It would be easy to implement a formula without understanding it but understanding goes a long way for me. The second formula would be easier to implement in my case. Feb 1, 2016 at 11:50