# https://en.wikipedia.org/wiki/Cosine_similarity # Cosine similarity measures how alike or different two vectors are # It divides the (dot) product between two vectors by the magnitude of each vector # make up vectors of the same length A = c(2, -4, 5, -3, 1) #B = c(1, 2, 3, -2, 2) #B = c(4, -8, 10, -6, 2) # B=2*A #B = c(-2, 4, -5, 3, -1) # B= -A #B = c(-2, -1, -3, -5, 0) # B is orthogonal to A # a product of two vectors prod = A*B # the dot-product is the sum of the elements above dot_prod= sum(prod) # a vector's magnitude is the square root of its dot product with itself mag_A = sqrt(sum(A*A)) mag_B = sqrt(sum(B*B)) # cos similarity should be between -1 and 1 cos_simil = dot_prod/(mag_A*mag_B)