Skip to content

Commit

Permalink
Add MinkowskiMetric abstract type (#265)
Browse files Browse the repository at this point in the history
Co-authored-by: Milan Bouchet-Valat <[email protected]>
  • Loading branch information
eliascarv and nalimilan authored Oct 23, 2024
1 parent 886ad02 commit 35c6d0d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Distances"
uuid = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
version = "0.10.11"
version = "0.10.12"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,20 @@ At the top of this hierarchy is an abstract class **PreMetric**, which is define
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
**SemiMetric** is an 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
**Metric** is an 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

**MinkowskiMetric** is an abstract type that encompasses a family of metrics defined by the formula

d(x, y) = sum(w .* (x - y) .^ p) ^ (1 / p)

where the `p` parameter defines the metric and `w` is a potential weight vector (all 1's by default).

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, derive the
values immediately for the remaining half by leveraging the symmetry of *semi-metrics*. Note
Expand Down
1 change: 1 addition & 0 deletions src/Distances.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export
PreMetric,
SemiMetric,
Metric,
MinkowskiMetric,

# generic functions
result_type,
Expand Down
23 changes: 23 additions & 0 deletions src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ abstract type SemiMetric <: PreMetric end
#
abstract type Metric <: SemiMetric end

"""
MinkowskiMetric <: Metric
A Minkowski metric is a metric that is defined by the formula:
`d(x, y) = sum(w .* (x - y) .^ p) ^ (1 / p)`
where the `p` parameter defines the metric
and `w` is a potential weight vector (all 1's by default).
Common Minkowski metrics:
* `Cityblock`/`WeightedCityblock`: Minkowski metric with `p=1`;
* `Euclidean`/`WeightedEuclidean`: Minkowski metric with `p=2`;
* `Chebyshev`: Limit of `d` as `p` approaches infinity;
* `Minkowski`/`WeightedMinkowski`: generic Minkowski metric for any `p`.
## Notes
* The difference between `Minkowski`/`WeightedMinkowski` and `MinkowskiMetric`
is that while the first are generic Minkowski metrics,
the second is the parent type of these metrics.
"""
abstract type MinkowskiMetric <: Metric end

evaluate(dist::PreMetric, a, b) = dist(a, b)

# Generic functions
Expand Down
19 changes: 11 additions & 8 deletions src/metrics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ abstract type UnionSemiMetric <: SemiMetric end

abstract type UnionMetric <: Metric end

abstract type UnionMinkowskiMetric <: MinkowskiMetric end

###########################################################
#
# Metric types
#
###########################################################

struct Euclidean <: UnionMetric
struct Euclidean <: UnionMinkowskiMetric
thresh::Float64
end

Expand Down Expand Up @@ -52,7 +54,7 @@ julia> pairwise(Euclidean(1e-12), x, x)
"""
Euclidean() = Euclidean(0)

struct WeightedEuclidean{W} <: UnionMetric
struct WeightedEuclidean{W} <: UnionMinkowskiMetric
weights::W
end

Expand Down Expand Up @@ -94,21 +96,21 @@ struct WeightedSqEuclidean{W} <: UnionSemiMetric
weights::W
end

struct Chebyshev <: UnionMetric end
struct Chebyshev <: UnionMinkowskiMetric end

struct Cityblock <: UnionMetric end
struct WeightedCityblock{W} <: UnionMetric
struct Cityblock <: UnionMinkowskiMetric end
struct WeightedCityblock{W} <: UnionMinkowskiMetric
weights::W
end

struct TotalVariation <: UnionMetric end
struct Jaccard <: UnionMetric end
struct RogersTanimoto <: UnionMetric end

struct Minkowski{T <: Real} <: UnionMetric
struct Minkowski{T <: Real} <: UnionMinkowskiMetric
p::T
end
struct WeightedMinkowski{W,T <: Real} <: UnionMetric
struct WeightedMinkowski{W,T <: Real} <: UnionMinkowskiMetric
weights::W
p::T
end
Expand Down Expand Up @@ -197,7 +199,7 @@ struct NormRMSDeviation <: PreMetric end
# Union types
const metrics = (Euclidean,SqEuclidean,PeriodicEuclidean,Chebyshev,Cityblock,TotalVariation,Minkowski,Hamming,Jaccard,RogersTanimoto,CosineDist,ChiSqDist,KLDivergence,RenyiDivergence,BrayCurtis,JSDivergence,SpanNormDist,GenKLDivergence)
const weightedmetrics = (WeightedEuclidean,WeightedSqEuclidean,WeightedCityblock,WeightedMinkowski,WeightedHamming)
const UnionMetrics = Union{UnionPreMetric,UnionSemiMetric,UnionMetric}
const UnionMetrics = Union{UnionPreMetric,UnionSemiMetric,UnionMetric,UnionMinkowskiMetric}

###########################################################
#
Expand All @@ -208,6 +210,7 @@ const UnionMetrics = Union{UnionPreMetric,UnionSemiMetric,UnionMetric}
parameters(::UnionPreMetric) = nothing
parameters(::UnionSemiMetric) = nothing
parameters(::UnionMetric) = nothing
parameters(::UnionMinkowskiMetric) = nothing
parameters(d::PeriodicEuclidean) = d.periods
for dist in weightedmetrics
@eval parameters(d::$dist) = d.weights
Expand Down

2 comments on commit 35c6d0d

@dkarrasch
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/117887

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.10.12 -m "<description of version>" 35c6d0dfec5b9b93481f006dbf8c1d0496ecf5f6
git push origin v0.10.12

Please sign in to comment.