Skip to content

Commit 3264917

Browse files
committed
Deprecate use of Mahalanobis distance
1 parent b38033d commit 3264917

File tree

5 files changed

+58
-63
lines changed

5 files changed

+58
-63
lines changed

docs/src/kernels.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,19 @@ where $r$ has the same dimension as $x$ and $r_i > 0$.
153153

154154
## Piecewise Polynomial Kernel
155155

156-
The [`PiecewisePolynomialKernel`](@ref) is defined for $x, x'\in \mathbb{R}^D$, a positive-definite matrix $P \in \mathbb{R}^{D \times D}$, and $V \in \{0,1,2,3\}$ as
156+
The [`PiecewisePolynomialKernel`](@ref) is defined for $x, x' \in \mathbb{R}^d$ and
157+
$v \in \{0,1,2,3\}$ as
157158
```math
158-
k(x,x'; P, V) = \max(1 - \sqrt{x^\top P x'}, 0)^{j + V} f_V(\sqrt{x^\top P x'}, j),
159+
k(x, x'; v) = \max(1 - \|x - x'\|, 0)^{j + V} f_v(\|x - x'\|, j),
159160
```
160-
where $j = \lfloor \frac{D}{2}\rfloor + V + 1$, and $f_V$ are polynomials defined as follows:
161+
where $j = \lfloor \frac{d}{2}\rfloor + v + 1$, and $f_v$ are polynomials defined as
162+
follows:
161163
```math
162164
\begin{aligned}
163-
f_0(r, j) &= 1, \\
164-
f_1(r, j) &= 1 + (j + 1) r, \\
165-
f_2(r, j) &= 1 + (j + 2) r + ((j^2 + 4j + 3) / 3) r^2, \\
166-
f_3(r, j) &= 1 + (j + 3) r + ((6 j^2 + 36j + 45) / 15) r^2 + ((j^3 + 9 j^2 + 23j + 15) / 15) r^3.
165+
f_0(r, j) &= 1, \\
166+
f_1(r, j) &= 1 + (j + 1) r, \\
167+
f_2(r, j) &= 1 + (j + 2) r + ((j^2 + 4j + 3) / 3) r^2, \\
168+
f_3(r, j) &= 1 + (j + 3) r + ((6 j^2 + 36j + 45) / 15) r^2 + ((j^3 + 9 j^2 + 23j + 15) / 15) r^3.
167169
\end{aligned}
168170
```
169171

src/KernelFunctions.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export FBMKernel
3131
export MaternKernel, Matern12Kernel, Matern32Kernel, Matern52Kernel
3232
export LinearKernel, PolynomialKernel
3333
export RationalQuadraticKernel, GammaRationalQuadraticKernel
34-
export MahalanobisKernel, GaborKernel, PiecewisePolynomialKernel
34+
export GaborKernel, PiecewisePolynomialKernel
3535
export PeriodicKernel, NeuralNetworkKernel
3636
export KernelSum, KernelProduct
3737
export TransformedKernel, ScaledKernel
@@ -84,7 +84,6 @@ include(joinpath("basekernels", "exponential.jl"))
8484
include(joinpath("basekernels", "exponentiated.jl"))
8585
include(joinpath("basekernels", "fbm.jl"))
8686
include(joinpath("basekernels", "gabor.jl"))
87-
include(joinpath("basekernels", "maha.jl"))
8887
include(joinpath("basekernels", "matern.jl"))
8988
include(joinpath("basekernels", "nn.jl"))
9089
include(joinpath("basekernels", "periodic.jl"))
@@ -112,6 +111,8 @@ include("zygote_adjoints.jl")
112111

113112
include("test_utils.jl")
114113

114+
include("deprecated.jl")
115+
115116
function __init__()
116117
@require Kronecker="2c470bb0-bcc8-11e8-3dad-c9649493f05e" begin
117118
include(joinpath("matrix", "kernelkroneckermat.jl"))

src/basekernels/maha.jl

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,60 @@
11
"""
2-
PiecewisePolynomialKernel{V}(maha::AbstractMatrix)
2+
PiecewisePolynomialKernel(; v::Int=0, d::Int)
3+
PiecewisePolynomialKernel{v}(d::Int)
34
4-
Piecewise Polynomial covariance function with compact support, V = 0,1,2,3.
5-
The kernel functions are 2V times continuously differentiable and the corresponding
6-
processes are hence V times mean-square differentiable. The kernel function is:
5+
Piecewise polynomial kernel with compact support.
6+
7+
The kernel is defined for ``x, x' \\in \\mathbb{R}^d`` and ``v \\in \\{0,1,2,3\\}`` as
8+
```math
9+
k(x, x'; v) = \\max(1 - \\|x - x'\\|, 0)^{j + V} f_v(\\|x - x'\\|, j),
10+
```
11+
where ``j = \\lfloor \\frac{d}{2}\\rfloor + v + 1``, and ``f_v`` are polynomials defined as
12+
follows:
713
```math
8-
κ(x, y) = max(1 - r, 0)^(j + V) * f(r, j) with j = floor(D / 2) + V + 1
14+
\\begin{aligned}
15+
f_0(r, j) &= 1, \\\\
16+
f_1(r, j) &= 1 + (j + 1) r, \\\\
17+
f_2(r, j) &= 1 + (j + 2) r + ((j^2 + 4j + 3) / 3) r^2, \\\\
18+
f_3(r, j) &= 1 + (j + 3) r + ((6 j^2 + 36j + 45) / 15) r^2 + ((j^3 + 9 j^2 + 23j + 15) / 15) r^3.
19+
\\end{aligned}
920
```
10-
where `r` is the Mahalanobis distance mahalanobis(x,y) with `maha` as the metric.
21+
22+
The kernel is ``2v`` times continuously differentiable and the corresponding Gaussian
23+
process is hence ``v`` times mean-square differentiable.
1124
"""
12-
struct PiecewisePolynomialKernel{V, A<:AbstractMatrix{<:Real}} <: SimpleKernel
13-
maha::A
25+
struct PiecewisePolynomialKernel{V} <: SimpleKernel
1426
j::Int
15-
function PiecewisePolynomialKernel{V}(maha::AbstractMatrix{<:Real}) where V
27+
function PiecewisePolynomialKernel{V}(d::Int) where V
1628
V in (0, 1, 2, 3) || error("Invalid parameter V=$(V). Should be 0, 1, 2 or 3.")
17-
LinearAlgebra.checksquare(maha)
18-
j = div(size(maha, 1), 2) + V + 1
19-
return new{V,typeof(maha)}(maha, j)
29+
d > 0 || error("number of dimensions has to be positive")
30+
j = div(d, 2) + V + 1
31+
return new{V}(j)
2032
end
2133
end
2234

23-
function PiecewisePolynomialKernel(;v::Integer=0, maha::AbstractMatrix{<:Real})
24-
return PiecewisePolynomialKernel{v}(maha)
25-
end
26-
27-
# Have to reconstruct the type parameter
28-
# See also https://github.com/FluxML/Functors.jl/issues/3#issuecomment-626747663
29-
function Functors.functor(::Type{<:PiecewisePolynomialKernel{V}}, x) where V
30-
function reconstruct_kernel(xs)
31-
return PiecewisePolynomialKernel{V}(xs.maha)
35+
# TODO: remove `maha` keyword argument in next breaking release
36+
function PiecewisePolynomialKernel(; v::Int=0, maha=nothing, d::Int=-1)
37+
if maha !== nothing
38+
Base.depwarn("keyword argument `maha` is deprecated", :PiecewisePolynomialKernel)
39+
d = size(maha, 1)
40+
return transform(PiecewisePolynomialKernel{v}(d), cholesky(maha).U)
41+
else
42+
return PiecewisePolynomialKernel{v}(d)
3243
end
33-
return (maha = x.maha,), reconstruct_kernel
3444
end
3545

36-
_f::PiecewisePolynomialKernel{0}, r, j) = 1
37-
_f::PiecewisePolynomialKernel{1}, r, j) = 1 + (j + 1) * r
38-
_f::PiecewisePolynomialKernel{2}, r, j) = 1 + (j + 2) * r + (j^2 + 4 * j + 3) / 3 * r.^2
39-
_f::PiecewisePolynomialKernel{3}, r, j) = 1 + (j + 3) * r +
46+
_f(::PiecewisePolynomialKernel{1}, r, j) = 1 + (j + 1) * r
47+
_f(::PiecewisePolynomialKernel{2}, r, j) = 1 + (j + 2) * r + (j^2 + 4 * j + 3) / 3 * r.^2
48+
_f(::PiecewisePolynomialKernel{3}, r, j) = 1 + (j + 3) * r +
4049
(6 * j^2 + 36j + 45) / 15 * r.^2 + (j^3 + 9 * j^2 + 23j + 15) / 15 * r.^3
4150

42-
kappa::PiecewisePolynomialKernel{V}, r) where V = max(1 - r, 0)^.j + V) * _f(κ, r, κ.j)
51+
kappa::PiecewisePolynomialKernel{0}, r) = max(1 - r, 0)^κ.j
52+
function kappa::PiecewisePolynomialKernel{V}, r) where V
53+
return max(1 - r, 0)^.j + V) * _f(κ, r, κ.j)
54+
end
4355

44-
metric(κ::PiecewisePolynomialKernel) = Mahalanobis.maha)
56+
metric(::PiecewisePolynomialKernel) = Euclidean()
4557

4658
function Base.show(io::IO, κ::PiecewisePolynomialKernel{V}) where {V}
47-
print(io, "Piecewise Polynomial Kernel (v = ", V, ", size(maha) = ", size.maha), ")")
59+
print(io, "Piecewise Polynomial Kernel (v = ", V, ", ⌊d/2⌋ = ", κ.j - 1 - V , ")")
4860
end

src/deprecated.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# TODO: remove tests when removed
2+
@deprecate MahalanobisKernel(; P::AbstractMatrix{<:Real}) transform(SqExponentialKernel(), cholesky(P).U)
3+
4+
# TODO: remove keyword argument `maha` when removed
5+
@deprecate PiecewisePolynomialKernel{V}(A::AbstractMatrix{<:Real}) where V transform(PiecewisePolynomialKernel{V}(size(A, 1)), cholesky(A).U)

0 commit comments

Comments
 (0)