@Test
 public void testDampedEqualKeys() {
   long[] keys = {2, 5, 6};
   double[] val1 = {1, 2, 1};
   double[] val2 = {1, 2, 5};
   SparseVector v1 = MutableSparseVector.wrap(keys, val1).freeze();
   SparseVector v2 = MutableSparseVector.wrap(keys, val2).freeze();
   assertEquals(0.375, dampedSimilarity.similarity(v1, v1), EPSILON);
   assertEquals(0.42705098, dampedSimilarity.similarity(v1, v2), EPSILON);
 }
 @Test
 public void testEqualKeys() {
   long[] keys = {2, 5, 6};
   double[] val1 = {1, 2, 1};
   double[] val2 = {1, 2, 5};
   SparseVector v1 = MutableSparseVector.wrap(keys, val1).freeze();
   SparseVector v2 = MutableSparseVector.wrap(keys, val2).freeze();
   assertEquals(1, similarity.similarity(v1, v1), EPSILON);
   assertEquals(0.745355993, similarity.similarity(v1, v2), EPSILON);
 }
 @Test
 public void testOverlap() {
   long[] k1 = {1, 2, 5, 6};
   double[] val1 = {3, 1, 2, 1};
   long[] k2 = {2, 3, 5, 6, 7};
   double[] val2 = {1, 7, 2, 5, 0};
   SparseVector v1 = MutableSparseVector.wrap(k1, val1).freeze();
   SparseVector v2 = MutableSparseVector.wrap(k2, val2).freeze();
   assertEquals(1, similarity.similarity(v1, v1), EPSILON);
   assertEquals(1, similarity.similarity(v2, v2), EPSILON);
   assertEquals(0.29049645, similarity.similarity(v1, v2), EPSILON);
 }
 @Test
 public void testDisjoint() {
   long[] k1 = {2, 5, 6};
   double[] val1 = {1, 3, 2};
   long[] k2 = {3, 4, 7};
   double[] val2 = {1, 3, 2};
   SparseVector v1, v2;
   v1 = MutableSparseVector.wrap(k1, val1).freeze();
   v2 = MutableSparseVector.wrap(k2, val2).freeze();
   assertEquals(0, similarity.similarity(v1, v2), EPSILON);
   assertEquals(0, dampedSimilarity.similarity(v1, v2), EPSILON);
 }
Example #5
0
 @Test
 public void testItemMeanBaseline() {
   ItemScorer pred = new ItemMeanRatingItemScorer.Builder(dao, 0.0).get();
   long[] items = {5, 7, 10};
   double[] values = {3, 6, 4};
   SparseVector map = MutableSparseVector.wrap(items, values).freeze();
   // unseen item, should be global mean
   assertThat(pred.score(10, 2), closeTo(RATINGS_DAT_MEAN, 0.001));
   // seen item - should be item average
   assertThat(pred.score(10, 5), closeTo(3.0, 0.001));
 }
 private SparseVector emptyVector() {
   long[] keys = {};
   double[] values = {};
   return MutableSparseVector.wrap(keys, values);
 }