|
| 1 | +""" |
| 2 | + NormalizedKernel(k::Kernel) |
| 3 | +
|
| 4 | +A normalized kernel derived from `k`. |
| 5 | +
|
| 6 | +# Definition |
| 7 | +
|
| 8 | +For inputs ``x, x'``, the normalized kernel ``\\widetilde{k}`` derived from |
| 9 | +kernel ``k`` is defined as |
| 10 | +```math |
| 11 | +\\widetilde{k}(x, x'; k) = \\frac{k(x, x')}{\\sqrt{k(x, x) k(x', x')}}. |
| 12 | +``` |
| 13 | +""" |
| 14 | +struct NormalizedKernel{Tk<:Kernel} <: Kernel |
| 15 | + kernel::Tk |
| 16 | +end |
| 17 | + |
| 18 | +@functor NormalizedKernel |
| 19 | + |
| 20 | +(κ::NormalizedKernel)(x, y) = κ.kernel(x, y) / sqrt(κ.kernel(x, x) * κ.kernel(y, y)) |
| 21 | + |
| 22 | +function kernelmatrix(κ::NormalizedKernel, x::AbstractVector, y::AbstractVector) |
| 23 | + return kernelmatrix(κ.kernel, x, y) ./ |
| 24 | + sqrt.( |
| 25 | + kernelmatrix_diag(κ.kernel, x) .* permutedims(kernelmatrix_diag(κ.kernel, y)) |
| 26 | + ) |
| 27 | +end |
| 28 | + |
| 29 | +function kernelmatrix(κ::NormalizedKernel, x::AbstractVector) |
| 30 | + x_diag = kernelmatrix_diag(κ.kernel, x) |
| 31 | + return kernelmatrix(κ.kernel, x) ./ sqrt.(x_diag .* permutedims(x_diag)) |
| 32 | +end |
| 33 | + |
| 34 | +function kernelmatrix_diag(κ::NormalizedKernel, x::AbstractVector) |
| 35 | + first_x = first(x) |
| 36 | + return Ones{typeof(κ(first_x, first_x))}(length(x)) |
| 37 | +end |
| 38 | + |
| 39 | +function kernelmatrix_diag(κ::NormalizedKernel, x::AbstractVector, y::AbstractVector) |
| 40 | + return kernelmatrix_diag(κ.kernel, x, y) ./ |
| 41 | + sqrt.(kernelmatrix_diag(κ.kernel, x) .* kernelmatrix_diag(κ.kernel, y)) |
| 42 | +end |
| 43 | + |
| 44 | +function kernelmatrix!( |
| 45 | + K::AbstractMatrix, κ::NormalizedKernel, x::AbstractVector, y::AbstractVector |
| 46 | +) |
| 47 | + kernelmatrix!(K, κ.kernel, x, y) |
| 48 | + K ./= |
| 49 | + sqrt.(kernelmatrix_diag(κ.kernel, x) .* permutedims(kernelmatrix_diag(κ.kernel, y))) |
| 50 | + return K |
| 51 | +end |
| 52 | + |
| 53 | +function kernelmatrix!(K::AbstractMatrix, κ::NormalizedKernel, x::AbstractVector) |
| 54 | + kernelmatrix!(K, κ.kernel, x) |
| 55 | + x_diag = kernelmatrix_diag(κ.kernel, x) |
| 56 | + K ./= sqrt.(x_diag .* permutedims(x_diag)) |
| 57 | + return K |
| 58 | +end |
| 59 | + |
| 60 | +function kernelmatrix_diag!( |
| 61 | + K::AbstractVector, κ::NormalizedKernel, x::AbstractVector, y::AbstractVector |
| 62 | +) |
| 63 | + kernelmatrix_diag!(K, κ.kernel, x, y) |
| 64 | + K ./= sqrt.(kernelmatrix_diag(κ.kernel, x) .* kernelmatrix_diag(κ.kernel, y)) |
| 65 | + return K |
| 66 | +end |
| 67 | + |
| 68 | +function kernelmatrix_diag!(K::AbstractVector, κ::NormalizedKernel, x::AbstractVector) |
| 69 | + first_x = first(x) |
| 70 | + return fill!(K, κ(first_x, first_x)) |
| 71 | +end |
| 72 | + |
| 73 | +Base.show(io::IO, κ::NormalizedKernel) = printshifted(io, κ, 0) |
| 74 | + |
| 75 | +function printshifted(io::IO, κ::NormalizedKernel, shift::Int) |
| 76 | + println(io, "Normalized Kernel:") |
| 77 | + for _ in 1:(shift + 1) |
| 78 | + print(io, "\t") |
| 79 | + end |
| 80 | + return printshifted(io, κ.kernel, shift + 1) |
| 81 | +end |
0 commit comments