Skip to content

Commit da9a191

Browse files
committed
Removed obsolete code and tests
1 parent c544470 commit da9a191

File tree

3 files changed

+33
-47
lines changed

3 files changed

+33
-47
lines changed

src/kernels/fbm.jl

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,27 @@ For `h=1/2`, this is the Wiener Kernel, for `h>1/2`, the increments are
1010
positively correlated and for `h<1/2` the increments are negatively correlated.
1111
"""
1212
struct FBMKernel{T<:Real} <: BaseKernel
13-
h::T
13+
h::Vector{T}
1414
function FBMKernel(; h::T=0.5) where {T<:Real}
1515
@assert h<=1.0 && h>=0.0 "FBMKernel: Given Hurst index h is invalid."
16-
return new{T}(h)
16+
return new{T}([h])
1717
end
1818
end
1919

20-
Base.show(io::IO, κ::FBMKernel) = print(io, "Fractional Brownian Motion Kernel (h = $(k.h))")
20+
Base.show(io::IO, κ::FBMKernel) = print(io, "Fractional Brownian Motion Kernel (h = $(first(k.h)))")
2121

2222
_fbm(modX, modY, modXY, h) = (modX^h + modY^h - modXY^h)/2
2323

2424
function kernelmatrix::FBMKernel, X::AbstractMatrix; obsdim::Int = defaultobs)
2525
@assert obsdim [1,2] "obsdim should be 1 or 2 (see docs of kernelmatrix))"
26-
modX = sum(abs2, X; dims = 3 - obsdim)
26+
modX = sum(abs2, X; dims = feature_dim(obsdim))
2727
modXX = pairwise(SqEuclidean(), X, dims = obsdim)
2828
return _fbm.(vec(modX), reshape(modX, 1, :), modXX, κ.h)
2929
end
3030

3131
function kernelmatrix!(K::AbstractMatrix, κ::FBMKernel, X::AbstractMatrix; obsdim::Int = defaultobs)
3232
@assert obsdim [1,2] "obsdim should be 1 or 2 (see docs of kernelmatrix))"
33-
modX = sum(abs2, X; dims = 3 - obsdim)
33+
modX = sum(abs2, X; dims = feature_dim(obsdim))
3434
modXX = pairwise(SqEuclidean(), X, dims = obsdim)
3535
K .= _fbm.(vec(modX), reshape(modX, 1, :), modXX, κ.h)
3636
return K
@@ -43,9 +43,9 @@ function kernelmatrix(
4343
obsdim::Int = defaultobs,
4444
)
4545
@assert obsdim [1,2] "obsdim should be 1 or 2 (see docs of kernelmatrix))"
46-
modX = sum(abs2, X, dims=3-obsdim)
47-
modY = sum(abs2, Y, dims=3-obsdim)
48-
modXY = pairwise(SqEuclidean(), X, Y,dims=obsdim)
46+
modX = sum(abs2, X, dims = feature_dim(obsdim))
47+
modY = sum(abs2, Y, dims = feature_dim(obsdim))
48+
modXY = pairwise(SqEuclidean(), X, Y,dims = obsdim)
4949
return _fbm.(vec(modX), reshape(modY, 1, :), modXY, κ.h)
5050
end
5151

@@ -57,9 +57,9 @@ function kernelmatrix!(
5757
obsdim::Int = defaultobs,
5858
)
5959
@assert obsdim [1,2] "obsdim should be 1 or 2 (see docs of kernelmatrix))"
60-
modX = sum(abs2, X, dims=3-obsdim)
61-
modY = sum(abs2, Y, dims=3-obsdim)
62-
modXY = pairwise(SqEuclidean(), X, Y,dims=obsdim)
60+
modX = sum(abs2, X, dims = feature_dim(obsdim))
61+
modY = sum(abs2, Y, dims = feature_dim(obsdim))
62+
modXY = pairwise(SqEuclidean(), X, Y,dims = obsdim)
6363
K .= _fbm.(vec(modX), reshape(modY, 1, :), modXY, κ.h)
6464
return K
6565
end
@@ -72,23 +72,15 @@ function _kernel(
7272
obsdim::Int = defaultobs
7373
)
7474
@assert length(x) == length(y) "x and y don't have the same dimension!"
75-
return κ(x,y)
75+
return kappa(κ, x, y)
7676
end
7777

78-
#Syntactic Sugar
79-
function::FBMKernel)(x::AbstractVector{<:Real}, y::AbstractVector{<:Real})
78+
function kappa::FBMKernel, x::AbstractVector{<:Real}, y::AbstractVector{<:Real})
8079
modX = sum(abs2, x)
8180
modY = sum(abs2, y)
8281
modXY = sqeuclidean(x, y)
83-
return (modX^κ.h + modY^κ.h - modXY^κ.h)/2
82+
h = first.h)
83+
return (modX^h + modY^h - modXY^h)/2
8484
end
8585

86-
::FBMKernel)(x::Real, y::Real) = (abs2(x)^κ.h + abs2(y)^κ.h - abs2(x-y)^κ.h)/2
87-
88-
function::FBMKernel)(X::AbstractMatrix{<:Real}, Y::AbstractMatrix{<:Real}; obsdim::Integer=defaultobs)
89-
return kernelmatrix(κ, X, Y, obsdim=obsdim)
90-
end
91-
92-
function::FBMKernel)(X::AbstractMatrix{<:Real}; obsdim::Integer=defaultobs)
93-
return kernelmatrix(κ, X, obsdim=obsdim)
94-
end
86+
::FBMKernel)(x::Real, y::Real) = (abs2(x)^first.h) + abs2(y)^first.h) - abs2(x-y)^first.h))/2

src/matrix/kernelmatrix.jl

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
"""
2-
```
3-
kernelmatrix!(K::Matrix, κ::Kernel, X::Matrix; obsdim::Integer=2)
4-
kernelmatrix!(K::Matrix, κ::Kernel, X::Matrix, Y::Matrix; obsdim::Integer=2)
5-
```
6-
In-place version of `kernelmatrix` where pre-allocated matrix `K` will be overwritten with the kernel matrix.
2+
kernelmatrix!(K::Matrix, κ::Kernel, X::Matrix; obsdim::Integer = 2)
3+
kernelmatrix!(K::Matrix, κ::Kernel, X::Matrix, Y::Matrix; obsdim::Integer = 2)
4+
5+
In-place version of [`kernelmatrix`](@ref) where pre-allocated matrix `K` will be overwritten with the kernel matrix.
76
"""
87
kernelmatrix!
98

@@ -21,7 +20,7 @@ function kernelmatrix!(
2120
map!(x->kappa(κ,x),K,pairwise(metric(κ),X,dims=obsdim))
2221
end
2322

24-
kernelmatrix!(K::Matrix, κ::TransformedKernel, X::AbstractMatrix; obsdim::Int = defaultobs) =
23+
kernelmatrix!(K::AbstractMatrix, κ::TransformedKernel, X::AbstractMatrix; obsdim::Int = defaultobs) =
2524
kernelmatrix!(K, kernel(κ), apply.transform, X, obsdim = obsdim), obsdim = obsdim)
2625

2726
function kernelmatrix!(
@@ -61,13 +60,12 @@ _kernel(κ::TransformedKernel, x::AbstractVector, y::AbstractVector; obsdim::Int
6160
_kernel(kernel(κ), apply.transform, x), apply.transform, y), obsdim = obsdim)
6261

6362
"""
64-
```
65-
kernelmatrix(κ::Kernel, X::Matrix ; obsdim::Int=2)
66-
kernelmatrix(κ::Kernel, X::Matrix, Y::Matrix; obsdim::Int=2)
67-
```
63+
kernelmatrix(κ::Kernel, X::Matrix; obsdim::Int = 2)
64+
kernelmatrix(κ::Kernel, X::Matrix, Y::Matrix; obsdim::Int = 2)
65+
6866
Calculate the kernel matrix of `X` (and `Y`) with respect to kernel `κ`.
69-
`obsdim=1` means the matrix `X` (and `Y`) has size #samples x #dimension
70-
`obsdim=2` means the matrix `X` (and `Y`) has size #dimension x #samples
67+
`obsdim = 1` means the matrix `X` (and `Y`) has size #samples x #dimension
68+
`obsdim = 2` means the matrix `X` (and `Y`) has size #dimension x #samples
7169
"""
7270
kernelmatrix
7371

@@ -109,12 +107,11 @@ kernelmatrix(κ::TransformedKernel, X::AbstractMatrix, Y::AbstractMatrix; obsdim
109107
kernelmatrix(kernel(κ), apply.transform, X, obsdim = obsdim), apply.transform, Y, obsdim = obsdim), obsdim = obsdim)
110108

111109
"""
112-
```
113-
kerneldiagmatrix(κ::Kernel, X::Matrix; obsdim::Int=2)
114-
```
110+
kerneldiagmatrix(κ::Kernel, X::Matrix; obsdim::Int = 2)
111+
115112
Calculate the diagonal matrix of `X` with respect to kernel `κ`
116-
`obsdim=1` means the matrix `X` has size #samples x #dimension
117-
`obsdim=2` means the matrix `X` has size #dimension x #samples
113+
`obsdim = 1` means the matrix `X` has size #samples x #dimension
114+
`obsdim = 2` means the matrix `X` has size #dimension x #samples
118115
"""
119116
function kerneldiagmatrix(
120117
κ::Kernel,
@@ -130,10 +127,9 @@ function kerneldiagmatrix(
130127
end
131128

132129
"""
133-
```
134-
kerneldiagmatrix!(K::AbstractVector,κ::Kernel, X::Matrix; obsdim::Int=2)
135-
```
136-
In place version of `kerneldiagmatrix`
130+
kerneldiagmatrix!(K::AbstractVector,κ::Kernel, X::Matrix; obsdim::Int = 2)
131+
132+
In place version of [`kerneldiagmatrix`](@ref)
137133
"""
138134
function kerneldiagmatrix!(
139135
K::AbstractVector,

test/kernels/custom.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ KernelFunctions.kappa(::MyKernel, d2::Real) = exp(-d2)
55
KernelFunctions.metric(::MyKernel) = SqEuclidean()
66

77
# some syntactic sugar
8-
::MyKernel)(d::Real) = kappa(κ, d)
98
::MyKernel)(x::AbstractVector{<:Real}, y::AbstractVector{<:Real}) = kappa(κ, x, y)
109
::MyKernel)(X::AbstractMatrix{<:Real}, Y::AbstractMatrix{<:Real}; obsdim = 2) = kernelmatrix(κ, X, Y; obsdim = obsdim)
1110
::MyKernel)(X::AbstractMatrix{<:Real}; obsdim = 2) = kernelmatrix(κ, X; obsdim = obsdim)
@@ -17,7 +16,6 @@ KernelFunctions.metric(::MyKernel) = SqEuclidean()
1716
@test kernelmatrix(MyKernel(), [1 2; 3 4], [5 6; 7 8]) == kernelmatrix(SqExponentialKernel(), [1 2; 3 4], [5 6; 7 8])
1817
@test kernelmatrix(MyKernel(), [1 2; 3 4]) == kernelmatrix(SqExponentialKernel(), [1 2; 3 4])
1918

20-
@test MyKernel()(3) == SqExponentialKernel()(3)
2119
@test MyKernel()([1, 2], [3, 4]) == SqExponentialKernel()([1, 2], [3, 4])
2220
@test MyKernel()([1 2; 3 4], [5 6; 7 8]) == SqExponentialKernel()([1 2; 3 4], [5 6; 7 8])
2321
@test MyKernel()([1 2; 3 4]) == SqExponentialKernel()([1 2; 3 4])

0 commit comments

Comments
 (0)