import collections


def distance_l1(ground_truth, signal, correction=1.0):
  """Computes the L1 distance between two {'string': frequency} dicts.

  Args:
    ground_truth: The ground truth dict.
    signal: The obtained heavy hitters dict.
    correction: The correction to boost the signal. Often the ratio between the
      overall client population and the number of clients that participated in a
      round.

  Returns:
    The L1 distance between the signal and its ground truth.
  """
  joined = collections.defaultdict(float)
  for k, v in ground_truth.items():
    joined[k] += float(v)
  for k, v in signal.items():
    joined[k] -= float(correction) * float(v)
  total = 0
  for v in joined.values():
    total += abs(v)
  return total