lib/pearson.rb
def coefficient(scores, entity1, entity2)
      shared_items = shared_items(scores, entity1, entity2)

      n = shared_items.length

      return 0 if n == 0

      sum1 = sum2 = sum1_sq = sum2_sq = psum = 0

      shared_items.each_key do |item|
        sum1 += scores[entity1][item].to_f
        sum2 += scores[entity2][item].to_f

        sum1_sq += scores[entity1][item].to_f**2
        sum2_sq += scores[entity2][item].to_f**2

        psum += scores[entity1][item].to_f*scores[entity2][item].to_f
      end

      num = psum - (sum1*sum2/n)
      den = ((sum1_sq - (sum1**2)/n) * (sum2_sq - (sum2**2)/n)) ** 0.5

      den == 0 ? 0 : num/den
    end
# Calculates the pearson correlation coefficient between
# two entities
#
# @param [Hash] Hash containing entity-item scores
# @param [String] First entity
# @param [String] Second entity
#
# @return [Float] Coefficient
Calculates the pearson correlation coefficient between
two entities
Calculates the pearson correlation coefficient between two entities
def coefficient(scores, entity1, entity2)
      shared_items = shared_items(scores, entity1, entity2)
      n = shared_items.length
      return 0 if n == 0
      sum1 = sum2 = sum1_sq = sum2_sq = psum = 0
      shared_items.each_key do |item|
        sum1 += scores[entity1][item].to_f
        sum2 += scores[entity2][item].to_f
        sum1_sq += scores[entity1][item].to_f**2
        sum2_sq += scores[entity2][item].to_f**2
        psum += scores[entity1][item].to_f*scores[entity2][item].to_f
      end
      num = psum - (sum1*sum2/n)
      den = ((sum1_sq - (sum1**2)/n) * (sum2_sq - (sum2**2)/n)) ** 0.5
      den == 0 ? 0 : num/den
    end
def coefficient ( scores , entity1 , entity2 ) shared_items = shared_items ( scores , entity1 , entity2 ) n = shared_items . length return 0 if n == 0 sum1 = sum2 = sum1_sq = sum2_sq = psum = 0 shared_items . each_key do | item | sum1 += scores [ entity1 ] [ item ] . to_f sum2 += scores [ entity2 ] [ item ] . to_f sum1_sq += scores [ entity1 ] [ item ] . to_f ** 2 sum2_sq += scores [ entity2 ] [ item ] . to_f ** 2 psum += scores [ entity1 ] [ item ] . to_f * scores [ entity2 ] [ item ] . to_f end num = psum - ( sum1 * sum2 / n ) den = ( ( sum1_sq - ( sum1 ** 2 ) / n ) * ( sum2_sq - ( sum2 ** 2 ) / n ) ) ** 0.5 den == 0 ? 0 : num / den end
Calculates the pearson correlation coefficient between
two entities
Calculates the pearson correlation coefficient between two entities
None
Float
scores
entity1
entity2
None