@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); }
@Override public double userSimilarity(long userID1, long userID2) throws TasteException { DataModel dataModel = getDataModel(); FastIDSet xPrefs = dataModel.getItemIDsFromUser(userID1); FastIDSet yPrefs = dataModel.getItemIDsFromUser(userID2); int xPrefsSize = xPrefs.size(); int yPrefsSize = yPrefs.size(); if (xPrefsSize == 0 && yPrefsSize == 0) { return Double.NaN; } if (xPrefsSize == 0 || yPrefsSize == 0) { return 0.0; } int intersectionSize = xPrefsSize < yPrefsSize ? yPrefs.intersectionSize(xPrefs) : xPrefs.intersectionSize(yPrefs); if (intersectionSize == 0) { return Double.NaN; } int unionSize = xPrefsSize + yPrefsSize - intersectionSize; return (double) intersectionSize / (double) unionSize; }
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); int xPrefsSize = xPrefs.size(); int yPrefsSize = yPrefs.size(); if (xPrefsSize == 0 && yPrefsSize == 0) { return Double.NaN; } if (xPrefsSize == 0 || yPrefsSize == 0) { return 0.0; } int intersectionSize = xPrefsSize < yPrefsSize ? yPrefs.intersectionSize(xPrefs) : xPrefs.intersectionSize(yPrefs); if (intersectionSize == 0) { return Double.NaN; } int unionSize = xPrefsSize + yPrefsSize - intersectionSize; return (double) intersectionSize / (double) unionSize; } }
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); int xPrefsSize = xPrefs.size(); int yPrefsSize = yPrefs.size(); if (xPrefsSize == 0 && yPrefsSize == 0) { return Double.NaN; } if (xPrefsSize == 0 || yPrefsSize == 0) { return 0.0; } int intersectionSize = xPrefsSize < yPrefsSize ? yPrefs.intersectionSize(xPrefs) : xPrefs.intersectionSize(yPrefs); if (intersectionSize == 0) { return Double.NaN; } int unionSize = xPrefsSize + yPrefsSize - intersectionSize; return (double) intersectionSize / (double) unionSize; } TanimotoCoefficientSimilarity(DataModel dataModel); }
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); int xPrefsSize = xPrefs.size(); int yPrefsSize = yPrefs.size(); if (xPrefsSize == 0 && yPrefsSize == 0) { return Double.NaN; } if (xPrefsSize == 0 || yPrefsSize == 0) { return 0.0; } int intersectionSize = xPrefsSize < yPrefsSize ? yPrefs.intersectionSize(xPrefs) : xPrefs.intersectionSize(yPrefs); if (intersectionSize == 0) { return Double.NaN; } int unionSize = xPrefsSize + yPrefsSize - 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(); }
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); int xPrefsSize = xPrefs.size(); int yPrefsSize = yPrefs.size(); if (xPrefsSize == 0 && yPrefsSize == 0) { return Double.NaN; } if (xPrefsSize == 0 || yPrefsSize == 0) { return 0.0; } int intersectionSize = xPrefsSize < yPrefsSize ? yPrefs.intersectionSize(xPrefs) : xPrefs.intersectionSize(yPrefs); if (intersectionSize == 0) { return Double.NaN; } int unionSize = xPrefsSize + yPrefsSize - 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();  }