-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecate use of Mahalanobis distance (#225)
- Loading branch information
Showing
11 changed files
with
133 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,89 @@ | ||
""" | ||
PiecewisePolynomialKernel{V}(maha::AbstractMatrix) | ||
@doc raw""" | ||
PiecewisePolynomialKernel(; degree::Int=0, dim::Int) | ||
PiecewisePolynomialKernel{degree}(dim::Int) | ||
Piecewise polynomial kernel of degree `degree` for inputs of dimension `dim` with support in | ||
the unit ball. | ||
# Definition | ||
Piecewise Polynomial covariance function with compact support, V = 0,1,2,3. | ||
The kernel functions are 2V times continuously differentiable and the corresponding | ||
processes are hence V times mean-square differentiable. The kernel function is: | ||
For inputs ``x, x' \in \mathbb{R}^d`` of dimension ``d``, the piecewise polynomial kernel | ||
of degree ``v \in \{0,1,2,3\}`` is defined as | ||
```math | ||
κ(x, y) = max(1 - r, 0)^(j + V) * f(r, j) with j = floor(D / 2) + V + 1 | ||
k(x, x'; v) = \max(1 - \|x - x'\|, 0)^{\alpha(v,d)} f_{v,d}(\|x - x'\|), | ||
``` | ||
where `r` is the Mahalanobis distance mahalanobis(x,y) with `maha` as the metric. | ||
where ``\alpha(v, d) = \lfloor \frac{d}{2}\rfloor + 2v + 1`` and ``f_{v,d}`` are | ||
polynomials of degree ``v`` given by | ||
```math | ||
\begin{aligned} | ||
f_{0,d}(r) &= 1, \\ | ||
f_{1,d}(r) &= 1 + (j + 1) r, \\ | ||
f_{2,d}(r) &= 1 + (j + 2) r + \big((j^2 + 4j + 3) / 3\big) r^2, \\ | ||
f_{3,d}(r) &= 1 + (j + 3) r + \big((6 j^2 + 36j + 45) / 15\big) r^2 + \big((j^3 + 9 j^2 + 23j + 15) / 15\big) r^3, | ||
\end{aligned} | ||
``` | ||
where ``j = \lfloor \frac{d}{2}\rfloor + v + 1``. | ||
The kernel is ``2v`` times continuously differentiable and the corresponding Gaussian | ||
process is hence ``v`` times mean-square differentiable. | ||
""" | ||
struct PiecewisePolynomialKernel{V,A<:AbstractMatrix{<:Real}} <: SimpleKernel | ||
maha::A | ||
j::Int | ||
function PiecewisePolynomialKernel{V}(maha::AbstractMatrix{<:Real}) where {V} | ||
V in (0, 1, 2, 3) || error("Invalid parameter V=$(V). Should be 0, 1, 2 or 3.") | ||
LinearAlgebra.checksquare(maha) | ||
j = div(size(maha, 1), 2) + V + 1 | ||
return new{V,typeof(maha)}(maha, j) | ||
struct PiecewisePolynomialKernel{D,C<:Tuple} <: SimpleKernel | ||
alpha::Int | ||
coeffs::C | ||
|
||
function PiecewisePolynomialKernel{D}(dim::Int) where {D} | ||
dim > 0 || error("number of dimensions has to be positive") | ||
j = div(dim, 2) + D + 1 | ||
alpha = j + D | ||
coeffs = piecewise_polynomial_coefficients(Val(D), j) | ||
return new{D,typeof(coeffs)}(alpha, coeffs) | ||
end | ||
end | ||
|
||
function PiecewisePolynomialKernel(; v::Integer=0, maha::AbstractMatrix{<:Real}) | ||
return PiecewisePolynomialKernel{v}(maha) | ||
end | ||
# TODO: remove `maha` keyword argument in next breaking release | ||
function PiecewisePolynomialKernel(; v::Int=-1, degree::Int=v, maha=nothing, dim::Int=-1) | ||
if v != -1 | ||
Base.depwarn( | ||
"keyword argument `v` is deprecated, use `degree` instead", | ||
:PiecewisePolynomialKernel, | ||
) | ||
end | ||
|
||
# Have to reconstruct the type parameter | ||
# See also https://github.com/FluxML/Functors.jl/issues/3#issuecomment-626747663 | ||
function Functors.functor(::Type{<:PiecewisePolynomialKernel{V}}, x) where {V} | ||
function reconstruct_kernel(xs) | ||
return PiecewisePolynomialKernel{V}(xs.maha) | ||
if maha !== nothing | ||
Base.depwarn( | ||
"keyword argument `maha` is deprecated, use a `LinearTransform` instead", | ||
:PiecewisePolynomialKernel, | ||
) | ||
dim = size(maha, 1) | ||
return transform( | ||
PiecewisePolynomialKernel{degree}(dim), LinearTransform(cholesky(maha).U) | ||
) | ||
else | ||
return PiecewisePolynomialKernel{degree}(dim) | ||
end | ||
return (maha=x.maha,), reconstruct_kernel | ||
end | ||
|
||
_f(κ::PiecewisePolynomialKernel{0}, r, j) = 1 | ||
_f(κ::PiecewisePolynomialKernel{1}, r, j) = 1 + (j + 1) * r | ||
_f(κ::PiecewisePolynomialKernel{2}, r, j) = 1 + (j + 2) * r + (j^2 + 4 * j + 3) / 3 * r .^ 2 | ||
function _f(κ::PiecewisePolynomialKernel{3}, r, j) | ||
return 1 + | ||
(j + 3) * r + | ||
(6 * j^2 + 36j + 45) / 15 * r .^ 2 + | ||
(j^3 + 9 * j^2 + 23j + 15) / 15 * r .^ 3 | ||
piecewise_polynomial_coefficients(::Val{0}, ::Int) = (1,) | ||
piecewise_polynomial_coefficients(::Val{1}, j::Int) = (1, j + 1) | ||
piecewise_polynomial_coefficients(::Val{2}, j::Int) = (1, j + 2, (j^2 + 4 * j)//3 + 1) | ||
function piecewise_polynomial_coefficients(::Val{3}, j::Int) | ||
return (1, j + 3, (2 * j^2 + 12 * j)//5 + 3, (j^3 + 9 * j^2 + 23 * j)//15 + 1) | ||
end | ||
|
||
function kappa(κ::PiecewisePolynomialKernel{V}, r) where {V} | ||
return max(1 - r, 0)^(κ.j + V) * _f(κ, r, κ.j) | ||
function piecewise_polynomial_coefficients(::Val{D}, ::Int) where {D} | ||
return error("invalid degree $D, only 0, 1, 2, or 3 are supported") | ||
end | ||
|
||
metric(κ::PiecewisePolynomialKernel) = Mahalanobis(κ.maha) | ||
kappa(κ::PiecewisePolynomialKernel, r) = max(1 - r, 0)^κ.alpha * evalpoly(r, κ.coeffs) | ||
|
||
metric(::PiecewisePolynomialKernel) = Euclidean() | ||
|
||
function Base.show(io::IO, κ::PiecewisePolynomialKernel{V}) where {V} | ||
function Base.show(io::IO, κ::PiecewisePolynomialKernel{D}) where {D} | ||
return print( | ||
io, "Piecewise Polynomial Kernel (v = ", V, ", size(maha) = ", size(κ.maha), ")" | ||
io, | ||
"Piecewise Polynomial Kernel (degree = ", | ||
D, | ||
", ⌊dim/2⌋ = ", | ||
κ.alpha - 1 - 2 * D, | ||
")", | ||
) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# TODO: remove tests when removed | ||
@deprecate MahalanobisKernel(; P::AbstractMatrix{<:Real}) transform( | ||
SqExponentialKernel(), LinearTransform(sqrt(2) .* cholesky(P).U) | ||
) | ||
|
||
# TODO: remove keyword argument `maha` when removed | ||
@deprecate PiecewisePolynomialKernel{V}(A::AbstractMatrix{<:Real}) where {V} transform( | ||
PiecewisePolynomialKernel{V}(size(A, 1)), LinearTransform(cholesky(A).U) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b2c2e52
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
b2c2e52
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/28010
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: