tverskyIndex<E> function
Finds the Tversky similarity index between two lists.
Parameters
source
is the variant settarget
is the prototype setalpha
is the variant coefficient. Default is 0.5beta
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;
}