-
Notifications
You must be signed in to change notification settings - Fork 32
/
maha.jl
23 lines (18 loc) · 653 Bytes
/
maha.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""
MahalanobisKernel(P::AbstractMatrix)
Mahalanobis distance-based kernel given by
```math
κ(x,y) = exp(-r^2), r^2 = maha(x,P,y) = (x-y)'*inv(P)*(x-y)
```
where the matrix P is the metric.
"""
struct MahalanobisKernel{T<:Real, A<:AbstractMatrix{T}} <: SimpleKernel
P::A
function MahalanobisKernel(P::AbstractMatrix{T}) where {T<:Real}
LinearAlgebra.checksquare(P)
new{T,typeof(P)}(P)
end
end
kappa(κ::MahalanobisKernel, d::T) where {T<:Real} = exp(-d)
metric(κ::MahalanobisKernel) = SqMahalanobis(κ.P)
Base.show(io::IO, κ::MahalanobisKernel) = print(io, "Mahalanobis Kernel (size(P) = ", size(κ.P), ")")