Skip to content

Commit

Permalink
Add 'MinkowskiMetric' abstract type
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv committed Oct 9, 2024
1 parent 886ad02 commit 000fbf5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
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((x - y) .^ p) ^ (1 / p)

where the `p` parameter defines the metric.

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
7 changes: 7 additions & 0 deletions src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ abstract type SemiMetric <: PreMetric end
#
abstract type Metric <: SemiMetric end

# a minkowski metric is a metric that is defined by the formula:
#
# d(x, y) = sum((x - y) .^ p) ^ (1 / p)
#
# where the `p` parameter defines the metric.
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

0 comments on commit 000fbf5

Please sign in to comment.