@Test public void testFullCorrelation2() throws Exception { DataModel dataModel = getDataModel( new long[] {1, 2}, new Double[][] { {1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}, }); double correlation = new SpearmanCorrelationSimilarity(dataModel).userSimilarity(1, 2); assertCorrelationEquals(1.0, correlation); }
@Override public double userSimilarity(long userID1, long userID2) throws TasteException { PreferenceArray xPrefs = dataModel.getPreferencesFromUser(userID1); PreferenceArray yPrefs = dataModel.getPreferencesFromUser(userID2); int xLength = xPrefs.length(); int yLength = yPrefs.length(); if (xLength <= 1 || yLength <= 1) { return Double.NaN; } xPrefs = xPrefs.clone(); yPrefs = yPrefs.clone(); xPrefs.sortByValue(); yPrefs.sortByValue(); float nextRank = 1.0f; for (int i = 0; i < xLength; i++) { if (yPrefs.hasPrefWithItemID(xPrefs.getItemID(i))) { xPrefs.setValue(i, nextRank); nextRank += 1.0f; } } nextRank = 1.0f; for (int i = 0; i < yLength; i++) { if (xPrefs.hasPrefWithItemID(yPrefs.getItemID(i))) { yPrefs.setValue(i, nextRank); nextRank += 1.0f; } } xPrefs.sortByItem(); yPrefs.sortByItem(); long xIndex = xPrefs.getItemID(0); long yIndex = yPrefs.getItemID(0); int xPrefIndex = 0; int yPrefIndex = 0; double sumXYRankDiff2 = 0.0; int count = 0; while (true) { int compare = xIndex < yIndex ? -1 : xIndex > yIndex ? 1 : 0; if (compare == 0) { double diff = xPrefs.getValue(xPrefIndex) - yPrefs.getValue(yPrefIndex); sumXYRankDiff2 += diff * diff; count++; } if (compare <= 0) { if (++xPrefIndex >= xLength) { break; } xIndex = xPrefs.getItemID(xPrefIndex); } if (compare >= 0) { if (++yPrefIndex >= yLength) { break; } yIndex = yPrefs.getItemID(yPrefIndex); } } if (count <= 1) { return Double.NaN; } return 1.0 - 6.0 * sumXYRankDiff2 / (count * (count * count - 1)); }
SpearmanCorrelationSimilarity implements UserSimilarity { @Override public double userSimilarity(long userID1, long userID2) throws TasteException { PreferenceArray xPrefs = dataModel.getPreferencesFromUser(userID1); PreferenceArray yPrefs = dataModel.getPreferencesFromUser(userID2); int xLength = xPrefs.length(); int yLength = yPrefs.length(); if (xLength <= 1 || yLength <= 1) { return Double.NaN; } xPrefs = xPrefs.clone(); yPrefs = yPrefs.clone(); xPrefs.sortByValue(); yPrefs.sortByValue(); float nextRank = 1.0f; for (int i = 0; i < xLength; i++) { if (yPrefs.hasPrefWithItemID(xPrefs.getItemID(i))) { xPrefs.setValue(i, nextRank); nextRank += 1.0f; } } nextRank = 1.0f; for (int i = 0; i < yLength; i++) { if (xPrefs.hasPrefWithItemID(yPrefs.getItemID(i))) { yPrefs.setValue(i, nextRank); nextRank += 1.0f; } } xPrefs.sortByItem(); yPrefs.sortByItem(); long xIndex = xPrefs.getItemID(0); long yIndex = yPrefs.getItemID(0); int xPrefIndex = 0; int yPrefIndex = 0; double sumXYRankDiff2 = 0.0; int count = 0; while (true) { int compare = xIndex < yIndex ? -1 : xIndex > yIndex ? 1 : 0; if (compare == 0) { double diff = xPrefs.getValue(xPrefIndex) - yPrefs.getValue(yPrefIndex); sumXYRankDiff2 += diff * diff; count++; } if (compare <= 0) { if (++xPrefIndex >= xLength) { break; } xIndex = xPrefs.getItemID(xPrefIndex); } if (compare >= 0) { if (++yPrefIndex >= yLength) { break; } yIndex = yPrefs.getItemID(yPrefIndex); } } if (count <= 1) { return Double.NaN; } return 1.0 - 6.0 * sumXYRankDiff2 / (count * (count * count - 1)); } }
SpearmanCorrelationSimilarity implements UserSimilarity { @Override public double userSimilarity(long userID1, long userID2) throws TasteException { PreferenceArray xPrefs = dataModel.getPreferencesFromUser(userID1); PreferenceArray yPrefs = dataModel.getPreferencesFromUser(userID2); int xLength = xPrefs.length(); int yLength = yPrefs.length(); if (xLength <= 1 || yLength <= 1) { return Double.NaN; } xPrefs = xPrefs.clone(); yPrefs = yPrefs.clone(); xPrefs.sortByValue(); yPrefs.sortByValue(); float nextRank = 1.0f; for (int i = 0; i < xLength; i++) { if (yPrefs.hasPrefWithItemID(xPrefs.getItemID(i))) { xPrefs.setValue(i, nextRank); nextRank += 1.0f; } } nextRank = 1.0f; for (int i = 0; i < yLength; i++) { if (xPrefs.hasPrefWithItemID(yPrefs.getItemID(i))) { yPrefs.setValue(i, nextRank); nextRank += 1.0f; } } xPrefs.sortByItem(); yPrefs.sortByItem(); long xIndex = xPrefs.getItemID(0); long yIndex = yPrefs.getItemID(0); int xPrefIndex = 0; int yPrefIndex = 0; double sumXYRankDiff2 = 0.0; int count = 0; while (true) { int compare = xIndex < yIndex ? -1 : xIndex > yIndex ? 1 : 0; if (compare == 0) { double diff = xPrefs.getValue(xPrefIndex) - yPrefs.getValue(yPrefIndex); sumXYRankDiff2 += diff * diff; count++; } if (compare <= 0) { if (++xPrefIndex >= xLength) { break; } xIndex = xPrefs.getItemID(xPrefIndex); } if (compare >= 0) { if (++yPrefIndex >= yLength) { break; } yIndex = yPrefs.getItemID(yPrefIndex); } } if (count <= 1) { return Double.NaN; } return 1.0 - 6.0 * sumXYRankDiff2 / (count * (count * count - 1)); } SpearmanCorrelationSimilarity(DataModel dataModel); }
SpearmanCorrelationSimilarity implements UserSimilarity { @Override public double userSimilarity(long userID1, long userID2) throws TasteException { PreferenceArray xPrefs = dataModel.getPreferencesFromUser(userID1); PreferenceArray yPrefs = dataModel.getPreferencesFromUser(userID2); int xLength = xPrefs.length(); int yLength = yPrefs.length(); if (xLength <= 1 || yLength <= 1) { return Double.NaN; } xPrefs = xPrefs.clone(); yPrefs = yPrefs.clone(); xPrefs.sortByValue(); yPrefs.sortByValue(); float nextRank = 1.0f; for (int i = 0; i < xLength; i++) { if (yPrefs.hasPrefWithItemID(xPrefs.getItemID(i))) { xPrefs.setValue(i, nextRank); nextRank += 1.0f; } } nextRank = 1.0f; for (int i = 0; i < yLength; i++) { if (xPrefs.hasPrefWithItemID(yPrefs.getItemID(i))) { yPrefs.setValue(i, nextRank); nextRank += 1.0f; } } xPrefs.sortByItem(); yPrefs.sortByItem(); long xIndex = xPrefs.getItemID(0); long yIndex = yPrefs.getItemID(0); int xPrefIndex = 0; int yPrefIndex = 0; double sumXYRankDiff2 = 0.0; int count = 0; while (true) { int compare = xIndex < yIndex ? -1 : xIndex > yIndex ? 1 : 0; if (compare == 0) { double diff = xPrefs.getValue(xPrefIndex) - yPrefs.getValue(yPrefIndex); sumXYRankDiff2 += diff * diff; count++; } if (compare <= 0) { if (++xPrefIndex >= xLength) { break; } xIndex = xPrefs.getItemID(xPrefIndex); } if (compare >= 0) { if (++yPrefIndex >= yLength) { break; } yIndex = yPrefs.getItemID(yPrefIndex); } } if (count <= 1) { return Double.NaN; } return 1.0 - 6.0 * sumXYRankDiff2 / (count * (count * count - 1)); } SpearmanCorrelationSimilarity(DataModel dataModel); @Override double userSimilarity(long userID1, long userID2); @Override void setPreferenceInferrer(PreferenceInferrer inferrer); @Override void refresh(Collection<Refreshable> alreadyRefreshed); }
SpearmanCorrelationSimilarity implements UserSimilarity { @Override public double userSimilarity(long userID1, long userID2) throws TasteException { PreferenceArray xPrefs = dataModel.getPreferencesFromUser(userID1); PreferenceArray yPrefs = dataModel.getPreferencesFromUser(userID2); int xLength = xPrefs.length(); int yLength = yPrefs.length(); if (xLength <= 1 || yLength <= 1) { return Double.NaN; } xPrefs = xPrefs.clone(); yPrefs = yPrefs.clone(); xPrefs.sortByValue(); yPrefs.sortByValue(); float nextRank = 1.0f; for (int i = 0; i < xLength; i++) { if (yPrefs.hasPrefWithItemID(xPrefs.getItemID(i))) { xPrefs.setValue(i, nextRank); nextRank += 1.0f; } } nextRank = 1.0f; for (int i = 0; i < yLength; i++) { if (xPrefs.hasPrefWithItemID(yPrefs.getItemID(i))) { yPrefs.setValue(i, nextRank); nextRank += 1.0f; } } xPrefs.sortByItem(); yPrefs.sortByItem(); long xIndex = xPrefs.getItemID(0); long yIndex = yPrefs.getItemID(0); int xPrefIndex = 0; int yPrefIndex = 0; double sumXYRankDiff2 = 0.0; int count = 0; while (true) { int compare = xIndex < yIndex ? -1 : xIndex > yIndex ? 1 : 0; if (compare == 0) { double diff = xPrefs.getValue(xPrefIndex) - yPrefs.getValue(yPrefIndex); sumXYRankDiff2 += diff * diff; count++; } if (compare <= 0) { if (++xPrefIndex >= xLength) { break; } xIndex = xPrefs.getItemID(xPrefIndex); } if (compare >= 0) { if (++yPrefIndex >= yLength) { break; } yIndex = yPrefs.getItemID(yPrefIndex); } } if (count <= 1) { return Double.NaN; } return 1.0 - 6.0 * sumXYRankDiff2 / (count * (count * count - 1)); } SpearmanCorrelationSimilarity(DataModel dataModel); @Override double userSimilarity(long userID1, long userID2); @Override void setPreferenceInferrer(PreferenceInferrer inferrer); @Override void refresh(Collection<Refreshable> alreadyRefreshed);  }