Permalink
Browse files

math.similarity: adding weighted-cosine-similarity.

  • Loading branch information...
mrjbq7 committed Oct 25, 2017
1 parent 8d58f60 commit 86778b349cb707be43422f117c6380dcd4033189
Showing with 22 additions and 1 deletion.
  1. +6 −0 extra/math/similarity/similarity-tests.factor
  2. +16 −1 extra/math/similarity/similarity.factor
@@ -19,3 +19,9 @@ CONSTANT: b { 0 0 0 0 2 3 1 }
{ 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
@@ -1,7 +1,8 @@
! 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
@@ -13,3 +14,17 @@ 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 / ;

0 comments on commit 86778b3

Please sign in to comment.