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 SparseFiniteGP type and associated functionality #136

Merged
merged 15 commits into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions src/Stheno.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ module Stheno
# include(joinpath("composite", "gradient.jl"))
# include(joinpath("composite", "integrate.jl"))

# approximate inference
include("approximate_inference.jl")

# Various stuff for convenience.
include(joinpath("util", "model.jl"))
include(joinpath("util", "plotting.jl"))
Expand Down
50 changes: 50 additions & 0 deletions src/approximate_inference.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import Base: rand, length
import Distributions: logpdf, AbstractMvNormal

export mean, std, cov, marginals, rand, logpdf, elbo, dtc
ElOceanografo marked this conversation as resolved.
Show resolved Hide resolved
export SparseFiniteGP

struct SparseFiniteGP{T1<:FiniteGP, T2<:FiniteGP} <: AbstractMvNormal
ElOceanografo marked this conversation as resolved.
Show resolved Hide resolved
fobs::T1
finducing::T2
end

function SparseFiniteGP(f::AbstractGP, x, xu, σ=1e-18, σu=σ)
return SparseFiniteGP(f(x, σ), f(xu, σu))
end
ElOceanografo marked this conversation as resolved.
Show resolved Hide resolved

# The below convenience constructor conflicts with the constructor for a FiniteGP with
# diagonal covariance matrix (in src/abstract_gp.jl, line 34)
# """
# (f::AbstractGP)(x::AbstractVector, xu::AbstractVector, σ=1e-18, σu=σ)
#
# Construct a `SparseFiniteGP` representing the projection of `f` at `x` with inducing points at `xu`.
# """
# (f::AbstractGP)(x::AbstractVector, xu::AbstractVector, σ=1e-18, σu=σ) = SparseFiniteGP(f, x, xu, σ, σu)
#
ElOceanografo marked this conversation as resolved.
Show resolved Hide resolved


length(f::SparseFiniteGP) = length(f.fobs)
mean(f::SparseFiniteGP) = mean(f.fobs)
# in Base, trying to instantiate a large dense matrix through e.g.
# inv(A::SparseArrays.AbstractSparseMatrixCSC) raises an error, since the resulting dense
# matrix will often be enormous and use up all available memory. Doing something similar here.
covariance_error = "The covariance matrix of a sparse GP can often be dense and can cause the computer to run out of memory. If you are sure you have enough memory, you can use `cov(f.fobs)`."
ElOceanografo marked this conversation as resolved.
Show resolved Hide resolved
cov(f::SparseFiniteGP) = error(covariance_error) #cov(f.fobs)
# Not sure how to implement the following...
# cov(fx::FiniteGP, gx::FiniteGP) = cov(fx.f, gx.f, fx.x, gx.x)
marginals(f::SparseFiniteGP) = Normal.(mean(f), sqrt.(cov_diag(f.fobs.f, f.fobs.x) .+ diag(f.fobs.Σy)))
ElOceanografo marked this conversation as resolved.
Show resolved Hide resolved

rand(rng::AbstractRNG, f::SparseFiniteGP, N::Int) = rand(rng, f.fobs, N)
rand(f::SparseFiniteGP, N::Int) = rand(Random.GLOBAL_RNG, f, N)
rand(rng::AbstractRNG, f::SparseFiniteGP) = vec(rand(rng, f, 1))
rand(f::SparseFiniteGP) = vec(rand(f, 1))

function elbo(f::SparseFiniteGP, y::AV{<:Real})
elbo(f.fobs, y, f.finducing)
end
ElOceanografo marked this conversation as resolved.
Show resolved Hide resolved
logpdf(f::SparseFiniteGP, y::AV{<:Real}) = elbo(f, y)
logpdf(f::SparseFiniteGP, Y::AbstractMatrix{<:Real}) = map(y -> logpdf(f, y), eachcol(Y))

PseudoObs(fxu::SparseFiniteGP, y) = PseudoObs(Obs(fxu.fobs, y), fxu.finducing)
←(fxu::SparseFiniteGP, y) = PseudoObs(fxu, y)
53 changes: 53 additions & 0 deletions test/gp/sparse_gp.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Stheno
ElOceanografo marked this conversation as resolved.
Show resolved Hide resolved
using Random
using Test
ElOceanografo marked this conversation as resolved.
Show resolved Hide resolved

x = 0:0.1:10
xu = 0:10
σ = 1.0
σu = 1e-3
f = GP(Matern32(), GPC())
ElOceanografo marked this conversation as resolved.
Show resolved Hide resolved

@timedtestset "sparse gp" begin
x = 0:0.1:10
xu = 0:10
σ = 1.0
σu = 1e-3
covariance_error = "The covariance matrix of a sparse GP can often be dense and can cause the computer to run out of memory. If you are sure you have enough memory, you can use `cov(f.fobs)`."
ElOceanografo marked this conversation as resolved.
Show resolved Hide resolved

@timedtestset "SparseFiniteGP Constructors" begin
f = GP(Matern32(), GPC())
@test SparseFiniteGP(f(x), f(xu)) == SparseFiniteGP(f, x, xu)
# @test SparseFiniteGP(f(x), f(xu)) == f(x, xu)
@test SparseFiniteGP(f(x, σ), f(xu, σ)) == SparseFiniteGP(f, x, xu, σ)
# @test SparseFiniteGP(f(x, σ), f(xu, σ)) == f(x, xu, σ)
@test SparseFiniteGP(f(x, σ), f(xu, σu)) == SparseFiniteGP(f, x, xu, σ, σu)
# @test SparseFiniteGP(f(x, σ), f(xu, σu)) == f(x, xu, σ, σu)
end

@timedtestset "SparseFiniteGP methods" begin
f = GP(Matern32(), GPC())
fx = f(x)
fxu = SparseFiniteGP(f, x, xu)
@test mean(fxu) == mean(fx)
@test rand(MersenneTwister(12345), fxu) == rand(MersenneTwister(12345), fx)
@test_throws ErrorException(covariance_error) cov(fxu)
@test cov(fxu.fobs) == cov(fx)
end

@timedtestset "SparseFiniteGP inference" begin
f = GP(Matern32(), GPC())
fx = f(x, σ)
fxu = SparseFiniteGP(f, x, xu, σ, σu)
y = rand(MersenneTwister(12345), fxu)

fpost1 = f | Stheno.PseudoObs(Obs(fxu.fobs, y), fxu.finducing)
fpost2 = f | (fxu ← y)
@test marginals(fpost1(x)) == marginals(fpost2(x))
@test elbo(fxu, y) == logpdf(fxu, y)
@test logpdf(fxu, y) == elbo(fxu.fobs, y, fxu.finducing)
yy = rand(fxu, 10)
logpdf(fx, yy)
logpdf(fxu, yy)
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ include("test_util.jl")

@testset "Stheno" begin

include(joinpath("gp", "sparse_gp.jl"))
ElOceanografo marked this conversation as resolved.
Show resolved Hide resolved
println("util:")
@timedtestset "util" begin
include(joinpath("util", "zygote_rules.jl"))
Expand Down