|
31 | 31 |
|
32 | 32 | @functor RationalKernel
|
33 | 33 |
|
34 |
| -function kappa(κ::RationalKernel, d::Real) |
35 |
| - return (one(d) + d / only(κ.α))^(-only(κ.α)) |
36 |
| -end |
| 34 | +__rational_kappa(α::Real, d::Real) = (one(d) + d / α)^(-α) |
| 35 | + |
| 36 | +kappa(κ::RationalKernel, d::Real) = __rational_kappa(only(κ.α), d) |
37 | 37 |
|
38 | 38 | metric(k::RationalKernel) = k.metric
|
39 | 39 |
|
| 40 | +# AD-performance optimisation. Is unit tested. |
| 41 | +function kernelmatrix(k::RationalKernel, x::AbstractVector, y::AbstractVector) |
| 42 | + return __rational_kappa.(only(k.α), pairwise(metric(k), x, y)) |
| 43 | +end |
| 44 | + |
| 45 | +# AD-performance optimisation. Is unit tested. |
| 46 | +function kernelmatrix(k::RationalKernel, x::AbstractVector) |
| 47 | + return __rational_kappa.(only(k.α), pairwise(metric(k), x)) |
| 48 | +end |
| 49 | + |
| 50 | +# AD-performance optimisation. Is unit tested. |
| 51 | +function kernelmatrix_diag(k::RationalKernel, x::AbstractVector, y::AbstractVector) |
| 52 | + return __rational_kappa.(only(k.α), colwise(metric(k), x, y)) |
| 53 | +end |
| 54 | + |
| 55 | +# AD-performance optimisation. Is unit tested. |
| 56 | +function kernelmatrix_diag(k::RationalKernel, x::AbstractVector) |
| 57 | + return __rational_kappa.(only(k.α), colwise(metric(k), x)) |
| 58 | +end |
| 59 | + |
40 | 60 | function Base.show(io::IO, κ::RationalKernel)
|
41 | 61 | return print(io, "Rational Kernel (α = ", only(κ.α), ", metric = ", κ.metric, ")")
|
42 | 62 | end
|
@@ -69,18 +89,59 @@ struct RationalQuadraticKernel{Tα<:Real,M} <: SimpleKernel
|
69 | 89 | end
|
70 | 90 | end
|
71 | 91 |
|
| 92 | +const _RQ_Euclidean = RationalQuadraticKernel{<:Real,<:Euclidean} |
| 93 | + |
72 | 94 | @functor RationalQuadraticKernel
|
73 | 95 |
|
74 |
| -function kappa(κ::RationalQuadraticKernel, d::Real) |
75 |
| - return (one(d) + d^2 / (2 * only(κ.α)))^(-only(κ.α)) |
76 |
| -end |
77 |
| -function kappa(κ::RationalQuadraticKernel{<:Real,<:Euclidean}, d²::Real) |
78 |
| - return (one(d²) + d² / (2 * only(κ.α)))^(-only(κ.α)) |
79 |
| -end |
| 96 | +__rq_kappa(α::Real, d::Real) = (one(d) + d^2 / (2 * α))^(-α) |
| 97 | +__rq_kappa_euclidean(α::Real, d²::Real) = (one(d²) + d² / (2 * α))^(-α) |
| 98 | + |
| 99 | +kappa(κ::RationalQuadraticKernel, d::Real) = __rq_kappa(only(κ.α), d) |
| 100 | +kappa(κ::_RQ_Euclidean, d²::Real) = __rq_kappa_euclidean(only(κ.α), d²) |
80 | 101 |
|
81 | 102 | metric(k::RationalQuadraticKernel) = k.metric
|
82 | 103 | metric(::RationalQuadraticKernel{<:Real,<:Euclidean}) = SqEuclidean()
|
83 | 104 |
|
| 105 | +# AD-performance optimisation. Is unit tested. |
| 106 | +function kernelmatrix(k::RationalQuadraticKernel, x::AbstractVector, y::AbstractVector) |
| 107 | + return __rq_kappa.(only(k.α), pairwise(metric(k), x, y)) |
| 108 | +end |
| 109 | + |
| 110 | +# AD-performance optimisation. Is unit tested. |
| 111 | +function kernelmatrix(k::RationalQuadraticKernel, x::AbstractVector) |
| 112 | + return __rq_kappa.(only(k.α), pairwise(metric(k), x)) |
| 113 | +end |
| 114 | + |
| 115 | +# AD-performance optimisation. Is unit tested. |
| 116 | +function kernelmatrix_diag(k::RationalQuadraticKernel, x::AbstractVector, y::AbstractVector) |
| 117 | + return __rq_kappa.(only(k.α), colwise(metric(k), x, y)) |
| 118 | +end |
| 119 | + |
| 120 | +# AD-performance optimisation. Is unit tested. |
| 121 | +function kernelmatrix_diag(k::RationalQuadraticKernel, x::AbstractVector) |
| 122 | + return __rq_kappa.(only(k.α), colwise(metric(k), x)) |
| 123 | +end |
| 124 | + |
| 125 | +# AD-performance optimisation. Is unit tested. |
| 126 | +function kernelmatrix(k::_RQ_Euclidean, x::AbstractVector, y::AbstractVector) |
| 127 | + return __rq_kappa_euclidean.(only(k.α), pairwise(SqEuclidean(), x, y)) |
| 128 | +end |
| 129 | + |
| 130 | +# AD-performance optimisation. Is unit tested. |
| 131 | +function kernelmatrix(k::_RQ_Euclidean, x::AbstractVector) |
| 132 | + return __rq_kappa_euclidean.(only(k.α), pairwise(SqEuclidean(), x)) |
| 133 | +end |
| 134 | + |
| 135 | +# AD-performance optimisation. Is unit tested. |
| 136 | +function kernelmatrix_diag(k::_RQ_Euclidean, x::AbstractVector, y::AbstractVector) |
| 137 | + return __rq_kappa_euclidean.(only(k.α), colwise(SqEuclidean(), x, y)) |
| 138 | +end |
| 139 | + |
| 140 | +# AD-performance optimisation. Is unit tested. |
| 141 | +function kernelmatrix_diag(k::_RQ_Euclidean, x::AbstractVector) |
| 142 | + return __rq_kappa_euclidean.(only(k.α), colwise(SqEuclidean(), x)) |
| 143 | +end |
| 144 | + |
84 | 145 | function Base.show(io::IO, κ::RationalQuadraticKernel)
|
85 | 146 | return print(
|
86 | 147 | io, "Rational Quadratic Kernel (α = ", only(κ.α), ", metric = ", κ.metric, ")"
|
@@ -121,12 +182,32 @@ end
|
121 | 182 |
|
122 | 183 | @functor GammaRationalKernel
|
123 | 184 |
|
124 |
| -function kappa(κ::GammaRationalKernel, d::Real) |
125 |
| - return (one(d) + d^only(κ.γ) / only(κ.α))^(-only(κ.α)) |
126 |
| -end |
| 185 | +__grk_kappa(α::Real, γ::Real, d::Real) = (one(d) + d^γ / α)^(-α) |
| 186 | + |
| 187 | +kappa(κ::GammaRationalKernel, d::Real) = __grk_kappa(only(κ.α), only(κ.γ), d) |
127 | 188 |
|
128 | 189 | metric(k::GammaRationalKernel) = k.metric
|
129 | 190 |
|
| 191 | +# AD-performance optimisation. Is unit tested. |
| 192 | +function kernelmatrix(k::GammaRationalKernel, x::AbstractVector, y::AbstractVector) |
| 193 | + return __grk_kappa.(only(k.α), only(k.γ), pairwise(metric(k), x, y)) |
| 194 | +end |
| 195 | + |
| 196 | +# AD-performance optimisation. Is unit tested. |
| 197 | +function kernelmatrix(k::GammaRationalKernel, x::AbstractVector) |
| 198 | + return __grk_kappa.(only(k.α), only(k.γ), pairwise(metric(k), x)) |
| 199 | +end |
| 200 | + |
| 201 | +# AD-performance optimisation. Is unit tested. |
| 202 | +function kernelmatrix_diag(k::GammaRationalKernel, x::AbstractVector, y::AbstractVector) |
| 203 | + return __grk_kappa.(only(k.α), only(k.γ), colwise(metric(k), x, y)) |
| 204 | +end |
| 205 | + |
| 206 | +# AD-performance optimisation. Is unit tested. |
| 207 | +function kernelmatrix_diag(k::GammaRationalKernel, x::AbstractVector) |
| 208 | + return __grk_kappa.(only(k.α), only(k.γ), colwise(metric(k), x)) |
| 209 | +end |
| 210 | + |
130 | 211 | function Base.show(io::IO, κ::GammaRationalKernel)
|
131 | 212 | return print(
|
132 | 213 | io,
|
|
0 commit comments