code: public static <E> double klDivergence(Counter<E> from, Counter<E> to) {
      double result = 0.0;
      double tot = (from.totalCount());
      double tot2 = (to.totalCount());
      // System.out.println("tot is " + tot + " tot2 is " + tot2);
      for (E key : from.keySet()) {
        double num = (from.getCount(key));
        if (num == 0) {
          continue;
        }
        num /= tot;
        double num2 = (to.getCount(key));
        num2 /= tot2;
        // System.out.println("num is " + num + " num2 is " + num2);
        double logFract = Math.log(num / num2);
        if (logFract == Double.NEGATIVE_INFINITY) {
          return Double.NEGATIVE_INFINITY; // can't recover
        }
        result += num * (logFract / LOG_E_2); // express it in log base 2
      }
      return result;
    }
docs: Calculates the KL divergence between the two counters. That is, it
  calculates KL(from || to). This method internally uses normalized counts
  (so they sum to one), but the value returned is meaningless if any of the
  counts are negative. In other words, how well can c1 be represented by c2.
  if there is some value in c1 that gets zero prob in c2, then return
  positive infinity.
  
  @return The KL divergence between the distributions