-
Notifications
You must be signed in to change notification settings - Fork 36
Add Fractional Brownian motion kernel #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ea40951
Add Fractional Brownian motion kernel
sharanry efbd5e9
Add new line at the end
sharanry 0eba776
Add kernalmatrix function(s)
sharanry 63c715c
Cover more cases
sharanry 89207d0
Fix bug
sharanry 5728189
Update src/kernels/fbm.jl
sharanry 275995d
Add tests
sharanry 1b78551
Add scalar case
sharanry 9da3b1b
Update src/kernels/fbm.jl
sharanry f6cd6da
Update src/kernels/fbm.jl
sharanry 3c87222
Add more tests
sharanry 2ef91fe
Update src/kernels/fbm.jl
sharanry 6958af0
Update src/kernels/fbm.jl
sharanry dd0cf19
Update src/kernels/fbm.jl
sharanry 746bde7
Apply suggestions from code review
sharanry f10c68d
Merge branch 'master' into fbm
sharanry 4e3a409
Add kernelmatrix! for Fractional Brownian motion kernel
sharanry 3f017c0
Make FBM functions actually mutable
sharanry e4b3bfe
Fix kernelmatrix! for FBMKernel
sharanry 9901e22
Add _kernel for FBMKernel to make kerneldiagmatrix work
sharanry bd726e9
Merge branch 'master' into fbm
sharanry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
""" | ||
FBMKernel(; h::Real=0.5) | ||
Fractional Brownian motion kernel with Hurst index h from (0,1) given by | ||
``` | ||
κ(x,y) = ( |x|²ʰ + |y|²ʰ - |x-y|²ʰ ) / 2 | ||
``` | ||
|
||
For h=1/2, this is the Wiener Kernel, for h>1/2, the increments are | ||
positively correlated and for h<1/2 the increments are negatively correlated. | ||
% | ||
sharanry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
struct FBMKernel{T<:Real} <: BaseKernel | ||
h::T | ||
function FBMKernel(;h::T=0.5) where {T<:Real} | ||
@assert h<=1.0 && h>=0.0 "FBMKernel: Given Hurst index h is invalid." | ||
new{T}(h) | ||
sharanry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
end | ||
|
||
kappa(κ::FBMKernel, d::Real) = error("Not Implemented: Please use `kernelmatrix` or `kerneldiagmatrix` instead.") | ||
sharanry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
_fbm(modX, modY, modXY, h) = (modX^h + modY^h - modXY^h)/2 | ||
|
||
function kernelmatrix(κ::FBMKernel, X::AbstractMatrix; obsdim::Int = defaultobs) | ||
@assert obsdim ∈ [1,2] "obsdim should be 1 or 2 (see docs of kernelmatrix))" | ||
K = map(modX->_fbm(modX, modX, 0, κ.h), pairwise(SqEuclidean(),X,dims=obsdim)) | ||
sharanry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
function kernelmatrix( | ||
κ::FBMKernel, | ||
X::AbstractMatrix, | ||
Y::AbstractMatrix; | ||
obsdim::Int = defaultobs, | ||
) | ||
@assert obsdim ∈ [1,2] "obsdim should be 1 or 2 (see docs of kernelmatrix))" | ||
|
||
modX = pairwise(SqEuclidean(),X,dims=obsdim) | ||
modY = pairwise(SqEuclidean(),Y,dims=obsdim) | ||
modXY = pairwise(SqEuclidean(),X-Y,dims=obsdim) | ||
K = map((x)->_fbm(x[1], x[2], x[3], κ.h), zip(modX, modY, modXY)) | ||
sharanry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
#Syntactic Sugar | ||
function (κ::FBMKernel)(x::AbstractVector{<:Real}, y::AbstractVector{<:Real}) | ||
modX = evaluate(SqEuclidean(), x, zero(x)) | ||
modY = evaluate(SqEuclidean(), y, zero(y)) | ||
modXY = evaluate(SqEuclidean(), x-y, zero(x-y)) | ||
sharanry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(modX^κ.h + modY^κ.h - modXY^κ.h)/2 | ||
sharanry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
(κ::FBMKernel)(X::AbstractMatrix{T}, Y::AbstractMatrix{T}; obsdim::Integer=defaultobs) where {T} = kernelmatrix(κ, X, Y, obsdim=obsdim) | ||
sharanry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(κ::FBMKernel)(X::AbstractMatrix{T}; obsdim::Integer=defaultobs) where {T} = kernelmatrix(κ, X, obsdim=obsdim) | ||
sharanry marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.