Search code examples
rcosine-similarity

Computing cosine.similarity in R gives different results compared to manual?


Here are my vectors:

lin_acc_mag_mean vel_ang_unc_mag_mean
             <dbl>                <dbl>
1            0.688                0.317


  lin_acc_mag_mean vel_ang_unc_mag_mean
             <dbl>                <dbl>
1             2.94                0.324

or for simplicity:

a <- c(.688,.317) 
b <- c(2.94, .324)

I want to compute tcR::cosine.similarity:

cosine.similarity(a,b, .do.norm = T) gives me 1.388816

If I will do it myself according to Wikipedia:

sum(c(.688,.317) * c(2.94, .324)) / (sqrt(sum(c(.688,.317) ^ 2)) * sqrt(sum(c(2.94, .324) ^ 2))) 

And I get 0.948604 so what is different here? Please advise. I suppose it is the normalization but will be happy for your help.


Solution

  • In the tcR package the cosine.similarity function contains the following:

    function (.alpha, .beta, .do.norm = NA, .laplace = 0) 
    {
        .alpha <- check.distribution(.alpha, .do.norm, .laplace)
        .beta <- check.distribution(.beta, .do.norm, .laplace)
        sum(.alpha * .beta)/(sum(.alpha^2) * sum(.beta^2))
    }
    

    The intervening check.distribution calculation returns a vector that sums to 1, but does not appear to be normalized.

    I'd recommend using the cosine function in the lsa package, instead. This one produces the correct value. It also permits calculation of the cosine similarity for a whole matrix of vectors organized in columns. For example, cosine(cbind(a,b,b,a)) yields the following:

             a        b        b        a
    a 1.000000 0.948604 0.948604 1.000000
    b 0.948604 1.000000 1.000000 0.948604
    b 0.948604 1.000000 1.000000 0.948604
    a 1.000000 0.948604 0.948604 1.000000