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

Commit c4cafb6

Browse files
committed
Delegate pearson function to the array based computation
1 parent 4390229 commit c4cafb6

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

algo/src/main/java/org/neo4j/graphalgo/similarity/Similarities.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.neo4j.graphalgo.similarity;
2020

21+
import org.neo4j.graphalgo.core.utils.Intersections;
2122
import org.neo4j.procedure.Description;
2223
import org.neo4j.procedure.Name;
2324
import org.neo4j.procedure.UserFunction;
@@ -75,25 +76,17 @@ public double pearsonSimilarity(@Name("vector1") List<Number> vector1, @Name("ve
7576
throw new RuntimeException("Vectors must be non-empty and of the same size");
7677
}
7778

78-
double vector1Mean = vector1.stream().mapToDouble(Number::doubleValue).average().orElse(1);
79-
double vector2Mean = vector2.stream().mapToDouble(Number::doubleValue).average().orElse(1);
79+
int len = Math.min(vector1.size(), vector2.size());
8080

81-
double dotProductMinusMean = 0d;
82-
double xLength = 0d;
83-
double yLength = 0d;
84-
for (int i = 0; i < vector1.size(); i++) {
85-
double weight1 = vector1.get(i).doubleValue();
86-
double weight2 = vector2.get(i).doubleValue();
87-
88-
double vector1Delta = weight1 - vector1Mean;
89-
double vector2Delta = weight2 - vector2Mean;
81+
double[] weights1 = new double[len];
82+
double[] weights2 = new double[len];
9083

91-
dotProductMinusMean += (vector1Delta * vector2Delta);
92-
xLength += vector1Delta * vector1Delta;
93-
yLength += vector2Delta * vector2Delta;
84+
for (int i = 0; i < len; i++) {
85+
weights1[i] = vector1.get(i).doubleValue();
86+
weights2[i] = vector2.get(i).doubleValue();
9487
}
9588

96-
return dotProductMinusMean / (Math.sqrt(xLength * yLength));
89+
return Intersections.pearson(weights1, weights2, len);
9790
}
9891

9992
@UserFunction("algo.similarity.euclideanDistance")

0 commit comments

Comments
 (0)