correlation_distance: Correlation and cosine distance

Description Usage Arguments Details Value Examples

Description

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

Usage

1
2
3

Arguments

x, y

Numeric vectors

Details

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| = √{∑_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:

Value

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.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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)

abdiv documentation built on Jan. 20, 2020, 5:07 p.m.