]> gitweb.factorcode.org Git - factor.git/blob - extra/math/similarity/similarity.factor
core: Add the shuffler words but without primitives.
[factor.git] / extra / math / similarity / similarity.factor
1 ! Copyright (C) 2012 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
3
4 USING: kernel math math.functions math.statistics math.vectors
5 sequences sequences.extras ;
6
7 IN: math.similarity
8
9 : euclidian-similarity ( a b -- n )
10     v- norm 1 + recip ;
11
12 : pearson-similarity ( a b -- n )
13     over length 3 < [ 2drop 1.0 ] [ population-corr 0.5 * 0.5 + ] if ;
14
15 : cosine-similarity ( a b -- n )
16     [ v. ] [ [ norm ] bi@ * ] 2bi / ;
17
18 <PRIVATE
19
20 : weighted-v. ( w a b -- n )
21     [ * * ] [ + ] 3map-reduce ;
22
23 : weighted-norm ( w a -- n )
24     [ absq * ] [ + ] 2map-reduce ;
25
26 PRIVATE>
27
28 : weighted-cosine-similarity ( w a b -- n )
29     [ weighted-v. ]
30     [ overd [ weighted-norm ] 2bi@ * ] 3bi / ;