Skip to content

Commit 0bfbcd3

Browse files
authored
Merge pull request #66 from theogf/docs_and_formatting
Corrected docs and improved formatting
2 parents 22ca93b + 6a8f17c commit 0bfbcd3

13 files changed

+128
-85
lines changed

src/kernels/constant.jl

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
ZeroKernel()
2+
ZeroKernel()
33
44
Create a kernel that always returning zero
55
```
@@ -13,28 +13,40 @@ kappa(κ::ZeroKernel, d::T) where {T<:Real} = zero(T)
1313

1414
metric(::ZeroKernel) = Delta()
1515

16+
Base.show(io::IO, ::ZeroKernel) = print(io, "Zero Kernel")
17+
18+
1619
"""
17-
`WhiteKernel()`
20+
WhiteKernel()
1821
1922
```
2023
κ(x,y) = δ(x,y)
2124
```
22-
Kernel function working as an equivalent to add white noise.
25+
Kernel function working as an equivalent to add white noise. Can also be called via `EyeKernel()`
2326
"""
2427
struct WhiteKernel <: BaseKernel end
2528

29+
"""
30+
EyeKernel()
31+
32+
See [WhiteKernel](@ref)
33+
"""
2634
const EyeKernel = WhiteKernel
2735

28-
kappa::WhiteKernel,δₓₓ::Real) = δₓₓ
36+
kappa::WhiteKernel, δₓₓ::Real) = δₓₓ
2937

3038
metric(::WhiteKernel) = Delta()
3139

40+
Base.show(io::IO, ::WhiteKernel) = print(io, "White Kernel")
41+
42+
3243
"""
33-
`ConstantKernel(c=1.0)`
44+
ConstantKernel(; c=1.0)
45+
46+
Kernel function always returning a constant value `c`
3447
```
3548
κ(x,y) = c
3649
```
37-
Kernel function always returning a constant value `c`
3850
"""
3951
struct ConstantKernel{Tc<:Real} <: BaseKernel
4052
c::Vector{Tc}
@@ -46,3 +58,5 @@ end
4658
kappa::ConstantKernel,x::Real) = first.c)*one(x)
4759

4860
metric(::ConstantKernel) = Delta()
61+
62+
Base.show(io::IO, κ::ConstantKernel) = print(io, "Constant Kernel (c = $(first.c)))")

src/kernels/cosine.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""
2-
CosineKernel
2+
CosineKernel()
33
44
The cosine kernel is a stationary kernel for a sinusoidal given by
55
```
66
κ(x,y) = cos( π * (x-y) )
77
```
8-
98
"""
109
struct CosineKernel <: BaseKernel end
1110

1211
kappa::CosineKernel, d::Real) = cospi(d)
13-
1412
metric(::CosineKernel) = Euclidean()
13+
14+
Base.show(io::IO, ::CosineKernel) = print(io, "Cosine Kernel")

src/kernels/exponential.jl

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
"""
2-
`SqExponentialKernel()`
2+
SqExponentialKernel()
33
4-
The squared exponential kernel is an isotropic Mercer kernel given by the formula:
4+
The squared exponential kernel is a Mercer kernel given by the formula:
55
```
66
κ(x,y) = exp(-‖x-y‖²)
77
```
8+
Can also be called via `SEKernel`, `GaussianKernel` or `SEKernel`.
89
See also [`ExponentialKernel`](@ref) for a
910
related form of the kernel or [`GammaExponentialKernel`](@ref) for a generalization.
1011
"""
1112
struct SqExponentialKernel <: BaseKernel end
1213

1314
kappa::SqExponentialKernel, d²::Real) = exp(-d²)
1415
iskroncompatible(::SqExponentialKernel) = true
15-
1616
metric(::SqExponentialKernel) = SqEuclidean()
1717

1818
Base.show(io::IO,::SqExponentialKernel) = print(io,"Squared Exponential Kernel")
@@ -23,10 +23,11 @@ const GaussianKernel = SqExponentialKernel
2323
const SEKernel = SqExponentialKernel
2424

2525
"""
26-
`ExponentialKernel([ρ=1.0])`
27-
The exponential kernel is an isotropic Mercer kernel given by the formula:
26+
ExponentialKernel()
27+
28+
The exponential kernel is a Mercer kernel given by the formula:
2829
```
29-
κ(x,y) = exp(-ρ‖x-y‖)
30+
κ(x,y) = exp(-‖x-y‖)
3031
```
3132
"""
3233
struct ExponentialKernel <: BaseKernel end
@@ -35,21 +36,24 @@ kappa(κ::ExponentialKernel, d::Real) = exp(-d)
3536
iskroncompatible(::ExponentialKernel) = true
3637
metric(::ExponentialKernel) = Euclidean()
3738

38-
Base.show(io::IO,::ExponentialKernel) = print(io,"Exponential Kernel")
39+
Base.show(io::IO, ::ExponentialKernel) = print(io, "Exponential Kernel")
3940

4041
## Alias ##
4142
const LaplacianKernel = ExponentialKernel
4243

4344
"""
44-
`GammaExponentialKernel([ρ=1.0, [γ=2.0]])`
45+
GammaExponentialKernel(; γ = 2.0)
46+
4547
The γ-exponential kernel is an isotropic Mercer kernel given by the formula:
4648
```
47-
κ(x,y) = exp(-ρ^(2γ)‖x-y‖^(2γ))
49+
κ(x,y) = exp(-‖x-y‖^(2γ))
4850
```
51+
Where `γ > 0`, (the keyword `γ` can be replaced by `gamma`)
52+
For `γ = 1`, see `SqExponentialKernel` and `γ = 0.5`, see `ExponentialKernel`
4953
"""
5054
struct GammaExponentialKernel{Tγ<:Real} <: BaseKernel
5155
γ::Vector{Tγ}
52-
function GammaExponentialKernel(;gamma::T=2.0, γ::T=gamma) where {T<:Real}
56+
function GammaExponentialKernel(; gamma::T=2.0, γ::T=gamma) where {T<:Real}
5357
@check_args(GammaExponentialKernel, γ, γ >= zero(T), "γ > 0")
5458
return new{T}([γ])
5559
end
@@ -58,3 +62,5 @@ end
5862
kappa::GammaExponentialKernel, d²::Real) = exp(-^first.γ))
5963
iskroncompatible(::GammaExponentialKernel) = true
6064
metric(::GammaExponentialKernel) = SqEuclidean()
65+
66+
Base.show(io::IO, κ::GammaExponentialKernel) = print(io, "Gamma Exponential Kernel (γ = $(first.γ)))")

src/kernels/exponentiated.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
"""
2-
`ExponentiatedKernel([ρ=1])`
2+
ExponentiatedKernel()
3+
34
The exponentiated kernel is a Mercer kernel given by:
45
```
5-
κ(x,y) = exp(ρ²xᵀy)
6+
κ(x,y) = exp(xᵀy)
67
```
78
"""
89
struct ExponentiatedKernel <: BaseKernel end
910

1011
kappa::ExponentiatedKernel, xᵀy::Real) = exp(xᵀy)
11-
1212
metric(::ExponentiatedKernel) = DotProduct()
13-
1413
iskroncompatible(::ExponentiatedKernel) = true
14+
15+
Base.show(io::IO, ::ExponentiatedKernel) = print(io, "Exponentiated Kernel")

src/kernels/fbm.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
"""
22
FBMKernel(; h::Real=0.5)
33
4-
Fractional Brownian motion kernel with Hurst index h from (0,1) given by
4+
Fractional Brownian motion kernel with Hurst index `h` from (0,1) given by
55
```
66
κ(x,y) = ( |x|²ʰ + |y|²ʰ - |x-y|²ʰ ) / 2
77
```
88
9-
For h=1/2, this is the Wiener Kernel, for h>1/2, the increments are
10-
positively correlated and for h<1/2 the increments are negatively correlated.
9+
For `h=1/2`, this is the Wiener Kernel, for `h>1/2`, the increments are
10+
positively correlated and for `h<1/2` the increments are negatively correlated.
1111
"""
1212
struct FBMKernel{T<:Real} <: BaseKernel
1313
h::T
14-
function FBMKernel(;h::T=0.5) where {T<:Real}
14+
function FBMKernel(; h::T=0.5) where {T<:Real}
1515
@assert h<=1.0 && h>=0.0 "FBMKernel: Given Hurst index h is invalid."
1616
return new{T}(h)
1717
end
1818
end
1919

20+
Base.show(io::IO, κ::FBMKernel) = print(io, "Fractional Brownian Motion Kernel (h = $(k.h))")
21+
2022
_fbm(modX, modY, modXY, h) = (modX^h + modY^h - modXY^h)/2
2123

2224
function kernelmatrix::FBMKernel, X::AbstractMatrix; obsdim::Int = defaultobs)
@@ -40,7 +42,7 @@ function kernelmatrix(
4042
Y::AbstractMatrix;
4143
obsdim::Int = defaultobs,
4244
)
43-
@assert obsdim [1,2] "obsdim should be 1 or 2 (see docs of kernelmatrix))"
45+
@assert obsdim [1,2] "obsdim should be 1 or 2 (see docs of kernelmatrix))"
4446
modX = sum(abs2, X, dims=3-obsdim)
4547
modY = sum(abs2, Y, dims=3-obsdim)
4648
modXY = pairwise(SqEuclidean(), X, Y,dims=obsdim)
@@ -54,7 +56,7 @@ function kernelmatrix!(
5456
Y::AbstractMatrix;
5557
obsdim::Int = defaultobs,
5658
)
57-
@assert obsdim [1,2] "obsdim should be 1 or 2 (see docs of kernelmatrix))"
59+
@assert obsdim [1,2] "obsdim should be 1 or 2 (see docs of kernelmatrix))"
5860
modX = sum(abs2, X, dims=3-obsdim)
5961
modY = sum(abs2, Y, dims=3-obsdim)
6062
modXY = pairwise(SqEuclidean(), X, Y,dims=obsdim)

src/kernels/kernelproduct.jl

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
"""
2-
`KernelProduct(kernels::Array{Kernel})`
3-
Create a multiplication of kernels.
4-
One can also use the operator `*`
2+
KernelProduct(kernels::Array{Kernel})
3+
4+
Create a product of kernels.
5+
One can also use the operator `*` :
56
```
6-
k1 = SqExponentialKernel()
7-
k2 = LinearKernel()
8-
k = KernelProduct([k1,k2])
9-
kernelmatrix(k,X) == kernelmatrix(k1,X).*kernelmatrix(k2,X)
10-
kernelmatrix(k,X) == kernelmatrix(k1*k2,X)
7+
k1 = SqExponentialKernel()
8+
k2 = LinearKernel()
9+
k = KernelProduct([k1, k2]) == k1 * k2
10+
kernelmatrix(k, X) == kernelmatrix(k1, X) .* kernelmatrix(k2, X)
11+
kernelmatrix(k, X) == kernelmatrix(k1 * k2, X)
1112
```
1213
"""
1314
struct KernelProduct <: Kernel
@@ -47,14 +48,14 @@ function kerneldiagmatrix(
4748
reduce(hadamard,kerneldiagmatrix.kernels[i],X,obsdim=obsdim) for i in 1:length(κ))
4849
end
4950

50-
function Base.show(io::IO::KernelProduct)
51-
printshifted(io,κ,0)
51+
function Base.show(io::IO, κ::KernelProduct)
52+
printshifted(io, κ, 0)
5253
end
5354

54-
function printshifted(io::IO::KernelProduct, shift::Int)
55-
print(io,"Product of $(length(κ)) kernels:")
55+
function printshifted(io::IO, κ::KernelProduct, shift::Int)
56+
print(io, "Product of $(length(κ)) kernels:")
5657
for i in 1:length(κ)
57-
print(io,"\n"*("\t"^(shift+1))*"- ")
58-
printshifted(io,κ.kernels[i],shift+2)
58+
print(io, "\n" * ("\t" ^ (shift + 1))* "- ")
59+
printshifted(io, κ.kernels[i], shift + 2)
5960
end
6061
end

src/kernels/kernelsum.jl

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
"""
2-
`KernelSum(kernels::Array{Kernel};weights::Array{Real}=ones(length(kernels)))`
3-
Create a positive weighted sum of kernels.
2+
KernelSum(kernels::Array{Kernel}; weights::Array{Real}=ones(length(kernels)))
3+
4+
Create a positive weighted sum of kernels. All weights should be positive.
45
One can also use the operator `+`
56
```
6-
k1 = SqExponentialKernel()
7-
k2 = LinearKernel()
8-
k = KernelSum([k1,k2])
9-
kernelmatrix(k,X) == kernelmatrix(k1,X).+kernelmatrix(k2,X)
10-
kernelmatrix(k,X) == kernelmatrix(k1+k2,X)
11-
kweighted = 0.5*k1 + 2.0*k2
7+
k1 = SqExponentialKernel()
8+
k2 = LinearKernel()
9+
k = KernelSum([k1, k2]) == k1 + k2
10+
kernelmatrix(k, X) == kernelmatrix(k1, X) .+ kernelmatrix(k2, X)
11+
kernelmatrix(k, X) == kernelmatrix(k1 + k2, X)
12+
kweighted = 0.5* k1 + 2.0*k2 == KernelSum([k1, k2], weights = [0.5, 2.0])
1213
```
1314
"""
1415
struct KernelSum <: Kernel
@@ -68,14 +69,14 @@ function kerneldiagmatrix(
6869
sum.weights[i] * kerneldiagmatrix.kernels[i], X, obsdim = obsdim) for i in 1:length(κ))
6970
end
7071

71-
function Base.show(io::IO::KernelSum)
72-
printshifted(io,κ,0)
72+
function Base.show(io::IO, κ::KernelSum)
73+
printshifted(io, κ, 0)
7374
end
7475

7576
function printshifted(io::IO::KernelSum, shift::Int)
7677
print(io,"Sum of $(length(κ)) kernels:")
7778
for i in 1:length(κ)
78-
print(io,"\n"*("\t"^(shift+1))*"- (w=$(κ.weights[i])) ")
79-
printshifted(io,κ.kernels[i],shift+2)
79+
print(io, "\n" * ("\t" ^ (shift + 1)) * "- (w = $(κ.weights[i])) ")
80+
printshifted(io, κ.kernels[i], shift + 2)
8081
end
8182
end

src/kernels/maha.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
MahalanobisKernel(P::AbstractMatrix)
33
4-
Mahalanobis distance-based kernel given by
4+
Mahalanobis distance-based kernel given by
55
```math
66
κ(x,y) = exp(-r^2), r^2 = maha(x,P,y) = (x-y)'*inv(P)*(x-y)
77
```
@@ -16,6 +16,7 @@ struct MahalanobisKernel{T<:Real, A<:AbstractMatrix{T}} <: BaseKernel
1616
end
1717
end
1818

19-
kappa::MahalanobisKernel, d::T) where {T<:Real} = exp(-d)
20-
19+
kappa::MahalanobisKernel, d::T) where {T<:Real} = exp(-d)
2120
metric::MahalanobisKernel) = SqMahalanobis.P)
21+
22+
Base.show(io::IO, κ::MahalanobisKernel) = print(io, "Mahalanobis Kernel (size(P) = $(size.P))")

src/kernels/matern.jl

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
2-
`MaternKernel([ρ=1.0,[ν=1.0]])`
3-
The matern kernel is an isotropic Mercer kernel given by the formula:
2+
MaternKernel(; ν = 1.0)
3+
4+
The matern kernel is a Mercer kernel given by the formula:
45
```
56
κ(x,y) = 2^{1-ν}/Γ(ν)*(√(2ν)‖x-y‖)^ν K_ν(√(2ν)‖x-y‖)
67
```
@@ -26,28 +27,34 @@ end
2627

2728
metric(::MaternKernel) = Euclidean()
2829

30+
Base.show(io::IO, κ::MaternKernel) = print(io, "Matern Kernel (ν = $(first.ν)))")
31+
2932
"""
30-
`Matern32Kernel([ρ=1.0])`
31-
The matern 3/2 kernel is an isotropic Mercer kernel given by the formula:
33+
Matern32Kernel()
34+
35+
The matern 3/2 kernel is a Mercer kernel given by the formula:
3236
```
33-
κ(x,y) = (1+√(3)ρ‖x-y‖)exp(-√(3)ρ‖x-y‖)
37+
κ(x,y) = (1+√(3)‖x-y‖)exp(-√(3)‖x-y‖)
3438
```
3539
"""
3640
struct Matern32Kernel <: BaseKernel end
3741

3842
kappa::Matern32Kernel, d::Real) = (1 + sqrt(3) * d) * exp(-sqrt(3) * d)
39-
4043
metric(::Matern32Kernel) = Euclidean()
4144

45+
Base.show(io::IO, ::Matern32Kernel) = print(io, "Matern 3/2 Kernel")
46+
4247
"""
43-
`Matern52Kernel([ρ=1.0])`
44-
The matern 5/2 kernel is an isotropic Mercer kernel given by the formula:
48+
Matern52Kernel()
49+
50+
The matern 5/2 kernel is a Mercer kernel given by the formula:
4551
```
46-
κ(x,y) = (1+√(5)ρ‖x-y‖ + 5ρ²‖x-y‖^2/3)exp(-√(5)ρ‖x-y‖)
52+
κ(x,y) = (1+√(5)‖x-y‖ + 5/3‖x-y‖^2)exp(-√(5)‖x-y‖)
4753
```
4854
"""
4955
struct Matern52Kernel <: BaseKernel end
5056

5157
kappa::Matern52Kernel, d::Real) = (1 + sqrt(5) * d + 5 * d^2 / 3) * exp(-sqrt(5) * d)
52-
5358
metric(::Matern52Kernel) = Euclidean()
59+
60+
Base.show(io::IO, ::Matern52Kernel) = print(io, "Matern 5/2 Kernel")

0 commit comments

Comments
 (0)