|
18 | 18 | */ |
19 | 19 | package org.neo4j.graphalgo.similarity; |
20 | 20 |
|
| 21 | +import org.neo4j.graphalgo.core.utils.Intersections; |
21 | 22 | import org.neo4j.procedure.Description; |
22 | 23 | import org.neo4j.procedure.Name; |
23 | 24 | import org.neo4j.procedure.UserFunction; |
@@ -75,25 +76,17 @@ public double pearsonSimilarity(@Name("vector1") List<Number> vector1, @Name("ve |
75 | 76 | throw new RuntimeException("Vectors must be non-empty and of the same size"); |
76 | 77 | } |
77 | 78 |
|
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()); |
80 | 80 |
|
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]; |
90 | 83 |
|
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(); |
94 | 87 | } |
95 | 88 |
|
96 | | - return dotProductMinusMean / (Math.sqrt(xLength * yLength)); |
| 89 | + return Intersections.pearson(weights1, weights2, len); |
97 | 90 | } |
98 | 91 |
|
99 | 92 | @UserFunction("algo.similarity.euclideanDistance") |
|
0 commit comments