Stheno with Makie

How can I plot Stheno intervals with Makie (instead of Plots.jl)?

Docs

Stheno.jl knows how to use Plots.jl to plot GPs, so it’s straightforward to look at the posterior:

x_plot = range(-4.0, 4.0; length=1000);
plot!(plt, f_posterior(x_plot); samples=10, label="", color=:blue);
display(plt);

img

I can’t find how that’s implemented. I want to implement with Makie’s band function.

I installed Stheno and it seems 0.7 is the recent version, for which the example is a bit different: https://juliagaussianprocesses.github.io/Stheno.jl/dev/getting_started/

Here’s how I transformed it for Makie, the only differences are really that Makie needs to separate the line and ribbon into two calls, and manually go over the columns of the matrix with the random lines, because we don’t by default convert matrices to different series.

This is the setup code
using Stheno
using Stheno.AbstractGPs


# Short length-scale and small variance.
l1 = 0.4
s1 = 0.2

# Long length-scale and larger variance.
l2 = 5.0
s2 = 1.0

# Specify a GaussianProcessProbabilisticProgramme object, which is itself a GP
# built from other GPs.
f = @gppp let
    f1 = s1 * stretch(GP(Matern52Kernel()), 1 / l1)
    f2 = s2 * stretch(GP(SEKernel()), 1 / l2)
    f3 = f1 + f2
end;

# Generate a sample from f3, one of the processes in f, at some random input locations.
# Add some iid observation noise, with zero-mean and variance 0.05.
const x = GPPPInput(:f3, collect(range(-5.0, 5.0; length=100)));
σ²_n = 0.05;
fx = f(x, σ²_n);
const y = rand(fx);

##


f_posterior = posterior(fx, y);

x_plot = range(-7.0, 7.0; length=1000);
xp = GPPPInput(:f3, x_plot);
ms = marginals(f_posterior(xp));

mea = mean.(ms)
st3 = 3std.(ms)

And this the actual plotting

using CairoMakie

f = Figure()
Axis(f[1, 1])

scatter!(x.x, y; color=:red);

lines!(x_plot, mean.(ms), color = :blue, linewidth = 2)

band!(x_plot, mea .- st3, mea .+ st3, color = (:blue, 0.2))

for col in eachcol(rand(f_posterior(xp), 10))
    lines!(x_plot, col, color = (:blue, 0.3))
end

f

4 Likes

Ha what a coincidence :smiley: I just put together some recipes for AbstractGPs yesterday: https://github.com/devmotion/AbstractGPsMakie.jl It’s the first iteration and not registered yet, and surely some things are lacking but it already includes the possibility to show an animation of a manifold of similar samples: Home · AbstractGPsMakie.jl

2 Likes

Awesome visualization, it’s surprisingly satisfying to watch.

EDIT: Link was broken, but fixed now in the post above.

Thanks, I’ll edit my post! I moved the repository to the JuliaGaussianProcesses organization an hour ago, it seems this broke the link. As far as I know, Github adds redirects for transferred Github repos automatically but it seems this is not the case for the github.io webpages.