This measure is called a `cosine' similarity as it computes the cosine of the angle between high-dimensional vectors. It can also be considered a Pearson correlation without centering. Because centering removes sparsity, and because centering has almost no influence for highly sparse matrices, this cosine similarity performs much better that the Pearson correlation, both related for speed and memory consumption.
The variant cosMissing
can be used when the available information itself is also sparse. In such a situation, a zero in the data matrix X, Y can mean either `zero value' or `missing data'. To deal with the missing data, matrices indicating the available data can be specified. Note that this really only makes sense when the available data is sparse itself. When, say, 90% of the data is available, the availX
matrix becomes very large, and the results does not differ strongly from the regular cosSparse
, i.e. ignoring the missing data.
Different normalizations of the columns and weightings of the rows can be specified.
The predefined normalizations are defined as a function of the matrix x and a `summation function' s (to be specified as a sparse matrix or a vector). This slight complexity is needed to be able to deal with missing data. With complete data, then s = rep(1,nrow(X))
, leads to crossprod(X,s) == colSums(X)
.
norm2
:
euclidean norm. The default setting, and the same normalization as used in the Pearson correlation. It is defined as
norm2 <- function(x,s) { drop(crossprod(x^2,s)) ^ 0.5 }
.
norm1
:
Manhattan, or taxi-cab norm, defined as
norm1 <- function(x,s) { abs(drop(crossprod(x,s))) }
.
normL
:
normalized Laplacian norm, used in spectral clustering of a graph, defined as
normL <- function(x,s) { abs(drop(crossprod(x,s))) ^ 0.5 }
.
The predefined weightings are defined as a function of the frequency of a row (s) and the number of columns (N):
idf
:
inverse document frequency, used typically in distributional semantics to down-weight high frequent rows. It is defined as
idf <- function(s,N) { log(N/(1+s)) }
.
isqrt
:
inverse square root, an alternative to idf, defined as
isqrt <- function(s,N) { s^-0.5 }
.
none
:
no weighting. This is only included for use inside later high-level functions (e.g. sim.words
). Normally, weight = NULL
gives identical results, but is slightly quicker.
none <- function(s,N) { s }
Further norms of weighting functions can be defined at will.