correlation_distance {abdiv}R Documentation

Correlation and cosine distance


The correlation and cosine distances, which are derived from the dot product of the two vectors.


correlation_distance(x, y)

cosine_distance(x, y)


x, y

Numeric vectors


For vectors x and y, the cosine distance is defined as the cosine of the angle between the vectors,

d(x, y) = 1 - \frac{x \cdot y}{|x| |y|},

where |x| is the magnitude or L2 norm of the vector, |x| = \sqrt{\sum_i x_i^2}. Relation to other definitions:

The correlation distance is simply equal to one minus the Pearson correlation between vectors. Mathematically, it is equivalent to the cosine distance between the vectors after they are centered (x - \bar{x}). Relation to other definitions:


The correlation or cosine distance. These are undefined if either x or y contain all zero elements, that is, if |x| = 0 or |y| = 0. In this case, we return NaN.


x <- c(2, 0)
y <- c(5, 5)
cosine_distance(x, y)
# The two vectors form a 45 degree angle, or pi / 4
1 - cos(pi / 4)

v <- c(3.5, 0.1, 1.4)
w <- c(3.3, 0.5, 0.9)
correlation_distance(v, w)
1 - cor(v, w)

[Package abdiv version 0.2.0 Index]