Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MinkowskiMetric abstract type #265

Merged
merged 9 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
eliascarv marked this conversation as resolved.
Show resolved Hide resolved

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

eliascarv marked this conversation as resolved.
Show resolved Hide resolved
###########################################################
#
# 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
Loading