JaccardDistance extends FastDistance { @Override public double calc(double[] array1, double[] array2) { if(array1.length > array2.length){ double[] tmp = array1; array1 = array2; array2 = tmp; } int intersect = 0, union = 0; for(int i = 0; i < array1.length; i++){ if(array1[i] != 0 && array2[i] != 0){ intersect++; union++; }else if(array1[i] != 0 || array2[i] != 0){ union++; } } for(int i = array1.length; i < array2.length; i++){ if(array2[i] != 0){ union++; } } return 1 - (double) intersect / union; }  @Override double calc(double[] array1, double[] array2); @Override double calc(Vector vec1, Vector vec2); @Override void updateLabel(FastDistanceData data);  }
@Test public void testContinuousDistance() { DenseVector vec1 = new DenseVector(new double[] {1, 0, 4, 0, 3}); DenseVector vec2 = new DenseVector(new double[] {0, 6, 1, 0, 4}); SparseVector vec3 = new SparseVector(5, new int[] {1, 3}, new double[] {0.1, 0.4}); SparseVector vec4 = new SparseVector(5, new int[] {2, 3}, new double[] {0.4, 0.1}); ContinuousDistance distance = new JaccardDistance(); Assert.assertEquals(distance.calc(vec1, vec2), 0.5, 0.01); Assert.assertEquals(distance.calc(vec1.getData(), vec2.getData()), 0.5, 0.01); Assert.assertEquals(distance.calc(vec1, vec3), 1.0, 0.01); Assert.assertEquals(distance.calc(vec3, vec4), 0.66, 0.01); Assert.assertEquals(distance.calc(vec3, vec1), 1.0, 0.01); }