Stheno with Makie

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