Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Extract methods for covariance and standard deviation

  • Loading branch information...
commit 84a9d4617e8c8c40845c209c9d96a1a36b38c103 1 parent a1c4a26
Robert Greiner authored November 14, 2011
15  Recommender/SimilarityScore/PearsonCorrelation.cs
@@ -37,9 +37,8 @@ public double Score()
37 37
 
38 38
             var pSum = new SumProducts(SimilarReviews, R1.Reviews, R2.Reviews).Calculate();
39 39
 
40  
-            var num = pSum - (sumR1 * sumR2 / n);
41  
-
42  
-            var den = Math.Sqrt((sumR1Sq - Math.Pow(sumR1, 2) / n) * (sumR2Sq - Math.Pow(sumR2, 2) / n));
  40
+            var num = Covariance(sumR1, sumR2, pSum, n);
  41
+            var den = Math.Sqrt(StandardDeviation(sumR1, sumR1Sq, n) * StandardDeviation(sumR2, sumR2Sq, n));
43 42
 
44 43
             if (den == 0.0)
45 44
             {
@@ -50,6 +49,16 @@ public double Score()
50 49
             return Math.Round(answer, 3);
51 50
         }
52 51
 
  52
+        private static double Covariance(double sumR1, double sumR2, double sumOfProducts, int n)
  53
+        {
  54
+            return sumOfProducts - (sumR1 * sumR2 / n);
  55
+        }
  56
+
  57
+        private static double StandardDeviation(double sum, double sumOfSquares, int numberOfItems)
  58
+        {
  59
+            return (sumOfSquares - Math.Pow(sum, 2) / numberOfItems);
  60
+        }
  61
+
53 62
         private bool ReviewersHaveNoSimilarReviews()
54 63
         {
55 64
             return SimilarReviews.Count == 0;

0 notes on commit 84a9d46

Please sign in to comment.
Something went wrong with that request. Please try again.