math.similarity: adding weighted-cosine-similarity.

factor-shell
John Benediktsson 2017-10-24 17:29:06 -07:00
parent 8d58f60bf9
commit 86778b349c
2 changed files with 22 additions and 1 deletions

View File

@ -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 a vneg cosine-similarity -1.0 1e-10 ~ ] unit-test
{ t } [ a b cosine-similarity 0.0944911182523068 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

View File

@ -1,7 +1,8 @@
! 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.functions math.statistics math.vectors
sequences sequences.extras ;
IN: math.similarity IN: math.similarity
@ -13,3 +14,17 @@ IN: math.similarity
: cosine-similarity ( a b -- n ) : cosine-similarity ( a b -- n )
[ v. ] [ [ norm ] bi@ * ] 2bi / ; [ 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 / ;