public Double evaluate(HashMap<String, Double> histogram) {
    if (histogram == null) {
      return null;
    }

    double total = 0.0;
    for (Double value : histogram.values()) {
      if (value != null) {
        if (value >= 0) {
          total += value;
        } else {
          return null;
        }
      }
    }

    if (total == 0) {
      return Double.valueOf(0.0);
    }

    double entropy = 0.0;
    for (Double value : histogram.values()) {
      if (value != null && value > 0) {
        entropy -= (value / total) * Math.log(value / total);
      }
    }

    // Clip small negative values.
    if (entropy < 0) {
      entropy = 0;
    } else {
      entropy /= log2;
    }

    return Double.valueOf(entropy);
  }