LogLikelihoodSimilarity extends AbstractItemSimilarity implements UserSimilarity { @Override public double itemSimilarity(long itemID1, long itemID2) throws TasteException { DataModel dataModel = getDataModel(); long preferring1 = dataModel.getNumUsersWithPreferenceFor(itemID1); long numUsers = dataModel.getNumUsers(); return doItemSimilarity(itemID1, itemID2, preferring1, numUsers); } LogLikelihoodSimilarity(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 testCorrelation() throws Exception { DataModel dataModel = getDataModel( new long[] {1, 2, 3, 4, 5}, new Double[][] { {1.0, 1.0}, {1.0, null, 1.0}, {null, null, 1.0, 1.0, 1.0}, {1.0, 1.0, 1.0, 1.0, 1.0}, {null, 1.0, 1.0, 1.0, 1.0}, }); LogLikelihoodSimilarity similarity = new LogLikelihoodSimilarity(dataModel); assertCorrelationEquals(0.12160727029227925, similarity.itemSimilarity(1, 0)); assertCorrelationEquals(0.12160727029227925, similarity.itemSimilarity(0, 1)); assertCorrelationEquals(0.5423213660693732, similarity.itemSimilarity(1, 2)); assertCorrelationEquals(0.5423213660693732, similarity.itemSimilarity(2, 1)); assertCorrelationEquals(0.6905400104897509, similarity.itemSimilarity(2, 3)); assertCorrelationEquals(0.6905400104897509, similarity.itemSimilarity(3, 2)); assertCorrelationEquals(0.8706358464330881, similarity.itemSimilarity(3, 4)); assertCorrelationEquals(0.8706358464330881, similarity.itemSimilarity(4, 3)); }
@Test public void testNoSimilarity() throws Exception { DataModel dataModel = getDataModel( new long[] {1, 2, 3, 4}, new Double[][] { {1.0, null, 1.0, 1.0}, {1.0, null, 1.0, 1.0}, {null, 1.0, 1.0, 1.0}, {null, 1.0, 1.0, 1.0}, }); LogLikelihoodSimilarity similarity = new LogLikelihoodSimilarity(dataModel); assertCorrelationEquals(Double.NaN, similarity.itemSimilarity(1, 0)); assertCorrelationEquals(Double.NaN, similarity.itemSimilarity(0, 1)); assertCorrelationEquals(0.0, similarity.itemSimilarity(2, 3)); assertCorrelationEquals(0.0, similarity.itemSimilarity(3, 2)); }