Skip to content

Commit

Permalink
math.similarity: adding weighted-cosine-similarity.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Oct 25, 2017
1 parent 8d58f60 commit 86778b3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 6 additions & 0 deletions extra/math/similarity/similarity-tests.factor
Expand Up @@ -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

17 changes: 16 additions & 1 deletion extra/math/similarity/similarity.factor
@@ -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

Expand All @@ -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.