def jaccard(x,y) #based on http://people.revoledu.com/kardi/tutorial/Similarity/Jaccard.html - asymmetrical a1b1 = a0b1 = a1b0 = a0b0 = 0 x.each_index do |i| if x[i]==0 if y[i]==0 a0b0=a0b0+1 elsif y[i]==1 a0b1=a0b1+1 end elsif x[i]==1 if y[i]==0 a1b0=a1b0+1 elsif y[i]==1 a1b1=a1b1+1 end end end a1b1/(a0b1+a1b0+a1b1).to_f end def simplematch(x,y) #based on http://people.revoledu.com/kardi/tutorial/Similarity/SimpleMatching.html a1b1 = a0b1 = a1b0 = a0b0 = 0 x.each_index do |i| #allows looping through both lists at once if x[i]==0 if y[i]==0 a0b0=a0b0+1 elsif y[i]==1 a0b1=a0b1+1 end elsif x[i]==1 if y[i]==0 a1b0=a1b0+1 elsif y[i]==1 a1b1=a1b1+1 end end end (a1b1+a0b0).to_f/x.length end def simpledistance(x,y) 1-simplematch(x,y) end