Skip to content

Commit db042d6

Browse files
committed
Add tests for MahalanobisKernel
1 parent 4109fe8 commit db042d6

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/kernels/maha.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
MahalanobisKernel(P::AbstractMatrix, kappa::Function)
2+
MahalanobisKernel(P::AbstractMatrix)
33
44
Mahalanobis distance-based kernel given by
55
```math
@@ -8,11 +8,11 @@
88
where the matrix P is the metric.
99
1010
"""
11-
struct MahalanobisKernel{T<:Real,A<:AbstractMatrix{T}} <: BaseKernel
11+
struct MahalanobisKernel{T<:Real, A<:AbstractMatrix{T}} <: BaseKernel
1212
P::A
1313
function MahalanobisKernel(P::AbstractMatrix{T}) where {T<:Real}
1414
LinearAlgebra.checksquare(P)
15-
new{T}(P)
15+
new{T,AbstractMatrix{T}}(P)
1616
end
1717
end
1818

test/test_kernels.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ x = rand()*2; v1 = rand(3); v2 = rand(3); id = IdentityTransform()
104104
@test kappa(PolynomialKernel(d=1.0,c=c),x) kappa(LinearKernel(c=c),x)
105105
end
106106
end
107+
@testset "Mahalanobis" begin
108+
P = rand(3,3)
109+
k = MahalanobisKernel(P)
110+
@test kappa(k,x) == exp(-x)
111+
@test k(v1,v2) exp(-sqmahalanobis(v1,v2, k.P))
112+
@test kappa(ExponentialKernel(),x) == kappa(k,x)
113+
end
107114
@testset "RationalQuadratic" begin
108115
@testset "RationalQuadraticKernel" begin
109116
α = 2.0

0 commit comments

Comments
 (0)