From 616a8db539d51f34379faf3517dbe0ed03ed1438 Mon Sep 17 00:00:00 2001 From: David Berdik Date: Tue, 17 Dec 2019 17:49:30 -0500 Subject: [PATCH] Fixed incorrect implementation of Bhattacharyya Distance and updated Bhattacharyya distance unit test --- .../distances/BhattacharyyaDistance.java | 19 +++++++--------- .../distances/BhattacharyyaDistanceTest.java | 22 ++++++++----------- 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/com/jgaap/distances/BhattacharyyaDistance.java b/src/com/jgaap/distances/BhattacharyyaDistance.java index cf9910a1a..e4abea20d 100644 --- a/src/com/jgaap/distances/BhattacharyyaDistance.java +++ b/src/com/jgaap/distances/BhattacharyyaDistance.java @@ -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 { @@ -36,16 +36,13 @@ public boolean showInGUI() { @Override public double distance(Histogram unknownHistogram, Histogram knownHistogram) throws DistanceCalculationException { - Set 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; } diff --git a/unittests/com/jgaap/distances/BhattacharyyaDistanceTest.java b/unittests/com/jgaap/distances/BhattacharyyaDistanceTest.java index 7ebbc92dc..e2fbfdea8 100644 --- a/unittests/com/jgaap/distances/BhattacharyyaDistanceTest.java +++ b/unittests/com/jgaap/distances/BhattacharyyaDistanceTest.java @@ -19,21 +19,17 @@ public void testDistance() EventSet set1 = new EventSet(); EventSet set2 = new EventSet(); Vector test1 = new Vector(); - 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 test2 = new Vector(); @@ -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)); } }