tests/testthat/test_gps.R

context('gaussian processes')


test_that('gaussian processes work with numeric vectors', {

  source("helpers.R")
  skip_if_not(greta:::check_tf_version())
  skip_if_not(gpflowr::gpflow_available())

  x <- 1:10
  x2 <- 11:30
  u <- c(2, 4, 6, 8)
  k <- rbf(1, 1)

  # full
  expect_ok(f <- gp(x, k))
  expect_ok(f2 <- project(f, x2))

  # sparse
  expect_ok(f <- gp(x, k, inducing = u))
  expect_ok(f2 <- project(f, x2))

})

test_that('gaussian processes work with numeric matrices', {

  source("helpers.R")
  skip_if_not(greta:::check_tf_version())
  skip_if_not(gpflowr::gpflow_available())

  x <- cbind(1:10, 2:11)
  x2 <- cbind(11:30, 12:31)
  u <- cbind(c(2, 4, 6, 8),
             c(3, 5, 7, 9))
  k <- rbf(1, 1)

  # full
  expect_ok(f <- gp(x, k))
  expect_ok(f2 <- project(f, x2))

  # sparse
  expect_ok(f <- gp(x, k, inducing = u))
  expect_ok(f2 <- project(f, x2))

})

test_that('gaussian processes work with greta arrays matrices', {

  source("helpers.R")
  skip_if_not(greta:::check_tf_version())
  skip_if_not(gpflowr::gpflow_available())

  x <- as_data(cbind(1:10, 2:11))
  x2 <- as_data(cbind(11:30, 12:31))
  u <- as_data(cbind(c(2, 4, 6, 8),
                     c(3, 5, 7, 9)))
  k <- rbf(1, 1)

  # full
  expect_ok(f <- gp(x, k))
  expect_ok(f2 <- project(f, x2))

  # sparse
  expect_ok(f <- gp(x, k, inducing = u))
  expect_ok(f2 <- project(f, x2))

})

test_that('gaussian processes can be defined in models and sampled from', {

  source("helpers.R")
  skip_if_not(greta:::check_tf_version())
  skip_if_not(gpflowr::gpflow_available())

  k <- rbf(1, 1)

  # full
  f = gp(1:10, k)
  expect_ok(m <- model(f))
  expect_ok(mcmc(m, warmup = 2, n_samples = 2))

  # sparse
  f = gp(1:10, k,
         inducing = c(2, 4, 6, 8))
  expect_ok(m <- model(f))
  expect_ok(mcmc(m, warmup = 2, n_samples = 2))

})

test_that('gaussian processes can be projected to new data', {

  source("helpers.R")
  skip_if_not(greta:::check_tf_version())
  skip_if_not(gpflowr::gpflow_available())

  k <- rbf(1, 1)

  # full
  f = gp(1:10, k)
  f2 <- project(f, 15:20)
  expect_ok(m <- model(f2))
  expect_ok(mcmc(m, warmup = 2, n_samples = 2))

  # sparse
  f = gp(1:10, k,
         inducing = c(2, 4, 6, 8))
  f2 <- project(f, 15:20)
  expect_ok(m <- model(f2))
  expect_ok(mcmc(m, warmup = 2, n_samples = 2))

})

test_that('gaussian processes can be projected with a different kernel', {

  source("helpers.R")
  skip_if_not(greta:::check_tf_version())
  skip_if_not(gpflowr::gpflow_available())

  k1 <- rbf(1, 1)
  k2 <- periodic(1, 1, 1)

  # full
  f = gp(1:10, k1)
  f2 <- project(f, 15:20, k2)
  expect_ok(m <- model(f2))
  expect_ok(mcmc(m, warmup = 2, n_samples = 2))

  # sparse
  f = gp(1:10, k1,
         inducing = c(2, 4, 6, 8))
  f2 <- project(f, 15:20, k2)
  expect_ok(m <- model(f2))
  expect_ok(mcmc(m, warmup = 2, n_samples = 2))

})

test_that('project() errors as expected', {

  source("helpers.R")
  skip_if_not(greta:::check_tf_version())
  skip_if_not(gpflowr::gpflow_available())

  f <- normal(0, 1, dim = 10)
  expect_error(f2 <- project(f, 15:20),
               "can only project from greta arrays created with greta.gp::gp")

})
greta-dev/greta.gp documentation built on Sept. 10, 2017, 12:24 a.m.