-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Change function names to be more clear about what they do * Fixed tests * Patch bump
- Loading branch information
Showing
13 changed files
with
99 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,62 @@ | ||
abstract type AbstractKernelEmbedding end | ||
|
||
measure(ke::AbstractKernelEmbedding) = ke.measure | ||
|
||
struct KernelEmbedding{Tk<:Kernel,Tm,Tσ<:Real,Tl} <: AbstractKernelEmbedding | ||
kernel::Tk # Kernel function | ||
σ::Tσ # Kernel variance | ||
l::Tl # Kernel lengthscale | ||
measure::Tm # Measure | ||
end | ||
|
||
function KernelEmbedding(bquad::AbstractBQ, prior) | ||
return KernelEmbedding(bquad.kernel, bquad.σ, bquad.l, prior) | ||
end | ||
|
||
scale(ke::KernelEmbedding) = ke.σ | ||
|
||
@doc raw""" | ||
kernel_mean(ke::KernelEmbedding{Kernel,Measure}, samples::AbstractVector) | ||
Compute the kernel mean of the kernel embedding `ke` for each one of | ||
the `samples` $$x_i$$: | ||
```math | ||
z_i = \int k(x, x_i)d\mu(x) | ||
``` | ||
""" | ||
kernel_mean | ||
|
||
function kernel_mean(ke::KernelEmbedding{<:SqExponentialKernel,<:AbstractMvNormal}, samples::AbstractVector) | ||
z = samples .- Ref(mean(measure(ke))) | ||
B = Λ(ke.l) | ||
return scale(ke) / sqrt(det(inv(B) * cov(measure(ke)) + I)) * | ||
exp.(- PDMats.invquad.(Ref(PDMat(B + cov(measure(ke)))), z) / 2) | ||
end | ||
|
||
@doc raw""" | ||
kernel_variance(ke::KernelEmbedding{Kernel,Measure}) | ||
Compute the kernel variance of the given kernel embedding: | ||
```math | ||
C = \int\int k(x,x')d\mu(x)d\mu(x') | ||
``` | ||
""" | ||
kernel_variance | ||
|
||
|
||
function kernel_variance(ke::KernelEmbedding{<:SqExponentialKernel,<:AbstractMvNormal}) | ||
B = Λ(ke.l) | ||
return scale(ke) / sqrt(det(2 * inv(B) * cov(measure(ke)) + I)) | ||
end | ||
|
||
|
||
# Turn the lengthscale into a Diagonal matrix of noise | ||
Λ(l::Real) = abs2(l) * I | ||
Λ(l::AbstractVector) = Diagonal(abs2.(l)) | ||
Λ(l::LowerTriangular) = Cholesky(l, :L, 1) | ||
Λ(l::AbstractMatrix) = l * l' | ||
|
||
# Turns a transform into a lengthscale | ||
transform_param(t::ScaleTransform) = inv(first(t.s)) | ||
transform_param(t::ARDTransform) = inv.(t.v) | ||
transform_param(t::LinearTransform) = inv(t.A) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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 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,22 @@ | ||
@testset "kernelembeddings" begin | ||
rng = MersenneTwister(42) | ||
N = 3 | ||
measure = MvNormal(ones(3), ones(3)) | ||
k = SqExponentialKernel() | ||
l = 2.0 | ||
σ = 0.5 | ||
ke = KernelEmbedding(k, σ, l, measure) | ||
kernel = σ * with_lengthscale(k, l) | ||
@test BQ.scale(ke) == σ | ||
|
||
sample = [rand(rng, 3)] | ||
@test kernel_mean(ke, sample) ≈ [mean(kernel.(sample, eachcol(rand(rng, measure, 10000))))] atol=1e-2 | ||
@test kernel_variance(ke) ≈ mean(kernel.(eachcol(rand(rng, measure, 10000)), eachcol(rand(rng, measure, 10000)))) atol=1e-2 | ||
|
||
s = 2.0 | ||
l = [1.0, 2.0] | ||
L = LowerTriangular(rand(2, 2)) | ||
@test BQ.Λ(s) ≈ s^2 * I | ||
@test BQ.Λ(l) ≈ Diagonal(abs2.(l)) | ||
@test Matrix(BQ.Λ(L)) ≈ L * L' | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
326570b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
326570b
There was a problem hiding this comment.
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/50102
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: