tverskyIndex<E> function

double tverskyIndex<E>(
  1. Iterable<E> source,
  2. Iterable<E> target, {
  3. double alpha = 0.5,
  4. double beta = 0.5,
})

Finds the Tversky similarity index between two lists.

Parameters

  • source is the variant set
  • target is the prototype set
  • alpha is the variant coefficient. Default is 0.5
  • beta is the prototype coefficient. Default is 0.5

Details

Tversky index is an asymmetric similarity measure between sets that compares a variant with a prototype. It is a generalization of the Sørensen–Dice coefficient and Jaccard index.

It may return NaN dependending on the values of alpha and beta.


Complexity: Time O(n log n) | Space O(n)

Implementation

double tverskyIndex<E>(
  Iterable<E> source,
  Iterable<E> target, {
  double alpha = 0.5,
  double beta = 0.5,
}) {
  if (source.isEmpty || target.isEmpty) return 0;

  Set<E> s = source is Set<E> ? source : source.toSet();
  Set<E> t = target is Set<E> ? target : target.toSet();

  // calculate intersection between source and target
  int c = 0;
  for (E e in t) {
    if (s.contains(e)) c++;
  }

  // calculate relative complement of target in source
  int a = s.length - c;

  // calculate relative complement of source in target
  int b = t.length - c;

  // calculate tversky index
  return c / (c + (alpha * a) + (beta * b));

  // double det = c + (alpha * a) + (beta * b);
  // return det == 0 ? 0 : c / det;
}