TanimotoCoefficientSimilarity extends AbstractItemSimilarity implements UserSimilarity { @Override public double userSimilarity(long userID1, long userID2) throws TasteException { DataModel dataModel = getDataModel(); FastIDSet xPrefs = dataModel.getItemIDsFromUser(userID1); FastIDSet yPrefs = dataModel.getItemIDsFromUser(userID2); if (xPrefs.isEmpty() && yPrefs.isEmpty()) { return Double.NaN; } if (xPrefs.isEmpty() || yPrefs.isEmpty()) { return 0.0; } int intersectionSize = xPrefs.intersectionSize(yPrefs); if (intersectionSize == 0) { return Double.NaN; } int unionSize = xPrefs.size() + yPrefs.size() - intersectionSize; return (double) intersectionSize / (double) unionSize; } TanimotoCoefficientSimilarity(DataModel dataModel); @Override void setPreferenceInferrer(PreferenceInferrer inferrer); @Override double userSimilarity(long userID1, long userID2); @Override double itemSimilarity(long itemID1, long itemID2); @Override double[] itemSimilarities(long itemID1, long[] itemID2s); @Override void refresh(Collection<Refreshable> alreadyRefreshed); @Override String toString();  }
@Test public void testNoCorrelation() throws Exception { DataModel dataModel = getDataModel( new long[] {1, 2}, new Double[][] { {null, 2.0, 3.0}, {1.0}, }); double correlation = new TanimotoCoefficientSimilarity(dataModel).userSimilarity(1, 2); assertCorrelationEquals(Double.NaN, correlation); }
@Test public void testFullCorrelation1() throws Exception { DataModel dataModel = getDataModel( new long[] {1, 2}, new Double[][] { {1.0}, {1.0}, }); double correlation = new TanimotoCoefficientSimilarity(dataModel).userSimilarity(1, 2); assertCorrelationEquals(1.0, correlation); }
@Test public void testFullCorrelation2() throws Exception { DataModel dataModel = getDataModel( new long[] {1, 2}, new Double[][] { {1.0, 2.0, 3.0}, {1.0}, }); double correlation = new TanimotoCoefficientSimilarity(dataModel).userSimilarity(1, 2); assertCorrelationEquals(0.3333333333333333, correlation); }
@Test public void testCorrelation1() throws Exception { DataModel dataModel = getDataModel( new long[] {1, 2}, new Double[][] { {null, 2.0, 3.0}, {1.0, 1.0}, }); double correlation = new TanimotoCoefficientSimilarity(dataModel).userSimilarity(1, 2); assertEquals(0.3333333333333333, correlation, EPSILON); }
@Test public void testCorrelation2() throws Exception { DataModel dataModel = getDataModel( new long[] {1, 2}, new Double[][] { {null, 2.0, 3.0, 1.0}, {1.0, 1.0, null, 0.0}, }); double correlation = new TanimotoCoefficientSimilarity(dataModel).userSimilarity(1, 2); assertEquals(0.5, correlation, EPSILON); }