@@ -10,28 +10,30 @@ For `h=1/2`, this is the Wiener Kernel, for `h>1/2`, the increments are
1010positively correlated and for `h<1/2` the increments are negatively correlated.
1111"""
1212struct FBMKernel{T<: Real } <: BaseKernel
13- h:: T
13+ h:: Vector{T}
1414 function FBMKernel (; h:: T = 0.5 ) where {T<: Real }
15- @assert h <= 1 .0 && h >= 0 .0 " FBMKernel: Given Hurst index h is invalid."
16- return new {T} (h )
15+ @assert 0 .0 <= h <= 1 .0 " FBMKernel: Given Hurst index h is invalid."
16+ return new {T} ([h] )
1717 end
1818end
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)) )" )
21+
22+ const sqroundoff = 1e-15
2123
2224_fbm (modX, modY, modXY, h) = (modX^ h + modY^ h - modXY^ h)/ 2
2325
2426function kernelmatrix (κ:: FBMKernel , X:: AbstractMatrix ; obsdim:: Int = defaultobs)
2527 @assert obsdim ∈ [1 ,2 ] " obsdim should be 1 or 2 (see docs of kernelmatrix))"
26- modX = sum (abs2, X; dims = 3 - obsdim)
27- modXX = pairwise (SqEuclidean (), X, dims = obsdim)
28+ modX = sum (abs2, X; dims = feature_dim ( obsdim) )
29+ modXX = pairwise (SqEuclidean (sqroundoff ), X, dims = obsdim)
2830 return _fbm .(vec (modX), reshape (modX, 1 , :), modXX, κ. h)
2931end
3032
3133function kernelmatrix! (K:: AbstractMatrix , κ:: FBMKernel , X:: AbstractMatrix ; obsdim:: Int = defaultobs)
3234 @assert obsdim ∈ [1 ,2 ] " obsdim should be 1 or 2 (see docs of kernelmatrix))"
33- modX = sum (abs2, X; dims = 3 - obsdim)
34- modXX = pairwise (SqEuclidean (), X, dims = obsdim)
35+ modX = sum (abs2, X; dims = feature_dim ( obsdim) )
36+ modXX = pairwise (SqEuclidean (sqroundoff ), X, dims = obsdim)
3537 K .= _fbm .(vec (modX), reshape (modX, 1 , :), modXX, κ. h)
3638 return K
3739end
@@ -43,9 +45,9 @@ function kernelmatrix(
4345 obsdim:: Int = defaultobs,
4446)
4547 @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)
48+ modX = sum (abs2, X, dims = feature_dim ( obsdim) )
49+ modY = sum (abs2, Y, dims = feature_dim ( obsdim) )
50+ modXY = pairwise (SqEuclidean (sqroundoff ), X, Y,dims = obsdim)
4951 return _fbm .(vec (modX), reshape (modY, 1 , :), modXY, κ. h)
5052end
5153
@@ -57,9 +59,9 @@ function kernelmatrix!(
5759 obsdim:: Int = defaultobs,
5860)
5961 @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)
62+ modX = sum (abs2, X, dims = feature_dim ( obsdim) )
63+ modY = sum (abs2, Y, dims = feature_dim ( obsdim) )
64+ modXY = pairwise (SqEuclidean (sqroundoff ), X, Y,dims = obsdim)
6365 K .= _fbm .(vec (modX), reshape (modY, 1 , :), modXY, κ. h)
6466 return K
6567end
@@ -72,23 +74,15 @@ function _kernel(
7274 obsdim:: Int = defaultobs
7375 )
7476 @assert length (x) == length (y) " x and y don't have the same dimension!"
75- return κ (x, y)
77+ return kappa (κ, x, y)
7678end
7779
78- # Syntactic Sugar
79- function (κ:: FBMKernel )(x:: AbstractVector{<:Real} , y:: AbstractVector{<:Real} )
80+ function kappa (κ:: FBMKernel , x:: AbstractVector{<:Real} , y:: AbstractVector{<:Real} )
8081 modX = sum (abs2, x)
8182 modY = sum (abs2, y)
82- modXY = sqeuclidean (x, y)
83- return (modX^ κ. h + modY^ κ. h - modXY^ κ. h)/ 2
83+ modXY = evaluate (SqEuclidean (sqroundoff), x, y)
84+ h = first (κ. h)
85+ return (modX^ h + modY^ h - modXY^ h)/ 2
8486end
8587
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
88+ (κ:: FBMKernel )(x:: Real , y:: Real ) = (abs2 (x)^ first (κ. h) + abs2 (y)^ first (κ. h) - abs2 (x- y)^ first (κ. h))/ 2
0 commit comments