-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimilarityCalculatorPearsonImpl.java
More file actions
47 lines (39 loc) · 1.82 KB
/
SimilarityCalculatorPearsonImpl.java
File metadata and controls
47 lines (39 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package ie.atu.sw;
/**
* Implementation of the SimilarityCalculator interface for calculate
* correlation between two vectors using Pearson algorithm
*/
public class SimilarityCalculatorPearsonImpl implements SimilarityCalculator {
/**
* Calculates Pearson coefficient
*
* @param vectorA first word vector
* @param vectorB second word vector
* @return A value between -1(less similar) and 1(perfect match)
* @throws IllegalArgumentException if vector have different length
* @throws ArithmeticException occurs if denominator is zero
*/
@Override
public double calculateSimilarity(double[] vectorA, double[] vectorB) {
// Big O = O(n) n. of words in cycle for
if (vectorA.length != vectorB.length) {
throw new IllegalArgumentException("Ops .. Vector must have same lenght!");
}
int n = vectorA.length;
double sumA = 0.0, sumB = 0.0, sumASquared = 0.0, sumBSquared = 0.0, sumProduct = 0.0;
for (int i = 0; i < n; i++) {//Big O = O(n)
sumA += vectorA[i]; //Big O = O(1)
sumB += vectorB[i]; //Big O = O(1)
sumASquared += vectorA[i] * vectorA[i]; //Big O = O(1)
sumBSquared += vectorB[i] * vectorB[i]; //Big O = O(1)
sumProduct += vectorA[i] * vectorB[i]; //Big O = O(1)
}
// Pearson Algorithm
double numerator = sumProduct - ((sumA * sumB) / n); //Big O = O(1)
double denominator = Math.sqrt((sumASquared - (sumA * sumA) / n) * (sumBSquared - (sumB * sumB) / n)); //Big O = O(1)
if (denominator == 0) {
throw new ArithmeticException("Ops.. Denominator equal zero");
}
return numerator / denominator;
}
}