Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Fixed calculation of denominator for Jaccard-similarity #907

Open
wants to merge 3 commits into
base: 3.5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ public double jaccardSimilarity(@Name("vector1") List<Number> vector1, @Name("ve
if (vector1 == null || vector2 == null) return 0;

HashSet<Number> intersectionSet = new HashSet<>(vector1);

// add size of vector1 and vector2 (ignoring duplicates) before calling retainAll(vector2)
long denom_sum = intersectionSet.size() + new HashSet<>(vector2).size();

intersectionSet.retainAll(vector2);
int intersection = intersectionSet.size();

long denominator = vector1.size() + vector2.size() - intersection;
long denominator = denom_sum - intersection;
return denominator == 0 ? 0 : (double) intersection / denominator;
}

Expand Down Expand Up @@ -172,10 +176,14 @@ public double overlapSimilarity(@Name("vector1") List<Number> vector1, @Name("ve
if (vector1 == null || vector2 == null) return 0;

HashSet<Number> intersectionSet = new HashSet<>(vector1);

long size1 = intersectionSet.size();
long size2 = new HashSet<>(vector2).size();

intersectionSet.retainAll(vector2);
int intersection = intersectionSet.size();

long denominator = Math.min(vector1.size(), vector2.size());
long denominator = Math.min(size1, size2);
return denominator == 0 ? 0 : (double) intersection / denominator;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (c) 2017 "Neo4j, Inc." <http://neo4j.com>
*
* This file is part of Neo4j Graph Algorithms <http://github.com/neo4j-contrib/neo4j-graph-algorithms>.
*
* Neo4j Graph Algorithms is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.graphalgo.similarity;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.*;

@RunWith(Parameterized.class)
public class SimilaritiesTest {

private final List<Number> input;

@Parameterized.Parameters(name = "{0}")
public static Collection<List<Number>> data() {
return Arrays.asList(
Arrays.asList(1, 2, 3),
Arrays.asList(1, 2, 3, 3),
Arrays.asList(104, 101, 108, 108, 111)
);
}

public SimilaritiesTest(List<Number> input) {
this.input = input;
}

@Test
public void jaccardIdenticalInput() {
// given identical input

// when
Similarities s = new Similarities();
double result = s.jaccardSimilarity(input, input);

// then
assertEquals(1.0, result, 0.01);
}

@Test
public void cosineIdenticalInput() {
// given identical input

// when
Similarities s = new Similarities();
double result = s.cosineSimilarity(input, input);

// then
assertEquals(1.0, result, 0.01);
}

@Test
public void pearsonIdenticalInput() {
// given identical input

// when
Similarities s = new Similarities();
double result = s.pearsonSimilarity(input, input, Collections.emptyMap());

// then
assertEquals(1.0, result, 0.01);
}

@Test
public void euclideanIdenticalInput() {
// given identical input

// when
Similarities s = new Similarities();
double result = s.euclideanSimilarity(input, input);

// then
assertEquals(1.0, result, 0.01);
}

@Test
public void overlapIdenticalInput() {
// given identical input

// when
Similarities s = new Similarities();
double result = s.overlapSimilarity(input, input);

// then
assertEquals(1.0, result, 0.01);
}
}