-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDissimilarityMetricsTest.java
More file actions
executable file
·52 lines (45 loc) · 2.21 KB
/
Copy pathDissimilarityMetricsTest.java
File metadata and controls
executable file
·52 lines (45 loc) · 2.21 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
48
49
50
51
52
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.*;
import java.util.zip.CRC32;
import java.util.Random;
public class DissimilarityMetricsTest {
private static void fill(boolean[] v, Random rng) {
for(int i = 0; i < v.length; i++) {
v[i] = rng.nextBoolean();
}
// Ensure that at least one element is true no matter what.
v[rng.nextInt(v.length)] = true;
}
private static final int N = 1000;
@Test public void testJaccard() { testDissimilarity(N, 619021331L, 0); }
@Test public void testMatching() { testDissimilarity(N, 2582992579L, 1); }
@Test public void testDice() { testDissimilarity(N, 864445653L, 2); }
@Test public void testRogersTanimono() { testDissimilarity(N, 2631246168L, 3); }
@Test public void testRussellRao() { testDissimilarity(N, 3219060315L, 4); }
@Test public void testSokalSneath() { testDissimilarity(N, 737788739L, 5); }
private void testDissimilarity(int n, long expected, int mode) {
CRC32 check = new CRC32();
Random rng = new Random(12345);
for(int i = 0; i < n; i++) {
int nn = 5 + i / 20;
boolean[] v1 = new boolean[nn], v2 = new boolean[nn];
fill(v1, rng); fill(v2, rng);
Fraction result;
if(mode == 0) { result = DissimilarityMetrics.jaccardDissimilarity(v1, v2); }
else if(mode == 1) { result = DissimilarityMetrics.matchingDissimilarity(v1, v2); }
else if(mode == 2) { result = DissimilarityMetrics.diceDissimilarity(v1, v2); }
else if(mode == 3) { result = DissimilarityMetrics.rogersTanimonoDissimilarity(v1, v2); }
else if(mode == 4) { result = DissimilarityMetrics.russellRaoDissimilarity(v1, v2); }
else { result = DissimilarityMetrics.sokalSneathDissimilarity(v1, v2); }
check.update(result.getNum().intValue());
check.update(result.getDen().intValue());
// if(i < 10) {
// System.out.println(Arrays.toString(v1) + " " + Arrays.toString(v2) + " " + result);
// }
}
assertEquals(expected, check.getValue());
}
}