public double Calculate(SimilarityList data) { var length = 0; double sumX = 0; double sumY = 0; double sumX2 = 0; double sumY2 = 0; // ReSharper disable once InconsistentNaming double sumXY = 0; foreach (var value in data.Data) { sumX += value.Item1; sumY += value.Item2; sumX2 += Math.Pow(value.Item1, 2); sumY2 += Math.Pow(value.Item2, 2); sumXY += value.Item1 * value.Item2; length++; } var numerator = sumXY - sumX * sumY / length; var denominator = Math.Sqrt(sumX2 - Math.Pow(sumX, 2) / length) * Math.Sqrt(sumY2 - Math.Pow(sumY, 2) / length); return(numerator / denominator); }
public double Calculate(SimilarityList data) { double multiSum = 0; double sumX2 = 0; double sumY2 = 0; foreach (var value in data.ZerodData) { multiSum += value.Item1 * value.Item2; sumX2 += Math.Pow(value.Item1, 2); sumY2 += Math.Pow(value.Item2, 2); } return(multiSum / (Math.Sqrt(sumX2) * Math.Sqrt(sumY2))); }
/// <summary> /// Calculate similarity and return a list of tuples with the user and how similar it is. /// </summary> public IEnumerable <Tuple <User, float> > ListSimilarities(Similarity similarity, float threshold = 1) { var users = new List <Tuple <User, float> >(); foreach (var user in Users) { var data = new SimilarityList(user.Ratings, User.Ratings, Articles); var result = (float)similarity.Calculate(data); if (result < threshold) { continue; } users.Add(new Tuple <User, float>(user, result)); } // Sort users.Sort((x, y) => y.Item2.CompareTo(x.Item2)); return(users); }
/// <summary> /// Converts two lists together based on the articles. /// </summary> public SimilarityWrapper(SimilarityList data) { Data = data; }
public double Distance(SimilarityList data) { return(Math.Sqrt((from value in data.Data select Math.Pow(value.Item1 - value.Item2, 2)).Sum())); }
public double Calculate(SimilarityList data) { return(1 / (1 + Distance(data))); }
public double Distance(SimilarityList data) { return((from value in data.Data select Math.Abs(value.Item1 - value.Item2)).Sum()); }