loss_fun: Loss Function for Kernel Process Model Tuning

View source: R/loss_fun.R

loss_funR Documentation

Loss Function for Kernel Process Model Tuning

Description

Computes prediction loss for kernel process models (BKP binary / DKP multi-class) during kernel hyperparameter tuning. Supports Brier score (MSE) and log-loss (cross-entropy) with leave-one-out cross-validation (LOOCV).

Usage

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")
)

Arguments

gamma

Numeric vector; log10-transformed kernel lengthscale parameters.

Xnorm

Numeric matrix; normalized input matrix (values in [0,1]).

y

Numeric vector; observed success counts (BKP model only).

m

Numeric vector; number of trials (BKP model only).

Y

Numeric matrix; observed class counts (DKP model only).

model

Character string; model type: "BKP" (binary) or "DKP" (multi-class).

prior

Character string; prior type: "noninformative", "fixed", "adaptive".

r0

Numeric scalar; prior precision (default = 2).

p0

Numeric; global prior mean (for fixed prior).

loss

Character string; loss type: "brier" (MSE) or "log_loss" (cross-entropy).

kernel

Character string; kernel function: "gaussian", "matern52", "matern32".

Details

This function is used internally to optimize kernel lengthscales. It converts log10 hyperparameters gamma to lengthscales theta, computes the kernel matrix, applies LOOCV (diag(K)=0), and calculates loss between estimated and empirical probabilities.

Value

Numeric scalar; loss value to minimize during optimization.

References

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

See Also

get_prior, kernel_matrix

Examples


# -------------------------- BKP Example --------------------------
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 = rep(0, 2), Xnorm = Xnorm, y = y, m = m, model = "BKP")

# -------------------------- DKP Example --------------------------
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 = rep(0, 2), Xnorm = Xnorm, Y = Y, model = "DKP")



NBKP documentation built on June 18, 2026, 1:06 a.m.