Module: Measurable::Tanimoto

Included in:
Measurable
Defined in:
lib/measurable/tanimoto.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

:nodoc:


32
33
34
35
36
37
38
39
# File 'lib/measurable/tanimoto.rb', line 32

def self.extended(base) # :nodoc:
  # Tanimoto similarity is the same as Jaccard similarity.
  base.instance_eval do
    extend Measurable::Jaccard
    alias :tanimoto_similarity :jaccard
  end
  super
end

.included(base) ⇒ Object

:nodoc:


41
42
43
44
45
46
47
# File 'lib/measurable/tanimoto.rb', line 41

def self.included(base) # :nodoc:
  base.class_eval do
    include Measurable::Jaccard
    alias :tanimoto_similarity :jaccard
  end
  super
end

Instance Method Details

#tanimoto(u, v) ⇒ Object

call-seq:

tanimoto(u, v) -> Float

Tanimoto distance is a coefficient explicitly chosen such as to allow for two dissimilar specimens to be similar to a third one. This breaks the triangle inequality, thus this isn't a metric.

More information and references on this are needed. It's left here mostly as a piece of curiosity.

See: # en.wikipedia.org/wiki/Jaccard_index#Tanimoto.27s_Definitions_of_Similarity_and_Distance

Arguments:

  • u -> An array of Numeric objects.

  • v -> An array of Numeric objects.

Returns:

  • A measure of the similarity between u and v.

Raises:

  • ArgumentError -> The sizes of u and v don't match.

Raises:

  • (ArgumentError)

25
26
27
28
29
30
# File 'lib/measurable/tanimoto.rb', line 25

def tanimoto(u, v)
  # TODO: Change this to a more specific, custom-made exception.
  raise ArgumentError if u.size != v.size

  -Math.log2(jaccard_index(u, v))
end