#NEWLINE# @ staticmethod #NEWLINE# def sim_pearson( p1 , p2 ) : #NEWLINE# #INDENT# '\n    Returns the Pearson correlation coefficient for p1 and p2\n    ' #NEWLINE# si = [ item for item in p1 if ( item in p2 ) ] #NEWLINE# n = len ( si ) #NEWLINE# if ( n != 0 ) : #NEWLINE# #INDENT# sum1 = sum ( [ p1 [ it ] for it in si ] ) #NEWLINE# sum2 = sum ( [ p2 [ it ] for it in si ] ) #NEWLINE# sum1Sq = sum ( [ pow ( p1 [ it ] , 2 ) for it in si ] ) #NEWLINE# sum2Sq = sum ( [ pow ( p2 [ it ] , 2 ) for it in si ] ) #NEWLINE# pSum = sum ( [ ( p1 [ it ] * p2 [ it ] ) for it in si ] ) #NEWLINE# num = ( pSum - ( ( sum1 * sum2 ) / n ) ) #NEWLINE# den = sqrt ( ( ( sum1Sq - ( pow ( sum1 , 2 ) / n ) ) * ( sum2Sq - ( pow ( sum2 , 2 ) / n ) ) ) ) #NEWLINE# if ( den == 0 ) : #NEWLINE# #INDENT# return 0 #NEWLINE# #UNINDENT# r = ( num / den ) #NEWLINE# return r #NEWLINE# #UNINDENT# return 0