{ t } [ a a vneg cosine-similarity -1.0 1e-10 ~ ] unit-test
{ t } [ a b cosine-similarity 0.0944911182523068 1e-10 ~ ] unit-test
+
+{ 3/100 } [
+ { 0 0 0 10 10 } { 0 0 1 1 1 } { 0 0 0 1 2 }
+ weighted-cosine-similarity
+] unit-test
+
! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
-USING: kernel math math.statistics math.vectors sequences ;
+USING: kernel math math.functions math.statistics math.vectors
+sequences sequences.extras ;
IN: math.similarity
: cosine-similarity ( a b -- n )
[ v. ] [ [ norm ] bi@ * ] 2bi / ;
+
+<PRIVATE
+
+: weighted-v. ( w a b -- n )
+ [ * * ] [ + ] 3map-reduce ;
+
+: weighted-norm ( w a -- n )
+ [ absq * ] [ + ] 2map-reduce ;
+
+PRIVATE>
+
+: weighted-cosine-similarity ( w a b -- n )
+ [ weighted-v. ]
+ [ [ over ] dip [ weighted-norm ] 2bi@ * ] 3bi / ;