]> gitweb.factorcode.org Git - factor.git/commitdiff
math.similarity: change cosine-similarity to match math formula.
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 24 Oct 2017 18:45:03 +0000 (11:45 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 24 Oct 2017 18:45:03 +0000 (11:45 -0700)
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.

extra/math/similarity/similarity-tests.factor
extra/math/similarity/similarity.factor

index d8be58b6c3a4714f5738e7ba5520839e2c0480a1..0fd32dc7e1dfffede8f6a8e878cb0125c8e2f4a5 100644 (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
+
index 133537ac4d923700b0b79d6bc7374c957729160c..1fdc82cfdb449aecb3b8200c58ed1e2bed8e77f1 100644 (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 / ;