I have a question about using the pearson correlation coefficient in a recommender system.
I currently have 3 collections in my database. 1 for users, 1 for restaurants and 1 for reviews.
I have written a function which takes 2 user id's and their list of submitted reviews and returns a double, which is the pearson correlation coefficient between the 2 users based on the reviews they've submitted.
So what the function does is make 2 lists of all reviews the users have submitted. Then a for loop checks if they have reviews which are left on the same restaurant, and places these reviews in a list. This list is used in calculating the coefficient.
I just wanted to know if I'm using this coefficient the right way. I want to give recommendations to the first user. Can I use this coefficient as a good indicator of someone who fits well with another user?
And if it's not a good way to match users, what would be a better way to do so?
In case anyone wonders, here's my function which calculates the coefficient.
public static double CalculatePearsonCorrelation(Guid userId1, List<Review> user1Reviews,
Guid userId2, List<Review> user2Reviews)
{
//Resetting the dictionary
restaurantRecommendations = new Dictionary<Guid, List<Review>>();
//Matching the reviews with the corresponding user
restaurantRecommendations.Add(userId1, user1Reviews);
restaurantRecommendations.Add(userId2, user2Reviews);
//Check if users have enough reviews to get a correct correlation
if (restaurantRecommendations[userId1].Count < 4)
throw new NotEnoughReviewsException("UserId " + userId1 + " doesn't contain enough reviews for this correlation");
if (restaurantRecommendations[userId2].Count < 4)
throw new NotEnoughReviewsException("UserId " + userId2 + " doesn't contain enough reviews for this correlation");
//This will be the list of reviews that are the same per subject for the two users.
List<Review> shared_items = new List<Review>();
//Loops through the list of reviews of the selected user (userId1)
foreach (var item in restaurantRecommendations[userId1])
{
//Checks if they have any reviews on subjects in common
if (restaurantRecommendations[userId2].Where(x => x.subj.Id == item.subj.Id).Count() != 0)
{
//Adds these reviews to a list on which the correlation will be based
shared_items.Add(item);
}
}
//If they don't have anything in common, the correlation will be 0
if (shared_items.Count() == 0)
return 0;
//I decided users need at least 4 subjects in common, else there won't be an accurate correlation
if (shared_items.Count() < 4)
throw new NotEnoughReviewsException("UserId " + userId1 + " and UserId " + userId2 + " don't have enough reviews in common for a correlation");
////////////////////////// Calculating the pearson correlation //////////////////////////
double product1_review_sum = 0.00f;
double product2_review_sum = 0.00f;
double product1_rating = 0f;
double product2_rating = 0f;
double critics_sum = 0f;
foreach (Review item in shared_items)
{
product1_review_sum += restaurantRecommendations[userId1].Where(x => x.subj.Id == item.subj.Id).FirstOrDefault().rating;
product2_review_sum += restaurantRecommendations[userId2].Where(x => x.subj.Id == item.subj.Id).FirstOrDefault().rating;
product1_rating += Math.Pow(restaurantRecommendations[userId1].Where(x => x.subj.Id == item.subj.Id).FirstOrDefault().rating, 2);
product2_rating += Math.Pow(restaurantRecommendations[userId2].Where(x => x.subj.Id == item.subj.Id).FirstOrDefault().rating, 2);
critics_sum += restaurantRecommendations[userId1].Where(x => x.subj.Id == item.subj.Id).FirstOrDefault().rating *
restaurantRecommendations[userId2].Where(x => x.subj.Id == item.subj.Id).FirstOrDefault().rating;
}
//Calculate pearson correlation
double num = critics_sum - (product1_review_sum * product2_review_sum / shared_items.Count);
double density = Math.Sqrt((product1_rating - Math.Pow(product1_review_sum, 2) / shared_items.Count) *
((product2_rating - Math.Pow(product2_review_sum, 2) / shared_items.Count)));
if (density == 0)
return 0;
return num / density;
}
}