-
Notifications
You must be signed in to change notification settings - Fork 36
Deprecate use of Mahalanobis distance #225
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
Merged
Changes from 6 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,65 @@ | ||
""" | ||
devmotion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PiecewisePolynomialKernel{V}(maha::AbstractMatrix) | ||
PiecewisePolynomialKernel(; v::Int=0, d::Int) | ||
PiecewisePolynomialKernel{v}(d::Int) | ||
|
||
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: | ||
Piecewise polynomial kernel with compact support. | ||
|
||
The kernel is defined for ``x, x' \\in \\mathbb{R}^d`` and ``v \\in \\{0,1,2,3\\}`` as | ||
```math | ||
k(x, x'; v) = \\max(1 - \\|x - x'\\|, 0)^{j + v} f_v(\\|x - x'\\|, j), | ||
``` | ||
where ``j = \\lfloor \\frac{d}{2}\\rfloor + v + 1``, and ``f_v`` are polynomials defined as | ||
follows: | ||
```math | ||
κ(x, y) = max(1 - r, 0)^(j + V) * f(r, j) with j = floor(D / 2) + V + 1 | ||
\\begin{aligned} | ||
f_0(r, j) &= 1, \\\\ | ||
f_1(r, j) &= 1 + (j + 1) r, \\\\ | ||
f_2(r, j) &= 1 + (j + 2) r + ((j^2 + 4j + 3) / 3) r^2, \\\\ | ||
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. | ||
\\end{aligned} | ||
``` | ||
where `r` is the Mahalanobis distance mahalanobis(x,y) with `maha` as the metric. | ||
|
||
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 | ||
struct PiecewisePolynomialKernel{V} <: SimpleKernel | ||
j::Int | ||
function PiecewisePolynomialKernel{V}(maha::AbstractMatrix{<:Real}) where {V} | ||
|
||
function PiecewisePolynomialKernel{V}(d::Int) 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) | ||
d > 0 || error("number of dimensions has to be positive") | ||
j = div(d, 2) + V + 1 | ||
return new{V}(j) | ||
end | ||
end | ||
|
||
function PiecewisePolynomialKernel(; v::Integer=0, maha::AbstractMatrix{<:Real}) | ||
return PiecewisePolynomialKernel{v}(maha) | ||
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) | ||
# TODO: remove `maha` keyword argument in next breaking release | ||
function PiecewisePolynomialKernel(; v::Int=0, maha=nothing, d::Int=-1) | ||
if maha !== nothing | ||
Base.depwarn("keyword argument `maha` is deprecated", :PiecewisePolynomialKernel) | ||
d = size(maha, 1) | ||
return transform(PiecewisePolynomialKernel{v}(d), LinearTransform(cholesky(maha).U)) | ||
theogf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else | ||
return PiecewisePolynomialKernel{v}(d) | ||
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) | ||
_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 | ||
(j + 3) * r + | ||
(6 * j^2 + 36j + 45) / 15 * r ^ 2 + | ||
(j^3 + 9 * j^2 + 23j + 15) / 15 * r ^ 3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JuliaFormatter wants this to be formatted return 1 +
(j + 3) * r +
(6 * j^2 + 36j + 45) / 15 * r ^ 2 +
(j^3 + 9 * j^2 + 23j + 15) / 15 * r ^ 3 This feels weird since usually we just increase the indentation level but do not align operators or lines. Maybe I missed it but I couldn't find any information about similar cases in BlueStyle - is this a bug in JuliaFormatter? |
||
end | ||
|
||
kappa(κ::PiecewisePolynomialKernel{0}, r) = max(1 - r, 0)^κ.j | ||
function kappa(κ::PiecewisePolynomialKernel{V}, r) where {V} | ||
return max(1 - r, 0)^(κ.j + V) * _f(κ, r, κ.j) | ||
end | ||
|
||
metric(κ::PiecewisePolynomialKernel) = Mahalanobis(κ.maha) | ||
metric(::PiecewisePolynomialKernel) = Euclidean() | ||
|
||
function Base.show(io::IO, κ::PiecewisePolynomialKernel{V}) where {V} | ||
return print( | ||
io, "Piecewise Polynomial Kernel (v = ", V, ", size(maha) = ", size(κ.maha), ")" | ||
) | ||
return print(io, "Piecewise Polynomial Kernel (v = ", V, ", ⌊d/2⌋ = ", κ.j - 1 - V, ")") | ||
end |
This file contains hidden or 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 hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.