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 4 commits
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(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
22 changes: 15 additions & 7 deletions src/metrics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@ abstract type UnionSemiMetric <: SemiMetric end

abstract type UnionMetric <: Metric end

# a minkowski metric is a metric that is defined by the formula:
eliascarv marked this conversation as resolved.
Show resolved Hide resolved
#
# 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).
abstract type MinkowskiMetric <: UnionMetric end

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

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

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

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

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

struct Chebyshev <: UnionMetric end
struct Chebyshev <: MinkowskiMetric end

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

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

struct Minkowski{T <: Real} <: UnionMetric
struct Minkowski{T <: Real} <: MinkowskiMetric
p::T
end
struct WeightedMinkowski{W,T <: Real} <: UnionMetric
struct WeightedMinkowski{W,T <: Real} <: MinkowskiMetric
weights::W
p::T
end
Expand Down
Loading