def compute_divergence(compound_counts_1, compound_counts_2, coef=0.1):
  """Compute compound divergence using Chernoff coefficient."""
  sum_1 = sum(compound_counts_1.values())
  sum_2 = sum(compound_counts_2.values())
  if not sum_1 or not sum_2:
    return 1.0

  # For efficiency, we avoid normalizing compound counts into relative
  # frequencies until later.
  # Note that: (a / b)**coef = (a**coef) / (b**coef)
  numerator = 0.0
  for compound, count_1 in compound_counts_1.items():
    if compound not in compound_counts_2:
      # Contribution will be 0.
      continue
    count_2 = compound_counts_2[compound]
    numerator += count_1**coef * count_2**(1.0 - coef)

  similarity = numerator / (sum_1**coef * sum_2**(1.0 - coef))
  return 1.0 - similarity
Compute compound divergence using Chernoff coefficient.
language/compgen/nqg/tasks/mcd_utils.py