Distances.jl
A Julia package for evaluating distances(metrics) between vectors.
This package also provides optimized functions to compute column-wise and pairwise distances, which are often substantially faster than a straightforward loop implementation. (See the benchmark section below for details).
Supported distances
- Euclidean distance
- Squared Euclidean distance
- Cityblock distance
- Total variation distance
- Jaccard distance
- Rogers-Tanimoto distance
- Chebyshev distance
- Minkowski distance
- Hamming distance
- Cosine distance
- Correlation distance
- Chi-square distance
- Kullback-Leibler divergence
- Generalized Kullback-Leibler divergence
- Rényi divergence
- Jensen-Shannon divergence
- Mahalanobis distance
- Squared Mahalanobis distance
- Bhattacharyya distance
- Hellinger distance
- Haversine distance
- Mean absolute deviation
- Mean squared deviation
- Root mean squared deviation
- Normalized root mean squared deviation
- Bray-Curtis dissimilarity
- Bregman divergence
For Euclidean distance
, Squared Euclidean distance
, Cityblock distance
, Minkowski distance
, and Hamming distance
, a weighted version is also provided.
Basic Use
The library supports three ways of computation: computing the distance between two vectors, column-wise computation, and pairwise computation.
Computing the distance between two vectors
Each distance corresponds to a distance type. You can always compute a certain distance between two vectors using the following syntax
r = evaluate(dist, x, y)
Here, dist is an instance of a distance type. For example, the type for Euclidean distance is Euclidean
(more distance types will be introduced in the next section), then you can compute the Euclidean distance between x
and y
as
r = evaluate(Euclidean(), x, y)
Common distances also come with convenient functions for distance evaluation. For example, you may also compute Euclidean distance between two vectors as below
r = euclidean(x, y)
Computing distances between corresponding columns
Suppose you have two m-by-n
matrix X
and Y
, then you can compute all distances between corresponding columns of X
and Y
in one batch, using the colwise
function, as
r = colwise(dist, X, Y)
The output r
is a vector of length n
. In particular, r[i]
is the distance between X[:,i]
and Y[:,i]
. The batch computation typically runs considerably faster than calling evaluate
column-by-column.
Note that either of X
and Y
can be just a single vector -- then the colwise
function will compute the distance between this vector and each column of the other parameter.
Computing pairwise distances
Let X
and Y
respectively have m
and n
columns. Then the pairwise
function with the dims=2
argument computes distances between each pair of columns in X
and Y
:
R = pairwise(dist, X, Y, dims=2)
In the output, R
is a matrix of size (m, n)
, such that R[i,j]
is the distance between X[:,i]
and Y[:,j]
. Computing distances for all pairs using pairwise
function is often remarkably faster than evaluting for each pair individually.
If you just want to just compute distances between columns of a matrix X
, you can write
R = pairwise(dist, X, dims=2)
This statement will result in an m-by-m
matrix, where R[i,j]
is the distance between X[:,i]
and X[:,j]
.
pairwise(dist, X)
is typically more efficient than pairwise(dist, X, X)
, as the former will take advantage of the symmetry when dist
is a semi-metric (including metric).
For performance reasons, it is recommended to use matrices with observations in columns (as shown above). Indeed,
the Array
type in Julia is column-major, making it more efficient to access memory column by column. However,
matrices with observations stored in rows are also supported via the argument dims=1
.
Computing column-wise and pairwise distances inplace
If the vector/matrix to store the results are pre-allocated, you may use the storage (without creating a new array) using the following syntax (i
being either 1
or 2
):
colwise!(r, dist, X, Y)
pairwise!(R, dist, X, Y, dims=i)
pairwise!(R, dist, X, dims=i)
Please pay attention to the difference, the functions for inplace computation are colwise!
and pairwise!
(instead of colwise
and pairwise
).
Distance type hierarchy
The distances are organized into a type hierarchy.
At the top of this hierarchy is an abstract class PreMetric, which is defined to be a function d
that satisfies
d(x, x) == 0 for all x
d(x, y) >= 0 for all x, y
SemiMetric is a abstract type that refines PreMetric. Formally, a semi-metric is a pre-metric that is also symmetric, as
d(x, y) == d(y, x) for all x, y
Metric is a abstract type that further refines SemiMetric. Formally, a metric is a semi-metric that also satisfies triangle inequality, as
d(x, z) <= d(x, y) + d(y, z) for all x, y, z
This type system has practical significance. For example, when computing pairwise distances between a set of vectors, you may only perform computation for half of the pairs, and derive the values immediately for the remaining halve by leveraging the symmetry of semi-metrics.
Each distance corresponds to a distance type. The type name and the corresponding mathematical definitions of the distances are listed in the following table.
type name | convenient syntax | math definition |
---|---|---|
Euclidean | euclidean(x, y) |
sqrt(sum((x - y) .^ 2)) |
SqEuclidean | sqeuclidean(x, y) |
sum((x - y).^2) |
Cityblock | cityblock(x, y) |
sum(abs(x - y)) |
TotalVariation | totalvariation(x, y) |
sum(abs(x - y)) / 2 |
Chebyshev | chebyshev(x, y) |
max(abs(x - y)) |
Minkowski | minkowski(x, y, p) |
sum(abs(x - y).^p) ^ (1/p) |
Hamming | hamming(k, l) |
sum(k .!= l) |
RogersTanimoto | rogerstanimoto(a, b) |
2(sum(a&!b) + sum(!a&b)) / (2(sum(a&!b) + sum(!a&b)) + sum(a&b) + sum(!a&!b)) |
Jaccard | jaccard(x, y) |
1 - sum(min(x, y)) / sum(max(x, y)) |
BrayCurtis | braycurtis(x, y) |
sum(abs(x - y)) / sum(abs(x + y)) |
CosineDist | cosine_dist(x, y) |
1 - dot(x, y) / (norm(x) * norm(y)) |
CorrDist | corr_dist(x, y) |
cosine_dist(x - mean(x), y - mean(y)) |
ChiSqDist | chisq_dist(x, y) |
sum((x - y).^2 / (x + y)) |
KLDivergence | kl_divergence(p, q) |
sum(p .* log(p ./ q)) |
GenKLDivergence | gkl_divergence(x, y) |
sum(p .* log(p ./ q) - p + q) |
RenyiDivergence | renyi_divergence(p, q, k) |
log(sum( p .* (p ./ q) .^ (k - 1))) / (k - 1) |
JSDivergence | js_divergence(p, q) |
KL(p, m) / 2 + KL(p, m) / 2 with m = (p + q) / 2 |
SpanNormDist | spannorm_dist(x, y) |
max(x - y) - min(x - y) |
BhattacharyyaDist | bhattacharyya(x, y) |
-log(sum(sqrt(x .* y) / sqrt(sum(x) * sum(y))) |
HellingerDist | hellinger(x, y) |
sqrt(1 - sum(sqrt(x .* y) / sqrt(sum(x) * sum(y)))) |
Haversine | haversine(x, y, r) |
Haversine formula |
Mahalanobis | mahalanobis(x, y, Q) |
sqrt((x - y)' * Q * (x - y)) |
SqMahalanobis | sqmahalanobis(x, y, Q) |
(x - y)' * Q * (x - y) |
MeanAbsDeviation | meanad(x, y) |
mean(abs.(x - y)) |
MeanSqDeviation | msd(x, y) |
mean(abs2.(x - y)) |
RMSDeviation | rmsd(x, y) |
sqrt(msd(x, y)) |
NormRMSDeviation | nrmsd(x, y) |
rmsd(x, y) / (maximum(x) - minimum(x)) |
WeightedEuclidean | weuclidean(x, y, w) |
sqrt(sum((x - y).^2 .* w)) |
WeightedSqEuclidean | wsqeuclidean(x, y, w) |
sum((x - y).^2 .* w) |
WeightedCityblock | wcityblock(x, y, w) |
sum(abs(x - y) .* w) |
WeightedMinkowski | wminkowski(x, y, w, p) |
sum(abs(x - y).^p .* w) ^ (1/p) |
WeightedHamming | whamming(x, y, w) |
sum((x .!= y) .* w) |
Bregman | bregman(F, ∇, x, y; inner = LinearAlgebra.dot) |
F(x) - F(y) - inner(∇(y), x - y) |
Note: The formulas above are using Julia's functions. These formulas are mainly for conveying the math concepts in a concise way. The actual implementation may use a faster way. The arguments x
and y
are arrays of real numbers; k
and l
are arrays of distinct elements of any kind; a and b are arrays of Bools; and finally, p
and q
are arrays forming a discrete probability distribution and are therefore both expected to sum to one.
Precision for Euclidean and SqEuclidean
For efficiency (see the benchmarks below), Euclidean
and
SqEuclidean
make use of BLAS3 matrix-matrix multiplication to
calculate distances. This corresponds to the following expansion:
(x-y)^2 == x^2 - 2xy + y^2
However, equality is not precise in the presence of roundoff error,
and particularly when x
and y
are nearby points this may not be
accurate. Consequently, Euclidean
and SqEuclidean
allow you to
supply a relative tolerance to force recalculation:
julia> x = reshape([0.1, 0.3, -0.1], 3, 1);
julia> pairwise(Euclidean(), x, x)
1×1 Array{Float64,2}:
7.45058e-9
julia> pairwise(Euclidean(1e-12), x, x)
1×1 Array{Float64,2}:
0.0
Benchmarks
The implementation has been carefully optimized based on benchmarks. The script in benchmark/benchmarks.jl
defines a benchmark suite
for a variety of distances, under column-wise and pairwise settings.
Here are benchmarks obtained running Julia 0.6 on a computer with a quad-core Intel Core i5-2500K processor @ 3.3 GHz.
The tables below can be replicated using the script in benchmark/print_table.jl
.
Column-wise benchmark
The table below compares the performance (measured in terms of average elapsed time of each iteration) of a straightforward loop implementation and an optimized implementation provided in Distances.jl. The task in each iteration is to compute a specific distance between corresponding columns in two 200-by-10000
matrices.
distance | loop | colwise | gain |
---|---|---|---|
SqEuclidean | 0.005460s | 0.001676s | 3.2582 |
Euclidean | 0.005513s | 0.001681s | 3.2792 |
Cityblock | 0.005409s | 0.001675s | 3.2292 |
Chebyshev | 0.008592s | 0.004575s | 1.8779 |
Minkowski | 0.056741s | 0.048808s | 1.1625 |
Hamming | 0.005320s | 0.001670s | 3.1847 |
CosineDist | 0.005663s | 0.001697s | 3.3378 |
CorrDist | 0.010000s | 0.013904s | 0.7192 |
ChiSqDist | 0.009626s | 0.004734s | 2.0333 |
KLDivergence | 0.046696s | 0.035091s | 1.3307 |
RenyiDivergence | 0.021123s | 0.012006s | 1.7594 |
RenyiDivergence | 0.080503s | 0.066987s | 1.2018 |
JSDivergence | 0.066404s | 0.059564s | 1.1148 |
BhattacharyyaDist | 0.013065s | 0.008807s | 1.4836 |
HellingerDist | 0.013013s | 0.008679s | 1.4993 |
WeightedSqEuclidean | 0.005534s | 0.001676s | 3.3028 |
WeightedEuclidean | 0.005601s | 0.001723s | 3.2513 |
WeightedCityblock | 0.005496s | 0.001675s | 3.2815 |
WeightedMinkowski | 0.057847s | 0.051389s | 1.1257 |
WeightedHamming | 0.005439s | 0.001673s | 3.2513 |
SqMahalanobis | 0.134717s | 0.019530s | 6.8980 |
Mahalanobis | 0.129455s | 0.020114s | 6.4361 |
BrayCurtis | 0.005666s | 0.001680s | 3.3736 |
We can see that using colwise
instead of a simple loop yields considerable gain (2x - 4x), especially when the internal computation of each distance is simple. Nonetheless, when the computation of a single distance is heavy enough (e.g. KLDivergence, RenyiDivergence), the gain is not as significant.
Pairwise benchmark
The table below compares the performance (measured in terms of average elapsed time of each iteration) of a straightforward loop implementation and an optimized implementation provided in Distances.jl. The task in each iteration is to compute a specific distance in a pairwise manner between columns in a 100-by-200
and 100-by-250
matrices, which will result in a 200-by-250
distance matrix.
distance | loop | pairwise | gain |
---|---|---|---|
SqEuclidean | 0.015116s | 0.000192s | 78.7747 |
Euclidean | 0.015565s | 0.000390s | 39.8829 |
Cityblock | 0.015048s | 0.001400s | 10.7469 |
Chebyshev | 0.023325s | 0.010921s | 2.1358 |
Minkowski | 0.143427s | 0.121050s | 1.1849 |
Hamming | 0.015191s | 0.001334s | 11.3856 |
CosineDist | 0.016688s | 0.000393s | 42.5158 |
CorrDist | 0.029024s | 0.000435s | 66.7043 |
ChiSqDist | 0.026035s | 0.012194s | 2.1351 |
KLDivergence | 0.115800s | 0.086968s | 1.3315 |
RenyiDivergence | 0.055551s | 0.029628s | 1.8749 |
RenyiDivergence | 0.205270s | 0.163031s | 1.2591 |
JSDivergence | 0.165078s | 0.148902s | 1.1086 |
BhattacharyyaDist | 0.035493s | 0.022429s | 1.5824 |
HellingerDist | 0.035028s | 0.021867s | 1.6019 |
WeightedSqEuclidean | 0.016330s | 0.000276s | 59.2117 |
WeightedEuclidean | 0.016600s | 0.000508s | 32.6478 |
WeightedCityblock | 0.015604s | 0.001816s | 8.5913 |
WeightedMinkowski | 0.159052s | 0.128427s | 1.2385 |
WeightedHamming | 0.015212s | 0.001634s | 9.3110 |
SqMahalanobis | 0.607881s | 0.000365s | 1665.3228 |
Mahalanobis | 0.623032s | 0.000604s | 1031.9581 |
BrayCurtis | 0.015843s | 0.002273s | 6.9695 |
For distances of which a major part of the computation is a quadratic form (e.g. Euclidean, CosineDist, Mahalanobis), the performance can be drastically improved by restructuring the computation and delegating the core part to GEMM
in BLAS. The use of this strategy can easily lead to 100x performance gain over simple loops (see the highlighted part of the table above).