math.similarity: Add Jaccard similarity metric

modern-harvey2
Doug Coleman 2017-10-11 17:17:05 -05:00
parent e9ad224752
commit d7c12986c6
1 changed files with 6 additions and 1 deletions

View File

@ -1,7 +1,7 @@
! Copyright (C) 2012 John Benediktsson ! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license ! See http://factorcode.org/license.txt for BSD license
USING: kernel math math.statistics math.vectors sequences ; USING: kernel math math.statistics math.vectors sequences sets ;
IN: math.similarity IN: math.similarity
@ -13,3 +13,8 @@ IN: math.similarity
: cosine-similarity ( a b -- n ) : cosine-similarity ( a b -- n )
[ v* sum ] [ [ norm ] bi@ * ] 2bi / 0.5 * 0.5 + ; [ v* sum ] [ [ norm ] bi@ * ] 2bi / 0.5 * 0.5 + ;
: jaccard-similarity ( a b -- n )
[ intersect cardinality dup ]
[ [ cardinality ] bi@ + swap - ] 2bi
[ drop 0 ] [ /f ] if-zero ;