Method: OpenTox::Algorithm::Similarity.weighted_cosine

Defined in:
lib/similarity.rb

.weighted_cosine(scaled_properties) ⇒ Float

Get weighted cosine similarity

http://stackoverflow.com/questions/1838806/euclidean-distance-vs-pearson-correlation-vs-cosine-similarity


54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/similarity.rb', line 54

def self.weighted_cosine scaled_properties 
  a,b,w = remove_nils scaled_properties
  return cosine(scaled_properties) if w.uniq.size == 1
  dot_product = 0
  magnitude_a = 0
  magnitude_b = 0
  (0..a.size-1).each do |i|
    dot_product += w[i].abs*a[i]*b[i]
    magnitude_a += w[i].abs*a[i]**2
    magnitude_b += w[i].abs*b[i]**2
  end
  dot_product/(Math.sqrt(magnitude_a)*Math.sqrt(magnitude_b))
end