Skip to content
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

Add with_period convenience function and a example to PeriodicTransform docstring #401

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/transform/periodic_transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ julia> f = rand(); t = PeriodicTransform(f); x = rand();
julia> t(x) == [sinpi(2 * f * x), cospi(2 * f * x)]
true
```

For 1 dimensional inputs it is possible to create a kernel equivalent
to the `PeriodicKernel` using `PeriodicTransform` and a
`SqExponentialKernel`.

```jldoctest
julia> wiggle_scale = 0.5; period = π/2; x = rand(); y = rand();

julia> k1 = with_lengthscale(PeriodicKernel(; r=[wiggle_scale / 2]), period);

julia> k2 = with_lengthscale(SqExponentialKernel(), wiggle_scale) ∘ PeriodicTransform(1/period)

julia> k1(x,y) ≈ k2(x,y)
true
```
"""
struct PeriodicTransform{Tf<:AbstractVector{<:Real}} <: Transform
f::Tf
Expand Down
21 changes: 21 additions & 0 deletions src/transform/with_period.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
with_period(kernel::Kernel, period::Real)

Construct a transformed kernel with `period`.

# Examples

```jldoctest
julia> kernel = with_period(SqExponentialKernel(), π/2);

julia> x = rand();

julia> y = rand();

julia> kernel(x, y) ≈ (SqExponentialKernel() ∘ PeriodicTransform(2/π))(x, y)
true
```
"""
function with_period(kernel::Kernel, period::Real)
return kernel ∘ PeriodicTransform(inv(period))
end