simulate {BKP}R Documentation

Simulate from a Fitted BKP or DKP Model

Description

Generates random samples from the posterior predictive distribution of a fitted BKP or DKP model at new input locations.

For BKP models, posterior samples are drawn from Beta distributions representing success probabilities, with optional binary class labels determined by a threshold.

For DKP models, posterior samples are drawn from Dirichlet distributions representing class probabilities, with optional class labels determined by the maximum a posteriori (MAP) rule if training responses are one-hot encoded.

Usage

## S3 method for class 'BKP'
simulate(object, Xnew, n_sim = 1, threshold = NULL, seed = NULL, ...)

## S3 method for class 'DKP'
simulate(object, Xnew, n_sim = 1, seed = NULL, ...)

Arguments

object

An object of class "BKP" or "DKP", typically returned by fit.BKP or fit.DKP.

Xnew

A numeric matrix or vector of new input locations for simulation.

n_sim

Number of posterior samples to generate (default = 1).

threshold

Classification threshold for binary output (only used for BKP). If specified, the output will include binary class labels with values above the threshold classified as 1 (default is NULL).

seed

Optional integer seed for reproducibility.

...

Additional arguments (currently unused).

Value

A list with the following components:

sims

For BKP: A numeric matrix of dimension nrow(Xnew) × n_sim, containing simulated success probabilities.
For DKP: A numeric array of dimension n_sim × q × nrow(Xnew), containing simulated class probabilities from Dirichlet posteriors, where q is the number of classes.

mean

For BKP: A numeric vector of posterior mean success probabilities at each Xnew.
For DKP: A numeric matrix of dimension nrow(Xnew) × q, containing posterior mean class probabilities.

class

For BKP: A binary matrix of dimension nrow(Xnew) × n_sim indicating simulated class labels (0/1), returned if threshold is specified.
For DKP: A numeric matrix of dimension nrow(Xnew) × n_sim containing MAP-predicted class labels, returned only when training data is single-label (i.e., each row of Y sums to 1).

See Also

fit.BKP, fit.DKP, predict.BKP, predict.DKP

Examples

## -------------------- BKP Simulation Example --------------------
set.seed(123)

# Define true success probability function
true_pi_fun <- function(x) {
  (1 + exp(-x^2) * cos(10 * (1 - exp(-x)) / (1 + exp(-x)))) / 2
}

n <- 30
Xbounds <- matrix(c(-2,2), nrow=1)
X <- tgp::lhs(n = n, rect = Xbounds)
true_pi <- true_pi_fun(X)
m <- sample(100, n, replace = TRUE)
y <- rbinom(n, size = m, prob = true_pi)

# Fit BKP model
model <- fit.BKP(X, y, m, Xbounds=Xbounds)

# Simulate 5 posterior draws of success probabilities
Xnew <- matrix(seq(-2, 2, length.out = 100), ncol = 1)
simulate(model, Xnew, n_sim = 5)

# Simulate binary classifications (threshold = 0.5)
simulate(model, Xnew, n_sim = 5, threshold = 0.5)

## -------------------- DKP Simulation Example --------------------
set.seed(123)

# Define true class probability function (3-class)
true_pi_fun <- function(X) {
  p <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2
  return(matrix(c(p/2, p/2, 1 - p), nrow = length(p)))
}

n <- 30
Xbounds <- matrix(c(-2, 2), nrow = 1)
X <- tgp::lhs(n = n, rect = Xbounds)
true_pi <- true_pi_fun(X)
m <- sample(100, n, replace = TRUE)

# Generate multinomial responses
Y <- t(sapply(1:n, function(i) rmultinom(1, size = m[i], prob = true_pi[i, ])))

# Fit DKP model
model <- fit.DKP(X, Y, Xbounds = Xbounds)

# Simulate 5 draws from posterior Dirichlet distributions at new point
Xnew <- matrix(seq(-2, 2, length.out = 100), ncol = 1)
simulate(model, Xnew = Xnew, n_sim = 5)


[Package BKP version 0.1.0 Index]