Skip to content

Commit 5c82b5c

Browse files
committed
Deprecate use of Mahalanobis distance
1 parent d00fb95 commit 5c82b5c

File tree

4 files changed

+60
-41
lines changed

4 files changed

+60
-41
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
@@ -90,7 +90,6 @@ include(joinpath("basekernels", "exponential.jl"))
9090
include(joinpath("basekernels", "exponentiated.jl"))
9191
include(joinpath("basekernels", "fbm.jl"))
9292
include(joinpath("basekernels", "gabor.jl"))
93-
include(joinpath("basekernels", "maha.jl"))
9493
include(joinpath("basekernels", "matern.jl"))
9594
include(joinpath("basekernels", "nn.jl"))
9695
include(joinpath("basekernels", "periodic.jl"))
@@ -118,6 +117,8 @@ include("zygote_adjoints.jl")
118117

119118
include("test_utils.jl")
120119

120+
include("deprecated.jl")
121+
121122
function __init__()
122123
@require Kronecker = "2c470bb0-bcc8-11e8-3dad-c9649493f05e" begin
123124
include(joinpath("matrix", "kernelkroneckermat.jl"))
Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,67 @@
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+
28+
function PiecewisePolynomialKernel{V}(d::Int) where V
1629
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)
30+
d > 0 || error("number of dimensions has to be positive")
31+
j = div(d, 2) + V + 1
32+
return new{V}(j)
2033
end
2134
end
2235

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)
36+
# TODO: remove `maha` keyword argument in next breaking release
37+
function PiecewisePolynomialKernel(; v::Int=0, maha=nothing, d::Int=-1)
38+
if maha !== nothing
39+
Base.depwarn("keyword argument `maha` is deprecated", :PiecewisePolynomialKernel)
40+
d = size(maha, 1)
41+
return transform(PiecewisePolynomialKernel{v}(d), cholesky(maha).U)
42+
else
43+
return PiecewisePolynomialKernel{v}(d)
3244
end
33-
return (maha=x.maha,), reconstruct_kernel
3445
end
3546

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-
function _f::PiecewisePolynomialKernel{3}, r, j)
47+
_f(::PiecewisePolynomialKernel{1}, r, j) = 1 + (j + 1) * r
48+
_f(::PiecewisePolynomialKernel{2}, r, j) = 1 + (j + 2) * r + (j^2 + 4 * j + 3) / 3 * r^2
49+
function _f(::PiecewisePolynomialKernel{3}, r, j)
4050
return 1 +
41-
(j + 3) * r +
42-
(6 * j^2 + 36j + 45) / 15 * r .^ 2 +
43-
(j^3 + 9 * j^2 + 23j + 15) / 15 * r .^ 3
51+
(j + 3) * r +
52+
(6 * j^2 + 36j + 45) / 15 * r ^ 2 +
53+
(j^3 + 9 * j^2 + 23j + 15) / 15 * r ^ 3
4454
end
4555

46-
function kappa::PiecewisePolynomialKernel{V}, r) where {V}
56+
kappa::PiecewisePolynomialKernel{0}, r) = max(1 - r, 0)^κ.j
57+
function kappa::PiecewisePolynomialKernel{V}, r) where V
4758
return max(1 - r, 0)^.j + V) * _f(κ, r, κ.j)
4859
end
4960

50-
metric(κ::PiecewisePolynomialKernel) = Mahalanobis.maha)
61+
metric(::PiecewisePolynomialKernel) = Euclidean()
5162

5263
function Base.show(io::IO, κ::PiecewisePolynomialKernel{V}) where {V}
5364
return print(
54-
io, "Piecewise Polynomial Kernel (v = ", V, ", size(maha) = ", size.maha), ")"
65+
io, "Piecewise Polynomial Kernel (v = ", V, ", ⌊d/2⌋ = ", κ.j - 1 - V , ")",
5566
)
5667
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)