|
1 | 1 | """
|
2 |
| - PiecewisePolynomialKernel{V}(maha::AbstractMatrix) |
| 2 | + PiecewisePolynomialKernel(; v::Int=0, d::Int) |
| 3 | + PiecewisePolynomialKernel{v}(d::Int) |
3 | 4 |
|
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: |
7 | 13 | ```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} |
9 | 20 | ```
|
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. |
11 | 24 | """
|
12 |
| -struct PiecewisePolynomialKernel{V,A<:AbstractMatrix{<:Real}} <: SimpleKernel |
13 |
| - maha::A |
| 25 | +struct PiecewisePolynomialKernel{V} <: SimpleKernel |
14 | 26 | j::Int
|
15 |
| - function PiecewisePolynomialKernel{V}(maha::AbstractMatrix{<:Real}) where {V} |
| 27 | + |
| 28 | + function PiecewisePolynomialKernel{V}(d::Int) where V |
16 | 29 | 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) |
20 | 33 | end
|
21 | 34 | end
|
22 | 35 |
|
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) |
32 | 44 | end
|
33 |
| - return (maha=x.maha,), reconstruct_kernel |
34 | 45 | end
|
35 | 46 |
|
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) |
40 | 50 | 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 |
44 | 54 | end
|
45 | 55 |
|
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 |
47 | 58 | return max(1 - r, 0)^(κ.j + V) * _f(κ, r, κ.j)
|
48 | 59 | end
|
49 | 60 |
|
50 |
| -metric(κ::PiecewisePolynomialKernel) = Mahalanobis(κ.maha) |
| 61 | +metric(::PiecewisePolynomialKernel) = Euclidean() |
51 | 62 |
|
52 | 63 | function Base.show(io::IO, κ::PiecewisePolynomialKernel{V}) where {V}
|
53 | 64 | 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 , ")", |
55 | 66 | )
|
56 | 67 | end
|
0 commit comments