Skip to contents

This function generates predictions using a Gaussian process model with a Gaussian RBF kernel. The hyperparameters are tuned using the training data by minimising the model's negative marginal log-likelihood.

Usage

fit_gpr(
  D2,
  training_idx,
  test_idx,
  y_train,
  y_test = NULL,
  centre = TRUE,
  use_gradient = TRUE,
  runs = 10,
  verbose = TRUE
)

Arguments

D2

A matrix containing squared pairwise distances between all objects.

training_idx

A vector with the row (and column) numbers corresponding to the training entries in D2.

test_idx

A vector with the row (and column) numbers corresponding to the test entries in D2.

y_train

A vector with the training outcomes.

y_test

Optionally, a vector with the test outcomes.

centre

If TRUE, the training outcomes are centred around their mean before fitting the model. The mean is then added back to the predictions at the end.

use_gradient

If TRUE, closed-form expressions for the RBF's gradient are used. Else, the optimiser uses the finite-differences method.

runs

Number of independent attempts to find a minimum when optimising the hyperparameters.

verbose

If TRUE, progress is shown on the console.

Value

A list containing the predictions for the test objects, the root mean squared error (if the true test outcomes are provided), and the three tuned hyperparameter values.

Examples

N1 <- 25
N2 <- 10
x_train <- seq(-pi, pi, length.out = N1)
x_test  <- runif(N2, -pi, pi)
y_train <- x_train * plogis(x_train) * cos(x_train)
y_test <- x_test * plogis(x_test) * cos(x_test)
D2 <- outer(c(x_train, x_test), c(x_train, x_test), "-")^2
fit <- fit_gpr(D2, seq_len(N1), N1 + seq_len(N2), y_train, y_test, runs = 50)
#> Hyperparameter search 1 of 50.
#> Hyperparameter search 2 of 50.
#> Current optimum improved from 33.4139022 to -158.2062976.
#> Hyperparameter search 3 of 50.
#> Hyperparameter search 4 of 50.
#> Current optimum improved from -158.2062976 to -161.123596.
#> Hyperparameter search 5 of 50.
#> Hyperparameter search 6 of 50.
#> Hyperparameter search 7 of 50.
#> Hyperparameter search 8 of 50.
#> Current optimum improved from -161.123596 to -163.6587123.
#> Hyperparameter search 9 of 50.
#> Current optimum improved from -163.6587123 to -165.4615012.
#> Hyperparameter search 10 of 50.
#> Hyperparameter search 11 of 50.
#> Hyperparameter search 12 of 50.
#> Hyperparameter search 13 of 50.
#> Hyperparameter search 14 of 50.
#> Hyperparameter search 15 of 50.
#> Hyperparameter search 16 of 50.
#> Hyperparameter search 17 of 50.
#> Hyperparameter search 18 of 50.
#> Hyperparameter search 19 of 50.
#> Hyperparameter search 20 of 50.
#> Hyperparameter search 21 of 50.
#> Hyperparameter search 22 of 50.
#> Hyperparameter search 23 of 50.
#> Hyperparameter search 24 of 50.
#> Hyperparameter search 25 of 50.
#> Hyperparameter search 26 of 50.
#> Hyperparameter search 27 of 50.
#> Hyperparameter search 28 of 50.
#> Hyperparameter search 29 of 50.
#> Hyperparameter search 30 of 50.
#> Hyperparameter search 31 of 50.
#> Hyperparameter search 32 of 50.
#> Hyperparameter search 33 of 50.
#> Hyperparameter search 34 of 50.
#> Hyperparameter search 35 of 50.
#> Hyperparameter search 36 of 50.
#> Hyperparameter search 37 of 50.
#> Hyperparameter search 38 of 50.
#> Current optimum improved from -165.4615012 to -165.9525145.
#> Hyperparameter search 39 of 50.
#> Hyperparameter search 40 of 50.
#> Hyperparameter search 41 of 50.
#> Hyperparameter search 42 of 50.
#> Hyperparameter search 43 of 50.
#> Hyperparameter search 44 of 50.
#> Hyperparameter search 45 of 50.
#> Hyperparameter search 46 of 50.
#> Hyperparameter search 47 of 50.
#> Hyperparameter search 48 of 50.
#> Current optimum improved from -165.9525145 to -166.2605829.
#> Hyperparameter search 49 of 50.
#> Hyperparameter search 50 of 50.
curve(x * plogis(x) * cos(x), -pi, pi)
points(x_train, y_train, pch = 1)
points(x_test, fit$test_predictions, pch = 16)

fit$RMSE
#> [1] 1.057625e-08