greta.gp 0.2.0

greta.gp
Author

Nicholas Tierney

Published

September 6, 2022

Parabolic curving bridges in Perth, Australia

Bridges in Perth, film, Nick Tierney

We are excited to announce that greta.gp version 0.2.0 is now on CRAN!

greta.gp provides a syntax to create and combine Gaussian process kernels in greta. You can then them to define either full rank or sparse Gaussian processes.

Here’s a brief example:

# simulate data
x <- runif(20, 0, 10)
y <- sin(x) + rnorm(20, 0, 0.5)
x_plot <- seq(-1, 11, length.out = 200)
library(greta)
library(greta.gp)
# hyperparameters
rbf_var <- lognormal(0, 1)
rbf_len <- lognormal(0, 1)
obs_sd <- lognormal(0, 1)
# kernel & GP
kernel <- rbf(rbf_len, rbf_var) + bias(1)
f <- gp(x, kernel)
# likelihood
distribution(y) <- normal(f, obs_sd)
# prediction
f_plot <- project(f, x_plot)
# fit the model by Hamiltonian Monte Carlo
m <- model(f_plot)
draws <- mcmc(m, n_samples = 250)
# plot 200 posterior samples
plot(
  y ~ x,
  pch = 16,
  col = grey(0.4),
  xlim = c(0, 10),
  ylim = c(-2.5, 2.5)
)
for (i in 1:200) {
  lines(
    draws[[1]][i, ] ~ x_plot,
    lwd = 2,
    col = rgb(0.7, 0.1, 0.4, 0.1)
  )
}