-
Notifications
You must be signed in to change notification settings - Fork 32
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
Deprecate use of Mahalanobis distance #225
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5c82b5c
Deprecate use of Mahalanobis distance
devmotion 99e1bf1
Add missing `LinearTransform`
devmotion fce0c64
Update tests
devmotion 25ffe66
Try to fix error in format check
devmotion a182f21
Fix most formatting issues
devmotion da32bdf
Merge branch 'dw/maha' of github.com:JuliaGaussianProcesses/KernelFun…
devmotion 46c75e9
Add documentation
devmotion e67cb06
Rename keyword arguments
devmotion df67a7f
Fix format and errors on Julia 1.3
devmotion 742781d
Remove remaining parts of `MahalanobisKernel`
devmotion 6fd00ec
Improve LaTeX output
devmotion da93193
Bump version and increase Compat version
devmotion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I forgot that we already depend on Compat which contains an implementation of
evalpoly
for Julia < 1.4. It was introduced in Compat 3.7. Is it OK to drop support for older versions?@theogf @willtebbutt @st--
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.
Latest version of Compat is 3.25.
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.
No objections here.