Skip to content

Commit

Permalink
Implement cov_diag and mean_and_cov_diag for FiniteGPs (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
devmotion authored Mar 29, 2021
1 parent 90896a9 commit b78f6dd
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "AbstractGPs"
uuid = "99985d1d-32ba-4be9-9821-2ec096f28918"
authors = ["JuliaGaussianProcesses Team"]
version = "0.2.20"
version = "0.2.21"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractGPs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module AbstractGPs

using KernelFunctions: ColVecs, RowVecs

export GP, mean, cov, std, cov_diag, mean_and_cov, marginals, rand,
export GP, mean, cov, std, cov_diag, mean_and_cov, mean_and_cov_diag, marginals,
logpdf, elbo, dtc, posterior, approx_posterior, VFE, DTC, sampleplot,
update_approx_posterior, LatentGP, ColVecs, RowVecs

Expand Down
6 changes: 3 additions & 3 deletions src/abstract_gp/abstract_gp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Statistics.cov(::AbstractGP, x::AbstractVector)
Compute only the diagonal elements of `cov(f(x))`.
"""
cov_diag(::AbstractGP, x::AbstractVector)
cov_diag(::AbstractGP, ::AbstractVector)

"""
cov(f::AbstractGP, x::AbstractVector, y::AbstractVector)
Expand All @@ -45,12 +45,12 @@ Statistics.cov(::AbstractGP, x::AbstractVector, y::AbstractVector)
Compute both `mean(f(x))` and `cov(f(x))`. Sometimes more efficient than separately
computation, particularly for posteriors.
"""
mean_and_cov(::AbstractGP, ::AbstractVector)
mean_and_cov(f::AbstractGP, x::AbstractVector) = (mean(f, x), cov(f, x))

"""
mean_and_cov_diag(f::AbstractGP, x::AbstractVector)
Compute both `mean(f(x))` and the diagonal elements of `cov(f(x))`. Sometimes more efficient
than separately computation, particularly for posteriors.
"""
mean_and_cov_diag(f::AbstractGP, x::AbstractVector)
mean_and_cov_diag(f::AbstractGP, x::AbstractVector) = (mean(f, x), cov_diag(f, x))
46 changes: 45 additions & 1 deletion src/abstract_gp/finite_gp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,25 @@ true
"""
Statistics.cov(f::FiniteGP) = cov(f.f, f.x) + f.Σy

"""
cov_diag(f::FiniteGP)
Compute only the diagonal elements of [`cov(f)`](@ref).
# Examples
```jldoctest
julia> fx = GP(Matern52Kernel())(randn(10), 0.1);
julia> cov_diag(fx) == diag(cov(fx))
true
```
"""
function cov_diag(f::FiniteGP)
Σy = f.Σy
return cov_diag(f.f, f.x) + view(Σy, diagind(Σy))
end

"""
mean_and_cov(f::FiniteGP)
Expand All @@ -111,6 +130,28 @@ function mean_and_cov(f::FiniteGP)
return m, C + f.Σy
end

"""
mean_and_cov_diag(f::FiniteGP)
Compute both `mean(f)` and the diagonal elements of `cov(f)`.
Sometimes more efficient than computing them separately, particularly for posteriors.
# Examples
```jldoctest
julia> fx = GP(SqExponentialKernel())(range(-3.0, 3.0; length=10), 0.1);
julia> mean_and_cov_diag(fx) == (mean(fx), cov_diag(fx))
true
```
"""
function mean_and_cov_diag(f::FiniteGP)
m, c = mean_and_cov_diag(f.f, f.x)
Σy = f.Σy
return m, c + view(Σy, diagind(Σy))
end

"""
cov(fx::FiniteGP, gx::FiniteGP)
Expand Down Expand Up @@ -154,7 +195,10 @@ julia> std.(fs) == sqrt.(diag(cov(f(x))))
true
```
"""
marginals(f::FiniteGP) = Normal.(mean(f), sqrt.(cov_diag(f.f, f.x) .+ diag(f.Σy)))
function marginals(f::FiniteGP)
m, c = mean_and_cov_diag(f)
return Normal.(m, sqrt.(c))
end

"""
rand(rng::AbstractRNG, f::FiniteGP, N::Int=1)
Expand Down
6 changes: 0 additions & 6 deletions src/gp/gp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ GP(mean, kernel::Kernel) = GP(CustomMean(mean), kernel)
GP(mean::Real, kernel::Kernel) = GP(ConstMean(mean), kernel)
GP(kernel::Kernel) = GP(ZeroMean(), kernel)



# AbstractGP interface implementation.

Statistics.mean(f::GP, x::AbstractVector) = _map(f.mean, x)
Expand All @@ -73,7 +71,3 @@ Statistics.cov(f::GP, x::AbstractVector) = kernelmatrix(f.kernel, x)
cov_diag(f::GP, x::AbstractVector) = kernelmatrix_diag(f.kernel, x)

Statistics.cov(f::GP, x::AbstractVector, x′::AbstractVector) = kernelmatrix(f.kernel, x, x′)

mean_and_cov(f::GP, x::AbstractVector) = (mean(f, x), cov(f, x))

mean_and_cov_diag(f::GP, x::AbstractVector) = (mean(f, x), cov_diag(f, x))
6 changes: 6 additions & 0 deletions test/abstract_gp/finite_gp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ end
@test FiniteGP(f, Xmat, σ², obsdim=2) == FiniteGP(f, ColVecs(Xmat), σ²)
@test mean(fx) == mean(f, x)
@test cov(fx) == cov(f, x)
@test cov_diag(fx) == diag(cov(fx))
@test cov(fx, fx′) == cov(f, x, x′)
@test mean.(marginals(fx)) == mean(f(x))
@test var.(marginals(fx)) == cov_diag(f, x)
Expand All @@ -29,6 +30,11 @@ end
@test m == mean(fx)
@test C == cov(fx)
end
let
m, c = mean_and_cov_diag(fx)
@test m == mean(fx)
@test c == cov_diag(fx)
end
end
@testset "rand (deterministic)" begin
rng = MersenneTwister(123456)
Expand Down

2 comments on commit b78f6dd

@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/33094

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.2.21 -m "<description of version>" b78f6ddc134742d8c0da3f5c2444114e8cd35d8d
git push origin v0.2.21

Please sign in to comment.