| Title: | Beta Kernel Process Modeling |
|---|---|
| Description: | Implements the Beta Kernel Process (BKP) for nonparametric modeling of spatially varying binomial probabilities, together with its extension, the Dirichlet Kernel Process (DKP), for categorical or multinomial data. The package provides functions for model fitting, predictive inference with uncertainty quantification, posterior simulation, and visualization in one-and two-dimensional input spaces. Multiple kernel functions (Gaussian, Matern 5/2, and Matern 3/2) are supported, with hyperparameters optimized through multi-start gradient-based search. For more details, see Zhao, Qing, and Xu (2025) <doi:10.48550/arXiv.2508.10447>. |
| Authors: | Jiangyan Zhao [cre, aut], Kunhai Qing [aut], Jin Xu [aut] |
| Maintainer: | Jiangyan Zhao <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.2.3.9000 |
| Built: | 2026-06-07 14:54:16 UTC |
| Source: | https://github.com/jiangyan-zhao/bkp |
The BKP package provides tools for Bayesian nonparametric modeling of binary/binomial or categorical/multinomial response data using the Beta Kernel Process (BKP) and its extension, the Dirichlet Kernel Process (DKP). These methods estimate latent probability surfaces through localized kernel smoothing under a Bayesian framework.
The package offers functionality for model fitting, posterior predictive inference with uncertainty quantification, simulation of posterior draws, and visualization in both one- and two-dimensional input spaces. It also supports flexible prior specification and hyperparameter tuning.
Core functionality is organized as follows:
fit_BKP, fit_DKP
Fit a BKP or DKP model to (multi)binomial response data.
predict.BKP, predict.DKP
Perform posterior predictive inference at new input locations, including predictive means, variances, and credible intervals. When observations correspond to single trials (binary or categorical responses), predicted class labels are returned automatically.
simulate.BKP, simulate.DKP
Generate simulated responses from the posterior predictive distribution of a fitted model.
plot.BKP, plot.DKP
Visualize model predictions and associated uncertainty in one- and two-dimensional input spaces;
for inputs with more than two dimensions, users can select one or two dimensions
to display via the dims argument.
summary.BKP, summary.DKP, print.BKP, print.DKP
Summarize or print the details of a fitted BKP or DKP model.
Zhao J, Qing K, Xu J (2025). BKP: An R Package for Beta Kernel Process Modeling. arXiv. https://doi.org/10.48550/arXiv.2508.10447.
Rolland P, Kavis A, Singla A, Cevher V (2019). Efficient learning of smooth probability functions from Bernoulli tests with guarantees. In Proceedings of the 36th International Conference on Machine Learning, ICML 2019, 9-15 June 2019, Long Beach, California, USA, volume 97 of Proceedings of Machine Learning Research, pp. 5459-5467. PMLR.
MacKenzie CA, Trafalis TB, Barker K (2014). A Bayesian Beta Kernel Model for Binary Classification and Online Learning Problems. Statistical Analysis and Data Mining: The ASA Data Science Journal, 7(6), 434-449.
Goetschalckx R, Poupart P, Hoey J (2011). Continuous Correlated Beta Processes. In Proceedings of the Twenty-Second International Joint Conference on Artificial Intelligence - Volume Volume Two, IJCAI’11, p. 1269-1274. AAAI Press.
Fits a Beta Kernel Process (BKP) model to binary or binomial response data using local kernel smoothing. The method constructs a flexible latent probability surface by updating Beta priors with kernel-weighted observations.
fit_BKP( X, y, m, Xbounds = NULL, prior = c("noninformative", "fixed", "adaptive"), r0 = 2, p0 = mean(y/m), kernel = c("gaussian", "matern52", "matern32", "wendland"), loss = c("brier", "log_loss"), n_multi_start = NULL, theta = NULL, isotropic = TRUE )fit_BKP( X, y, m, Xbounds = NULL, prior = c("noninformative", "fixed", "adaptive"), r0 = 2, p0 = mean(y/m), kernel = c("gaussian", "matern52", "matern32", "wendland"), loss = c("brier", "log_loss"), n_multi_start = NULL, theta = NULL, isotropic = TRUE )
X |
A numeric input matrix of size |
y |
A numeric vector of observed successes (length |
m |
A numeric vector of total binomial trials (length |
Xbounds |
Optional |
prior |
Type of prior: |
r0 |
Global prior precision (used when |
p0 |
Global prior mean (used when |
kernel |
Kernel function for local weighting: |
loss |
Loss function for kernel hyperparameter tuning: |
n_multi_start |
Number of initial points used in multi-start
optimization of the kernel lengthscale parameters. If |
theta |
Optional. A positive scalar or numeric vector of length |
isotropic |
Logical. If |
A list of class "BKP" containing the fitted BKP model,
including:
theta_optOptimized kernel hyperparameters (lengthscales).
kernelKernel function used, as a string.
isotropicLogical flag indicating whether a shared lengthscale (TRUE) or per-dimension lengthscales (FALSE) was used.
lossLoss function used for hyperparameter tuning.
loss_minLoss value at the selected/provided kernel parameters.
XOriginal input matrix ().
XnormNormalized input matrix scaled to .
XboundsNormalization bounds for each input dimension ().
yObserved success counts.
mObserved binomial trial counts.
priorType of prior used.
r0Prior precision parameter.
p0Prior mean (for fixed priors).
alpha0Prior Beta shape parameter .
beta0Prior Beta shape parameter .
alpha_nPosterior shape parameter .
beta_nPosterior shape parameter .
Zhao J, Qing K, Xu J (2025). BKP: An R Package for Beta Kernel Process Modeling. arXiv. https://doi.org/10.48550/arXiv.2508.10447.
fit_DKP for modeling multinomial responses via the
Dirichlet Kernel Process. predict.BKP,
plot.BKP, simulate.BKP, and
summary.BKP for prediction, visualization, posterior
simulation, and summarization of a fitted BKP model.
#-------------------------- 1D 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 model1 <- fit_BKP(X, y, m, Xbounds=Xbounds) print(model1) #-------------------------- 2D Example --------------------------- set.seed(123) # Define 2D latent function and probability transformation true_pi_fun <- function(X) { if(is.null(nrow(X))) X <- matrix(X, nrow=1) m <- 8.6928 s <- 2.4269 x1 <- 4*X[,1]- 2 x2 <- 4*X[,2]- 2 a <- 1 + (x1 + x2 + 1)^2 * (19- 14*x1 + 3*x1^2- 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1- 3*x2)^2 * (18- 32*x1 + 12*x1^2 + 48*x2- 36*x1*x2 + 27*x2^2) f <- log(a*b) f <- (f- m)/s return(pnorm(f)) # Transform to probability } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) 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 model2 <- fit_BKP(X, y, m, Xbounds=Xbounds) print(model2)#-------------------------- 1D 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 model1 <- fit_BKP(X, y, m, Xbounds=Xbounds) print(model1) #-------------------------- 2D Example --------------------------- set.seed(123) # Define 2D latent function and probability transformation true_pi_fun <- function(X) { if(is.null(nrow(X))) X <- matrix(X, nrow=1) m <- 8.6928 s <- 2.4269 x1 <- 4*X[,1]- 2 x2 <- 4*X[,2]- 2 a <- 1 + (x1 + x2 + 1)^2 * (19- 14*x1 + 3*x1^2- 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1- 3*x2)^2 * (18- 32*x1 + 12*x1^2 + 48*x2- 36*x1*x2 + 27*x2^2) f <- log(a*b) f <- (f- m)/s return(pnorm(f)) # Transform to probability } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) 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 model2 <- fit_BKP(X, y, m, Xbounds=Xbounds) print(model2)
Fits a DKP model for categorical or multinomial response data by locally smoothing observed counts to estimate latent Dirichlet parameters. The model constructs flexible latent probability surfaces by updating Dirichlet priors using kernel-weighted observations.
fit_DKP( X, Y, Xbounds = NULL, prior = c("noninformative", "fixed", "adaptive"), r0 = 2, p0 = colMeans(Y/rowSums(Y)), kernel = c("gaussian", "matern52", "matern32", "wendland"), loss = c("brier", "log_loss"), n_multi_start = NULL, theta = NULL, isotropic = TRUE )fit_DKP( X, Y, Xbounds = NULL, prior = c("noninformative", "fixed", "adaptive"), r0 = 2, p0 = colMeans(Y/rowSums(Y)), kernel = c("gaussian", "matern52", "matern32", "wendland"), loss = c("brier", "log_loss"), n_multi_start = NULL, theta = NULL, isotropic = TRUE )
X |
A numeric input matrix of size |
Y |
Numeric matrix of observed multinomial counts, with dimension
|
Xbounds |
Optional |
prior |
Type of prior: |
r0 |
Global prior precision (used when |
p0 |
Global prior mean vector (used only when |
kernel |
Kernel function for local weighting: |
loss |
Loss function for kernel hyperparameter tuning: |
n_multi_start |
Number of initial points used in multi-start
optimization of the kernel lengthscale parameters. If |
theta |
Optional. A positive scalar or numeric vector of length |
isotropic |
Logical. If |
A list of class "DKP" representing the fitted DKP model, with
the following components:
theta_optOptimized kernel hyperparameters (lengthscales).
kernelKernel function used, as a string.
isotropicLogical flag indicating whether a shared lengthscale (TRUE) or per-dimension lengthscales (FALSE) was used.
lossLoss function used for hyperparameter tuning.
loss_minMinimum loss value achieved during kernel
hyperparameter optimization. Set to NA if theta is user-specified.
XOriginal (unnormalized) input matrix of size n × d.
XnormNormalized input matrix scaled to .
XboundsMatrix specifying normalization bounds for each input dimension.
YObserved multinomial counts of size n × q.
priorType of prior used.
r0Prior precision parameter.
p0Prior mean (for fixed priors).
alpha0Prior Dirichlet parameters at each input location (scalar or matrix).
alpha_nPosterior Dirichlet parameters after kernel smoothing.
Zhao J, Qing K, Xu J (2025). BKP: An R Package for Beta Kernel Process Modeling. arXiv. https://doi.org/10.48550/arXiv.2508.10447.
fit_BKP for modeling binomial responses via the Beta
Kernel Process. predict.DKP, plot.DKP,
simulate.DKP for prediction, visualization, and posterior
simulation from a fitted DKP model. summary.DKP,
print.DKP for inspecting model summaries.
#-------------------------- 1D Example --------------------------- set.seed(123) # Define true class probability function (3-class) true_pi_fun <- function(X) { p1 <- 1/(1+exp(-3*X)) p2 <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2 return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 30 Xbounds <- matrix(c(-2, 2), nrow = 1) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model1 <- fit_DKP(X, Y, Xbounds = Xbounds) print(model1) #-------------------------- 2D Example --------------------------- set.seed(123) # Define latent function and transform to 3-class probabilities true_pi_fun <- function(X) { if (is.null(nrow(X))) X <- matrix(X, nrow = 1) m <- 8.6928; s <- 2.4269 x1 <- 4 * X[,1] - 2 x2 <- 4 * X[,2] - 2 a <- 1 + (x1 + x2 + 1)^2 * (19 - 14*x1 + 3*x1^2 - 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1 - 3*x2)^2 * (18 - 32*x1 + 12*x1^2 + 48*x2 - 36*x1*x2 + 27*x2^2) f <- (log(a*b)- m)/s p1 <- pnorm(f) # Transform to probability p2 <- sin(pi * X[,1]) * sin(pi * X[,2]) return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model2 <- fit_DKP(X, Y, Xbounds = Xbounds) print(model2)#-------------------------- 1D Example --------------------------- set.seed(123) # Define true class probability function (3-class) true_pi_fun <- function(X) { p1 <- 1/(1+exp(-3*X)) p2 <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2 return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 30 Xbounds <- matrix(c(-2, 2), nrow = 1) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model1 <- fit_DKP(X, Y, Xbounds = Xbounds) print(model1) #-------------------------- 2D Example --------------------------- set.seed(123) # Define latent function and transform to 3-class probabilities true_pi_fun <- function(X) { if (is.null(nrow(X))) X <- matrix(X, nrow = 1) m <- 8.6928; s <- 2.4269 x1 <- 4 * X[,1] - 2 x2 <- 4 * X[,2] - 2 a <- 1 + (x1 + x2 + 1)^2 * (19 - 14*x1 + 3*x1^2 - 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1 - 3*x2)^2 * (18 - 32*x1 + 12*x1^2 + 48*x2 - 36*x1*x2 + 27*x2^2) f <- (log(a*b)- m)/s p1 <- pnorm(f) # Transform to probability p2 <- sin(pi * X[,1]) * sin(pi * X[,2]) return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model2 <- fit_DKP(X, Y, Xbounds = Xbounds) print(model2)
Fits a TwinBKP model for binary or binomial response data.
The workflow follows fit_BKP(), but kernel hyperparameter tuning
is accelerated by first selecting g representative support points
from the full dataset using the Twining package, then optimizing
the kernel lengthscales on these g points only.
The final posterior update still uses all n observations.
fit_TwinBKP( X, y, m, Xbounds = NULL, prior = c("noninformative", "fixed", "adaptive"), r0 = 2, p0 = mean(y/m), kernel = c("gaussian", "matern52", "matern32", "wendland"), loss = c("brier", "log_loss"), n_multi_start = NULL, theta = NULL, isotropic = TRUE, g_nums = NULL )fit_TwinBKP( X, y, m, Xbounds = NULL, prior = c("noninformative", "fixed", "adaptive"), r0 = 2, p0 = mean(y/m), kernel = c("gaussian", "matern52", "matern32", "wendland"), loss = c("brier", "log_loss"), n_multi_start = NULL, theta = NULL, isotropic = TRUE, g_nums = NULL )
X |
A numeric input matrix of size |
y |
A numeric vector of observed successes (length |
m |
A numeric vector of total binomial trials (length |
Xbounds |
Optional |
prior |
Type of prior: |
r0 |
Global prior precision (used when |
p0 |
Global prior mean (used when |
kernel |
Kernel function for local weighting: |
loss |
Loss function for kernel hyperparameter tuning: |
n_multi_start |
Number of initial points used in multi-start
optimization of the kernel lengthscale parameters. If |
theta |
Optional. A positive scalar or numeric vector of length |
isotropic |
Logical. If |
g_nums |
Positive integer. Number of global support points selected by
the Twining algorithm for hyperparameter tuning. If |
A list of class "TwinBKP" with the same structure as
"BKP", plus:
g_numsNumber of support points used for tuning.
tune_idxRow indices of the selected support points.
alpha0_global, beta0_globalGlobal-stage prior parameters on support points.
alpha_n_global, beta_n_globalGlobal-stage posterior parameters on support points.
mean_global, var_globalGlobal-stage posterior mean/variance
on support points; used directly by fitted.TwinBKP() and
summary.TwinBKP() without recomputation.
set.seed(123) true_pi_fun <- function(x) { (1 + exp(-x^2) * cos(10 * (1 - exp(-x)) / (1 + exp(-x)))) / 2 } n <- 10000 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) model <- fit_TwinBKP(X, y, m, Xbounds = Xbounds, g_nums = 100) print(model)set.seed(123) true_pi_fun <- function(x) { (1 + exp(-x^2) * cos(10 * (1 - exp(-x)) / (1 + exp(-x)))) / 2 } n <- 10000 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) model <- fit_TwinBKP(X, y, m, Xbounds = Xbounds, g_nums = 100) print(model)
Fits a TwinDKP model for categorical or multinomial response data.
Similar to fit_TwinBKP(), the method first selects g_nums
representative support points using Twining, then optimizes kernel
hyperparameters on these support points only.
fit_TwinDKP( X, Y, Xbounds = NULL, prior = c("noninformative", "fixed", "adaptive"), r0 = 2, p0 = colMeans(Y/rowSums(Y)), kernel = c("gaussian", "matern52", "matern32", "wendland"), loss = c("brier", "log_loss"), n_multi_start = NULL, theta = NULL, isotropic = TRUE, g_nums = NULL )fit_TwinDKP( X, Y, Xbounds = NULL, prior = c("noninformative", "fixed", "adaptive"), r0 = 2, p0 = colMeans(Y/rowSums(Y)), kernel = c("gaussian", "matern52", "matern32", "wendland"), loss = c("brier", "log_loss"), n_multi_start = NULL, theta = NULL, isotropic = TRUE, g_nums = NULL )
X |
A numeric input matrix of size |
Y |
Numeric matrix of observed multinomial counts, with dimension
|
Xbounds |
Optional |
prior |
Type of prior: |
r0 |
Global prior precision (used when |
p0 |
Global prior mean vector (used only when |
kernel |
Kernel function for local weighting: |
loss |
Loss function for kernel hyperparameter tuning: |
n_multi_start |
Number of initial points used in multi-start
optimization of the kernel lengthscale parameters. If |
theta |
Optional. A positive scalar or numeric vector of length |
isotropic |
Logical. If |
g_nums |
Positive integer. Number of global support points selected by
the Twining algorithm for hyperparameter tuning. If |
A list of class "TwinDKP" with the same structure as
"DKP", plus:
g_numsNumber of support points used for tuning.
tune_idxRow indices of the selected support points.
alpha0_globalGlobal-stage prior Dirichlet parameters on support points.
alpha_n_globalGlobal-stage posterior Dirichlet parameters on support points.
mean_global, var_globalGlobal-stage posterior mean/variance
on support points; used directly by fitted.TwinDKP() and
summary.TwinDKP() without recomputation.
Compute the posterior fitted values from a fitted BKP or
DKP object. For a BKP object, this returns the posterior mean
probability of the positive class. For a DKP object, this returns
the posterior mean probabilities for each class.
## S3 method for class 'BKP' fitted(object, ...) ## S3 method for class 'DKP' fitted(object, ...) ## S3 method for class 'TwinBKP' fitted(object, ...) ## S3 method for class 'TwinDKP' fitted(object, ...)## S3 method for class 'BKP' fitted(object, ...) ## S3 method for class 'DKP' fitted(object, ...) ## S3 method for class 'TwinBKP' fitted(object, ...) ## S3 method for class 'TwinDKP' fitted(object, ...)
object |
An object of class |
... |
Additional arguments (currently unused). |
For a BKP model, the fitted values correspond to the
posterior mean probability of the positive class, computed from the Beta
Kernel Process. For a DKP model, the fitted values correspond to the
posterior mean probabilities for each class, derived from the posterior
Dirichlet distribution of the class probabilities.
For TwinBKP, fitted values are computed on the global support set
selected during fit_TwinBKP(). Specifically, this method computes
posterior Beta parameters on object$Xnorm_global and returns the
posterior mean for those support
points.
This is intentionally different from BKP, where fitted values are
naturally available on all training points. In TwinBKP, full-sample behavior
is prediction-oriented and should be obtained via predict.TwinBKP().
For TwinDKP, fitted values are reported on the global support points
selected during fit_TwinDKP(). The method computes posterior mean
class probabilities on object$Xnorm_global.
A numeric vector (for BKP) or a numeric matrix (for
DKP) containing posterior mean estimates at the training inputs.
Zhao J, Qing K, Xu J (2025). BKP: An R Package for Beta Kernel Process Modeling. arXiv. https://doi.org/10.48550/arXiv.2508.10447.
# -------------------------- BKP --------------------------- 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) # Extract fitted values fitted(model) # -------------------------- DKP --------------------------- #' set.seed(123) # Define true class probability function (3-class) true_pi_fun <- function(X) { p1 <- 1/(1+exp(-3*X)) p2 <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2 return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 30 Xbounds <- matrix(c(-2, 2), nrow = 1) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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) # Extract fitted values fitted(model)# -------------------------- BKP --------------------------- 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) # Extract fitted values fitted(model) # -------------------------- DKP --------------------------- #' set.seed(123) # Define true class probability function (3-class) true_pi_fun <- function(X) { p1 <- 1/(1+exp(-3*X)) p2 <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2 return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 30 Xbounds <- matrix(c(-2, 2), nrow = 1) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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) # Extract fitted values fitted(model)
Computes prior parameters for the Beta Kernel Process (BKP, for
binary outcomes) or Dirichlet Kernel Process (DKP, for multi-class
outcomes). Supports prior = "noninformative", "fixed", and
"adaptive" strategies.
get_prior( prior = c("noninformative", "fixed", "adaptive"), model = c("BKP", "DKP"), r0 = 2, p0 = NULL, y = NULL, m = NULL, Y = NULL, K = NULL )get_prior( prior = c("noninformative", "fixed", "adaptive"), model = c("BKP", "DKP"), r0 = 2, p0 = NULL, y = NULL, m = NULL, Y = NULL, K = NULL )
prior |
Type of prior: |
model |
A character string specifying the model type: |
r0 |
Global prior precision (used when |
p0 |
For BKP, a scalar in |
y |
A numeric vector of observed successes (length |
m |
A numeric vector of total binomial trials (length |
Y |
A numeric matrix of observed class counts ( |
K |
A precomputed kernel matrix, typically obtained from
|
prior = "noninformative": flat prior; all parameters set to 1.
prior = "fixed":
BKP: uniform Beta prior Beta(r0 * p0, r0 * (1 - p0)) across locations.
DKP: all rows of alpha0 set to r0 * p0.
prior = "adaptive":
BKP: prior mean estimated at each location via kernel smoothing of observed
proportions y/m, with precision r0.
DKP: prior parameters computed by kernel-weighted smoothing of observed
class frequencies in Y, scaled by r0.
If model = "BKP": a list with
alpha0Vector of prior alpha parameters for the Beta
distribution, length n.
beta0Vector of prior beta parameters for the Beta
distribution, length n.
If model = "DKP": a matrix alpha0 of prior Dirichlet
parameters at each input location (n × q).
Zhao J, Qing K, Xu J (2025). BKP: An R Package for Beta Kernel Process Modeling. arXiv. https://doi.org/10.48550/arXiv.2508.10447
fit_BKP for fitting Beta Kernel Process models,
fit_DKP for fitting Dirichlet Kernel Process models,
predict.BKP and predict.DKP for making
predictions, kernel_matrix for computing kernel matrices used
in prior construction.
# -------------------------- BKP --------------------------- set.seed(123) n <- 10 X <- matrix(runif(n * 2), ncol = 2) y <- rbinom(n, size = 5, prob = 0.6) m <- rep(5, n) K <- kernel_matrix(X) prior_bkp <- get_prior( model = "BKP", prior = "adaptive", r0 = 2, y = y, m = m, K = K ) # -------------------------- DKP --------------------------- set.seed(123) n <- 15; q <- 3 X <- matrix(runif(n * 2), ncol = 2) true_pi <- t(apply(X, 1, function(x) { raw <- c( exp(-sum((x - 0.2)^2)), exp(-sum((x - 0.5)^2)), exp(-sum((x - 0.8)^2)) ) raw / sum(raw) })) m <- sample(10:20, n, replace = TRUE) Y <- t(sapply(1:n, function(i) rmultinom(1, size = m[i], prob = true_pi[i, ]))) K <- kernel_matrix(X, theta = 0.2, kernel = "gaussian") prior_dkp <- get_prior( model = "DKP", prior = "adaptive", r0 = 2, Y = Y, K = K )# -------------------------- BKP --------------------------- set.seed(123) n <- 10 X <- matrix(runif(n * 2), ncol = 2) y <- rbinom(n, size = 5, prob = 0.6) m <- rep(5, n) K <- kernel_matrix(X) prior_bkp <- get_prior( model = "BKP", prior = "adaptive", r0 = 2, y = y, m = m, K = K ) # -------------------------- DKP --------------------------- set.seed(123) n <- 15; q <- 3 X <- matrix(runif(n * 2), ncol = 2) true_pi <- t(apply(X, 1, function(x) { raw <- c( exp(-sum((x - 0.2)^2)), exp(-sum((x - 0.5)^2)), exp(-sum((x - 0.8)^2)) ) raw / sum(raw) })) m <- sample(10:20, n, replace = TRUE) Y <- t(sapply(1:n, function(i) rmultinom(1, size = m[i], prob = true_pi[i, ]))) K <- kernel_matrix(X, theta = 0.2, kernel = "gaussian") prior_dkp <- get_prior( model = "DKP", prior = "adaptive", r0 = 2, Y = Y, K = K )
Computes the kernel matrix between two sets of input locations using a specified kernel function. Supports both isotropic and anisotropic lengthscales. Available kernels include the Gaussian, Matérn 5/2, Matérn 3/2, and Wendland compactly supported kernel.
kernel_matrix( X, Xprime = NULL, theta = 0.1, kernel = c("gaussian", "matern52", "matern32", "wendland"), isotropic = TRUE )kernel_matrix( X, Xprime = NULL, theta = 0.1, kernel = c("gaussian", "matern52", "matern32", "wendland"), isotropic = TRUE )
X |
A numeric matrix (or vector) of input locations with shape |
Xprime |
An optional numeric matrix of input locations with shape |
theta |
A positive numeric value or vector specifying the kernel
lengthscale(s). If |
kernel |
A character string specifying the kernel function. Must be one
of |
isotropic |
Logical. If |
Let and denote two input points.
The scaled distance is defined as
The available kernels are defined as:
Gaussian:
Matérn 5/2:
Matérn 3/2:
Wendland:
The function performs consistency checks on input dimensions and
automatically broadcasts theta when it is a scalar.
A numeric matrix of size , where each element
gives the kernel similarity between input and
.
Zhao J, Qing K, Xu J (2025). BKP: An R Package for Beta Kernel Process Modeling. arXiv. https://doi.org/10.48550/arXiv.2508.10447.
Rasmussen, C. E., & Williams, C. K. I. (2006). Gaussian Processes for Machine Learning. MIT Press.
# Basic usage with default Xprime = X X <- matrix(runif(20), ncol = 2) K1 <- kernel_matrix(X, theta = 0.2, kernel = "gaussian") # Anisotropic lengthscales with Matérn 5/2 K2 <- kernel_matrix(X, theta = c(0.1, 0.3), kernel = "matern52", isotropic = FALSE) # Isotropic Matérn 3/2 K3 <- kernel_matrix(X, theta = 1, kernel = "matern32") # Use Xprime different from X Xprime <- matrix(runif(10), ncol = 2) K4 <- kernel_matrix(X, Xprime, theta = 0.2, kernel = "gaussian")# Basic usage with default Xprime = X X <- matrix(runif(20), ncol = 2) K1 <- kernel_matrix(X, theta = 0.2, kernel = "gaussian") # Anisotropic lengthscales with Matérn 5/2 K2 <- kernel_matrix(X, theta = c(0.1, 0.3), kernel = "matern52", isotropic = FALSE) # Isotropic Matérn 3/2 K3 <- kernel_matrix(X, theta = 1, kernel = "matern32") # Use Xprime different from X Xprime <- matrix(runif(10), ncol = 2) K4 <- kernel_matrix(X, Xprime, theta = 0.2, kernel = "gaussian")
Computes the loss for fitting BKP (binary) or DKP (multi-class) models. Supports Brier score (mean squared error) and log-loss (cross-entropy) under different prior specifications.
loss_fun( gamma, Xnorm, y = NULL, m = NULL, Y = NULL, model = c("BKP", "DKP"), prior = c("noninformative", "fixed", "adaptive"), r0 = 2, p0 = NULL, loss = c("brier", "log_loss"), kernel = c("gaussian", "matern52", "matern32", "wendland"), isotropic = TRUE )loss_fun( gamma, Xnorm, y = NULL, m = NULL, Y = NULL, model = c("BKP", "DKP"), prior = c("noninformative", "fixed", "adaptive"), r0 = 2, p0 = NULL, loss = c("brier", "log_loss"), kernel = c("gaussian", "matern52", "matern32", "wendland"), isotropic = TRUE )
gamma |
A numeric vector of log10-transformed kernel hyperparameters. |
Xnorm |
A numeric matrix of normalized input features ( |
y |
A numeric vector of observed successes (length |
m |
A numeric vector of total binomial trials (length |
Y |
A numeric matrix of observed class counts ( |
model |
A character string specifying the model type: |
prior |
Type of prior: |
r0 |
Global prior precision (used when |
p0 |
For BKP, a scalar in |
loss |
Loss function for kernel hyperparameter tuning: |
kernel |
Kernel function for local weighting: |
isotropic |
Logical. If |
A single numeric value representing the total loss (to be minimized). The value corresponds to either the Brier score (squared error) or the log-loss (cross-entropy).
Zhao J, Qing K, Xu J (2025). BKP: An R Package for Beta Kernel Process Modeling. arXiv. https://doi.org/10.48550/arXiv.2508.10447.
fit_BKP for fitting BKP models, fit_DKP
for fitting DKP models, get_prior for constructing prior
parameters, kernel_matrix for computing kernel matrices.
# -------------------------- BKP --------------------------- set.seed(123) n <- 10 Xnorm <- matrix(runif(2 * n), ncol = 2) m <- rep(10, n) y <- rbinom(n, size = m, prob = runif(n)) loss_fun(gamma = 0, Xnorm = Xnorm, y = y, m = m, model = "BKP") # -------------------------- DKP --------------------------- set.seed(123) n <- 10 q <- 3 Xnorm <- matrix(runif(2 * n), ncol = 2) Y <- matrix(rmultinom(n, size = 10, prob = rep(1/q, q)), nrow = n, byrow = TRUE) loss_fun(gamma = 0, Xnorm = Xnorm, Y = Y, model = "DKP")# -------------------------- BKP --------------------------- set.seed(123) n <- 10 Xnorm <- matrix(runif(2 * n), ncol = 2) m <- rep(10, n) y <- rbinom(n, size = m, prob = runif(n)) loss_fun(gamma = 0, Xnorm = Xnorm, y = y, m = m, model = "BKP") # -------------------------- DKP --------------------------- set.seed(123) n <- 10 q <- 3 Xnorm <- matrix(runif(2 * n), ncol = 2) Y <- matrix(rmultinom(n, size = 10, prob = rep(1/q, q)), nrow = n, byrow = TRUE) loss_fun(gamma = 0, Xnorm = Xnorm, Y = Y, model = "DKP")
Retrieve the key model parameters from a fitted BKP or
DKP object. For a BKP model, this typically includes the
optimized kernel hyperparameters and posterior Beta parameters. For a
DKP model, this includes the kernel hyperparameters and posterior
Dirichlet parameters.
parameter(object, ...) ## S3 method for class 'BKP' parameter(object, ...) ## S3 method for class 'DKP' parameter(object, ...)parameter(object, ...) ## S3 method for class 'BKP' parameter(object, ...) ## S3 method for class 'DKP' parameter(object, ...)
object |
An object of class |
... |
Additional arguments (currently unused). |
A named list containing:
theta: Estimated kernel hyperparameters.
alpha_n: Posterior Dirichlet/Beta parameters.
beta_n: (BKP only) Posterior Beta parameters.
Zhao J, Qing K, Xu J (2025). BKP: An R Package for Beta Kernel Process Modeling. arXiv. https://doi.org/10.48550/arXiv.2508.10447.
fit_BKP for fitting BKP models, fit_DKP
for fitting DKP models.
# -------------------------- BKP --------------------------- 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) # Extract posterior and kernel parameters parameter(model) # -------------------------- DKP --------------------------- #' set.seed(123) # Define true class probability function (3-class) true_pi_fun <- function(X) { p1 <- 1/(1+exp(-3*X)) p2 <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2 return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 30 Xbounds <- matrix(c(-2, 2), nrow = 1) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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) # Extract posterior quantiles parameter(model)# -------------------------- BKP --------------------------- 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) # Extract posterior and kernel parameters parameter(model) # -------------------------- DKP --------------------------- #' set.seed(123) # Define true class probability function (3-class) true_pi_fun <- function(X) { p1 <- 1/(1+exp(-3*X)) p2 <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2 return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 30 Xbounds <- matrix(c(-2, 2), nrow = 1) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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) # Extract posterior quantiles parameter(model)
Visualizes fitted BKP (binary) or DKP
(multi-class) models according to the input dimensionality. For 1D inputs,
it shows predicted class probabilities with credible intervals and observed
data. For 2D inputs, it generates contour plots of posterior summaries. For
higher-dimensional inputs, users must specify which dimensions to plot.
## S3 method for class 'BKP' plot( x, only_mean = FALSE, n_grid = 80, dims = NULL, engine = c("base", "ggplot"), ... ) ## S3 method for class 'DKP' plot( x, only_mean = FALSE, n_grid = 80, dims = NULL, engine = c("base", "ggplot"), ... ) ## S3 method for class 'TwinBKP' plot( x, only_mean = FALSE, n_grid = 80, dims = NULL, engine = c("base", "ggplot"), l_nums = NULL, v_nums = NULL, ... ) ## S3 method for class 'TwinDKP' plot( x, only_mean = FALSE, n_grid = 80, dims = NULL, engine = c("base", "ggplot"), l_nums = NULL, v_nums = NULL, ... )## S3 method for class 'BKP' plot( x, only_mean = FALSE, n_grid = 80, dims = NULL, engine = c("base", "ggplot"), ... ) ## S3 method for class 'DKP' plot( x, only_mean = FALSE, n_grid = 80, dims = NULL, engine = c("base", "ggplot"), ... ) ## S3 method for class 'TwinBKP' plot( x, only_mean = FALSE, n_grid = 80, dims = NULL, engine = c("base", "ggplot"), l_nums = NULL, v_nums = NULL, ... ) ## S3 method for class 'TwinDKP' plot( x, only_mean = FALSE, n_grid = 80, dims = NULL, engine = c("base", "ggplot"), l_nums = NULL, v_nums = NULL, ... )
x |
An object of class |
only_mean |
Logical. If |
n_grid |
Positive integer specifying the number of grid points per
dimension for constructing the prediction grid. Larger values produce
smoother and more detailed surfaces, but increase computation time. Default
is |
dims |
Integer vector indicating which input dimensions to plot. Must
have length 1 (for 1D) or 2 (for 2D). If |
engine |
Character string specifying the plotting backend.
Either |
... |
Additional arguments passed to internal plotting routines (currently unused). |
l_nums |
Optional local neighbor count passed to
|
v_nums |
Optional validation size passed to
|
The plotting behavior depends on the dimensionality of the input covariates:
1D inputs:
For BKP (binary/binomial data), the function plots the posterior mean curve with a shaded 95% credible interval, overlaid with the observed proportions ().
For DKP (categorical/multinomial data), it plots one curve per class, each with a shaded credible interval and the observed class frequencies.
For classification tasks, an optional curve of the maximum posterior class probability can be displayed to visualize decision confidence.
2D inputs:
For both BKP and DKP models, the function generates contour plots over a 2D prediction grid.
Users can choose to plot only the predictive mean surface (only_mean = TRUE) or a set of four summary plots (only_mean = FALSE):
Predictive mean
97.5th percentile (upper bound of 95% credible interval)
Predictive variance
2.5th percentile (lower bound of 95% credible interval)
For DKP, these surfaces are generated separately for each class.
For classification tasks, predictive class probabilities can also be visualized as the maximum posterior probability surface.
Input dimensions greater than 2:
The function does not automatically support visualization and will terminate with an error.
Users must specify which dimensions to visualize via the dims argument (length 1 or 2).
plot.TwinBKP() follows the same high-level workflow as
plot.BKP(), but all predictive quantities are produced through
predict.TwinBKP() on a constructed grid.
For 1D: a dense line grid is generated, then mean and credible interval are plotted with observed proportions.
For 2D: a lattice/ggplot surface grid is generated, then mean/upper/lower/ variance panels are visualized from prediction outputs.
plot.TwinDKP() follows the same workflow as plot.DKP(), but
all predictive quantities are computed via predict.TwinDKP() on a
constructed grid.
For 1D: draw class-wise mean and credible interval curves with observed proportions (or class indicators for single-label classification).
For 2D: draw predicted class map + max probability for classification, or class-wise mean/upper/variance/lower panels for multinomial probability data.
This function is called for its side effects and does not return a value. It produces plots visualizing the predicted probabilities, credible intervals, and posterior summaries.
Zhao J, Qing K, Xu J (2025). BKP: An R Package for Beta Kernel Process Modeling. arXiv. https://doi.org/10.48550/arXiv.2508.10447.
fit_BKP and fit_DKP for fitting BKP and
DKP models, respectively; predict.BKP and
predict.DKP for generating predictions from fitted BKP and
DKP models.
# ============================================================== # # ========================= BKP Examples ======================= # # ============================================================== # #-------------------------- 1D 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 model1 <- fit_BKP(X, y, m, Xbounds=Xbounds) # Plot results plot(model1) #-------------------------- 2D Example --------------------------- set.seed(123) # Define 2D latent function and probability transformation true_pi_fun <- function(X) { if(is.null(nrow(X))) X <- matrix(X, nrow=1) m <- 8.6928 s <- 2.4269 x1 <- 4*X[,1]- 2 x2 <- 4*X[,2]- 2 a <- 1 + (x1 + x2 + 1)^2 * (19- 14*x1 + 3*x1^2- 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1- 3*x2)^2 * (18- 32*x1 + 12*x1^2 + 48*x2- 36*x1*x2 + 27*x2^2) f <- log(a*b) f <- (f- m)/s return(pnorm(f)) # Transform to probability } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) 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 model2 <- fit_BKP(X, y, m, Xbounds=Xbounds) # Plot results plot(model2, n_grid = 50) # ============================================================== # # ========================= DKP Examples ======================= # # ============================================================== # #-------------------------- 1D Example --------------------------- set.seed(123) # Define true class probability function (3-class) true_pi_fun <- function(X) { p1 <- 1/(1+exp(-3*X)) p2 <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2 return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 30 Xbounds <- matrix(c(-2, 2), nrow = 1) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model1 <- fit_DKP(X, Y, Xbounds = Xbounds) # Plot results plot(model1) #-------------------------- 2D Example --------------------------- set.seed(123) # Define latent function and transform to 3-class probabilities true_pi_fun <- function(X) { if (is.null(nrow(X))) X <- matrix(X, nrow = 1) m <- 8.6928; s <- 2.4269 x1 <- 4 * X[,1] - 2 x2 <- 4 * X[,2] - 2 a <- 1 + (x1 + x2 + 1)^2 * (19 - 14*x1 + 3*x1^2 - 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1 - 3*x2)^2 * (18 - 32*x1 + 12*x1^2 + 48*x2 - 36*x1*x2 + 27*x2^2) f <- (log(a*b)- m)/s p1 <- pnorm(f) # Transform to probability p2 <- sin(pi * X[,1]) * sin(pi * X[,2]) return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model2 <- fit_DKP(X, Y, Xbounds = Xbounds) # Plot results plot(model2, n_grid = 50)# ============================================================== # # ========================= BKP Examples ======================= # # ============================================================== # #-------------------------- 1D 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 model1 <- fit_BKP(X, y, m, Xbounds=Xbounds) # Plot results plot(model1) #-------------------------- 2D Example --------------------------- set.seed(123) # Define 2D latent function and probability transformation true_pi_fun <- function(X) { if(is.null(nrow(X))) X <- matrix(X, nrow=1) m <- 8.6928 s <- 2.4269 x1 <- 4*X[,1]- 2 x2 <- 4*X[,2]- 2 a <- 1 + (x1 + x2 + 1)^2 * (19- 14*x1 + 3*x1^2- 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1- 3*x2)^2 * (18- 32*x1 + 12*x1^2 + 48*x2- 36*x1*x2 + 27*x2^2) f <- log(a*b) f <- (f- m)/s return(pnorm(f)) # Transform to probability } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) 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 model2 <- fit_BKP(X, y, m, Xbounds=Xbounds) # Plot results plot(model2, n_grid = 50) # ============================================================== # # ========================= DKP Examples ======================= # # ============================================================== # #-------------------------- 1D Example --------------------------- set.seed(123) # Define true class probability function (3-class) true_pi_fun <- function(X) { p1 <- 1/(1+exp(-3*X)) p2 <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2 return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 30 Xbounds <- matrix(c(-2, 2), nrow = 1) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model1 <- fit_DKP(X, Y, Xbounds = Xbounds) # Plot results plot(model1) #-------------------------- 2D Example --------------------------- set.seed(123) # Define latent function and transform to 3-class probabilities true_pi_fun <- function(X) { if (is.null(nrow(X))) X <- matrix(X, nrow = 1) m <- 8.6928; s <- 2.4269 x1 <- 4 * X[,1] - 2 x2 <- 4 * X[,2] - 2 a <- 1 + (x1 + x2 + 1)^2 * (19 - 14*x1 + 3*x1^2 - 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1 - 3*x2)^2 * (18 - 32*x1 + 12*x1^2 + 48*x2 - 36*x1*x2 + 27*x2^2) f <- (log(a*b)- m)/s p1 <- pnorm(f) # Transform to probability p2 <- sin(pi * X[,1]) * sin(pi * X[,2]) return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model2 <- fit_DKP(X, Y, Xbounds = Xbounds) # Plot results plot(model2, n_grid = 50)
Generates posterior summaries from a fitted Beta Kernel Process (BKP) or Dirichlet Kernel Process (DKP) model at new input locations. For BKP models, summaries can be returned either for the latent success probability or for a future success count under the Beta-Binomial posterior predictive distribution. For DKP models, summaries are returned for the class probability vector.
## S3 method for class 'BKP' predict( object, Xnew = NULL, CI_level = 0.95, threshold = 0.5, type = c("probability", "count"), Mnew = NULL, ... ) ## S3 method for class 'DKP' predict( object, Xnew = NULL, CI_level = 0.95, type = c("probability", "count"), Mnew = NULL, ... )## S3 method for class 'BKP' predict( object, Xnew = NULL, CI_level = 0.95, threshold = 0.5, type = c("probability", "count"), Mnew = NULL, ... ) ## S3 method for class 'DKP' predict( object, Xnew = NULL, CI_level = 0.95, type = c("probability", "count"), Mnew = NULL, ... )
object |
An object of class |
Xnew |
A numeric vector or matrix of new input locations at which to
generate predictions. If |
CI_level |
Numeric between 0 and 1 specifying the credible level for
posterior intervals (default |
threshold |
Numeric between 0 and 1 specifying the classification
threshold for binary predictions based on posterior mean (used only for
BKP; default is |
type |
Character string specifying the prediction target for BKP models.
The default |
Mnew |
Positive integer trial size used only for BKP prediction when
|
... |
Additional arguments passed to generic |
A list containing posterior or posterior predictive summaries:
XThe original training input locations.
XnewThe new input locations for prediction. If NULL,
predictions are returned at the training input locations.
alpha_n, beta_n
Posterior shape parameters:
BKP: Vectors alpha_n and beta_n of Beta posterior
shape parameters.
DKP: Matrix alpha_n of Dirichlet posterior concentration
parameters, with rows corresponding to input locations and columns to
classes.
meanMean of the prediction target:
BKP with type = "probability": posterior mean of the latent
success probability.
BKP with type = "count": posterior predictive mean of the
future success count.
DKP: matrix of posterior mean class probabilities.
varianceVariance of the prediction target:
BKP with type = "probability": posterior variance of the
latent success probability.
BKP with type = "count": posterior predictive variance of
the future success count.
DKP: matrix of posterior variances for class probabilities.
lowerLower bound of the credible interval:
BKP with type = "probability": lower Beta posterior
quantile for the latent success probability.
BKP with type = "count": lower Beta-Binomial posterior
predictive quantile for the future success count.
DKP: matrix of lower posterior quantiles for class probabilities.
upperUpper bound of the credible interval:
BKP with type = "probability": upper Beta posterior
quantile for the latent success probability.
BKP with type = "count": upper Beta-Binomial posterior
predictive quantile for the future success count.
DKP: matrix of upper posterior quantiles for class probabilities.
classPredicted label, where applicable:
BKP: binary class label based on posterior mean and
threshold, only for binary data with m = 1 and
type = "probability".
DKP: predicted class label with the highest posterior mean probability.
CI_levelThe specified credible interval level.
typeThe prediction target used for BKP prediction.
MnewThe trial sizes used for count prediction, returned only
when type = "count".
Zhao J, Qing K, Xu J (2025). BKP: An R Package for Beta Kernel Process Modeling. arXiv. https://doi.org/10.48550/arXiv.2508.10447.
fit_BKP and fit_DKP for model fitting;
plot.BKP and plot.DKP for visualization of
fitted models.
# ============================================================== # # ========================= BKP Examples ======================= # # ============================================================== # #-------------------------- 1D 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 model1 <- fit_BKP(X, y, m, Xbounds=Xbounds) # Prediction on training data predict(model1) # Prediction on new data Xnew <- matrix(seq(-2, 2, length = 10), ncol=1) #new data points predict(model1, Xnew = Xnew) # Posterior predictive summaries for future success counts Mnew <- sample(100, nrow(Xnew), replace = TRUE) predict(model1, Xnew = Xnew, type = "count", Mnew = Mnew) #-------------------------- 2D Example --------------------------- set.seed(123) # Define 2D latent function and probability transformation true_pi_fun <- function(X) { if(is.null(nrow(X))) X <- matrix(X, nrow=1) m <- 8.6928 s <- 2.4269 x1 <- 4*X[,1]- 2 x2 <- 4*X[,2]- 2 a <- 1 + (x1 + x2 + 1)^2 * (19- 14*x1 + 3*x1^2- 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1- 3*x2)^2 * (18- 32*x1 + 12*x1^2 + 48*x2- 36*x1*x2 + 27*x2^2) f <- log(a*b) f <- (f- m)/s return(pnorm(f)) # Transform to probability } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) 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 model2 <- fit_BKP(X, y, m, Xbounds=Xbounds) # Prediction on training data predict(model2) # Prediction on new data x1 <- seq(Xbounds[1,1], Xbounds[1,2], length.out = 10) x2 <- seq(Xbounds[2,1], Xbounds[2,2], length.out = 10) Xnew <- expand.grid(x1 = x1, x2 = x2) predict(model2, Xnew = Xnew) # ============================================================== # # ========================= DKP Examples ======================= # # ============================================================== # #-------------------------- 1D Example --------------------------- set.seed(123) # Define true class probability function (3-class) true_pi_fun <- function(X) { p1 <- 1/(1+exp(-3*X)) p2 <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2 return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 30 Xbounds <- matrix(c(-2, 2), nrow = 1) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model1 <- fit_DKP(X, Y, Xbounds = Xbounds) # Prediction on training data predict(model1) # Prediction on new data Xnew = matrix(seq(-2, 2, length = 10), ncol=1) #new data points predict(model1, Xnew) #-------------------------- 2D Example --------------------------- set.seed(123) # Define latent function and transform to 3-class probabilities true_pi_fun <- function(X) { if (is.null(nrow(X))) X <- matrix(X, nrow = 1) m <- 8.6928; s <- 2.4269 x1 <- 4 * X[,1] - 2 x2 <- 4 * X[,2] - 2 a <- 1 + (x1 + x2 + 1)^2 * (19 - 14*x1 + 3*x1^2 - 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1 - 3*x2)^2 * (18 - 32*x1 + 12*x1^2 + 48*x2 - 36*x1*x2 + 27*x2^2) f <- (log(a*b)- m)/s p1 <- pnorm(f) # Transform to probability p2 <- sin(pi * X[,1]) * sin(pi * X[,2]) return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model2 <- fit_DKP(X, Y, Xbounds = Xbounds) # Prediction on training data predict(model2) # Prediction on new data x1 <- seq(Xbounds[1,1], Xbounds[1,2], length.out = 10) x2 <- seq(Xbounds[2,1], Xbounds[2,2], length.out = 10) Xnew <- expand.grid(x1 = x1, x2 = x2) predict(model2, Xnew)# ============================================================== # # ========================= BKP Examples ======================= # # ============================================================== # #-------------------------- 1D 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 model1 <- fit_BKP(X, y, m, Xbounds=Xbounds) # Prediction on training data predict(model1) # Prediction on new data Xnew <- matrix(seq(-2, 2, length = 10), ncol=1) #new data points predict(model1, Xnew = Xnew) # Posterior predictive summaries for future success counts Mnew <- sample(100, nrow(Xnew), replace = TRUE) predict(model1, Xnew = Xnew, type = "count", Mnew = Mnew) #-------------------------- 2D Example --------------------------- set.seed(123) # Define 2D latent function and probability transformation true_pi_fun <- function(X) { if(is.null(nrow(X))) X <- matrix(X, nrow=1) m <- 8.6928 s <- 2.4269 x1 <- 4*X[,1]- 2 x2 <- 4*X[,2]- 2 a <- 1 + (x1 + x2 + 1)^2 * (19- 14*x1 + 3*x1^2- 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1- 3*x2)^2 * (18- 32*x1 + 12*x1^2 + 48*x2- 36*x1*x2 + 27*x2^2) f <- log(a*b) f <- (f- m)/s return(pnorm(f)) # Transform to probability } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) 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 model2 <- fit_BKP(X, y, m, Xbounds=Xbounds) # Prediction on training data predict(model2) # Prediction on new data x1 <- seq(Xbounds[1,1], Xbounds[1,2], length.out = 10) x2 <- seq(Xbounds[2,1], Xbounds[2,2], length.out = 10) Xnew <- expand.grid(x1 = x1, x2 = x2) predict(model2, Xnew = Xnew) # ============================================================== # # ========================= DKP Examples ======================= # # ============================================================== # #-------------------------- 1D Example --------------------------- set.seed(123) # Define true class probability function (3-class) true_pi_fun <- function(X) { p1 <- 1/(1+exp(-3*X)) p2 <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2 return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 30 Xbounds <- matrix(c(-2, 2), nrow = 1) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model1 <- fit_DKP(X, Y, Xbounds = Xbounds) # Prediction on training data predict(model1) # Prediction on new data Xnew = matrix(seq(-2, 2, length = 10), ncol=1) #new data points predict(model1, Xnew) #-------------------------- 2D Example --------------------------- set.seed(123) # Define latent function and transform to 3-class probabilities true_pi_fun <- function(X) { if (is.null(nrow(X))) X <- matrix(X, nrow = 1) m <- 8.6928; s <- 2.4269 x1 <- 4 * X[,1] - 2 x2 <- 4 * X[,2] - 2 a <- 1 + (x1 + x2 + 1)^2 * (19 - 14*x1 + 3*x1^2 - 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1 - 3*x2)^2 * (18 - 32*x1 + 12*x1^2 + 48*x2 - 36*x1*x2 + 27*x2^2) f <- (log(a*b)- m)/s p1 <- pnorm(f) # Transform to probability p2 <- sin(pi * X[,1]) * sin(pi * X[,2]) return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model2 <- fit_DKP(X, Y, Xbounds = Xbounds) # Prediction on training data predict(model2) # Prediction on new data x1 <- seq(Xbounds[1,1], Xbounds[1,2], length.out = 10) x2 <- seq(Xbounds[2,1], Xbounds[2,2], length.out = 10) Xnew <- expand.grid(x1 = x1, x2 = x2) predict(model2, Xnew)
Prediction method for TwinBKP. For each prediction point, the function runs:
Local point selection: use a kd-tree to find l_nums nearest neighbors
from the training set as local points.
Wendland kernel hyperparameter: compute the coverage radius
using Equation (14) as the single local-kernel
hyperparameter.
Validation set: randomly sample v_nums = 2 * g_nums points from non-global points.
Mixing weight : minimize the loss of the mixed kernel
on the validation set to obtain the
optimal .
Posterior prediction: perform BKP posterior updating with the mixed kernel and return posterior mean/variance/confidence interval.
## S3 method for class 'TwinBKP' predict( object, Xnew, l_nums = NULL, v_nums = NULL, CI_level = 0.95, threshold = 0.5, return_type = c("probability", "count"), n_trials = NULL, ... )## S3 method for class 'TwinBKP' predict( object, Xnew, l_nums = NULL, v_nums = NULL, CI_level = 0.95, threshold = 0.5, return_type = c("probability", "count"), n_trials = NULL, ... )
object |
A |
Xnew |
Prediction input matrix (unnormalized), with dimension |
l_nums |
Positive integer. Number of local neighbors per prediction point.
If |
v_nums |
Positive integer. Validation set size. If |
CI_level |
Numeric confidence level. Default is |
threshold |
Classification threshold (only used for classification with
|
return_type |
Character string specifying prediction scale:
|
n_trials |
Positive integer total trial count used only when
|
... |
Unused. |
A list of class "predict_TwinBKP" containing:
XOriginal training input matrix.
meanPosterior predictive mean matrix of size n_new x 1.
variancePosterior predictive variance matrix of size n_new x 1.
lower, upperLower/upper confidence interval matrices of size n_new x 1.
alpha_n, beta_nPosterior Beta shape parameters at prediction points.
lambdaMixing weight for each prediction point
(length n_new).
theta_lLocal kernel hyperparameter for each prediction point
(length n_new).
local_idxList of selected local training-row indices for each prediction point.
XnewOriginal prediction coordinates.
Xnew_normNormalized prediction coordinates.
# ============================================================== # # ======================= TwinBKP Examples ====================== # # ============================================================== # #-------------------------- 1D 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 <- 100 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 TwinBKP model (global stage only) model1 <- fit_TwinBKP(X, y, m, Xbounds = Xbounds, g_nums = 10) # Prediction on new data Xnew <- matrix(seq(-2, 2, length.out = 10), ncol = 1) predict(model1, Xnew = Xnew, l_nums = 10) #-------------------------- 2D Example --------------------------- set.seed(123) # Define 2D latent function and probability transformation true_pi_fun <- function(X) { if (is.null(nrow(X))) X <- matrix(X, nrow = 1) m <- 8.6928 s <- 2.4269 x1 <- 4 * X[, 1] - 2 x2 <- 4 * X[, 2] - 2 a <- 1 + (x1 + x2 + 1)^2 * (19 - 14 * x1 + 3 * x1^2 - 14 * x2 + 6 * x1 * x2 + 3 * x2^2) b <- 30 + (2 * x1 - 3 * x2)^2 * (18 - 32 * x1 + 12 * x1^2 + 48 * x2 - 36 * x1 * x2 + 27 * x2^2) f <- log(a * b) f <- (f - m) / s pnorm(f) } n <- 50 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) 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 TwinBKP model model2 <- fit_TwinBKP(X, y, m, Xbounds = Xbounds, g_nums = 12) # Prediction on new data x1 <- seq(Xbounds[1, 1], Xbounds[1, 2], length.out = 8) x2 <- seq(Xbounds[2, 1], Xbounds[2, 2], length.out = 8) Xnew <- expand.grid(x1 = x1, x2 = x2) predict(model2, Xnew = Xnew, l_nums = 12)# ============================================================== # # ======================= TwinBKP Examples ====================== # # ============================================================== # #-------------------------- 1D 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 <- 100 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 TwinBKP model (global stage only) model1 <- fit_TwinBKP(X, y, m, Xbounds = Xbounds, g_nums = 10) # Prediction on new data Xnew <- matrix(seq(-2, 2, length.out = 10), ncol = 1) predict(model1, Xnew = Xnew, l_nums = 10) #-------------------------- 2D Example --------------------------- set.seed(123) # Define 2D latent function and probability transformation true_pi_fun <- function(X) { if (is.null(nrow(X))) X <- matrix(X, nrow = 1) m <- 8.6928 s <- 2.4269 x1 <- 4 * X[, 1] - 2 x2 <- 4 * X[, 2] - 2 a <- 1 + (x1 + x2 + 1)^2 * (19 - 14 * x1 + 3 * x1^2 - 14 * x2 + 6 * x1 * x2 + 3 * x2^2) b <- 30 + (2 * x1 - 3 * x2)^2 * (18 - 32 * x1 + 12 * x1^2 + 48 * x2 - 36 * x1 * x2 + 27 * x2^2) f <- log(a * b) f <- (f - m) / s pnorm(f) } n <- 50 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) 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 TwinBKP model model2 <- fit_TwinBKP(X, y, m, Xbounds = Xbounds, g_nums = 12) # Prediction on new data x1 <- seq(Xbounds[1, 1], Xbounds[1, 2], length.out = 8) x2 <- seq(Xbounds[2, 1], Xbounds[2, 2], length.out = 8) Xnew <- expand.grid(x1 = x1, x2 = x2) predict(model2, Xnew = Xnew, l_nums = 12)
Prediction method for TwinDKP. For each prediction point, the function runs:
Local point selection via kNN with l_nums neighbors.
Local Wendland kernel construction with hyperparameter theta_l.
Validation subset sampling with size v_nums.
Mixing-weight optimization for .
Posterior prediction for class probabilities and uncertainty.
## S3 method for class 'TwinDKP' predict( object, Xnew = NULL, l_nums = NULL, v_nums = NULL, CI_level = 0.95, return_type = c("probability", "count"), n_trials = NULL, ... )## S3 method for class 'TwinDKP' predict( object, Xnew = NULL, l_nums = NULL, v_nums = NULL, CI_level = 0.95, return_type = c("probability", "count"), n_trials = NULL, ... )
object |
A |
Xnew |
Prediction input matrix (unnormalized), with dimension
|
l_nums |
Positive integer. Number of local neighbors per prediction
point. If |
v_nums |
Positive integer. Validation set size. If |
CI_level |
Numeric confidence level in |
return_type |
Character string specifying prediction scale:
|
n_trials |
Positive integer total trial count used only when
|
... |
Unused. |
A list of class "predict_TwinDKP" containing:
XOriginal training input matrix.
XnewPrediction input matrix used.
Xnew_normNormalized prediction matrix.
alpha_nPosterior Dirichlet parameters at Xnew
(n_new x q).
meanPosterior mean class probabilities (n_new x q).
variancePosterior variances (n_new x q).
lower, upperMarginal credible interval bounds
(n_new x q).
lambdaOptimized mixing weights (length n_new).
theta_lLocal kernel hyperparameter (length n_new).
local_idxList of local training indices for each point.
CI_levelCredible interval level.
l_nums, v_nums, g_numsEffective tuning sizes used.
classPredicted class labels when rowSums(Y)=1.
Provides formatted console output for fitted BKP/DKP model objects, their summaries, predictions, and simulations. The following specialized methods are supported:
print.BKP, print.DKP – display fitted model objects.
print.summary_BKP, print.summary_DKP – display
model summaries.
print.predict_BKP, print.predict_DKP – display
posterior predictive results.
print.simulate_BKP, print.simulate_DKP – display
posterior simulations.
## S3 method for class 'BKP' print(x, ...) ## S3 method for class 'summary_BKP' print(x, ...) ## S3 method for class 'predict_BKP' print(x, ...) ## S3 method for class 'simulate_BKP' print(x, ...) ## S3 method for class 'DKP' print(x, ...) ## S3 method for class 'summary_DKP' print(x, ...) ## S3 method for class 'predict_DKP' print(x, ...) ## S3 method for class 'simulate_DKP' print(x, ...) ## S3 method for class 'TwinBKP' print(x, ...) ## S3 method for class 'summary_TwinBKP' print(x, ...) ## S3 method for class 'simulate_TwinBKP' print(x, ...) ## S3 method for class 'TwinDKP' print(x, ...) ## S3 method for class 'summary_TwinDKP' print(x, ...) ## S3 method for class 'predict_TwinDKP' print(x, ...) ## S3 method for class 'simulate_TwinDKP' print(x, ...)## S3 method for class 'BKP' print(x, ...) ## S3 method for class 'summary_BKP' print(x, ...) ## S3 method for class 'predict_BKP' print(x, ...) ## S3 method for class 'simulate_BKP' print(x, ...) ## S3 method for class 'DKP' print(x, ...) ## S3 method for class 'summary_DKP' print(x, ...) ## S3 method for class 'predict_DKP' print(x, ...) ## S3 method for class 'simulate_DKP' print(x, ...) ## S3 method for class 'TwinBKP' print(x, ...) ## S3 method for class 'summary_TwinBKP' print(x, ...) ## S3 method for class 'simulate_TwinBKP' print(x, ...) ## S3 method for class 'TwinDKP' print(x, ...) ## S3 method for class 'summary_TwinDKP' print(x, ...) ## S3 method for class 'predict_TwinDKP' print(x, ...) ## S3 method for class 'simulate_TwinDKP' print(x, ...)
x |
An object of class |
... |
Additional arguments passed to the generic |
Invisibly returns the input object. Called for the side effect of printing human-readable summaries to the console.
Zhao J, Qing K, Xu J (2025). BKP: An R Package for Beta Kernel Process Modeling. arXiv. https://doi.org/10.48550/arXiv.2508.10447.
fit_BKP, fit_DKP for model fitting;
summary.BKP, summary.DKP for model summaries;
predict.BKP, predict.DKP for posterior
prediction; simulate.BKP, simulate.DKP for
posterior simulations.
# ============================================================== # # ========================= BKP Examples ======================= # # ============================================================== # #-------------------------- 1D 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 model1 <- fit_BKP(X, y, m, Xbounds=Xbounds) print(model1) # fitted object print(summary(model1)) # summary print(predict(model1)) # predictions print(simulate(model1, nsim=3)) # posterior simulations #-------------------------- 2D Example --------------------------- set.seed(123) # Define 2D latent function and probability transformation true_pi_fun <- function(X) { if(is.null(nrow(X))) X <- matrix(X, nrow=1) m <- 8.6928 s <- 2.4269 x1 <- 4*X[,1]- 2 x2 <- 4*X[,2]- 2 a <- 1 + (x1 + x2 + 1)^2 * (19- 14*x1 + 3*x1^2- 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1- 3*x2)^2 * (18- 32*x1 + 12*x1^2 + 48*x2- 36*x1*x2 + 27*x2^2) f <- log(a*b) f <- (f- m)/s return(pnorm(f)) # Transform to probability } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) 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 model2 <- fit_BKP(X, y, m, Xbounds=Xbounds) print(model2) # fitted object print(summary(model2)) # summary print(predict(model2)) # predictions print(simulate(model2, nsim=3)) # posterior simulations # ============================================================== # # ========================= DKP Examples ======================= # # ============================================================== # #-------------------------- 1D Example --------------------------- set.seed(123) # Define true class probability function (3-class) true_pi_fun <- function(X) { p1 <- 1/(1+exp(-3*X)) p2 <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2 return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 30 Xbounds <- matrix(c(-2, 2), nrow = 1) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model1 <- fit_DKP(X, Y, Xbounds = Xbounds) print(model1) # fitted object print(summary(model1)) # summary print(predict(model1)) # predictions print(simulate(model1, nsim=3)) # posterior simulations #-------------------------- 2D Example --------------------------- set.seed(123) # Define latent function and transform to 3-class probabilities true_pi_fun <- function(X) { if (is.null(nrow(X))) X <- matrix(X, nrow = 1) m <- 8.6928; s <- 2.4269 x1 <- 4 * X[,1] - 2 x2 <- 4 * X[,2] - 2 a <- 1 + (x1 + x2 + 1)^2 * (19 - 14*x1 + 3*x1^2 - 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1 - 3*x2)^2 * (18 - 32*x1 + 12*x1^2 + 48*x2 - 36*x1*x2 + 27*x2^2) f <- (log(a*b)- m)/s p1 <- pnorm(f) # Transform to probability p2 <- sin(pi * X[,1]) * sin(pi * X[,2]) return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model2 <- fit_DKP(X, Y, Xbounds = Xbounds) print(model2) # fitted object print(summary(model2)) # summary print(predict(model2)) # predictions print(simulate(model2, nsim=3)) # posterior simulations# ============================================================== # # ========================= BKP Examples ======================= # # ============================================================== # #-------------------------- 1D 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 model1 <- fit_BKP(X, y, m, Xbounds=Xbounds) print(model1) # fitted object print(summary(model1)) # summary print(predict(model1)) # predictions print(simulate(model1, nsim=3)) # posterior simulations #-------------------------- 2D Example --------------------------- set.seed(123) # Define 2D latent function and probability transformation true_pi_fun <- function(X) { if(is.null(nrow(X))) X <- matrix(X, nrow=1) m <- 8.6928 s <- 2.4269 x1 <- 4*X[,1]- 2 x2 <- 4*X[,2]- 2 a <- 1 + (x1 + x2 + 1)^2 * (19- 14*x1 + 3*x1^2- 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1- 3*x2)^2 * (18- 32*x1 + 12*x1^2 + 48*x2- 36*x1*x2 + 27*x2^2) f <- log(a*b) f <- (f- m)/s return(pnorm(f)) # Transform to probability } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) 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 model2 <- fit_BKP(X, y, m, Xbounds=Xbounds) print(model2) # fitted object print(summary(model2)) # summary print(predict(model2)) # predictions print(simulate(model2, nsim=3)) # posterior simulations # ============================================================== # # ========================= DKP Examples ======================= # # ============================================================== # #-------------------------- 1D Example --------------------------- set.seed(123) # Define true class probability function (3-class) true_pi_fun <- function(X) { p1 <- 1/(1+exp(-3*X)) p2 <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2 return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 30 Xbounds <- matrix(c(-2, 2), nrow = 1) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model1 <- fit_DKP(X, Y, Xbounds = Xbounds) print(model1) # fitted object print(summary(model1)) # summary print(predict(model1)) # predictions print(simulate(model1, nsim=3)) # posterior simulations #-------------------------- 2D Example --------------------------- set.seed(123) # Define latent function and transform to 3-class probabilities true_pi_fun <- function(X) { if (is.null(nrow(X))) X <- matrix(X, nrow = 1) m <- 8.6928; s <- 2.4269 x1 <- 4 * X[,1] - 2 x2 <- 4 * X[,2] - 2 a <- 1 + (x1 + x2 + 1)^2 * (19 - 14*x1 + 3*x1^2 - 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1 - 3*x2)^2 * (18 - 32*x1 + 12*x1^2 + 48*x2 - 36*x1*x2 + 27*x2^2) f <- (log(a*b)- m)/s p1 <- pnorm(f) # Transform to probability p2 <- sin(pi * X[,1]) * sin(pi * X[,2]) return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model2 <- fit_DKP(X, Y, Xbounds = Xbounds) print(model2) # fitted object print(summary(model2)) # summary print(predict(model2)) # predictions print(simulate(model2, nsim=3)) # posterior simulations
Compute posterior quantiles from a fitted BKP or
DKP model. For a BKP object, this returns the posterior
quantiles of the positive class probability. For a DKP object, this
returns posterior quantiles for each class probability.
## S3 method for class 'BKP' quantile(x, probs = c(0.025, 0.5, 0.975), ...) ## S3 method for class 'DKP' quantile(x, probs = c(0.025, 0.5, 0.975), ...) ## S3 method for class 'TwinBKP' quantile( x, probs = c(0.025, 0.5, 0.975), Xnew = NULL, n_grid = 1000, l_nums = NULL, v_nums = NULL, ... ) ## S3 method for class 'TwinDKP' quantile( x, probs = c(0.025, 0.5, 0.975), Xnew = NULL, n_grid = 1000, l_nums = NULL, v_nums = NULL, ... )## S3 method for class 'BKP' quantile(x, probs = c(0.025, 0.5, 0.975), ...) ## S3 method for class 'DKP' quantile(x, probs = c(0.025, 0.5, 0.975), ...) ## S3 method for class 'TwinBKP' quantile( x, probs = c(0.025, 0.5, 0.975), Xnew = NULL, n_grid = 1000, l_nums = NULL, v_nums = NULL, ... ) ## S3 method for class 'TwinDKP' quantile( x, probs = c(0.025, 0.5, 0.975), Xnew = NULL, n_grid = 1000, l_nums = NULL, v_nums = NULL, ... )
x |
An object of class |
probs |
Numeric vector of probabilities specifying which posterior
quantiles to return. Defaults to |
... |
Additional arguments (currently unused). |
Xnew |
Optional prediction points for quantile evaluation. |
n_grid |
Number of generated points when |
l_nums |
Optional local neighbor count passed to
|
v_nums |
Optional validation size passed to
|
For a BKP model, posterior quantiles are computed from the
Beta Kernel Process for the positive class probability. For a DKP
model, posterior quantiles for each class are approximated using the Beta
approximation of the marginal distributions of the posterior Dirichlet
distribution.
For TwinBKP, posterior quantiles are computed by first calling
predict.TwinBKP() at Xnew, then applying qbeta() to
returned alpha_n and beta_n. If Xnew = NULL, the method
generates n_grid points in Xbounds via Latin hypercube sampling
and then performs prediction.
For TwinDKP, posterior quantiles are prediction-driven.
The method first calls predict.TwinDKP() at Xnew and then
computes class-wise marginal quantiles via Beta approximation:
for class , use qbeta(probs, alpha_{ij}, sum(alpha_i)-alpha_{ij}).
If Xnew = NULL, n_grid points are generated in
x$Xbounds by Latin hypercube sampling.
For BKP: a numeric vector (if length(probs) = 1) or a
numeric matrix (if length(probs) > 1) of posterior quantiles. Rows
correspond to observations, and columns correspond to the requested
probabilities.
For DKP: a numeric matrix (if length(probs) = 1) or a 3D
array (if length(probs) > 1) of posterior quantiles. Dimensions
correspond to observations × classes × probabilities.
fit_BKP, fit_DKP for model fitting.
# -------------------------- BKP --------------------------- 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) # Extract posterior quantiles quantile(model) # -------------------------- DKP --------------------------- #' set.seed(123) # Define true class probability function (3-class) true_pi_fun <- function(X) { p1 <- 1/(1+exp(-3*X)) p2 <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2 return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } 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) # Extract posterior quantiles quantile(model)# -------------------------- BKP --------------------------- 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) # Extract posterior quantiles quantile(model) # -------------------------- DKP --------------------------- #' set.seed(123) # Define true class probability function (3-class) true_pi_fun <- function(X) { p1 <- 1/(1+exp(-3*X)) p2 <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2 return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } 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) # Extract posterior quantiles quantile(model)
Generates random draws from the posterior predictive distribution of a fitted BKP or DKP model at specified input locations.
For BKP models, posterior samples are generated from Beta distributions characterizing success probabilities. Optionally, binary class labels can be derived by applying a user-specified classification threshold.
For DKP models, posterior samples are generated from Dirichlet distributions characterizing class probabilities. If training responses are single-label (i.e., one-hot encoded), class labels may additionally be assigned using the maximum a posteriori (MAP) rule.
## S3 method for class 'BKP' simulate(object, nsim = 1, seed = NULL, Xnew = NULL, threshold = NULL, ...) ## S3 method for class 'DKP' simulate(object, nsim = 1, seed = NULL, Xnew = NULL, ...) ## S3 method for class 'TwinBKP' simulate( object, nsim = 1, seed = NULL, Xnew = NULL, n_grid = 1000, l_nums = NULL, v_nums = NULL, threshold = NULL, ... ) ## S3 method for class 'TwinDKP' simulate( object, nsim = 1, seed = NULL, Xnew = NULL, n_grid = 1000, l_nums = NULL, v_nums = NULL, ... )## S3 method for class 'BKP' simulate(object, nsim = 1, seed = NULL, Xnew = NULL, threshold = NULL, ...) ## S3 method for class 'DKP' simulate(object, nsim = 1, seed = NULL, Xnew = NULL, ...) ## S3 method for class 'TwinBKP' simulate( object, nsim = 1, seed = NULL, Xnew = NULL, n_grid = 1000, l_nums = NULL, v_nums = NULL, threshold = NULL, ... ) ## S3 method for class 'TwinDKP' simulate( object, nsim = 1, seed = NULL, Xnew = NULL, n_grid = 1000, l_nums = NULL, v_nums = NULL, ... )
object |
An object of class |
nsim |
Number of posterior samples to generate (default = |
seed |
Optional integer seed for reproducibility. |
Xnew |
A numeric matrix or vector of new input locations at which simulations are generated. |
threshold |
Classification threshold for binary decisions (BKP only).
When specified, posterior draws exceeding the threshold are classified as
1, and those below as 0. The default is |
... |
Additional arguments (currently unused). |
n_grid |
Number of generated points when |
l_nums |
Optional local neighbor count passed to
|
v_nums |
Optional validation size passed to
|
For TwinBKP, posterior simulation is prediction-driven:
Build/receive Xnew.
Call predict.TwinBKP() to obtain alpha_n, beta_n.
Draw nsim samples from Beta(alpha_n, beta_n) at each
point.
If Xnew = NULL, n_grid points are generated in Xbounds
(default 1000) and used for simulation.
For TwinDKP, posterior simulation is prediction-driven:
Build/receive Xnew.
Call predict.TwinDKP() to obtain alpha_n.
Draw nsim samples from class-wise Dirichlet posteriors.
If Xnew = NULL, n_grid points are generated in Xbounds
(default 1000) and used for simulation.
A list with the following components:
samplesFor BKP: A numeric matrix of size nrow(Xnew) × nsim, where
each column corresponds to one posterior draw of success probabilities.
For DKP: A numeric array of dimension nsim × q × nrow(Xnew),
containing simulated class probabilities from Dirichlet posteriors, where
q is the number of classes.
meanFor 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.
classFor BKP: An integer matrix of dimension
nrow(Xnew) × nsim, indicating simulated binary class labels (0/1),
returned when threshold is specified.
For DKP: An integer matrix of dimension
nrow(Xnew) × nsim, where each entry corresponds to a MAP-predicted
class label, returned only when training data is single-label.
XThe training input matrix used to fit the BKP/DKP model.
XnewThe new input locations at which simulations are generated.
thresholdThe classification threshold used for generating binary class labels (if provided).
Zhao J, Qing K, Xu J (2025). BKP: An R Package for Beta Kernel Process Modeling. arXiv. https://doi.org/10.48550/arXiv.2508.10447.
fit_BKP, fit_DKP for model fitting;
predict.BKP, predict.DKP for posterior
prediction.
## -------------------- BKP -------------------- 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 = 5), ncol = 1) simulate(model, Xnew = Xnew, nsim = 5) # Simulate binary classifications (threshold = 0.5) simulate(model, Xnew = Xnew, nsim = 5, threshold = 0.5) ## -------------------- DKP -------------------- set.seed(123) # Define true class probability function (3-class) true_pi_fun <- function(X) { p1 <- 1/(1+exp(-3*X)) p2 <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2 return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 30 Xbounds <- matrix(c(-2, 2), nrow = 1) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 = 5), ncol = 1) simulate(model, Xnew = Xnew, nsim = 5)## -------------------- BKP -------------------- 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 = 5), ncol = 1) simulate(model, Xnew = Xnew, nsim = 5) # Simulate binary classifications (threshold = 0.5) simulate(model, Xnew = Xnew, nsim = 5, threshold = 0.5) ## -------------------- DKP -------------------- set.seed(123) # Define true class probability function (3-class) true_pi_fun <- function(X) { p1 <- 1/(1+exp(-3*X)) p2 <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2 return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 30 Xbounds <- matrix(c(-2, 2), nrow = 1) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 = 5), ncol = 1) simulate(model, Xnew = Xnew, nsim = 5)
Provides a structured summary of a fitted Beta Kernel Process (BKP) or Dirichlet Kernel Process (DKP) model. This function reports the model configuration, prior specification, kernel settings, and key posterior quantities, giving users a concise overview of the fitting results.
## S3 method for class 'BKP' summary(object, ...) ## S3 method for class 'DKP' summary(object, ...) ## S3 method for class 'TwinBKP' summary(object, ...) ## S3 method for class 'TwinDKP' summary(object, ...)## S3 method for class 'BKP' summary(object, ...) ## S3 method for class 'DKP' summary(object, ...) ## S3 method for class 'TwinBKP' summary(object, ...) ## S3 method for class 'TwinDKP' summary(object, ...)
object |
An object of class |
... |
Additional arguments passed to the generic |
For TwinBKP, summary() reports global-stage information and
posterior summaries on the global support points selected by Twining.
This mirrors the role of summary.BKP, while respecting that TwinBKP
fits global hyperparameters first and performs full prediction behavior at
predict.TwinBKP() stage.
For TwinDKP, summary() focuses on global-stage tuning
information and posterior summaries computed on Twining support points.
Full prediction on arbitrary inputs should be obtained via
predict.TwinDKP().
A list containing key summaries of the fitted model:
n_obsNumber of training observations.
input_dimInput dimensionality (number of columns in X).
kernelKernel type used in the model.
theta_optEstimated kernel hyperparameters.
lossLoss function type used in the model.
loss_minMinimum value of the loss function achieved.
priorPrior type used (e.g., "noninformative", "fixed", "adaptive").
r0Prior precision parameter.
p0Prior mean parameter.
post_meanPosterior mean estimates.
For BKP: posterior mean success probabilities at training points.
For DKP: posterior mean class probabilities ().
post_varPosterior variance estimates. For BKP: variance of success probabilities. For DKP: variance for each class probability.
n_class(Only for DKP) Number of classes in the response.
Zhao J, Qing K, Xu J (2025). BKP: An R Package for Beta Kernel Process Modeling. arXiv. https://doi.org/10.48550/arXiv.2508.10447.
fit_BKP, fit_DKP for model fitting.
# ============================================================== # # ========================= BKP Examples ======================= # # ============================================================== # #-------------------------- 1D 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 model1 <- fit_BKP(X, y, m, Xbounds=Xbounds) summary(model1) #-------------------------- 2D Example --------------------------- set.seed(123) # Define 2D latent function and probability transformation true_pi_fun <- function(X) { if(is.null(nrow(X))) X <- matrix(X, nrow=1) m <- 8.6928 s <- 2.4269 x1 <- 4*X[,1]- 2 x2 <- 4*X[,2]- 2 a <- 1 + (x1 + x2 + 1)^2 * (19- 14*x1 + 3*x1^2- 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1- 3*x2)^2 * (18- 32*x1 + 12*x1^2 + 48*x2- 36*x1*x2 + 27*x2^2) f <- log(a*b) f <- (f- m)/s return(pnorm(f)) # Transform to probability } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) 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 model2 <- fit_BKP(X, y, m, Xbounds=Xbounds) summary(model2) # ============================================================== # # ========================= DKP Examples ======================= # # ============================================================== # #-------------------------- 1D Example --------------------------- set.seed(123) # Define true class probability function (3-class) true_pi_fun <- function(X) { p1 <- 1/(1+exp(-3*X)) p2 <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2 return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 30 Xbounds <- matrix(c(-2, 2), nrow = 1) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model1 <- fit_DKP(X, Y, Xbounds = Xbounds) summary(model1) #-------------------------- 2D Example --------------------------- set.seed(123) # Define latent function and transform to 3-class probabilities true_pi_fun <- function(X) { if (is.null(nrow(X))) X <- matrix(X, nrow = 1) m <- 8.6928; s <- 2.4269 x1 <- 4 * X[,1] - 2 x2 <- 4 * X[,2] - 2 a <- 1 + (x1 + x2 + 1)^2 * (19 - 14*x1 + 3*x1^2 - 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1 - 3*x2)^2 * (18 - 32*x1 + 12*x1^2 + 48*x2 - 36*x1*x2 + 27*x2^2) f <- (log(a*b)- m)/s p1 <- pnorm(f) # Transform to probability p2 <- sin(pi * X[,1]) * sin(pi * X[,2]) return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model2 <- fit_DKP(X, Y, Xbounds = Xbounds) summary(model2)# ============================================================== # # ========================= BKP Examples ======================= # # ============================================================== # #-------------------------- 1D 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 model1 <- fit_BKP(X, y, m, Xbounds=Xbounds) summary(model1) #-------------------------- 2D Example --------------------------- set.seed(123) # Define 2D latent function and probability transformation true_pi_fun <- function(X) { if(is.null(nrow(X))) X <- matrix(X, nrow=1) m <- 8.6928 s <- 2.4269 x1 <- 4*X[,1]- 2 x2 <- 4*X[,2]- 2 a <- 1 + (x1 + x2 + 1)^2 * (19- 14*x1 + 3*x1^2- 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1- 3*x2)^2 * (18- 32*x1 + 12*x1^2 + 48*x2- 36*x1*x2 + 27*x2^2) f <- log(a*b) f <- (f- m)/s return(pnorm(f)) # Transform to probability } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) 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 model2 <- fit_BKP(X, y, m, Xbounds=Xbounds) summary(model2) # ============================================================== # # ========================= DKP Examples ======================= # # ============================================================== # #-------------------------- 1D Example --------------------------- set.seed(123) # Define true class probability function (3-class) true_pi_fun <- function(X) { p1 <- 1/(1+exp(-3*X)) p2 <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2 return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 30 Xbounds <- matrix(c(-2, 2), nrow = 1) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model1 <- fit_DKP(X, Y, Xbounds = Xbounds) summary(model1) #-------------------------- 2D Example --------------------------- set.seed(123) # Define latent function and transform to 3-class probabilities true_pi_fun <- function(X) { if (is.null(nrow(X))) X <- matrix(X, nrow = 1) m <- 8.6928; s <- 2.4269 x1 <- 4 * X[,1] - 2 x2 <- 4 * X[,2] - 2 a <- 1 + (x1 + x2 + 1)^2 * (19 - 14*x1 + 3*x1^2 - 14*x2 + 6*x1*x2 + 3*x2^2) b <- 30 + (2*x1 - 3*x2)^2 * (18 - 32*x1 + 12*x1^2 + 48*x2 - 36*x1*x2 + 27*x2^2) f <- (log(a*b)- m)/s p1 <- pnorm(f) # Transform to probability p2 <- sin(pi * X[,1]) * sin(pi * X[,2]) return(matrix(c(p1/2, p2/2, 1 - (p1+p2)/2), nrow = length(p1))) } n <- 100 Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2) X <- tgp::lhs(n = n, rect = Xbounds) true_pi <- true_pi_fun(X) m <- sample(150, 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 model2 <- fit_DKP(X, Y, Xbounds = Xbounds) summary(model2)