Class: Suggestor::Algorithms::EuclideanDistance

Inherits:
Object
  • Object
show all
Includes:
RecommendationAlgorithm
Defined in:
lib/suggestor/algorithms/euclidean_distance.rb

Overview

The closest they are, the more similar their tastes are. More info at:

http://en.wikipedia.org/wiki/Euclidean_metric
http://en.wikipedia.org/wiki/Distance_correlation

Instance Attribute Summary

Attributes included from RecommendationAlgorithm

#collection

Instance Method Summary collapse

Methods included from RecommendationAlgorithm

#initialize, #recommended_to, #shared_items, #similar_related_to, #similar_to

Instance Method Details

#inverse_of_squares(first, second) ⇒ Object



30
31
32
# File 'lib/suggestor/algorithms/euclidean_distance.rb', line 30

def inverse_of_squares(first, second)
  1/(1+Math.sqrt(sum_squares(first, second)))
end

#similarity_score(first, second) ⇒ Object



25
26
27
28
# File 'lib/suggestor/algorithms/euclidean_distance.rb', line 25

def similarity_score(first, second)
  return 0.0 if nothing_shared?(first, second)
  inverse_of_squares(first, second)
end

#sum_squares(first, second) ⇒ Object



34
35
36
37
38
# File 'lib/suggestor/algorithms/euclidean_distance.rb', line 34

def sum_squares(first, second)
  shared_items(first, second).inject(0.0) do |sum, item|
    sum + ( values_for(first)[item] - values_for(second)[item] ) ** 2
  end
end