Skip to content

Commit

Permalink
Fixed incorrect implementation of Bhattacharyya Distance and updated …
Browse files Browse the repository at this point in the history
…Bhattacharyya distance unit test
  • Loading branch information
DavidBerdik committed Dec 17, 2019
1 parent 0591860 commit 616a8db
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
19 changes: 8 additions & 11 deletions src/com/jgaap/distances/BhattacharyyaDistance.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

/**
* Bhattacharyya Distance
* d = abs( log( sum( sqrt( abs(xi - yi) ) ) / sum(xi) ) )
* d = -log(sum(sqrt(xi * yi)))
*
* @author Adam Sargent
* @version 1.0
* @author David Berdik
* @version 2.0
*/

public class BhattacharyyaDistance extends DistanceFunction {
Expand All @@ -36,16 +36,13 @@ public boolean showInGUI() {
@Override
public double distance(Histogram unknownHistogram, Histogram knownHistogram)
throws DistanceCalculationException {

Set<Event> events = Sets.union(unknownHistogram.uniqueEvents(), knownHistogram.uniqueEvents());
double distance = 0.0;

double distance = 0.0, sumNumer = 0.0, sumDenom = 0.0;

for(Event event: events){
sumNumer += Math.sqrt(Math.abs(unknownHistogram.relativeFrequency(event) - knownHistogram.relativeFrequency(event)));
sumDenom += unknownHistogram.relativeFrequency(event);
}
distance = Math.abs(Math.log(sumNumer / sumDenom));
for(Event event : events)
distance += Math.sqrt(unknownHistogram.relativeFrequency(event) * knownHistogram.relativeFrequency(event));

distance = -Math.log(distance);
return distance;
}

Expand Down
22 changes: 9 additions & 13 deletions unittests/com/jgaap/distances/BhattacharyyaDistanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,17 @@ public void testDistance()
EventSet set1 = new EventSet();
EventSet set2 = new EventSet();
Vector<Event> test1 = new Vector<Event>();
test1.add(new Event("one", null));
test1.add(new Event("two", null));
test1.add(new Event("three", null));
test1.add(new Event("four", null));
test1.add(new Event("five", null));
test1.add(new Event("six", null));
test1.add(new Event("seven", null));
test1.add(new Event("eight", null));
test1.add(new Event("nine", null));
test1.add(new Event("ten", null));
test1.add(new Event("Lorem", null));
test1.add(new Event("Lorem", null));
test1.add(new Event("ipsum", null));
test1.add(new Event("ipsum", null));
test1.add(new Event("ipsum", null));
test1.add(new Event("ipsum", null));
test1.add(new Event("ipsum", null));
set1.addEvents(test1);
set2.addEvents(test1);
double result = new BhattacharyyaDistance().distance(new EventMap(set1), new EventMap(set2));
assertTrue(Double.isInfinite(result));

assertTrue(result == 0);

set2 = new EventSet();
Vector<Event> test2 = new Vector<Event>();
Expand All @@ -49,7 +45,7 @@ public void testDistance()
test2.add(new Event("10", null));
set2.addEvents(test2);
result = new BhattacharyyaDistance().distance(new EventMap(set1), new EventMap(set2));
assertTrue(DistanceTestHelper.inRange(result, Math.log(20 * Math.sqrt(.1)), 0.0000000001));
assertTrue(Double.isInfinite(result));
}

}

0 comments on commit 616a8db

Please sign in to comment.