|
| 1 | +""" |
| 2 | + RationalKernel(; α::Real=2.0) |
| 3 | +
|
| 4 | +Rational kernel with shape parameter `α`. |
| 5 | +
|
| 6 | +# Definition |
| 7 | +
|
| 8 | +For inputs ``x, x' \\in \\mathbb{R}^d``, the rational kernel with shape parameter |
| 9 | +``\\alpha > 0`` is defined as |
| 10 | +```math |
| 11 | +k(x, x'; \\alpha) = \\bigg(1 + \\frac{\\|x - x'\\|_2}{\\alpha}\\bigg)^{-\\alpha}. |
| 12 | +``` |
| 13 | +
|
| 14 | +The [`ExponentialKernel`](@ref) is recovered in the limit as ``\\alpha \\to \\infty``. |
| 15 | +
|
| 16 | +See also: [`GammaRationalKernel`](@ref) |
| 17 | +""" |
| 18 | +struct RationalKernel{Tα<:Real} <: SimpleKernel |
| 19 | + α::Vector{Tα} |
| 20 | + function RationalKernel(; alpha::T=2.0, α::T=alpha) where {T} |
| 21 | + @check_args(RationalKernel, α, α > zero(T), "α > 0") |
| 22 | + return new{T}([α]) |
| 23 | + end |
| 24 | +end |
| 25 | + |
| 26 | +@functor RationalKernel |
| 27 | + |
| 28 | +function kappa(κ::RationalKernel, d::Real) |
| 29 | + return (one(d) + d / first(κ.α))^(-first(κ.α)) |
| 30 | +end |
| 31 | + |
| 32 | +metric(::RationalKernel) = Euclidean() |
| 33 | + |
| 34 | +function Base.show(io::IO, κ::RationalKernel) |
| 35 | + return print(io, "Rational Kernel (α = $(first(κ.α)))") |
| 36 | +end |
| 37 | + |
| 38 | +""" |
| 39 | + RationalQuadraticKernel(; α::Real=2.0) |
| 40 | +
|
| 41 | +Rational-quadratic kernel with shape parameter `α`. |
| 42 | +
|
| 43 | +# Definition |
| 44 | +
|
| 45 | +For inputs ``x, x' \\in \\mathbb{R}^d``, the rational-quadratic kernel with shape parameter |
| 46 | +``\\alpha > 0`` is defined as |
| 47 | +```math |
| 48 | +k(x, x'; \\alpha) = \\bigg(1 + \\frac{\\|x - x'\\|_2^2}{2\\alpha}\\bigg)^{-\\alpha}. |
| 49 | +``` |
| 50 | +
|
| 51 | +The [`SqExponentialKernel`](@ref) is recovered in the limit as ``\\alpha \\to \\infty``. |
| 52 | +
|
| 53 | +See also: [`GammaRationalKernel`](@ref) |
| 54 | +""" |
| 55 | +struct RationalQuadraticKernel{Tα<:Real} <: SimpleKernel |
| 56 | + α::Vector{Tα} |
| 57 | + function RationalQuadraticKernel(; alpha::T=2.0, α::T=alpha) where {T} |
| 58 | + @check_args(RationalQuadraticKernel, α, α > zero(T), "α > 0") |
| 59 | + return new{T}([α]) |
| 60 | + end |
| 61 | +end |
| 62 | + |
| 63 | +@functor RationalQuadraticKernel |
| 64 | + |
| 65 | +function kappa(κ::RationalQuadraticKernel, d²::T) where {T<:Real} |
| 66 | + return (one(T) + d² / (2 * first(κ.α)))^(-first(κ.α)) |
| 67 | +end |
| 68 | + |
| 69 | +metric(::RationalQuadraticKernel) = SqEuclidean() |
| 70 | + |
| 71 | +function Base.show(io::IO, κ::RationalQuadraticKernel) |
| 72 | + return print(io, "Rational Quadratic Kernel (α = $(first(κ.α)))") |
| 73 | +end |
| 74 | + |
| 75 | +""" |
| 76 | + GammaRationalKernel(; α::Real=2.0, γ::Real=2.0) |
| 77 | +
|
| 78 | +γ-rational kernel with shape parameters `α` and `γ`. |
| 79 | +
|
| 80 | +# Definition |
| 81 | +
|
| 82 | +For inputs ``x, x' \\in \\mathbb{R}^d``, the γ-rational kernel with shape |
| 83 | +parameters ``\\alpha > 0`` and ``\\gamma \\in (0, 2]`` is defined as |
| 84 | +```math |
| 85 | +k(x, x'; \\alpha, \\gamma) = \\bigg(1 + \\frac{\\|x - x'\\|_2^{\\gamma}}{\\alpha}\\bigg)^{-\\alpha}. |
| 86 | +``` |
| 87 | +
|
| 88 | +The [`GammaExponentialKernel`](@ref) is recovered in the limit as ``\\alpha \\to \\infty``. |
| 89 | +
|
| 90 | +!!! warning |
| 91 | + The default value of parameter `γ` will be changed to `1.0` in the next breaking release |
| 92 | + of KernelFunctions. |
| 93 | +
|
| 94 | +See also: [`RationalKernel`](@ref), [`RationalQuadraticKernel`](@ref) |
| 95 | +""" |
| 96 | +struct GammaRationalKernel{Tα<:Real,Tγ<:Real} <: SimpleKernel |
| 97 | + α::Vector{Tα} |
| 98 | + γ::Vector{Tγ} |
| 99 | + # function GammaRationalKernel(; |
| 100 | + # alpha::Real=2.0, gamma::Real=1.0, α::Real=alpha, γ::Real=gamma |
| 101 | + # ) |
| 102 | + function GammaRationalKernel(; alpha::Real=2.0, gamma=nothing, α::Real=alpha, γ=gamma) |
| 103 | + γ2 = if γ === nothing |
| 104 | + Base.depwarn( |
| 105 | + "the default value of parameter `γ` of the `GammaRationalKernel` will " * |
| 106 | + "be changed to `1.0` in the next breaking release of KernelFunctions", |
| 107 | + :GammaRationalKernel, |
| 108 | + ) |
| 109 | + 2.0 |
| 110 | + else |
| 111 | + γ |
| 112 | + end |
| 113 | + @check_args(GammaRationalKernel, α, α > zero(α), "α > 0") |
| 114 | + @check_args(GammaRationalKernel, γ2, zero(γ2) < γ2 ≤ 2, "γ ∈ (0, 2]") |
| 115 | + return new{typeof(α),typeof(γ2)}([α], [γ2]) |
| 116 | + end |
| 117 | +end |
| 118 | + |
| 119 | +@functor GammaRationalKernel |
| 120 | + |
| 121 | +function kappa(κ::GammaRationalKernel, d::Real) |
| 122 | + return (one(d) + d^first(κ.γ) / first(κ.α))^(-first(κ.α)) |
| 123 | +end |
| 124 | + |
| 125 | +metric(::GammaRationalKernel) = Euclidean() |
| 126 | + |
| 127 | +function Base.show(io::IO, κ::GammaRationalKernel) |
| 128 | + return print(io, "Gamma Rational Kernel (α = $(first(κ.α)), γ = $(first(κ.γ)))") |
| 129 | +end |
0 commit comments