![JAR search and dependency download from the Maven repository](/logo.png)
org.simmetrics.metrics.EuclideanDistance Maven / Gradle / Ivy
/*
* #%L
* Simmetrics Core
* %%
* Copyright (C) 2014 - 2015 Simmetrics Authors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package org.simmetrics.metrics;
import static com.google.common.collect.Multisets.union;
import static java.lang.Math.sqrt;
import org.simmetrics.MultisetDistance;
import org.simmetrics.MultisetMetric;
import com.google.common.collect.Multiset;
/**
* Calculates the Euclidean distance and similarity over two multisets.
*
*
* similarity(a,b) = 1 - distance(a,b) / √(∣a∣² + ∣b∣²)
* distance(a,b) = ∣∣a - b∣∣
*
*
*
*
* This class is immutable and thread-safe.
*
* @see Wikipedia - Euclidean Distance
* @param
* type of the token
*
*/
public final class EuclideanDistance implements MultisetMetric, MultisetDistance {
@Override
public float compare(Multiset a, Multiset b) {
if (a.isEmpty() && b.isEmpty()) {
return 1.0f;
}
float maxDistance = (float) sqrt((a.size() * a.size()) + (b.size() * b.size()));
return 1.0f - distance(a, b) / maxDistance;
}
@Override
public float distance(Multiset a, Multiset b) {
// Lager set first for performance improvement.
// See: MultisetUnionSize benchmark
if(a.size() < b.size()){
final Multiset swap = a; a = b; b = swap;
}
float distance = 0.0f;
for (T token : union(a, b).elementSet()) {
float frequencyInA = a.count(token);
float frequencyInB = b.count(token);
distance += ((frequencyInA - frequencyInB) * (frequencyInA - frequencyInB));
}
return (float) sqrt(distance);
}
@Override
public String toString() {
return "EuclideanDistance";
}
}