math.similarity: change cosine-similarity to match math formula.

Specifically, instead of returning values [0,1], we now return [-1,1]:

* −1 meaning exactly opposite
* 1 meaning exactly the same
* 0 indicating orthogonality (decorrelation)
* in-between values indicating intermediate similarity or dissimilarity.
factor-shell
John Benediktsson 2017-10-24 11:45:03 -07:00
parent c3354c3167
commit ee1bfc7fc2
2 changed files with 5 additions and 3 deletions

View File

@ -1,7 +1,7 @@
! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: math.functions math.similarity tools.test ;
USING: math.functions math.similarity math.vectors tools.test ;
IN: math.similarity.tests
@ -15,4 +15,6 @@ CONSTANT: b { 0 0 0 0 2 3 1 }
{ t } [ a b pearson-similarity 0.2376861940759582 1e-10 ~ ] unit-test
{ t } [ a a cosine-similarity 1.0 1e-10 ~ ] unit-test
{ t } [ a b cosine-similarity 0.5472455591261534 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

View File

@ -12,4 +12,4 @@ IN: math.similarity
over length 3 < [ 2drop 1.0 ] [ population-corr 0.5 * 0.5 + ] if ;
: cosine-similarity ( a b -- n )
[ v* sum ] [ [ norm ] bi@ * ] 2bi / 0.5 * 0.5 + ;
[ v* sum ] [ [ norm ] bi@ * ] 2bi / ;