post_sample: Generates joint posterior samples from a trained GP/DGP

View source: R/sample.R

post_sampleR Documentation

Generates joint posterior samples from a trained GP/DGP

Description

Acts on a gp, gpvec, dgp2, dgp2vec, dgp3, or dgp3vec object. Generates joint samples from the posterior distribution at the provided locations.

Usage

post_sample(object, x_new, nper = 1, ...)

## S3 method for class 'gp'
post_sample(object, x_new, nper = 1, grad = FALSE, cores = 1, ...)

## S3 method for class 'gpvec'
post_sample(
  object,
  x_new,
  nper = 1,
  m = NULL,
  ord_new = NULL,
  grad = FALSE,
  cores = 1,
  ...
)

## S3 method for class 'dgp2'
post_sample(
  object,
  x_new,
  nper = 1,
  grad = FALSE,
  mean_map = TRUE,
  cores = 1,
  ...
)

## S3 method for class 'dgp2vec'
post_sample(
  object,
  x_new,
  nper = 1,
  m = NULL,
  ord_new = NULL,
  grad = FALSE,
  mean_map = TRUE,
  cores = 1,
  ...
)

## S3 method for class 'dgp3'
post_sample(object, x_new, nper = 1, mean_map = TRUE, cores = 1, ...)

## S3 method for class 'dgp3vec'
post_sample(
  object,
  x_new,
  nper = 1,
  m = NULL,
  ord_new = NULL,
  mean_map = TRUE,
  cores = 1,
  ...
)

Arguments

object

object from fit_one_layer, fit_two_layer, or fit_three_layer with burn-in already removed

x_new

vector or matrix of predictive input locations

nper

the number of samples to generate from each MCMC iteration. The total number of samples will equal nper*object$nmcmc

...

N/A

grad

logical indicating whether to additionally calculate/return samples of the gradient (one and two layer models only)

cores

number of cores to use for SNOW parallelization

m

size of Vecchia conditioning sets (only for fits with vecchia = TRUE), defaults to the twice the m used for MCMC

ord_new

optional ordering for Vecchia approximation with lite = FALSE, must correspond to rows of x_new, defaults to random, is applied to all layers in deeper models

mean_map

logical indicating whether to map hidden layers using conditional mean (mean_map = TRUE) or using a random sample from the full MVN distribution (two or three layer models only)

Details

By default, one sample is generated per each MCMC iteration. This may be increased with the nper argument.

SNOW parallelization reduces computation time but requires more memory storage.

Value

If grad = FALSE, returns matrix of samples. Rows correspond to x_new locations. If grad = TRUE, returns a list with y and dydx containing the respective samples.

References

Sauer, A. (2023). Deep Gaussian process surrogates for computer experiments. *Ph.D. Dissertation, Department of Statistics, Virginia Polytechnic Institute and State University.*

Examples


# Simple step function
f <- function(x) {
  return(pnorm((x - 0.5) / 0.065))
}

# Training data
x <- seq(0, 1, length = 5)
y <- f(x)

# Testing data
xx <- seq(0, 1, length = 100)
yy <- f(xx)

plot(xx, yy, type = "l")
points(x, y, col = 2)

# Conduct MCMC
fit <- fit_two_layer(x, y, nmcmc = 2000, true_g = 1e-6, cov = "exp2")
plot(fit, hidden = TRUE)
fit <- trim(fit, 1000, 2)

# Generate posterior samples, including gradients
samples <- post_sample(fit, xx, grad = TRUE, cores = 1)

# Plot samples
par(mfrow = c(1, 2))
matplot(xx, t(samples$y), type = "l")
points(x, y, pch = 20)
matplot(xx, t(samples$dy), type = "l")



deepgp documentation built on July 30, 2026, 5:12 p.m.