Skip to content

Commit

Permalink
Add with_lengthscale (alternative/extension of #335) (#336)
Browse files Browse the repository at this point in the history
Co-authored-by: willtebbutt <wct23@cam.ac.uk>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: ST John <st--@users.noreply.github.com>
  • Loading branch information
4 people committed Jul 10, 2021
1 parent 58cb069 commit 2181649
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "KernelFunctions"
uuid = "ec8451be-7e33-11e9-00cf-bbf324bd1392"
version = "0.10.6"
version = "0.10.7"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
6 changes: 6 additions & 0 deletions docs/src/transform.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ SelectTransform
ChainTransform
PeriodicTransform
```

## Convenience functions

```@docs
with_lengthscale
```
5 changes: 5 additions & 0 deletions docs/src/userguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ For example, a squared exponential kernel is created by
k = SqExponentialKernel() ∘ ScaleTransform(2.0)
k = compose(SqExponentialKernel(), ScaleTransform(2.0))
```
Alternatively, you can use the convenience function [`with_lengthscale`](@ref):
```julia
k = with_lengthscale(SqExponentialKernel(), 0.5)
```
[`with_lengthscale`](@ref) also works with vector-valued lengthscales for ARD.
Check the [Input Transforms](@ref input_transforms) page for more details.

!!! tip "How do I set the kernel variance?"
Expand Down
2 changes: 2 additions & 0 deletions src/KernelFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export Transform,
IdentityTransform,
FunctionTransform,
PeriodicTransform
export with_lengthscale

export NystromFact, nystrom

Expand Down Expand Up @@ -75,6 +76,7 @@ include(joinpath("transform", "selecttransform.jl"))
include(joinpath("transform", "chaintransform.jl"))
include(joinpath("transform", "periodic_transform.jl"))
include(joinpath("kernels", "transformedkernel.jl"))
include(joinpath("transform", "with_lengthscale.jl"))

include(joinpath("basekernels", "constant.jl"))
include(joinpath("basekernels", "cosine.jl"))
Expand Down
43 changes: 43 additions & 0 deletions src/transform/with_lengthscale.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
with_lengthscale(kernel::Kernel, lengthscale::Real)
Construct a transformed kernel with `lengthscale`.
# Examples
```jldoctest
julia> kernel = with_lengthscale(SqExponentialKernel(), 2.5);
julia> x = rand(2);
julia> y = rand(2);
julia> kernel(x, y) ≈ (SqExponentialKernel() ∘ ScaleTransform(0.4))(x, y)
true
```
"""
function with_lengthscale(kernel::Kernel, lengthscale::Real)
return kernel ScaleTransform(inv(lengthscale))
end

"""
with_lengthscale(kernel::Kernel, lengthscales::AbstractVector{<:Real})
Construct a transformed "ARD" kernel with different `lengthscales` for each dimension.
# Examples
```jldoctest
julia> kernel = with_lengthscale(SqExponentialKernel(), [0.5, 2.5]);
julia> x = rand(2);
julia> y = rand(2);
julia> kernel(x, y) ≈ (SqExponentialKernel() ∘ ARDTransform([2, 0.4]))(x, y)
true
```
"""
function with_lengthscale(kernel::Kernel, lengthscales::AbstractVector{<:Real})
return kernel ARDTransform(map(inv, lengthscales))
end
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ include("test_utils.jl")
print(" ")
include(joinpath("transform", "periodic_transform.jl"))
print(" ")
include(joinpath("transform", "with_lengthscale.jl"))
print(" ")
end
@info "Ran tests on Transform"
end
Expand Down
17 changes: 17 additions & 0 deletions test/transform/with_lengthscale.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@testset "with_lengthscale" begin
@testset "ScaleTransform" begin
l = exp(rand())
kernel = @inferred(with_lengthscale(SqExponentialKernel(), l))

@test kernel isa TransformedKernel{<:SqExponentialKernel,<:ScaleTransform}
@test kernel.transform.s[1] inv(l)
end

@testset "ARDTransform" begin
l = map(exp, rand(5))
kernel = @inferred(with_lengthscale(SqExponentialKernel(), l))

@test kernel isa TransformedKernel{<:SqExponentialKernel,<:ARDTransform}
@test kernel.transform.v map(inv, l)
end
end

2 comments on commit 2181649

@devmotion
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/40659

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.10.7 -m "<description of version>" 2181649542385288d7f1524c5a6c1a2ff00aece4
git push origin v0.10.7

Please sign in to comment.