@Override public double distance(Histogram unknownHistogram, Histogram knownHistogram) { Set<Event> events = Sets.union(unknownHistogram.uniqueEvents(), knownHistogram.uniqueEvents()); double distance = 0.0, sumNumer = 0.0, sumDenom = 0.0; for (Event event : events) { double known = knownHistogram.relativeFrequency(event); double unknown = unknownHistogram.relativeFrequency(event); sumNumer += Math.abs(unknown - known); sumDenom += unknown + known; } distance = sumNumer / sumDenom; return distance; }
@Override public double distance(Histogram unknownHistogram, Histogram knownHistogram) throws DistanceCalculationException { Set<Event> events = Sets.union(unknownHistogram.uniqueEvents(), knownHistogram.uniqueEvents()); double distance = 0.0, sumNumer = 0.0, sumUnknown = 0.0, sumKnown = 0.0; for (Event event : events) { sumNumer += unknownHistogram.relativeFrequency(event) * knownHistogram.relativeFrequency(event); sumUnknown += unknownHistogram.relativeFrequency(event); sumKnown += knownHistogram.relativeFrequency(event); } distance = Math.sqrt(2 - 2 * (sumNumer / Math.sqrt(sumUnknown * sumUnknown * sumKnown * sumKnown))); return distance; }