This repository has been archived by the owner on Jun 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright (c) 2013 Dahua Lin | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Distances | ||
|
||
Evaluation of distances between vectors. | ||
|
||
## A List of supported distances | ||
|
||
* Minkowski distance | ||
* Euclidean distance | ||
* Squared euclidean distance | ||
* Cityblock distance | ||
* Chebyshev distance | ||
* Hamming distance | ||
* Cosine distance | ||
* Spearman distance | ||
* Kullback-Leibler divergence | ||
* Jensen-Shannon divergence | ||
|
||
## Features | ||
|
||
* Many of the distances above accepts a weight vector as an optional argument to calculate weighted distances. | ||
* The module supports computation of distances in different ways: | ||
- compute a distance between two vectors | ||
- compute distances between a vector and an array comprised of multiple vectors | ||
- compute distances between corresponding vectors in two arrays along a specific dimension | ||
- compute distances between columns in two matrices in a pairwise manner | ||
* Specialized functions are used to compute pairwise (Squared) Euclidean distances in a much faster way. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
module Distances | ||
|
||
export | ||
GeneralizedDistance, | ||
Distance, | ||
cdist, | ||
pdist, | ||
EuclideanDistance, | ||
SquaredEuclideanDistance, | ||
eucdist, | ||
sqeucdist | ||
|
||
########################################################################## | ||
# | ||
# The type hierarchy for distances | ||
# | ||
########################################################################## | ||
|
||
abstract GeneralizedDistance | ||
abstract Distance <: GeneralizedDistance | ||
|
||
type EuclideanDistance <: Distance | ||
end | ||
|
||
type SquaredEuclideanDistance <: Distance | ||
end | ||
|
||
########################################################################## | ||
# | ||
# General cdist implementation | ||
# | ||
# Remarks | ||
# -------- | ||
# cdist(dist, a::Vector, b::Vector) should be implemented respectively | ||
# for each distance. | ||
# | ||
# This general implementation extends them to matrices. | ||
# | ||
########################################################################## | ||
|
||
function cdist(dist::GeneralizedDistance, a::AbstractVector, b::AbstractMatrix) | ||
m, n = size(b) | ||
|
||
# calculate the first distance (to determine value type) | ||
|
||
d1 = cdist(dist, a, b[:,1]) | ||
r = Array(typeof(d1), n) | ||
r[1] = d1 | ||
|
||
# calculate the rest | ||
|
||
for i = 2 : n | ||
r[i] = cdist(dist, a, b[:,i]) | ||
end | ||
|
||
return r | ||
end | ||
|
||
function cdist(dist::GeneralizedDistance, a::AbstractMatrix, b::AbstractVector) | ||
m, n = size(a) | ||
|
||
# calculate the first distance (to determine value type) | ||
|
||
d1 = cdist(dist, a[:,1], b) | ||
r = Array(typeof(d1), n) | ||
r[1] = d1 | ||
|
||
# calculate the rest | ||
|
||
for i = 2 : n | ||
r[i] = cdist(dist, sub(a, 1:m, i), b) | ||
end | ||
|
||
return r | ||
end | ||
|
||
function cdist(dist::GeneralizedDistance, a::AbstractMatrix, b::AbstractMatrix) | ||
@assert size(a, 2) == size(b, 2) | ||
n = size(a, 2) | ||
|
||
# calculate the first distance (to determine distance value type) | ||
|
||
d1 = cdist(dist, a[:,1], b[:,1]) | ||
r = Array(typeof(d1), n) | ||
r[1] = d1 | ||
|
||
# calculate the rest | ||
|
||
for i = 2 : n | ||
r[i] = cdist(dist, a[:,i], b[:,i]) | ||
end | ||
|
||
return r | ||
end | ||
|
||
|
||
########################################################################## | ||
# | ||
# General pdist implementation | ||
# | ||
########################################################################## | ||
|
||
function pdist(dist::GeneralizedDistance, a::Matrix, b::Matrix) | ||
m = size(a, 2) | ||
n = size(b, 1) | ||
|
||
# calculate the first distance (to determine distance value type) | ||
|
||
d1 = cdist(dist, a[:,1], b[:,1]) | ||
r = Array(typeof(d1), (m, n)) | ||
r[1, 1] = d1 | ||
|
||
# calculate the rest | ||
|
||
for i = 2 : m | ||
b1 = b[:,1] | ||
r[i, 1] = cdist(dist, a[:,i], b1) | ||
end | ||
|
||
for j = 2 : n | ||
bj = b[:,j] | ||
r[i, j] = cdist(dist, a[:,i], bj) | ||
end | ||
|
||
return r | ||
end | ||
|
||
########################################################################## | ||
# | ||
# Specific distances | ||
# | ||
########################################################################## | ||
|
||
function cdist{T<:Real}(dist::EuclideanDistance, a::AbstractVector{T}, b::AbstractVector{T}) | ||
s::T = 0 | ||
n = length(a) | ||
for i = 1 : n # devectorize to make it faster | ||
v = a[i] - b[i] | ||
s += v * v | ||
end | ||
return sqrt(s) | ||
end | ||
|
||
function cdist{T<:Real}(dist::SquaredEuclideanDistance, a::AbstractVector{T}, b::AbstractVector{T}) | ||
s::T = 0 | ||
n = length(a) | ||
for i = 1 : n # devectorize to make it faster | ||
v = a[i] - b[i] | ||
s += v * v | ||
end | ||
return s | ||
end | ||
|
||
|
||
########################################################################## | ||
# | ||
# Convenient functions | ||
# | ||
########################################################################## | ||
|
||
eucdist(a::AbstractArray, b::AbstractArray) = cdist(EuclideanDistance(), a, b) | ||
sqeucdist(a::AbstractArray, b::AbstractArray) = cdist(SquaredEuclideanDistance(), a, b) | ||
|
||
|
||
end # module end | ||
|
||
|